netzke-basepack 0.12.6 → 0.12.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.12.7 - 2015-08-09
2
+ * Fix the strong_default_attrs option support
3
+ * Support column action's isDisabled option
4
+ * Store tree nodes expand/collapse state
5
+
1
6
  # 0.12.6 - 2015-07-11
2
7
  * Regression: show cell editor on adding/editing a record in grid
3
8
  * Fix occasional autoload-related problems
@@ -58,7 +58,7 @@ module Netzke
58
58
  a[:handler] = Netzke::Core::JsonLiteral.new <<-JS
59
59
  function() {
60
60
  var cmp = Ext.getCmp('#{js_id}'),
61
- f = cmp.on#{a[:handler].to_s.camelize};
61
+ f = cmp.on#{a[:handler].to_s.camelize};
62
62
  if (Ext.isFunction(f)) {
63
63
  f.apply(cmp, arguments);
64
64
  } else {
@@ -67,6 +67,12 @@ module Netzke
67
67
  }
68
68
  JS
69
69
 
70
+ a[:is_disabled] &&= Netzke::Core::JsonLiteral.new <<-JS
71
+ function() {
72
+ var cmp = Ext.getCmp('#{js_id}');
73
+ return cmp.#{a[:is_disabled].to_s.camelize(:lower)}.apply(cmp, arguments);
74
+ }
75
+ JS
70
76
  a[:icon] = "#{Netzke::Core.icons_uri}/#{a[:icon]}.png" if a[:icon].is_a?(Symbol)
71
77
  end
72
78
  end
@@ -217,7 +217,7 @@ module Netzke::Basepack::DataAdapters
217
217
 
218
218
  # Assigns new value to an (association) attribute in a given record
219
219
  # +role+ - role provided for mass assignment protection
220
- def set_record_value_for_attribute(record, attr, value, role = :default)
220
+ def set_record_value_for_attribute(record, attr, value)
221
221
  end
222
222
 
223
223
  # Returns human attribute name
@@ -252,21 +252,23 @@ module Netzke::Basepack::DataAdapters
252
252
  v
253
253
  end
254
254
 
255
- def set_record_value_for_attribute(r, a, v, role = :default)
256
- v = v.to_time_in_current_zone if v.is_a?(Date) # convert Date to Time
257
- unless a[:read_only]
258
- if a[:setter]
259
- a[:setter].call(r, v)
260
- elsif r.respond_to?("#{a[:name]}=")
261
- r.send("#{a[:name]}=", v)
262
- elsif association_attr?(a)
263
- split = a[:name].to_s.split(/\.|__/)
264
- if a[:nested_attribute]
255
+ def set_record_value_for_attribute(record, attr, value)
256
+ ::Rails.logger.debug "\n!!! attr: #{attr.inspect}\n"
257
+ value = value.to_time_in_current_zone if value.is_a?(Date) # convert Date to Time
258
+ unless attr[:read_only]
259
+ if attr[:setter]
260
+ attr[:setter].call(record, value)
261
+ elsif record.respond_to?("#{attr[:name]}=")
262
+ ::Rails.logger.debug "\n!!! value: #{value.inspect}\n"
263
+ record.send("#{attr[:name]}=", value)
264
+ elsif association_attr?(attr)
265
+ split = attr[:name].to_s.split(/\.|__/)
266
+ if attr[:nested_attribute]
265
267
  # We want:
266
268
  # set_value_for_attribute({:name => :assoc_1__assoc_2__method, :nested_attribute => true}, 100)
267
269
  # =>
268
- # r.assoc_1.assoc_2.method = 100
269
- split.inject(r) { |r,m| m == split.last ? (r && r.send("#{m}=", v) && r.save) : r.send(m) }
270
+ # record.assoc_1.assoc_2.method = 100
271
+ split.inject(record) { |r,m| m == split.last ? (r && r.send("#{m}=", v) && r.save) : r.send(m) }
270
272
  else
271
273
  if split.size == 2
272
274
  # search for association and assign it to r
@@ -274,9 +276,9 @@ module Netzke::Basepack::DataAdapters
274
276
  assoc_method = split.last
275
277
  if assoc
276
278
  if assoc.macro == :has_one
277
- assoc_instance = r.send(assoc.name)
279
+ assoc_instance = record.send(assoc.name)
278
280
  if assoc_instance
279
- assoc_instance.send("#{assoc_method}=", v)
281
+ assoc_instance.send("#{assoc_method}=", value)
280
282
  assoc_instance.save # what should we do when this fails?..
281
283
  else
282
284
  # what should we do in this case?
@@ -285,13 +287,13 @@ module Netzke::Basepack::DataAdapters
285
287
 
286
288
  # set the foreign key to the passed value
287
289
  # not that if a negative value is passed, we reset the association (set it to nil)
288
- r.send("#{assoc.foreign_key}=", v.to_i < 0 ? nil : v)
290
+ record.send("#{assoc.foreign_key}=", value.to_i < 0 ? nil : value)
289
291
  end
290
292
  else
291
293
  logger.warn "Netzke: Association #{assoc} is not known for class #{@data_class}"
292
294
  end
293
295
  else
294
- logger.warn "Netzke: Wrong attribute name: #{a[:name]}"
296
+ logger.warn "Netzke: Wrong attribute name: #{attr[:name]}"
295
297
  end
296
298
  end
297
299
  end
@@ -62,7 +62,7 @@ module Netzke
62
62
  @record = data_class.new if @record.nil?
63
63
 
64
64
  hsh.each_pair do |k,v|
65
- data_adapter.set_record_value_for_attribute(@record, fields[k.to_sym].nil? ? {:name => k} : fields[k.to_sym], v, config.role || :default)
65
+ data_adapter.set_record_value_for_attribute(@record, fields[k.to_sym].nil? ? {:name => k} : fields[k.to_sym], v)
66
66
  end
67
67
 
68
68
  # did we have complete success?
@@ -6,7 +6,6 @@
6
6
  if (this.isLocked) return this.callParent();
7
7
 
8
8
  this.plugins = this.plugins || [];
9
- this.features = this.features || [];
10
9
 
11
10
  // Enable filters feature
12
11
  if (this.enableColumnFilters) {
@@ -124,22 +124,24 @@ module Netzke
124
124
  end
125
125
 
126
126
  def update_record(record, attrs)
127
- # merge with strong default attirbutes
128
- attrs.merge!(config[:strong_default_attrs]) if config[:strong_default_attrs]
129
-
130
127
  attrs.each_pair do |k,v|
131
128
  attr = final_columns_hash[k.to_sym]
132
129
  next if attr.nil?
133
130
  data_adapter.set_record_value_for_attribute(record, attr, v)
134
131
  end
135
132
 
133
+ strong_attrs = config[:strong_default_attrs] || {}
134
+
135
+ strong_attrs.each_pair do |k,v|
136
+ data_adapter.set_record_value_for_attribute(record, {name: k.to_s}, v)
137
+ end
138
+
136
139
  if record.save
137
140
  {record: data_adapter.record_to_array(record, final_columns(:with_meta => true))}
138
141
  else
139
142
  {error: record.errors.to_a}
140
143
  end
141
144
  end
142
-
143
145
  end
144
146
  end
145
147
  end
@@ -2,7 +2,9 @@ module Netzke
2
2
  module Basepack
3
3
  # Ext.tree.Panel-based component with the following features:
4
4
  #
5
- # * CRUD operations (only R is implemented atm)
5
+ # * CRUD operations
6
+ # * Persistence of node expand/collapse state
7
+ # * (TODO) Node reordering by DnD
6
8
  #
7
9
  # == Simple example
8
10
  #
@@ -11,7 +13,7 @@ module Netzke
11
13
  # super
12
14
  # c.model = "FileRecord"
13
15
  # c.columns = [
14
- # {name: :name, xtype: :treecolumn},
16
+ # {name: :name, xtype: :treecolumn}, # this column will show tree nodes
15
17
  # :size
16
18
  # ]
17
19
  # end
@@ -40,7 +42,7 @@ module Netzke
40
42
  # [root]
41
43
  #
42
44
  # By default, the component will pick whatever record is returned by `TreeModel.root`, and use it as the root
43
- # record. However, sometimes the model table has multiple root records (which `parent_id` set to `nil`), and all
45
+ # record. However, sometimes the model table has multiple root records (whith `parent_id` set to `nil`), and all
44
46
  # of them should be shown in the panel. To achive this, you can define the `root` config option,
45
47
  # which will serve as a virtual root record for those records. You may set it to `true`, or a hash of
46
48
  # attributes, e.g.:
@@ -49,6 +51,10 @@ module Netzke
49
51
  #
50
52
  # Note, that the root record can be hidden from the tree by specifying the `Ext.tree.Panel`'s `root_visible`
51
53
  # config option set to `false`, which is probably what you want when you have multiple root records.
54
+ #
55
+ # == Persisting nodes' expand/collapse state
56
+ #
57
+ # If the model includes the `expanded` DB field, the expand/collapse state will get stored in the DB.
52
58
  class Tree < Netzke::Base
53
59
  NODE_ATTRS = {
54
60
  boolean: %w[leaf checked expanded expandable qtip qtitle],
@@ -94,11 +100,23 @@ module Netzke
94
100
  def read(params = {})
95
101
  {}.tap do |res|
96
102
  records = get_records(params)
97
- res[:data] = records.map{|r| data_adapter.record_to_hash(r, final_columns(:with_meta => true)).netzke_literalize_keys}
103
+ res[:children] = records.map{|r| node_to_hash(r, final_columns(with_meta: true)).netzke_literalize_keys.symbolize_keys}
98
104
  res[:total] = count_records(params) if config[:enable_pagination]
99
105
  end
100
106
  end
101
107
 
108
+ def node_to_hash(record, columns)
109
+ data_adapter.record_to_hash(record, columns).tap do |hash|
110
+ if is_node_expanded?(record) && record.children.count > 0
111
+ hash[:children] = record.children.map {|child| node_to_hash(child, columns).netzke_literalize_keys.symbolize_keys}
112
+ end
113
+ end
114
+ end
115
+
116
+ def is_node_expanded?(record)
117
+ record.respond_to?(:expanded) && record.expanded?
118
+ end
119
+
102
120
  def js_configure(c)
103
121
  super
104
122
 
@@ -166,6 +184,14 @@ module Netzke
166
184
  this.delete(:set_form_values)
167
185
  end
168
186
 
187
+ endpoint :server_update_node_state do |params, this|
188
+ node = data_adapter.find_record(params[:id])
189
+ if node.respond_to?(:expanded)
190
+ node.expanded = params[:expanded]
191
+ data_adapter.save_record(node)
192
+ end
193
+ end
194
+
169
195
  protected
170
196
 
171
197
  def bbar
@@ -8,12 +8,24 @@
8
8
 
9
9
  delete this.root;
10
10
 
11
- this.plugins = [];
11
+ this.plugins = this.plugins || [];
12
12
  this.plugins.push(Ext.create('Ext.grid.plugin.CellEditing', {pluginId: 'celleditor'}));
13
13
 
14
14
  this.callParent();
15
15
 
16
16
  this.setDynamicActionProperties(); // enable/disable actions (buttons) depending on selection
17
+ this.getView().on('afteritemcollapse', this.handleNodeStateChange, this);
18
+ this.getView().on('afteritemexpand', this.handleNodeStateChange, this);
19
+
20
+ this.store.on('load', function(){
21
+ var root = this.getRootNode();
22
+ root.collapse();
23
+ root.expand(false);
24
+ }, this);
25
+ },
26
+
27
+ handleNodeStateChange: function(node){
28
+ this.serverUpdateNodeState({id: node.get('id'), expanded: node.isExpanded()});
17
29
  },
18
30
 
19
31
  // Process selectionchange event to enable/disable actions
@@ -54,8 +66,7 @@
54
66
  netzkeBuildReader: function() {
55
67
  var modelName = Netzke.modelName(this.id);
56
68
  return Ext.create('Ext.data.reader.Json', {
57
- model: modelName,
58
- rootProperty: 'data'
69
+ model: modelName
59
70
  });
60
71
  },
61
72
 
@@ -107,6 +118,6 @@
107
118
  store.sync();
108
119
  }
109
120
  }, this);
110
- }
121
+ },
111
122
 
112
123
  }
@@ -1,5 +1,5 @@
1
1
  module Netzke
2
2
  module Basepack
3
- VERSION = "0.12.6"
3
+ VERSION = "0.12.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netzke-basepack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.6
4
+ version: 0.12.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-11 00:00:00.000000000 Z
12
+ date: 2015-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: netzke-core