Class: ExtNode

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/extjsml/basenode.rb

Overview

require “active_support/all”

Constant Summary collapse

@@refs =
{}
@@events_store =
{}
@@generator =

def to_s

o = self.dup
n = get_deep
o.parent = nil
o.config[:items] = nil
%Q{
  #{"  "*n}#{o.xtype}(#{o.config}) 
    #{"  "*n}>#{o.childs}"}

end

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xtype, config = {}, parent = nil) ⇒ ExtNode

def self.inherited(klass)

klass.class_attribute :before_filters
klass.before_filters = []

end



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/extjsml/basenode.rb', line 12

def initialize(xtype, config = {}, parent = nil)
	@xtype = xtype
   @config ||= {} # init config first for future use
   @config.merge! :autoDestroy => true
   # TODO able to alias config key
   override_config config
   # Hook
   do_alias_config
   prepare_config rescue "next"
	apply_config @default_config unless @default_config.nil?
	@parent = parent
	@childs = []
end

Instance Attribute Details

#childsObject

Returns the value of attribute childs.



5
6
7
# File 'lib/extjsml/basenode.rb', line 5

def childs
  @childs
end

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/extjsml/basenode.rb', line 5

def config
  @config
end

#deep_lvlObject

Returns the value of attribute deep_lvl.



5
6
7
# File 'lib/extjsml/basenode.rb', line 5

def deep_lvl
  @deep_lvl
end

#default_configObject

Returns the value of attribute default_config.



5
6
7
# File 'lib/extjsml/basenode.rb', line 5

def default_config
  @default_config
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/extjsml/basenode.rb', line 5

def parent
  @parent
end

#xtypeObject

Returns the value of attribute xtype.



5
6
7
# File 'lib/extjsml/basenode.rb', line 5

def xtype
  @xtype
end

Class Method Details

.before_to_extjs(method) ⇒ Object



336
337
338
339
340
# File 'lib/extjsml/basenode.rb', line 336

def self.before_to_extjs(method)
  # @before_filters ||= []
  before_filters << method
  # p method, name, @@before_filters, self 
end

.get_before_filtersObject



342
343
344
345
# File 'lib/extjsml/basenode.rb', line 342

def self.get_before_filters
  before_filters
  # @@before_filters 
end

.get_eventsObject



276
277
278
# File 'lib/extjsml/basenode.rb', line 276

def self.get_events
  @@events_store 
end

.get_refsObject



280
281
282
# File 'lib/extjsml/basenode.rb', line 280

def self.get_refs
  @@refs 
end

.reset_generator_configObject



332
333
334
# File 'lib/extjsml/basenode.rb', line 332

def self.reset_generator_config
  @@generator = {}
end

.set_generator_config(option) ⇒ Object



328
329
330
# File 'lib/extjsml/basenode.rb', line 328

def self.set_generator_config(option)
  @@generator.merge! option 
end

Instance Method Details

#add_child(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/extjsml/basenode.rb', line 61

def add_child(node)
   return if node.nil?
   if node.is_a? Array
     @childs += node
     node.each do |n|
       n.set_parent self  
     end
   else
     @childs << node
     node.set_parent self
   end
end

#apply_config(h) ⇒ Object



188
189
190
191
192
193
# File 'lib/extjsml/basenode.rb', line 188

def apply_config(h)
  if not @config[:cls].nil? and not h[:cls].nil?
    @config[:cls] += " #{h[:cls]}"
  end
  @config = h.merge @config 
end

#build_abstract_functionObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/extjsml/basenode.rb', line 284

def build_abstract_function
  # unless root?
  #   @config = @config.merge({ :ref => "../"*(at_deep-1) + conv_id_to_ref }) unless @config[:id].nil?
  # end

  code = [] 
    p @xtype, @config
  if has_child?
    childs.each do |c|
      code += c.build_abstract_function
    end
  else
    return code if @config[:call].nil?
    code << "this.#{conv_id_to_ref}.on('#{@config[:call_on]}', this.#{@config[:call_fn]}, this)"
  end

  code
end

#child_of?(*xxtype) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
# File 'lib/extjsml/basenode.rb', line 168

def child_of?(*xxtype)
  if parent.nil?
    false
  elsif xxtype.include?(parent.xtype)
    true
  else
    parent.child_of?(*xxtype)
  end
end

#child_of_form?Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
# File 'lib/extjsml/basenode.rb', line 178

def child_of_form?
  if parent.nil?
    false
  elsif parent.xtype == "form"
    true
  else
    parent.child_of_form?
  end
end

#collect_eventsObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/extjsml/basenode.rb', line 259

def collect_events
  del_keys = []
  @config.each do |k, v|
    if k =~ /^on-/i
      del_keys << k
      # v is fn_name
      ev = /^on-(.*)/.match(k)[1]
      @@events_store[conv_id_to_ref.to_sym] = [ev, v]
      # p conv_id_to_ref, ev, v
    end
  end  

  del_keys.each do |k|
    @config.delete k
  end
end

#collect_ref(ref) ⇒ Object



253
254
255
256
# File 'lib/extjsml/basenode.rb', line 253

def collect_ref(ref)
  return false if not ref or @config[:id].nil?
  @@refs[@config[:id].dup] = ref.dup
end

#conv_id_to_labelObject



53
54
55
# File 'lib/extjsml/basenode.rb', line 53

def conv_id_to_label
  @config[:id].split("-").map(&:capitalize) * " "
end

#conv_id_to_nameObject



57
58
59
# File 'lib/extjsml/basenode.rb', line 57

def conv_id_to_name
  @config[:id].nil? ? "" : @config[:id].split("-") * "_"
end

#conv_id_to_refObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/extjsml/basenode.rb', line 41

def conv_id_to_ref
  return nil unless @config[:id]
  sid = @config[:id].split("-")
  temp = sid[1..-1]
  fword = sid[0];
  temp.each do |el|
   fword += (el[0].upcase + el[1..-1])
  end
  
  fword
end

#do_alias_configObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/extjsml/basenode.rb', line 26

def do_alias_config
  if self.class.class_variable_defined? :@@ALIAS_CONFIG
    # replace alias with actual config attributes
    ac = self.class.class_variable_get :@@ALIAS_CONFIG
    nc = {}
    @config.each do |k, v|
      if ac.keys.include? k
        nc[ac[k]] = @config[k] 
        @config.delete k
      end
    end
    @config.merge! nc
  end
end

#do_layoutObject



82
83
84
# File 'lib/extjsml/basenode.rb', line 82

def do_layout
	raise "Not Implemented"	
end

#find(xtype, option = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/extjsml/basenode.rb', line 122

def find(xtype, option = nil)
  return false unless has_child?
  if(!option.nil? and option[:recursive])
    return false if option[:recursive] == 0
    option[:recursive] -= 1
  end
  found = false
  childs.each do |c|
    if c.xtype == xtype
      unless option.nil?
        match_option = true
        option.each do |k, v|
          next if k == :recursive
          match_option = false if option[k] != c.config[k] 
        end
        if match_option
          found = c 
          break
        end
      else
        found = c 
        break
      end
    end
    found = c.find xtype, option
    break if found
  end
  found 
end

#find_field_elementsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/extjsml/basenode.rb', line 107

def find_field_elements
  element = []
  if self.childs.count > 0
    childs.each do |c|
      if c.is_field_element?
        element << c
      else
        element += c.find_field_elements
      end
    end
  end

  element
end

#find_parent(*xtype) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/extjsml/basenode.rb', line 94

def find_parent(*xtype)
  return nil if parent.nil? 
  if xtype.include? parent.xtype
    parent
  else 
    parent.find_parent(*xtype)
  end
end

#get_all_siblingsObject



86
87
88
# File 'lib/extjsml/basenode.rb', line 86

def get_all_siblings
  parent.childs rescue [] 
end

#get_deepObject



303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/extjsml/basenode.rb', line 303

def get_deep()
  deep = 0
  node = self
  while true
    unless node.root? 
      node = node.parent
      deep += 1
    else 
      break
    end
  end
  deep
end

#has_child?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/extjsml/basenode.rb', line 207

def has_child?
 childs.count > 0 
end

#is_field_element?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/extjsml/basenode.rb', line 103

def is_field_element?
  ExtUtil.field_xtype.include? self.xtype
end

#override_config(h) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/extjsml/basenode.rb', line 199

def override_config(h)
  if not @config[:cls].nil? and not h[:cls].nil?
    @config[:cls] += " #{h[:cls]}"
    delete h[:cls]
  end
  @config.merge! h 
end

#prepare_config(h) ⇒ Object



90
91
92
# File 'lib/extjsml/basenode.rb', line 90

def prepare_config(h)
   @default_config.merge! @@magic_config
end

#remove_childs(xtype, options = {:recursive => 1}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/extjsml/basenode.rb', line 152

def remove_childs(xtype, options = {:recursive => 1})
  return false unless has_child?
  opt = options
  if opt[:recursive]
    opt[:recursive] -= 1
  end
  new_childs = []
  childs.each do |c|
    c.remove_childs xtype if opt[:recursive] > 0
    unless c.xtype == xtype 
      new_childs << c
    end
  end
  self.childs = new_childs
end

#remove_config(key) ⇒ Object



195
196
197
# File 'lib/extjsml/basenode.rb', line 195

def remove_config(key)
  @config.delete key
end

#root?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/extjsml/basenode.rb', line 78

def root?
	@parent.nil?
end

#set_parent(node) ⇒ Object



74
75
76
# File 'lib/extjsml/basenode.rb', line 74

def set_parent(node)
	@parent = node	
end

#to_extjs(at_deep = 0) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/extjsml/basenode.rb', line 211

def to_extjs(at_deep = 0)
  # if self.before_filters.count > 0
  #   self.before_filters.each do |method|
  #     self.send method, at_deep
  #   end
  # end

  ref = conv_id_to_ref
  # if not root? and ref
    # TODO skip if id is the same of instance method
    # puts "../"*(at_deep == 0 ? 0 : at_deep-1 ) + conv_id_to_ref unless @config[:id].nil?
    # @config = @config.merge({ :ref => "../"*(at_deep-1) + conv_id_to_ref }) unless @config[:id].nil?
  # end

  # not gen id
  @config[:cmp_id] = conv_id_to_ref
  unless @@generator[:noid].nil? 
    # skip if force gen id
    unless @config[:forceId]
      # remove id
      @config.delete :id
      # @config[:id] = ExtUtil.random_id
    end
    # gen random id for ref
  end

  collect_events
  # TODO remove ref for *column xtype ?
  collect_ref(ref)

  if has_child?
    child_h = { :items => [] }
    childs.each do |c|
      child_h[:items] << c.to_extjs(at_deep + 1) 
    end
    h = {:xtype => @xtype}.merge(@config).merge(child_h)
  else
    {:xtype => @xtype}.merge(@config)
  end
end