Class: DashcodeConverter::Nib::NibItem

Inherits:
Object
  • Object
show all
Defined in:
lib/dashcode-converter/nib/nib-item.rb,
lib/dashcode-converter/nib/collection-view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, nib) ⇒ NibItem

Returns a new instance of NibItem.



9
10
11
12
13
14
15
# File 'lib/dashcode-converter/nib/nib-item.rb', line 9

def initialize(name, nib)
  @name= name
  @nib= nib
  @view= nil
  base_classname= ""
  @spec= {}
end

Instance Attribute Details

#classnameObject

Returns the value of attribute classname.



7
8
9
# File 'lib/dashcode-converter/nib/nib-item.rb', line 7

def classname
  @classname
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/dashcode-converter/nib/nib-item.rb', line 6

def name
  @name
end

#nibObject (readonly)

Returns the value of attribute nib.



6
7
8
# File 'lib/dashcode-converter/nib/nib-item.rb', line 6

def nib
  @nib
end

#specObject

Returns the value of attribute spec.



7
8
9
# File 'lib/dashcode-converter/nib/nib-item.rb', line 7

def spec
  @spec
end

#viewObject

Returns the value of attribute view.



7
8
9
# File 'lib/dashcode-converter/nib/nib-item.rb', line 7

def view
  @view
end

Instance Method Details

#adjust_declaration_for_CollectionView(decl) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dashcode-converter/nib/collection-view.rb', line 7

def adjust_declaration_for_CollectionView(decl)
  if decl.include?("dataArrayBinding")
    decl.delete("dataArray")

    array_controller_name= nib.unique_name(name[1..-1])
    array_controller= NibItem.new(array_controller_name, nib)
    array_controller.classname="coherent.ArrayController"
    array_controller.spec= {
        "contentBinding" => decl.delete("dataArrayBinding")
      }
    nib.add_item(array_controller)
    nib.add_owner_reference(array_controller_name,array_controller_name)
    
    decl["contentBinding"]= {
      :keypath => "#{array_controller_name}.arrangedObjects"
    }
    decl["selectionIndexesBinding"]= {
      :keypath => "#{array_controller_name}.selectionIndexes"
    }
  end
  
  decl.delete("useDataSource")
  decl.delete("sampleRows")
  decl.delete("labelElementId")
  decl.delete("listStyle")
  decl["content"]= decl.delete("dataArray") if decl.include?("dataArray")
  decl
end

#base_classnameObject



24
25
26
# File 'lib/dashcode-converter/nib/nib-item.rb', line 24

def base_classname
  @base_classname ||= @classname.split(".")[-1]
end

#declarationObject



17
18
19
20
21
22
# File 'lib/dashcode-converter/nib/nib-item.rb', line 17

def declaration
  return @declaration if @declaration
  
  values= @spec.to_json(JSON_PARAMS)
  @declaration= "'#{name}': #{@classname}(#{values})"
end

#fixup_html(html) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dashcode-converter/nib/nib-item.rb', line 28

def fixup_html(html)
  if 'coherent.Button'==@classname
    html.name='button'
  end
  if @spec.include?('text')
    html.content= @spec.delete('text')
  end

  # allow class specific fixups
  if self.respond_to?("fixup_html_for_#{base_classname}")
    self.send("fixup_html_for_#{base_classname}", html)
  end
end

#fixup_html_for_CollectionView(html) ⇒ Object

Need to fix up the bindings for CollectionViews because Coherent before version 3.0 used a whacky star notation for binding to values in the list view.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dashcode-converter/nib/collection-view.rb', line 39

def fixup_html_for_CollectionView(html)
  template= View.new(nib.unique_name("#{name[1..-1]}Item"), nib)
  template.is_template= true
  nib.add_view(template)
  @spec["viewTemplate"]= JavascriptCode("REF('#{template.name}')")
  
  html.css("[id]").each { |node|
    next unless (item= view.items_by_id["##{node["id"]}"])
    view.remove_item(item)
    template.add_item(item)
  }
end

#parse_spec(spec) ⇒ Object



42
43
44
45
46
47
48
49
50
51
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
# File 'lib/dashcode-converter/nib/nib-item.rb', line 42

def parse_spec(spec)
  nib_item= {}
  spec.each { |key, value|
    next if ["view", "Class", "propertyValues"].include?(key)
  
    # translate Dashcode's onclick parameter into a target/action pair
    if "onclick"==key
      key="action"
      nib_item["target"]= "owner"
      nib.owner.add_action_method(value)
    end

    nib_item[key]= value
  }
  nib_item.merge!(spec['propertyValues']) if spec['propertyValues']

  nib_item.each { |key, value|
    # rewrite relative bindings to use representedObject which is the
    # way this works in Coherent 3.0.
    if key =~ /Binding$/ && value.include?("keypath")
      value["keypath"]= value["keypath"].gsub(/^\*\./, "representedObject.")
    end
  }

  parts= (spec['view']||spec['Class']).split(".")
  if 2==parts.length
    parts[0]= "coherent" if parts[0]=="DC"
    parts[1]= CLASSNAME_LOOKUP[parts[1]] if CLASSNAME_LOOKUP.include?(parts[1])
  end

  @classname= parts.join(".")

  if self.respond_to?("adjust_declaration_for_#{base_classname}")
    nib_item= self.send("adjust_declaration_for_#{base_classname}", nib_item)
  end

  @spec= nib_item
end