Class: HWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/hwidgets/hwidget.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag = nil, innerHTML = "", **args) ⇒ HWidget

Returns a new instance of HWidget.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hwidgets/hwidget.rb', line 65

def initialize(tag = nil, innerHTML = "", **args)
  @tag = tag
  @properties = {}
  @systemProperties = {}
  @placeholders = {}
  @innerHTML = innerHTML
  @parent = nil
  @childs = []
  @styles = {}
  @enablePlaceholder = false # va automaticamente a true con setPlaceholder
  @closeTag = true # se false il tag non viene chiuso - serve ad es. per input
  @slots = {}

  self.set(args)

end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



4
5
6
# File 'lib/hwidgets/hwidget.rb', line 4

def tag
  @tag
end

Class Method Details

.testObject



374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/hwidgets/hwidget.rb', line 374

def self.test()
  widget = HWidget.new('div', "new_div")
  widget.set(title1: 't1', 
             title2: 't2',
             title3: 't3',
             title4: '')
  widget.set(title2: nil)
  widget.setStyle(color: 'red')
  puts widget.openTag
  puts widget.html
  puts widget.closeTag
end

.widgetSpaceObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/hwidgets/hwidget.rb', line 388

def self.widgetSpace()
  
  puts "###########################################################".hight_white
  puts "#                                                         #".hight_white
  puts "#                 Hypersonic Object Space                 #".hight_white
  puts "#                                                         #".hight_white
  puts "###########################################################".hight_white

  objs = []
  ObjectSpace.each_object() {|obj| objs << obj if obj.class <= HWidget }
  objs.each do |obj|
    puts "#{obj.object_id.to_s.hight_purple}: #{obj.class.to_s.yellow} - #{obj.tag.hight_cyan} #{obj.get(:class)}" 
  end
  
  #puth ObjectSpace.count_objects

  return "Total Widgets: #{objs.length}".green

end

Instance Method Details

#_addJsSlot(eventName, functionName, overwrite = false, *args) ⇒ Object

supporta piu’ funzioni per lo stesso evento



181
182
183
184
185
186
187
188
# File 'lib/hwidgets/hwidget.rb', line 181

def _addJsSlot(eventName, functionName, overwrite = false, *args)      

  @slots.delete(eventName) if overwrite
  @slots[eventName] = Array.new() unless(@slots[eventName])
  @slots[eventName] << {functionName: functionName, args: args}
  return self

end

#_set(property, value, overwrite = false) ⇒ Object



125
126
127
128
129
# File 'lib/hwidgets/hwidget.rb', line 125

def _set(property, value, overwrite = false)      
  @properties[property] = value if (!@properties.key?(property) || overwrite)
  #@properties.delete(property) unless (value)
  return self
end

#_setStyle(property, value) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# File 'lib/hwidgets/hwidget.rb', line 312

def _setStyle(property, value)
  
  unless (value)
    @styles.delete(property)
    return self
  end
  @styles[property] = value
  return self

end

#addJsFunction(eventName, functionName, *args) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/hwidgets/hwidget.rb', line 156

def addJsFunction(eventName, functionName, *args)      

  function = ""
  if(@properties[eventName])
    function = "#{@properties[eventName]}; "
  end
  
  function = self.buildSignature(function, functionName, *args)
  
  return self.set("#{eventName}": function, overwrite: true) 

end

#appendChild(widget) ⇒ Object Also known as: <<



113
114
115
116
117
118
# File 'lib/hwidgets/hwidget.rb', line 113

def appendChild(widget)
  return self.appendChilds(widget) if widget.class == Array
  widget.setParent(self)
  @childs << widget
  return self
end

#appendChilds(widgets) ⇒ Object



108
109
110
111
# File 'lib/hwidgets/hwidget.rb', line 108

def appendChilds(widgets)
  widgets.each { |widget| self.appendChild(widget) }
  return self
end

#buildSignature(function, functionName, *args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hwidgets/hwidget.rb', line 141

def buildSignature(function, functionName, *args) 
  
  params = ['this']
  args.each do |arg|
    if(arg.class == Symbol)
      params << arg # se passo un simbolo ad es. event
    else
      params << arg.to_js_format
    end
  end
  return "#{function}#{functionName}(#{params.join(",")})"

end

#closeTagObject



294
295
296
297
# File 'lib/hwidgets/hwidget.rb', line 294

def closeTag()
  return "" unless (@tag)
  return (@closeTag) ? HIO.htmlEcholnCloseBlock("</#{@tag}>") : ""
end

#connect(event, receiver, method, attributes: :null, id: "#id", args: nil, overwrite: false, **options) ⇒ Object

id deve essere un oid



232
233
234
235
236
237
238
239
240
241
# File 'lib/hwidgets/hwidget.rb', line 232

def connect(event, receiver, method, attributes: :null, id: "#id", args: nil, overwrite: false, **options)      
  oid = hm().malloc({receiver: receiver, method: method, args: args}, id).obj.object_id.to_s
  options[:getElem] = "byId" unless options[:getElem]
  options[:mode] = "set" unless options[:mode]

  cks = Digest::SHA256.hexdigest oid + hc.value("secret_token")
  cks += "-" + Time.now.to_i.to_s # serve (forse) in caso di riallocazione dello stesso indirizzo di memoria
  hotLog( "HWidget::connect(id: #{id}, oid: #{oid} - #{options[:hotLog].green}" ) if options[:hotLog]
  return self._addJsSlot(event, :'return hajax.signalManager', overwrite, attributes, options, id, oid, cks)
end

#copyConstructorObject



82
83
84
# File 'lib/hwidgets/hwidget.rb', line 82

def copyConstructor
  return Marshal.load(Marshal.dump(self))
end

#get(property) ⇒ Object



243
244
245
# File 'lib/hwidgets/hwidget.rb', line 243

def get(property)      
    return @properties[property]
end

#getChildsObject



41
42
43
# File 'lib/hwidgets/hwidget.rb', line 41

def getChilds()   
  return @childs
end

#getElementBy(property, value, searchType) ⇒ Object

searchType 0: serch only among childs searchType 1: search top down starting from childs



97
98
99
100
101
102
103
104
105
106
# File 'lib/hwidgets/hwidget.rb', line 97

def getElementBy(property, value, searchType)
 
  result = (self.properties.key?(property) and self.properties[properties] == value) ? [self] : nil
  @childs.each do |child|
    result << child if child.properties.key?(property) and child.properties[property] == value
  end
  @childs.each { |child| result += child.getElementBy(property, value, searchType) } if searchType == 1
  return result

end

#getSystemProperty(property) ⇒ Object



365
366
367
# File 'lib/hwidgets/hwidget.rb', line 365

def getSystemProperty(property)      
    return @systemProperties[property]
end

#hotLog(str) ⇒ Object



258
259
260
# File 'lib/hwidgets/hwidget.rb', line 258

def hotLog(str)
  HHotLogger.append(str, "log4widgets")
end

#htmlObject



304
305
306
307
308
309
310
# File 'lib/hwidgets/hwidget.rb', line 304

def html() 
  
  html = @innerHTML
  @childs.each { |child| html += child.html() }
  return self.openTag() + HIO.htmlEcholn(html) + self.closeTag()  

end

#openTagObject



284
285
286
287
288
289
290
291
292
# File 'lib/hwidgets/hwidget.rb', line 284

def openTag()
  
  return "" unless (@tag)

  slash = (@closeTag) ? "" : "/"
  result = self.strProperties()
  return HIO.htmlEcholnOpenBlock("<#{@tag} #{result}#{slash}>")

end

#replacePlaceholder(str) ⇒ Object



247
248
249
250
251
252
253
254
255
256
# File 'lib/hwidgets/hwidget.rb', line 247

def replacePlaceholder(str)

  return str unless @enablePlaceholder
  @placeholders.each do |key, value|
    str = str.gsub(key, value.to_s)
  end
   
  return str

end

#resetObject



299
300
301
302
# File 'lib/hwidgets/hwidget.rb', line 299

def reset()
  @parent = nil
  @childs = []
end

#set(overwrite: false, **properties) ⇒ Object

set property



132
133
134
135
136
137
138
139
# File 'lib/hwidgets/hwidget.rb', line 132

def set(overwrite: false, **properties)
    
  properties.each do |property, value|
    self._set(property, value, overwrite)
  end
  return self

end

#setChilds(childs) ⇒ Object



36
37
38
39
# File 'lib/hwidgets/hwidget.rb', line 36

def setChilds(childs)   
  @childs = childs
  return self
end

#setClosedTag(bool = true) ⇒ Object



369
370
371
372
# File 'lib/hwidgets/hwidget.rb', line 369

def setClosedTag(bool=true)
  @closeTag = bool
  return self
end

#setCloseTag(closeTag) ⇒ Object



55
56
57
58
# File 'lib/hwidgets/hwidget.rb', line 55

def setCloseTag(closeTag)   
  @closeTag = closeTag
  return self
end

#setEnablePlaceholder(enablePlaceholder = true) ⇒ Object



50
51
52
53
# File 'lib/hwidgets/hwidget.rb', line 50

def setEnablePlaceholder(enablePlaceholder = true)   
  @enablePlaceholder = enablePlaceholder
  return self
end

#setInnerHTML(innerHTML) ⇒ Object



26
27
28
29
# File 'lib/hwidgets/hwidget.rb', line 26

def setInnerHTML(innerHTML)   
  @innerHTML = innerHTML
  return self
end

#setParent(parent) ⇒ Object



31
32
33
34
# File 'lib/hwidgets/hwidget.rb', line 31

def setParent(parent)   
  @parent = parent
  return self
end

#setPlaceholder(placeholder, value) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
# File 'lib/hwidgets/hwidget.rb', line 344

def setPlaceholder(placeholder, value)

  @enablePlaceholder = true
  unless (value)
    @placeholders.delete(placeholder)
    return self
  end
  @placeholders[placeholder] = value
  return self

end

#setPlaceholders(placeholders) ⇒ Object



21
22
23
24
# File 'lib/hwidgets/hwidget.rb', line 21

def setPlaceholders(placeholders)
  @placeholders = placeholders
  return self
end

#setProperties(properties) ⇒ Object



11
12
13
14
# File 'lib/hwidgets/hwidget.rb', line 11

def setProperties(properties)
  @properties = properties
  return self
end

#setSlots(slots) ⇒ Object



60
61
62
63
# File 'lib/hwidgets/hwidget.rb', line 60

def setSlots(slots)   
  @slots = slots
  return self
end

#setStyle(**properties) ⇒ Object

set style



324
325
326
327
328
329
# File 'lib/hwidgets/hwidget.rb', line 324

def setStyle(**properties)
  properties.each do |property, value|
    self._setStyle(property, value)
  end
  return self
end

#setStyles(styles) ⇒ Object



45
46
47
48
# File 'lib/hwidgets/hwidget.rb', line 45

def setStyles(styles)   
  @styles = styles
  return self
end

#setSystemProperties(systemProperties) ⇒ Object



16
17
18
19
# File 'lib/hwidgets/hwidget.rb', line 16

def setSystemProperties(systemProperties)
  @systemProperties = systemProperties
  return self
end

#setSystemProperty(property, value) ⇒ Object



356
357
358
359
360
361
362
363
# File 'lib/hwidgets/hwidget.rb', line 356

def setSystemProperty(property, value)      
    unless (value)
      @systemProperties.delete(property)
      return self
    end
    @systemProperties[property] = value
    return self
end

#setTag(tag) ⇒ Object



6
7
8
9
# File 'lib/hwidgets/hwidget.rb', line 6

def setTag(tag)
  @tag = tag
  return self
end

#storeSlotsObject



169
170
171
172
173
174
175
176
177
# File 'lib/hwidgets/hwidget.rb', line 169

def storeSlots  

  @slots.each do |key, elem|
    elem.each do |value|
      self.addJsFunction(key, value[:functionName], *value[:args])
    end
  end

end

#storeStyleObject



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/hwidgets/hwidget.rb', line 331

def storeStyle() 

  return "" if @styles.empty?

  result = ""
  @styles.each do |key, value| 
    result += "#{key}:#{value}; "
  end

  self.set(style: result)

end

#strPropertiesObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/hwidgets/hwidget.rb', line 262

def strProperties() 

  propertiesTmp = Marshal.load(Marshal.dump(@properties))
  self.storeSlots()
  self.storeStyle()
  return "" if @properties.empty?

  result = ""
  @properties.each do |key, value| 
    if(value)
      value = self.replacePlaceholder(value)
      result += "#{key}=\"#{value}\" "
    else
      result += "#{key} "
    end
  end

  @properties = propertiesTmp
  return result

end

#unset(property) ⇒ Object



121
122
123
# File 'lib/hwidgets/hwidget.rb', line 121

def unset(property)      
    @properties.delete(property)
end