Class: Netzke::Communitypack::TreePanel

Inherits:
Base
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Includes:
Basepack::Columns, Basepack::DataAccessor
Defined in:
lib/netzke/communitypack/tree_panel.rb

Overview

Ext.tree.TreePanel-based component

TODO: Add documentation for usage

Instance Method Summary (collapse)

Instance Method Details

- (Object) augment_column_config(c)

Call super and then set the data_index



69
70
71
72
73
# File 'lib/netzke/communitypack/tree_panel.rb', line 69

def augment_column_config(c)
  super
  set_default_data_index(c)
  set_default_xtype c
end

- (Object) configure(c)

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/netzke/communitypack/tree_panel.rb', line 37

def configure(c) #:nodoc:
  super
  c.title = c.title || self.class.js_config.properties[:title] || data_class.name.pluralize
  c.columns = final_columns(with_meta: true)
  # Set it to the primary key if not given and camelize it
  # Setting it to anything else than the primary key is especially useful when instances of different class are shown in one tree
  # because the primary key MUST be unique!
  c.pri = (c.pri || data_class.primary_key).to_s.camelize(:lower)
  # Add extra fields for a tree: A method ':name(r)' is called for every record to retrieve the value
  c.extra_fields ||= []
  c.extra_fields << {:name => 'leaf', :type => 'boolean'} # This will call leaf?(r) for every record
  c.extra_fields << {:name => 'expanded', :type => 'boolean'} # This will call expanded?(r) for every record
  # only if the node id property is different from the data class' primary key, we need to add and extra field
  c.extra_fields << {:name => c.pri.to_s.camelize(:lower), :type => 'string'} if c.pri != data_class.primary_key
  # we can not operate with inline data for now, so we just prohibit to use it
  c.load_inline_data = false
end

- (Fixnum) count_records(params)

Counts the total records



167
168
169
170
171
172
# File 'lib/netzke/communitypack/tree_panel.rb', line 167

def count_records(params)
  scope_data_class(params) do
    params[:scope] = config[:scope]
    data_adapter.count_records(params, final_columns)
  end
end

- (Boolean) expanded?(r)

Is the record a expanded or not? Note: It's recommended to override this method



198
199
200
# File 'lib/netzke/communitypack/tree_panel.rb', line 198

def expanded?(r)
  false
end

- (Array) get_children(params)

Retrieves all children for a node Note: It's recommended to override this method



135
136
137
138
139
140
141
# File 'lib/netzke/communitypack/tree_panel.rb', line 135

def get_children(params)
  scope_data_class(params) do
    params[:limit] = config[:rows_per_page] if config[:enable_pagination] && (params[:id].nil? || params[:id] == 'root')
    params[:scope] = config[:scope]
    data_adapter.get_records(params, final_columns)
  end
end

- (Hash) get_data(*args)

Method that is called by the get_data endpoint Calls the get_children method and returns the serialized records



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/netzke/communitypack/tree_panel.rb', line 93

def get_data(*args)
  params = args.first || {} # params are optional!
  if !config[:prohibit_read]
    {}.tap do |res|
      # set children to an instance variable in order to access them later
      @records = get_children(params)

      # Serialize children
      res[:data] = serialize_data(@records)
      res[:total] = count_records(params) if config[:enable_pagination] && (params[:id].nil? || params[:id] == 'root')
    end
  else
    flash :error => "You don't have permissions to read data"
    { :netzke_feedback => @flash }
  end
end

- (Object) get_data_endpoint

Returns something like: [ { 'id'=> 1, 'text'=> 'A folder Node', 'leaf'=> false }, { 'id'=> 2, 'text'=> 'A leaf Node', 'leaf'=> true } ]



84
85
86
# File 'lib/netzke/communitypack/tree_panel.rb', line 84

endpoint :get_data do |params, this|
  this.merge! get_data(params)
end

- (NilClass, Array) get_inline_children(r)

Should return all children of the record that should also be serialized in the current request Note: It's recommended to override this method

If an empty array is returned, the tree assumes that there are no children available for this node (and thus you can't open it!)



180
181
182
# File 'lib/netzke/communitypack/tree_panel.rb', line 180

def get_inline_children(r)
  nil
end

- (Object) js_config

Configure dynamic JS properties for instantiation



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

def js_config
  super.tap do |c|
    # Hand over inline data to the js config hash
    c[:inline_data] = get_data if config[:load_inline_data]
  end
end

- (Boolean) leaf?(r)

Is the record a leaf or not? Note: It's recommended to override this method



189
190
191
# File 'lib/netzke/communitypack/tree_panel.rb', line 189

def leaf?(r)
  r.children.empty?
end

- (Boolean) node_id(r)

Is the record a leaf or not? Note: It's recommended to override this method



207
208
209
# File 'lib/netzke/communitypack/tree_panel.rb', line 207

def node_id(r)
  r.send(data_class.primary_key)
end

- (Object) scope_data_class(params, &block)

Scopes the data class depending on the config of the parent_key and the node



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/netzke/communitypack/tree_panel.rb', line 146

def scope_data_class(params, &block)
  if config[:parent_key]
    # The value of the pri property of the expanded node is passed as params[:id] ('root' for the root collection)
    if params[:id].nil? || params[:id] == 'root'
      data_class.where(config[:parent_key] => nil).scoping do
        yield
      end
    else
      data_class.where(config[:parent_key] => params[:id]).scoping do
        yield
      end
    end
  else
    yield
  end
end

- (Array) serialize_data(records)

Serializes an array of objects



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/netzke/communitypack/tree_panel.rb', line 114

def serialize_data(records)
  records.map { |r|
    data_adapter.record_to_hash(r, final_columns(:with_meta => true)).tap { |h|

      config[:extra_fields].each do |f|
        name = f[:name].underscore.to_sym
        h[name] = send("#{name}#{f[:type] == 'boolean' ? '?' : ''}", r)
      end

      inline_children = get_inline_children(r)
      h[:data] = serialize_data(inline_children) unless inline_children.nil?
      h
    }
  }
end

- (Object) set_default_data_index(c)

Set data_index The name of the field configuration of the Ext JS model will be set to this value This is neccessary since the data is serialized as a hash (with camelized keys) so the data_index must also be camelized



64
65
66
# File 'lib/netzke/communitypack/tree_panel.rb', line 64

def set_default_data_index(c)
  c[:data_index] = c[:name].camelize(:lower)
end

- (Object) set_default_xtype(c)

Sets the xtype to 'treecolumn' for the column with name equal to the :treecolumn value of the config



56
57
58
# File 'lib/netzke/communitypack/tree_panel.rb', line 56

def set_default_xtype(c)
  c[:xtype] = 'treecolumn' if c[:name].to_s == config[:treecolumn].to_s
end