Class: Netzke::Communitypack::Workspace

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

Overview

A component that allows for dynamical loading/unloading of other Netzke components in tabs. It can be manipulated by calling the <js>loadChild</js> method, e.g.:

workspace.loadChild("UserGrid", {newTab: true})
  • will load a UserGrid component from the server in a new tab.

Instance Method Summary collapse

Instance Method Details

#componentsObject

Overriding this to allow for dynamically declared components



39
40
41
# File 'lib/netzke/communitypack/workspace.rb', line 39

def components
  stored_tabs.inject({}){ |r,tab| r.merge(tab[:name].to_sym => tab.reverse_merge(:prevent_header => true, :lazy_loading => true, :border => false)) }.merge(:cmp0 => dashboard_config)
end

#dashboard_configObject



31
32
33
34
35
36
# File 'lib/netzke/communitypack/workspace.rb', line 31

def dashboard_config
  {
    :title => "Dashboard",
    :class_name => "Netzke::Basepack::Panel"
  }.merge!(@passed_config[:dashboard_config] || {}).merge(:name => 'cmp0', :lazy_loading => false)
end

#default_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/netzke/communitypack/workspace.rb', line 17

def default_config
  super.tap do |c|
    c[:items] = ([dashboard_config] + stored_tabs).each_with_index.map do |tab,i|
      {
        :layout => 'fit',
        :title => tab[:title],
        :closable => i > 0, # all closable except first
        :netzke_component_id => tab[:name],
        :items => !components[tab[:name].to_sym][:lazy_loading] && [tab[:name].to_sym.component]
      }
    end
  end
end

#deliver_component_endpoint(params) ⇒ Object

Overriding the deliver_component endpoint, to dynamically add tabs and replace components in existing tabs



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
# File 'lib/netzke/communitypack/workspace.rb', line 44

def deliver_component_endpoint(params)
  cmp_name = params[:name]
  cmp_index = cmp_name.sub("cmp", "").to_i

  if params[:component].present?
    current_tabs = stored_tabs

    # we need to instantiate the newly added child to get access to its title
    cmp_class = constantize_class_name(params[:component])
    raise RuntimeError, "Could not find class #{params[:component]}" if cmp_class.nil?

    cmp_config = {:name => params[:name], :class_name => cmp_class.name}.merge(params[:config] || {}).symbolize_keys
    cmp_instance = cmp_class.new(cmp_config, self)
    new_tab_short_config = cmp_config.merge(:title => cmp_instance.js_config[:title] || cmp_instance.class.js_properties[:title]) # here we set the title

    if stored_tabs.empty? || cmp_index > stored_tabs.last[:name].sub("cmp", "").to_i
      # add new tab to persistent storage
      current_tabs << new_tab_short_config
    else
      # replace existing tab in the storage
      current_tabs[current_tabs.index(current_tabs.detect{ |tab| tab[:name] == cmp_name })] = new_tab_short_config
    end

    component_session[:items] = current_tabs
    @stored_tabs = nil # reset cache
  end

  super(params)
end