Class: Netzke::TabPanel

Inherits:
Base
  • Object
show all
Defined in:
lib/netzke/tab_panel.rb

Overview

TabPanel

Features:

  • Dynamically loads widgets for the tabs that get activated for the first time

  • Is loaded along with the active widget - saves a request to the server

  • Provides the method markTabsOutdated to mark all inactive tabs as ‘outdated’, and calls “update” method on widgets in tabs when they get activated

TODO:

  • Stores the last active tab in persistent_config

  • Introduce a second or two delay before informing the server about a tab switched

Direct Known Subclasses

ConfigurationPanel, MasqueradeSelector

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TabPanel

some configuration normalization



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/netzke/tab_panel.rb', line 111

def initialize(*args)
  super
  
  # to remove duplicated active tabs
  first_active = nil

  items.each_with_index do |item, i|
    # if the item is provided without a name, give it a generated name
    item[:name] ||= "item#{i}"

    # remove duplicated "active" configuration
    if item[:active]
      if first_active.nil?
        first_active = item.name
      else
        item[:active] = nil
      end
    end
  end
  
  # the first tab is forced to become active, if none was configured as active
  items.first[:active] = true and first_active = items.first.name if first_active.nil?
  
  widget_session[:active_tab] = first_active
end

Class Method Details

.js_base_classObject



16
17
18
# File 'lib/netzke/tab_panel.rb', line 16

def self.js_base_class
  "Ext.TabPanel"
end

.js_extend_propertiesObject



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/netzke/tab_panel.rb', line 20

def self.js_extend_properties
  {
    :id_delimiter => "___", # the default was "__", which conflicts with Netzke's double underscore notation
    :defaults => {:layout => 'fit'}, # all tabs will be Ext.Panel-s with layout 'fit' ("fit-panels")
    
    :render => <<-END_OF_JAVASCRIPT.l,
    
    :load_widget_into => <<-END_OF_JAVASCRIPT.l,
    
    :mark_tabs_outdated => <<-END_OF_JAVASCRIPT.l,
      function(){
        this.items.each(function(i){
          if (this.getActiveTab() != i){
            i.outdated = true
          }
        }, this);
      }
    END_OF_JAVASCRIPT
    
    # bulkExecute in active tab
    :execute_in_active_tab => <<-END_OF_JAVASCRIPT.l,
    
    :get_loaded_children => <<-END_OF_JAVASCRIPT.l,
    
    :on_tab_change => <<-END_OF_JAVASCRIPT.l
      function(self, tab) {
        // load widget into the panel if it wasn't loaded yet
        if (!tab.getWidget()) {
          this.loadWidgetInto(tab);
        }
        
        // inform the server about active tab change
        this.apiActivateTab({tab:tab.widget});
        
        // call "update" on the widget
        if (tab.outdated) {
          tab.outdated = false;
          var widget = tab.getWidget();
          if (widget && widget.update) {widget.update.call(widget)};
        }
      }
    END_OF_JAVASCRIPT
  }
end

Instance Method Details

#api_activate_tab(params) ⇒ Object



161
162
163
164
# File 'lib/netzke/tab_panel.rb', line 161

def api_activate_tab(params)
  widget_session[:active_tab] = params[:tab]
  {}
end

#fit_panelsObject

“Fit panels” - Panels with layout ‘fit’ that serve as containers for (dynamically) loaded widgets



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/netzke/tab_panel.rb', line 148

def fit_panels
  res = []
  items.each_with_index do |item, i|
    item_config = {
      :id => item[:active] && id_name + '_active',
      :title => item[:title] || (item[:name] && item[:name].humanize),
      :widget => item[:name] # to know which fit-panel will load which widget
    }
    res << item_config
  end
  res
end

#get_active_tabObject



166
167
168
# File 'lib/netzke/tab_panel.rb', line 166

def get_active_tab
  aggregatee_instance(widget_session[:active_tab])
end

#initial_aggregateesObject

the items are late aggregatees, besides the one that is configured active



138
139
140
141
142
143
144
145
# File 'lib/netzke/tab_panel.rb', line 138

def initial_aggregatees
  res = {}
  items.each_with_index do |item, i|
    item[:late_aggregation] = !item[:active] && !item[:preloaded]
    res.merge!(item[:name].to_sym => item)
  end
  res
end

#itemsObject



99
100
101
# File 'lib/netzke/tab_panel.rb', line 99

def items
  @items ||= config[:items]
end

#js_configObject



103
104
105
106
107
108
# File 'lib/netzke/tab_panel.rb', line 103

def js_config
  super.merge({
    :items => fit_panels,
    :active_tab => id_name + '_active' # id of the fit panel that is active
  })
end