cirandas.net

ref: master

plugins/pjax/lib/pjax_plugin.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class PjaxPlugin < Noosfero::Plugin

  def self.plugin_name
    _('Pjax plugin')
  end

  def self.plugin_description
    _("Use pjax for page's links")
  end

  def stylesheet?
    true
  end

  def js_files
    ['jquery.pjax.js', 'patchwork.js', 'loading-overlay', 'pjax', ].map{ |j| "javascripts/#{j}" }
  end

  def head_ending
    #TODO: add pjax meta
  end

  def body_beginning
    lambda{ render 'pjax_layouts/load_state_script' }
  end

  PjaxCheck = lambda do
    return unless request.headers['X-PJAX']
    # raise makes pjax fallback to a regular request
    raise "Pjax can't be used here" if params[:controller] == 'account'

    @pjax = true
    @pjax_loaded_themes = request.headers['X-PJAX-Themes'].to_s.split(',') || []

    unless self.respond_to? :get_layout_with_pjax
      self.class.send :define_method, :get_layout_with_pjax do
        if @pjax then 'pjax' else get_layout_without_pjax end
      end
      self.class.alias_method_chain :get_layout, :pjax
    end
  end

  def application_controller_filters
    [{
      :type => 'before_filter', :method_name => 'pjax_check',
      :options => {}, :block => PjaxCheck,
    }]
  end

  protected

end