Class: MotionKit::TreeLayout
- Inherits:
-
BaseLayout
- Object
- BaseLayout
- MotionKit::TreeLayout
- Defined in:
- lib/motion-kit/layouts/tree_layout.rb
Overview
A sensible parent class for any Tree-like layout class. Platform agnostic. Any platform-specific tasks are offloaded to child elements (add_child, remove_child). You could use a TreeLayout subclass to construct a hierarchy representing a family tree, for instance. But that would be a silly use of MotionKit.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from BaseLayout
Class Method Summary collapse
-
.view(name) ⇒ Object
This is an ‘attr_reader`-like method that also calls `build_view` if the assigned to ivars in your `layout` method.
Instance Method Summary collapse
-
#add(element, element_id = nil, &block) ⇒ Object
Instantiates a view via ‘create` and adds the view to the current target.
-
#all(element_id, in: root) ⇒ Object
Same as ‘all`, but with the root view specified.
-
#build ⇒ Object
Builds the layout and then returns self for chaining.
-
#built? ⇒ Boolean
(also: #build?)
Checks if the layout has been built yet or not.
- #call_style_method(element, element_id) ⇒ Object
-
#create(element, element_id = nil, &block) ⇒ Object
instantiates a view, possibly running a ‘layout block’ to add child views.
- #create_default_root_context ⇒ Object
- #first(element_id, in: root) ⇒ Object
-
#get(element_id, in: root) ⇒ Object
Same as ‘get`, but with the root view specified.
- #initial(&block) ⇒ Object
-
#last(element_id, in: root) ⇒ Object
Same as ‘last`, but with the root view specified.
-
#nth(element_id, at: index, in: root) ⇒ Object
Same as ‘nth`, but with the root view specified.
-
#reapply(&block) ⇒ Object
Calls the style method of all objects in the view hierarchy.
-
#reapply!(root = nil) ⇒ Object
Calls the style method of all objects in the view hierarchy.
-
#remove(element_id, from: root) ⇒ Object
Same as ‘remove`, but with the root view specified.
-
#root(element, element_id = nil, &block) ⇒ Object
Assign a view to act as the ‘root’ view for this layout.
-
#view ⇒ Object
The main view.
Methods inherited from BaseLayout
#add_deferred_block, #apply, #apply_with_context, #apply_with_target, #context, #deferred, #deferred_blocks, delegate_method_fix, #initialize, #ipad?, #iphone35?, #iphone4?, #iphone?, #method_missing, #orientation?, #retina?, #set_layout, #target, #v
Methods included from BaseLayoutClassMethods
#layout_for, #memoize, #new_child, #target_klasses, #targets
Constructor Details
This class inherits a constructor from MotionKit::BaseLayout
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class MotionKit::BaseLayout
Class Method Details
.view(name) ⇒ Object
This is an ‘attr_reader`-like method that also calls `build_view` if the assigned to ivars in your `layout` method.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 31 def view(name) ivar_name = "@#{name}" define_method(name) do unless instance_variable_get(ivar_name) view = self.get(name) unless view build_view unless @view view = instance_variable_get(ivar_name) || self.get(name) end self.send("#{name}=", view) return view end return instance_variable_get(ivar_name) end # KVO compliance attr_writer name end |
Instance Method Details
#add(element, element_id = nil, &block) ⇒ Object
Instantiates a view via ‘create` and adds the view to the current target. If no view exists on the stack, a default root view can be created if that has been enabled. The block is run in the context of the new view.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 179 def add(element, element_id=nil, &block) # make sure we have a target - raises NoContextError if none exists self.target element = initialize_element(element) unless @context create_default_root_context end self.apply(:add_child, element) create(element, element_id) if block context(element, &block) end element end |
#all(element_id, in: root) ⇒ Object
Same as ‘all`, but with the root view specified.
230 231 232 233 234 235 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 230 def all(element_id) if @layout != self return @layout.all(element_id) end self.all(element_id, in: self.view) end |
#build ⇒ Object
Builds the layout and then returns self for chaining.
60 61 62 63 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 60 def build view self end |
#built? ⇒ Boolean Also known as: build?
Checks if the layout has been built yet or not.
66 67 68 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 66 def built? !@view.nil? end |
#call_style_method(element, element_id) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 135 def call_style_method(element, element_id) style_method = "#{element_id}_style" if @layout.respond_to?(style_method) @layout.context(element) do @layout.send(style_method) end end return element end |
#create(element, element_id = nil, &block) ⇒ Object
instantiates a view, possibly running a ‘layout block’ to add child views.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 118 def create(element, element_id=nil, &block) element = initialize_element(element) if element_id # We set the optional id and call the '_style' method, if it's been # defined. element.motion_kit_id = element_id self.call_style_method(element, element_id) end if block context(element, &block) end element end |
#create_default_root_context ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 273 def create_default_root_context if @assign_root # Originally I thought default_root should be `apply`ied like other # view-related methods, but actually this method *only* gets called # from within the `layout` method, and so should already be inside the # correct Layout subclass. @context = root(preset_root || default_root) else raise NoContextError.new("No top level view specified (missing outer 'create' method?)") end end |
#first(element_id, in: root) ⇒ Object
206 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 206 def first(element_id) ; get(element_id) ; end |
#get(element_id, in: root) ⇒ Object
Same as ‘get`, but with the root view specified.
200 201 202 203 204 205 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 200 def get(element_id) if @layout != self return @layout.get(element_id) end self.get(element_id, in: self.view) end |
#initial(&block) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 167 def initial(&block) raise ArgumentError.new('Block required') unless block if @layout_state == :initial yield end return self end |
#last(element_id, in: root) ⇒ Object
Same as ‘last`, but with the root view specified.
217 218 219 220 221 222 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 217 def last(element_id) if @layout != self return @layout.last(element_id) end self.last(element_id, in: self.view) end |
#nth(element_id, at: index, in: root) ⇒ Object
Same as ‘nth`, but with the root view specified.
243 244 245 246 247 248 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 243 def nth(element_id, index) if @layout != self return @layout.nth(element_id, index) end self.all(element_id, in: self.view)[index] end |
#reapply(&block) ⇒ Object
Calls the style method of all objects in the view hierarchy
158 159 160 161 162 163 164 165 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 158 def reapply(&block) raise ArgumentError.new('Block required') unless block if @layout_state == :reapply yield end return self end |
#reapply!(root = nil) ⇒ Object
Calls the style method of all objects in the view hierarchy
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 146 def reapply!(root=nil) root ||= self.view @layout_state = :reapply MotionKit.find_all_views(root) do |view| call_style_method(view, view.motion_kit_id) if view.motion_kit_id end @layout_state = :initial return self end |
#remove(element_id, from: root) ⇒ Object
Same as ‘remove`, but with the root view specified.
257 258 259 260 261 262 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 257 def remove(element_id) if @layout != self return @layout.remove(element_id) end self.remove(element_id, from: self.view) end |
#root(element, element_id = nil, &block) ⇒ Object
Assign a view to act as the ‘root’ view for this layout. This method can only be called once, and must be called before ‘add` is called for the first time (otherwise `add` will create a default root view). This method must be called from inside `layout`, otherwise you should just use `create`.
You can also call this method with just an element_id, and the default root view will be created.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 79 def root(element, element_id=nil, &block) if @view raise ContextConflictError.new("Already created the root view") end unless @assign_root raise InvalidRootError.new("You should only create a 'root' view from inside the 'layout' method (use 'create' elsewhere)") end @assign_root = false # this method can be called with just a symbol, to assign the root element_id if element.is_a?(Symbol) element_id = element # See note below about why we don't need to `apply(:default_root)` element = preset_root || default_root elsif @preset_root && @preset_root != element # You're trying to make two roots, one at initialization # and one in your layout itself. raise ContextConflictError.new("Already created the root view") end @view = initialize_element(element) if block if @context raise ContextConflictError.new("Already in a context") end context(@view) do # We're just using the `create` method for its side effects: calling the # style method and calling the block. create(@view, element_id, &block) end elsif element_id create(@view, element_id) end return @view end |
#view ⇒ Object
The main view. This method builds the layout and returns the root view.
52 53 54 55 56 57 |
# File 'lib/motion-kit/layouts/tree_layout.rb', line 52 def view if @layout != self return @layout.view end @view ||= build_view end |