Class: MetaRuby::GUI::HTML::Collection

Inherits:
Qt::Object
  • Object
show all
Defined in:
lib/metaruby/gui/html/collection.rb

Overview

Base class providing functionality to first rendering a collection of object, and then render one item in the collection when the user clicks on the collection element

Rendering is delegated to other objects through a RenderingManager. The main interface to the manager is #register_type

Defined Under Namespace

Classes: Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Collection

Create a collection that acts on a page



27
28
29
30
31
32
33
34
# File 'lib/metaruby/gui/html/collection.rb', line 27

def initialize(page)
    super()
    @page = page
    @manager = RenderingManager.new(page)

    @object_id_to_object = Hash.new
    @registered_exceptions = Array.new
end

Instance Attribute Details

#managerRenderingManager (readonly)

Returns the object that manages the rendering objects, i.e. the objects that convert the element collections into HTML.

Returns:

  • (RenderingManager)

    the object that manages the rendering objects, i.e. the objects that convert the element collections into HTML



15
16
17
# File 'lib/metaruby/gui/html/collection.rb', line 15

def manager
  @manager
end

#object_id_to_object{Integer=>Object} (readonly)

Returns mapping from an element’s object_id to the corresponding object.

Returns:

  • ({Integer=>Object})

    mapping from an element’s object_id to the corresponding object



18
19
20
# File 'lib/metaruby/gui/html/collection.rb', line 18

def object_id_to_object
  @object_id_to_object
end

#page#push (readonly)

Returns the page on which we publish the HTML.

Returns:

  • (#push)

    the page on which we publish the HTML



11
12
13
# File 'lib/metaruby/gui/html/collection.rb', line 11

def page
  @page
end

#registered_exceptions<Exception> (readonly)

Returns exceptions caught during element rendering.

Returns:

  • (<Exception>)

    exceptions caught during element rendering



20
21
22
# File 'lib/metaruby/gui/html/collection.rb', line 20

def registered_exceptions
  @registered_exceptions
end

Instance Method Details

#clearObject

Clear the current view



54
55
56
57
58
# File 'lib/metaruby/gui/html/collection.rb', line 54

def clear
    @object_id_to_object.clear
    registered_exceptions.clear
    manager.clear
end

#disableObject

Disable the current renderer



48
49
50
51
# File 'lib/metaruby/gui/html/collection.rb', line 48

def disable
    disconnect(page, SIGNAL('linkClicked(const QUrl&)'), self, SLOT('linkClickedHandler(const QUrl&)'))
    manager.disable
end


65
66
67
68
69
70
71
# File 'lib/metaruby/gui/html/collection.rb', line 65

def element_link_target(object, interactive)
    if interactive
        id =  "link://metaruby/#{namespace}#{object.object_id}"
    else
        id =  "##{object.object_id}"
    end
end

#enableObject

Enable the current renderer



42
43
44
45
# File 'lib/metaruby/gui/html/collection.rb', line 42

def enable
    connect(page, SIGNAL('linkClicked(const QUrl&)'), self, SLOT('linkClickedHandler(const QUrl&)'))
    manager.enable
end

#linkClickedHandler(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handler called when a link is clicked on the page. It renders an object when the link is a link to an object of the collection, or passes the URL to the linkClicked signal otherwise.



105
106
107
108
109
110
111
112
# File 'lib/metaruby/gui/html/collection.rb', line 105

def linkClickedHandler(url)
    if url.host == "metaruby" && url.path =~ /^\/#{Regexp.quote(namespace)}(\d+)/
        object = object_id_to_object[Integer($1)]
        render_element(object)
    else
        super
    end
end

#namespaceObject

The namespace used on URIs generated by the collection



61
62
63
# File 'lib/metaruby/gui/html/collection.rb', line 61

def namespace
    object_id.to_s + "/"
end

#register_type(type, rendering_class, render_options = Hash.new) ⇒ Object

Registers a certain kind of model as well as the information needed to display it

It registers the given type on the model browser so that it gets displayed there.

Parameters:

  • type (Class)

    objects whose class or ancestry include ‘type’ will be rendered using the provided rendering class. If more tha none matches, the first one is used.

  • rendering_class (Class)

    a class from which a relevant rendering object can be created. The generated instances must follow the rules described in the documentation of ModelBrowser

  • render_options (Hash) (defaults to: Hash.new)

    a set of options that must be passed to the renderer’s #render method



37
38
39
# File 'lib/metaruby/gui/html/collection.rb', line 37

def register_type(type, rendering_class, render_options = Hash.new)
    manager.register_type(type, rendering_class, render_options)
end

#render_all_elements(all, options) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/metaruby/gui/html/collection.rb', line 91

def render_all_elements(all, options)
    all.each do |element|
        object_id = element.object.object_id
        page.push(nil, "<h1 id=#{object_id}>#{element.format % element.text}</h1>")

        render_element(element.object, options.merge(element.rendering_options))
    end
end

#render_element(object, options = Hash.new) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/metaruby/gui/html/collection.rb', line 116

def render_element(object, options = Hash.new)
    page.restore
    registered_exceptions.clear
    options = Hash[id: "#{namespace}/currently_rendered_element"].merge(options)
    begin
        manager.render(object, options)
    rescue ::Exception => e
        registered_exceptions << e
    end
    emit updated
    page.page.current_frame.scrollToAnchor(options[:id])
end

Render a list of Element

Parameters:

  • title (String)

    the section title

  • links (Array<Element>)

    the links that should be rendered

  • push_options (Hash) (defaults to: Hash.new)

    additional options that should be passed to page#render_list



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/metaruby/gui/html/collection.rb', line 79

def render_links(title, links, push_options = Hash.new)
    links.each do |el|
        object_id_to_object[el.object.object_id] = el.object
    end

    links = links.map do |el|
        a_node = el.format % ["<a href=\"#{el.url}\">#{el.text}</a>"]
        [a_node, el.attributes || Hash.new]
    end
    page.render_list(title, links, push_options)
end