Class: Comet::SourceGenerator
- Inherits:
-
Object
- Object
- Comet::SourceGenerator
- Defined in:
- lib/comet-html/source-generator.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #dom_generator(el, indent) ⇒ Object
- #generate ⇒ Object
- #generate_anchors_initializers ⇒ Object
- #generate_binding_initializers ⇒ Object
- #generate_constructor_body ⇒ Object
- #generate_constructor_footer ⇒ Object
- #generate_constructor_header ⇒ Object
- #generate_dom_constructor ⇒ Object
- #generate_element_initializers ⇒ Object
- #generate_method_bind_attributes ⇒ Object
- #generate_method_trigger_binding_updates ⇒ Object
- #get_root_from_parent_code(parent_symbol) ⇒ Object
- #inherits_binding_methods? ⇒ Boolean
-
#initialize(object) ⇒ SourceGenerator
constructor
A new instance of SourceGenerator.
- #make_parent_to_root_initializer(parent_symbol) ⇒ Object
- #root_ptr ⇒ Object
- #tag_name ⇒ Object
Constructor Details
#initialize(object) ⇒ SourceGenerator
Returns a new instance of SourceGenerator.
5 6 7 |
# File 'lib/comet-html/source-generator.rb', line 5 def initialize object @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
3 4 5 |
# File 'lib/comet-html/source-generator.rb', line 3 def object @object end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
3 4 5 |
# File 'lib/comet-html/source-generator.rb', line 3 def source @source end |
Instance Method Details
#dom_generator(el, indent) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/comet-html/source-generator.rb', line 170 def dom_generator el, indent result = "" count = 0 el.children.each do |el| sub_object = object.find_class_for el if !sub_object.nil? && sub_object.is_anchorable? result += "," if count > 0 result += "\n" + (" " * indent) result += sub_object.anchor_name count += 1 elsif !(el["_cheerp_class"].nil?) next elsif el.name != "text" && el.name != "comment" reference = object.find_reference_for(el) html_attributes = make_attr_from_el el result += "," if count > 0 result += "\n" + (" " * indent) result += if reference.nil? then "Comet::Element(\"#{el.name}\")" else reference.name end result += ".attr({#{html_attributes.join ','}})" if html_attributes.count > 0 children_result = dom_generator el, indent + 2 result += ".inner({#{children_result}\n#{" " * indent}})" if children_result.length > 0 count += 1 elsif el.name == "text" && !(el.text.strip.empty?) escaped_text = el.text.gsub /"/, "\\\"" escaped_text.gsub! "\n", '\n' result += "," if count > 0 result += "\n" + (" " * indent) result += "Comet::Element(\"span\").text(\"#{escaped_text}\")" count += 1 end end result end |
#generate ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/comet-html/source-generator.rb', line 9 def generate @source = generate_constructor_header @source += generate_constructor_body @source += + "\n" @source += generate_method_bind_attributes + "\n" @source += generate_method_trigger_binding_updates @source end |
#generate_anchors_initializers ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/comet-html/source-generator.rb', line 207 def generate_anchors_initializers result = "" object.repeaters.each do |repeater| result += " #{repeater.repeater_name}.set_anchor(#{repeater.anchor_name}, #{anchor_symbol_to_enum :append});\n" end object.slots.each do |slot| result += " #{slot.slot_ref}.set_anchor(#{slot.anchor_name}, #{anchor_symbol_to_enum :append});\n" result += " #{slot.slot_ref}.set_element(std::make_shared<#{slot.typename}>(this));\n" end object.slot_plugins.each do |slot_plugin| ref = object.find_reference_for(slot_plugin.on_element) initializer = if slot_plugin.has_ref? "root->#{slot_plugin.el["ref"]}" else "std::make_shared<#{slot_plugin.typename}>(#{slot_plugin.constructor_params})" end result += " #{ref.name}.slot_#{slot_plugin.slot_name}.set_element(#{initializer});\n" end result end |
#generate_binding_initializers ⇒ Object
119 120 121 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 151 152 153 154 155 156 157 158 |
# File 'lib/comet-html/source-generator.rb', line 119 def generate_binding_initializers result = "" object.bindings.each do |binding| ref = object.find_reference_for binding.el initializer = if binding.binds_to_cpp_property? "Comet::Bindable([this]() { #{ref.name}.set_#{binding.attribute_name}(#{binding.code}); })" elsif binding.attribute_name == "show" "Comet::Bindable([this]() { #{ref.name}.visible(#{binding.code}); })" elsif binding.attribute_name == "text" "Comet::Bindable([this]() { #{ref.name}.text(#{binding.code}); })" elsif binding.attribute_name == "innerhtml" "Comet::Bindable([this]() { #{ref.name}.html(#{binding.code}); })" else "Comet::Bindable(#{ref.name}, \"#{binding.attribute_name}\", [this]() -> std::string { return Comet::lexical_cast<std::string>(#{binding.code}); })" end result += " bound_attributes.push_back(#{initializer}#{binding.bind_mode});\n" end object.repeaters.each do |repeater| refresh_code = "#{repeater.repeater_name}.refresh(this, #{repeater.list_name});" initializer = "Comet::Bindable([this]() { #{refresh_code} })" result += " bound_attributes.push_back(#{initializer}#{repeater.bind_mode});\n" end object.event_listeners.each do |event_listener| ref = object.find_reference_for event_listener.el unless event_listener.is_cpp result += " #{ref.name}.events->on(\"#{event_listener.attribute_name}\", [this](client::Event* _event) { #{event_listener.code}; });\n" else result += " #{ref.name}.signaler.connect([this](std::string signal_name)\n" result += " {\n" result += " if (signal_name == \"#{event_listener.attribute_name}\")\n" result += " {\n" result += " #{event_listener.code};\n" result += " }\n" result += " });\n" end end result end |
#generate_constructor_body ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/comet-html/source-generator.rb', line 95 def generate_constructor_body result = generate_element_initializers result += generate_binding_initializers result += generate_dom_constructor result += generate_anchors_initializers result end |
#generate_constructor_footer ⇒ Object
103 104 105 106 107 |
# File 'lib/comet-html/source-generator.rb', line 103 def result = "" result += "}\n" result end |
#generate_constructor_header ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/comet-html/source-generator.rb', line 52 def generate_constructor_header initializers = [] parent_symbol = "__p" root_getter = get_root_from_parent_code parent_symbol case object.superclass when object.context.template_base_type then initializers << "#{object.context.template_base_type}(\"#{tag_name}\")" # when object.context.template_base_subtype then # initializers << "#{object.context.template_base_subtype}(\"#{tag_name}\", #{parent_symbol}->signaler)" end # Constructor result = "HtmlTemplate::#{object.typename}::#{object.typename}(" unless object.is_root? initializers << make_parent_to_root_initializer(parent_symbol) result += "#{object.parent.typename}* #{parent_symbol}" else initializers << "root(this)" end (object.refs.select{|r| r.has_initializer?}).each do |ref| initializers << "#{ref.name}(#{ref.initializer root_getter})" end unless object.is_root? initializers << "signaler(#{parent_symbol}->signaler)" unless object.implements_ibindable_view? initializers << "parent(#{parent_symbol})" end if object.class == Repeater initializers << "#{object.value_name}(__i)" result += ", #{object.value_type} __i" end unless object.is_root? object.slots.each do |slot| initializers << "#{slot.slot_ref}(#{root_getter}->#{slot.slot_ref})" end end result += ")" result += " : " + initializers.join(", ") if initializers.count > 0 result += "\n" result += "{\n" result end |
#generate_dom_constructor ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/comet-html/source-generator.rb', line 160 def generate_dom_constructor result = "" source_el = if object.is_root? then object.el.first else object.el end html_attributes = make_attr_from_el source_el result += " attr({#{html_attributes.join ','}});\n" if html_attributes.length > 0 dom_code = dom_generator object.el, 4 result += " inner({#{dom_code}\n });\n" unless dom_code.empty? result end |
#generate_element_initializers ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/comet-html/source-generator.rb', line 109 def generate_element_initializers result = "" object.refs.each do |ref| if ref.type == "Comet::Element" result += " #{ref.name} = Comet::Element(\"#{ref.el.name}\");\n" end end result end |
#generate_method_bind_attributes ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/comet-html/source-generator.rb', line 234 def generate_method_bind_attributes result = "" result += "void HtmlTemplate::#{object.typename}::bind_attributes()\n" result += "{\n" if inherits_binding_methods? result += " #{object.superclass}::bind_attributes();\n" else result += " bound_attributes.enable(signaler);\n" end object.refs.each do |ref| if ref.el && object.context.has_cpp_type?(ref.el) result += " #{ref.name}.bind_attributes();\n" result += " signaler.connect([this](std::string _event) { #{ref.name}.signaler.trigger(_event); });\n" end end object.repeaters.each do |repeater| result += " #{repeater.repeater_name}.bind_attributes();\n" end object.slots.each do |slot| result += " if (#{slot.slot_ref}.has_element())\n" result += " #{slot.slot_ref}.get_element()->bind_attributes();\n" end result += "}\n" result end |
#generate_method_trigger_binding_updates ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/comet-html/source-generator.rb', line 260 def generate_method_trigger_binding_updates result = "" result += "void HtmlTemplate::#{object.typename}::trigger_binding_updates()\n" result += "{\n" if inherits_binding_methods? result += " #{object.superclass}::trigger_binding_updates();\n" else result += " bound_attributes.update();\n" end object.refs.each do |ref| if ref.el && object.context.has_cpp_type?(ref.el) result += " #{ref.name}.trigger_binding_updates();\n" end end object.repeaters.each do |repeater| result += " #{repeater.repeater_name}.trigger_binding_updates();\n" end object.slots.each do |slot| result += " if (#{slot.slot_ref}.has_element())\n" result += " #{slot.slot_ref}.get_element()->trigger_binding_updates();\n" end result += "}\n" result end |
#get_root_from_parent_code(parent_symbol) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/comet-html/source-generator.rb', line 34 def get_root_from_parent_code parent_symbol if object.is_root? "this" else result = parent_symbol current_parent = object.parent until current_parent.parent.nil? result += "->parent" current_parent = current_parent.parent end result end end |
#inherits_binding_methods? ⇒ Boolean
230 231 232 |
# File 'lib/comet-html/source-generator.rb', line 230 def inherits_binding_methods? !([object.context.template_base_type, object.context.template_base_subtype].include? object.superclass) end |
#make_parent_to_root_initializer(parent_symbol) ⇒ Object
48 49 50 |
# File 'lib/comet-html/source-generator.rb', line 48 def make_parent_to_root_initializer parent_symbol "root(#{get_root_from_parent_code(parent_symbol)})" end |
#root_ptr ⇒ Object
30 31 32 |
# File 'lib/comet-html/source-generator.rb', line 30 def root_ptr if object.parent.nil? then "this" else "root" end end |
#tag_name ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/comet-html/source-generator.rb', line 18 def tag_name if @object.parent.nil? if @object.el.first["tag-name"].nil? @object.typename.dasherize else @object.el.first["tag-name"] end else @object.el.name end end |