Module: AttributeScope

Included in:
ViewScope
Defined in:
lib/volt/server/html_parser/attribute_scope.rb

Overview

Included into ViewScope to provide processing for attributes

Instance Method Summary collapse

Instance Method Details

#add_id_to_attributes(attributes) ⇒ Object



100
101
102
103
104
105
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 100

def add_id_to_attributes(attributes)
  id = attributes['id'] ||= "id#{@binding_number}"
  @binding_number += 1

  return id.to_s
end

#add_multiple_attribute(tag_name, id, attribute_name, parts, content) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 64

def add_multiple_attribute(tag_name, id, attribute_name, parts, content)
  case attribute_name
  when 'checked', 'value'
    if parts.size > 1
      if tag_name == 'textarea'
        raise "The content of text area's can not be bound to multiple bindings."
      else
        # Multiple ReactiveValue's can not be passed to value or checked attributes.
        raise "Multiple bindings can not be passed to a #{attribute_name} binding."
      end
    end
  end

  reactive_template_path = add_reactive_template(content)

  save_binding(id, "lambda { |__p, __t, __c, __id| AttributeBinding.new(__p, __t, __c, __id, #{attribute_name.inspect}, Proc.new { ReactiveTemplate.new(__p, __c, #{reactive_template_path.inspect}) }) }")
end

#add_reactive_template(content) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 82

def add_reactive_template(content)
  path = @path + "/_rv#{@binding_number}"
  new_handler = ViewHandler.new(path, false)
  @binding_number += 1

  SandlebarsParser.new(content, new_handler)

  # Close out the last scope
  new_handler.scope.last.close_scope

  # Copy in the templates from the new handler
  new_handler.templates.each_pair do |key, value|
    @handler.templates[key] = value
  end

  return path
end

#add_single_attribute(id, attribute_name, parts) ⇒ Object

Add an attribute binding on the tag, bind directly to the getter in the binding



57
58
59
60
61
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 57

def add_single_attribute(id, attribute_name, parts)
  getter = parts[0][1..-2]

  save_binding(id, "lambda { |__p, __t, __c, __id| AttributeBinding.new(__p, __t, __c, __id, #{attribute_name.inspect}, Proc.new { #{getter} }) }")
end

#attribute_string(attributes) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 107

def attribute_string(attributes)
  attr_str = attributes.map {|v| "#{v[0]}=\"#{v[1]}\"" }.join(' ')
  if attr_str.size > 0
    # extra space
    attr_str = " " + attr_str
  end

  return attr_str
end

#process_attribute(tag_name, attributes, attribute_name, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 34

def process_attribute(tag_name, attributes, attribute_name, value)
  parts = value.split(/(\{[^\}]+\})/).reject(&:blank?)
  binding_count = parts.count {|p| p[0] == '{' && p[-1] == '}'}

  # if this attribute has bindings
  if binding_count > 0
    # Setup an id
    id = add_id_to_attributes(attributes)

    if parts.size > 1
      # Multiple bindings
      add_multiple_attribute(tag_name, id, attribute_name, parts, value)
    elsif parts.size == 1 && binding_count == 1
      # A single binding
      add_single_attribute(id, attribute_name, parts)
    end

    # Remove the attribute
    attributes.delete(attribute_name)
  end
end

#process_attributes(tag_name, attributes) ⇒ Object

Take the attributes and create any bindings



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 4

def process_attributes(tag_name, attributes)
  new_attributes = attributes.dup

  attributes.each_pair do |name, value|
    if name[0..1] == 'e-'
      process_event_binding(tag_name, new_attributes, name, value)
    else
      process_attribute(tag_name, new_attributes, name, value)
    end
  end

  return new_attributes
end

#process_event_binding(tag_name, attributes, name, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/volt/server/html_parser/attribute_scope.rb', line 18

def process_event_binding(tag_name, attributes, name, value)
  id = add_id_to_attributes(attributes)

  event = name[2..-1]

  if tag_name == 'a'
    # For links, we need to add blank href to make it clickable.
    attributes['href'] ||= ''
  end

  # Remove the e- attribute
  attributes.delete(name)

  save_binding(id, "lambda { |__p, __t, __c, __id| EventBinding.new(__p, __t, __c, __id, #{event.inspect}, Proc.new {|event| #{value} })}")
end