Class: Spider::TemplateBlocks::Widget

Inherits:
Block show all
Defined in:
lib/spiderfw/templates/blocks/widget.rb

Instance Attribute Summary

Attributes inherited from Block

#allowed_blocks, #doctype, #el, #template

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Block

#compile_content, #compile_text, #escape_text, #get_following, #initialize, #inspect, #parse_content, var_to_scene, #var_to_scene, #vars_to_scene, vars_to_scene

Constructor Details

This class inherits a constructor from Spider::TemplateBlocks::Block

Class Method Details

.attributes_to_init_params(attributes) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/spiderfw/templates/blocks/widget.rb', line 7

def self.attributes_to_init_params(attributes)
    init_params = []
    attributes.each do |key, val|
        if (!val.empty? && val[0].chr == '@')
            sval = var_to_scene(val, 'scene')
        elsif (!val.empty? && val[0].chr == '$')
            sval = val[1..-1]
        else
            sval = '"'+val+'"'
        end
        init_key = key
        init_key = "\"#{init_key}\"" unless key =~ /^[\w\d]+$/
        init_params << ":#{init_key} => #{sval}"
    end
    init_params
end

Instance Method Details

#compile(options = {}) ⇒ Object



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
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
80
# File 'lib/spiderfw/templates/blocks/widget.rb', line 24

def compile(options={})
    klass = Spider::Template.get_registered_class(@el.name)
    init_params = []
    id = @el.get_attribute('id')
    raise TemplateCompileError, "Widget #{@el.name} does not have an id" unless id
    template_attr = @el.get_attribute('template')
    @el.remove_attribute('template')
    if klass.respond_to?(:compile_block)
        init, c = klass.compile_block(@el, id, @el.attributes.to_hash, options)
        return CompiledBlock.new(init, c)
    end

    html = ""
    @el.each_child do |ch|
        html += ch.to_html
    end
    runtime_content, overrides = klass.parse_content_xml(html)

    template = nil
    overrides += @template.overrides_for(id)
    
    asset_profiles = @el.get_attribute('sp:asset-profiles')
    if asset_profiles
        asset_profiles = asset_profiles.split(/,\s*/).map{ |pr| pr.to_sym } 
        @el.remove_attribute('sp:asset-profiles')
    end
    
    template = klass.load_template(template_attr || klass.default_template)
    template.asset_profiles = asset_profiles if asset_profiles
    # @template is the containing template, template is the widget's template
    if overrides.length > 0
        template.add_overrides overrides
        @template.add_subtemplate(id, template, klass)
    else
        @template.add_widget_template(template, klass)
    end

    init = ""
    t_param = 'nil'
    t_options = {}
    
    t_options[:asset_profiles] = asset_profiles if asset_profiles
    
    if (template)
        # FIXME: the subtemplate shouldn't be loaded at this point
        init = "t = load_subtemplate('#{id}', #{t_options.inspect})\n"
        t_param = 't'
    end
    html.gsub!("'", "\\\\'")
    
    init_params = self.class.attributes_to_init_params(@el.attributes.to_hash)
    runtime_content.gsub!("'", "\\\\'") if runtime_content
    
    init += "add_widget('#{id}', #{klass}.new(@request, @response), {#{init_params.join(', ')}}, '#{runtime_content}', #{t_param})\n"
    c = "yield :\"#{id}\"\n"
    return CompiledBlock.new(init, c)
end