Class: Netzke::Tree::Base

Overview

Ext.tree.Panel-based component with the following features:

  • CRUD operations

  • Persistence of node expand/collapse state

  • Node reordering by DnD

Client-side methods are documented here: api.netzke.org/client/classes/Netzke.Tree.Base.html.

Simple example

class Files < Netzke::Tree::Base
  def configure(c)
    super
    c.model = "FileRecord"
    c.columns = [
      {name: :name, xtype: :treecolumn}, # this column will show tree nodes
      :size
    ]
  end
end

Instance configuration

The following config options are supported:

model

Name of the ActiveRecord model that provides data to this Tree, e.g. “FileRecord” The model must respond to the following methods:

  • TreeModel.root - the root record

  • TreeModel#children - child records

Note that the awesome_nested_set gem implements the above, so, feel free to use it.

columns

An array of columns to be displayed in the tree. See the “Columns” section in the `Netzke::Grid::Base`. Additionally, you probably will want to specify which column will have the tree nodes UI by providing the `xtype` config option set to `:treecolumn`.

root

By default, the component will pick whatever record is returned by `TreeModel.root`, and use it as the root record. However, sometimes the model table has multiple root records (whith `parent_id` set to `nil`), and all of them should be shown in the panel. To achive this, you can define the `root` config option, which will serve as a virtual root record for those records. You may set it to `true`, or a hash of attributes, e.g.:

c.root = {name: 'Root', size: 1000}

Note, that the root record can be hidden from the tree by specifying the `Ext.tree.Panel`'s `root_visible` config option set to `false`, which is probably what you want when you have multiple root records.

scope

A Proc or a Hash used to scope out grid data. The Proc will receive the current relation as a parameter and must return the modified relation. For example:

class Books < Netzke::Grid::Base
  def configure(c)
    super
    c.model = Book
    c.scope = lambda {|r| r.where(author_id: 1) }
  end
end

Hash is being accepted for conivience, it will be directly passed to `where`. So the above can be rewritten as:

class Books < Netzke::Grid::Base
  def configure(c)
    super
    c.model = Book
    c.scope = {author_id: 1}
  end
end
drag_drop

Enables drag and drop in the tree.

Persisting nodes' expand/collapse state

If the model includes the `expanded` DB field, the expand/collapse state will get stored in the DB.

Constant Summary

NODE_ATTRS =
{
  boolean: %w[leaf checked expanded expandable qtip qtitle],
  string: %w[icon icon_cls href href_target qtip qtitle]
}

Constants included from Basepack::Attributes

Basepack::Attributes::ATTRIBUTE_METHOD_NAME

Constants included from Basepack::Columns

Basepack::Columns::COLUMN_METHOD_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Basepack::DataAccessor

#model, #model_adapter

Methods included from Basepack::Attributes

#apply_attribute_dsl, #association_attr?, #association_value_defaults, #attribute_overrides, #attributes, #augment_attribute_config

Methods included from Basepack::Columns

#append_association_values_column, #attributes_for_search, #build_column_config, #default_form_items, #final_columns, #final_columns_hash, #form_items, #insert_primary_column, #js_columns, #non_meta_columns

Methods included from Grid::Permissions

#allowed_to?, #allowed_to_read?, #permissions

Methods included from Grid::Components

#configure_form, #configure_form_window

Methods included from Grid::Actions

#has_add_action?, #has_add_in_form_action?, #has_apply_action?, #has_delete_action?, #has_edit_action?, #has_edit_in_form_action?, #has_search_action?

Methods included from Grid::Services

#count_records, #create, #destroy, #on_data_changed, #update

Methods included from Grid::Endpoints

#attempt_operation

Methods included from Grid::Configuration

#bbar, #context_menu, #default_bbar, #default_context_menu, #default_tools, #tools, #validate_config

Class Method Details

.i18n_idObject

Borrow translations from the grid for now



122
123
124
# File 'lib/netzke/tree/base.rb', line 122

def i18n_id
  "netzke.grid.base"
end

.server_side_config_optionsObject



117
118
119
# File 'lib/netzke/tree/base.rb', line 117

def server_side_config_options
  super + [:model]
end

Instance Method Details

#columnsObject



127
128
129
# File 'lib/netzke/tree/base.rb', line 127

def columns
  add_node_interface_methods(super)
end

#configure_client(c) ⇒ Object

Overrides `Grid::Configuration#configure_client`



162
163
164
165
166
# File 'lib/netzke/tree/base.rb', line 162

def configure_client(c)
  super

  c.root ||= model_adapter.record_to_hash(model_adapter.root, final_columns).netzke_literalize_keys
end

#get_records(params) ⇒ Object

Overrides Grid::Services#get_records



132
133
134
135
136
137
138
# File 'lib/netzke/tree/base.rb', line 132

def get_records(params)
  if params[:id] == 'root'
    model_adapter.find_root_records(config[:scope])
  else
    model_adapter.find_record_children(model_adapter.find_record(params[:id]), config[:scope])
  end
end

#is_node_expanded?(record) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/netzke/tree/base.rb', line 157

def is_node_expanded?(record)
  record.respond_to?(:expanded) && record.expanded?
end

#node_to_hash(record, columns) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/netzke/tree/base.rb', line 149

def node_to_hash(record, columns)
  model_adapter.record_to_hash(record, columns).tap do |hash|
    if is_node_expanded?(record)
      hash["children"] = model_adapter.find_record_children(record, config[:scope]).map {|child| node_to_hash(child, columns).netzke_literalize_keys}
    end
  end
end

#read(params = {}) ⇒ Object

Overrides Grid::Services#read so we send records as key-value JSON (instead of array)



141
142
143
144
145
146
147
# File 'lib/netzke/tree/base.rb', line 141

def read(params = {})
  {}.tap do |res|
    records = get_records(params)
    res["children"] = records.map{|r| node_to_hash(r, final_columns).netzke_literalize_keys}
    res["total"] = count_records(params)  if config[:enable_pagination]
  end
end