Module: Hyalite

Defined in:
lib/hyalite.rb,
lib/hyalite/dom.rb,
lib/hyalite/main.rb,
lib/hyalite/mount.rb,
lib/hyalite/utils.rb,
lib/hyalite/logger.rb,
lib/hyalite/element.rb,
lib/hyalite/updates.rb,
lib/hyalite/version.rb,
lib/hyalite/component.rb,
lib/hyalite/dom/event.rb,
lib/hyalite/reconciler.rb,
lib/hyalite/short_hand.rb,
lib/hyalite/dom_property.rb,
lib/hyalite/update_queue.rb,
lib/hyalite/browser_event.rb,
lib/hyalite/dom_component.rb,
lib/hyalite/input_wrapper.rb,
lib/hyalite/dom_operations.rb,
lib/hyalite/multi_children.rb,
lib/hyalite/text_component.rb,
lib/hyalite/synthetic_event.rb,
lib/hyalite/event_dispatcher.rb,
lib/hyalite/instance_handles.rb,
lib/hyalite/internal_component.rb,
lib/hyalite/linked_value_utils.rb,
lib/hyalite/composite_component.rb,
lib/hyalite/dom/event/drag_event.rb,
lib/hyalite/dom/event/focus_event.rb,
lib/hyalite/dom/event/mouse_event.rb,
lib/hyalite/dom/event/touch_event.rb,
lib/hyalite/reconcile_transaction.rb,
lib/hyalite/dom/event/data_transfer.rb,
lib/hyalite/dom_property_operations.rb,
lib/hyalite/dom/event/keyboard_event.rb,
lib/hyalite/dom/event/mouse_event_interface.rb,
lib/hyalite/event_plugin/change_event_plugin.rb,
lib/hyalite/event_plugin/simple_event_plugin.rb,
lib/hyalite/event_plugin/event_plugin_registry.rb

Defined Under Namespace

Modules: BrowserEvent, Component, DOM, DOMOperations, DOMProperty, DOMPropertyOperations, Dom, InstanceHandles, InternalComponent, LinkedValueUtils, Logger, Mount, MultiChildren, Reconciler Classes: ChangeEventPlugin, CompositeComponent, DOMComponent, ElementObject, EmptyComponent, EventDispatcher, EventPluginRegistry, HookSetter, InputWrapper, ReconcileTransaction, SimpleEventPlugin, SyntheticEvent, TextComponent, TopLevelWrapper, UpdateQueue, Updates

Constant Summary collapse

RESERVED_PROPS =
[:key, :ref, :children]
ESCAPE_LOOKUP =
{
  '&' => '&',
  '>' => '>',
  '<' => '&lt;',
  '"' => '&quot;',
  '\'' => '&#x27;',
}
ESCAPE_REGEX =
/[&><"']/
VERSION =
"0.3.4"

Class Method Summary collapse

Class Method Details

.create_element(type, config = nil, *children) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hyalite/main.rb', line 15

def create_element(type, config = nil, *children)
  key = nil
  ref = nil

  props = {}

  case config
  when String
    children = [config]
  when Array
    children = config
  when Hash
    key = config[:key]
    ref = config[:ref]

    config.each do |name, value|
      unless RESERVED_PROPS.include?(name)
        props[name] = config[name];
      end
    end
  end

  props[:children] = case children.length
                     when 0
                       nil
                     when 1
                       children.first
                     else
                       children
                     end

  ElementObject.new(type, key, ref, Hyalite.current_owner, props).tap do |el|
    children.each {|child| child.parent = el if child.is_a?(ElementObject) }
    element_created(el)
  end
end

.create_element_hook(key, &block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/hyalite/main.rb', line 59

def create_element_hook(key, &block)
  @hooks ||= []
  hook_setter = HookSetter.new(@hooks)
  yield hook_setter
ensure
  hook_setter.destroy
end

.current_owner(current_owner = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/hyalite/main.rb', line 143

def current_owner(current_owner = nil)
  if current_owner && block_given?
    begin
      @current_owner = current_owner
      yield(@current_owner)
    ensure
      @current_owner = nil
    end
  else
    @current_owner
  end
end

.element_created(element) ⇒ Object



52
53
54
55
56
57
# File 'lib/hyalite/main.rb', line 52

def element_created(element)
  return unless @hooks
  @hooks.each do |hook|
    hook.call(element)
  end
end

.escape_text_content_for_browser(text) ⇒ Object



16
17
18
# File 'lib/hyalite/utils.rb', line 16

def self.escape_text_content_for_browser(text)
  text.gsub(ESCAPE_REGEX) {|s| ESCAPE_LOOKUP[s] }
end

.find_dom_node(component_or_element) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/hyalite/main.rb', line 156

def find_dom_node(component_or_element)
  return component_or_element if component_or_element.is_a?(DOM::Node) && component_or_element.element?

  if instance_map.has_key?(component_or_element)
    return Mount.node(instance_map[component_or_element].root_node_id)
  end
end

.fn(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hyalite/main.rb', line 85

def fn(&block)
  Class.new {
    include Component
    include Component::ShortHand

    def self.render_proc=(proc)
      @render_proc = proc
    end

    def self.render_proc
      @render_proc
    end

    def render
      self.instance_exec(@props, &self.class.render_proc)
    end
  }.tap{|cl| cl.render_proc = block }
end

.instance_mapObject



139
140
141
# File 'lib/hyalite/main.rb', line 139

def instance_map
  @instance_map ||= {}
end

.instantiate_component(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hyalite/main.rb', line 104

def instantiate_component(node)
  node = EmptyComponent.empty_element if node.nil?

  case node
  when ElementObject
    case node.type
    when String
      DOMComponent.new node
    when Class
      if node.type.include?(InternalComponent)
        node.type.new
      elsif node.type.include?(Component)
        CompositeComponent.new node
      else
        raise "Encountered invalid type of Hyalite node. type: #{node.type}"
      end
    end
  when String, Numeric
    TextComponent.new node
  when EmptyComponent
    CompositeComponent.new node
  else
    raise "Encountered invalid Hyalite node: #{node}"
  end
end

.quote_attribute_value_for_browser(text) ⇒ Object



12
13
14
# File 'lib/hyalite/utils.rb', line 12

def self.quote_attribute_value_for_browser(text)
  "\"#{escape_text_content_for_browser(text)}\""
end

.render(next_element, container, &block) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/hyalite/main.rb', line 130

def render(next_element, container, &block)
  case container
  when DOM::Node
    Mount.render_subtree_into_container(next_element, container, &block)
  when Enumerable
    container.map {|node| render(next_element, node, &block) }
  end
end

.updatesObject



126
127
128
# File 'lib/hyalite/updates.rb', line 126

def self.updates
  @updates ||= Updates.new
end