Class: MotionPrime::Section
Constant Summary
collapse
- KEYBOARD_HEIGHT_PORTRAIT =
Basic Sample
class MySection < MotionPrime::Section
element :title, text: "Hello World"
element :avatar, type: :image, image: 'defaults/avatar.jpg'
end
216
- KEYBOARD_HEIGHT_LANDSCAPE =
162
- DEFAULT_CONTENT_HEIGHT =
65
Instance Attribute Summary collapse
#cached_draw_image, #container_element, #container_gesture_recognizers
Class Method Summary
collapse
Instance Method Summary
collapse
#clear_delegated, #delegated_by
#bind_gesture_on_container_for, #draw_in, #prerender_elements_for_state, #prerender_enabled?, #strong_references
#container_view, #init_container_element, #load_container_with_elements
#calculate_frome_for
Methods included from HasStyles
#prepare_gradient
#camelize_factory, #class_factory, #low_camelize_factory
#normalize_object, #normalize_options
#api_client, #current_user, #reset_current_user, #user_signed_in?
Constructor Details
#initialize(options = {}) ⇒ Section
Returns a new instance of Section.
28
29
30
31
32
33
34
35
36
37
38
|
# File 'motion-prime/sections/base_section.rb', line 28
def initialize(options = {})
@options = options
run_callbacks :initialize do
@options[:screen] = @options[:screen].try(:weak_ref)
self.screen = options[:screen]
@model = options[:model]
@name = options[:name] ||= default_name
@options_block = options[:block]
end
end
|
Instance Attribute Details
#elements ⇒ Object
Returns the value of attribute elements.
24
25
26
|
# File 'motion-prime/sections/base_section.rb', line 24
def elements
@elements
end
|
#model ⇒ Object
Returns the value of attribute model.
24
25
26
|
# File 'motion-prime/sections/base_section.rb', line 24
def model
@model
end
|
#name ⇒ Object
Returns the value of attribute name.
24
25
26
|
# File 'motion-prime/sections/base_section.rb', line 24
def name
@name
end
|
#options ⇒ Object
Returns the value of attribute options.
24
25
26
|
# File 'motion-prime/sections/base_section.rb', line 24
def options
@options
end
|
#screen ⇒ Object
Returns the value of attribute screen.
24
25
26
|
# File 'motion-prime/sections/base_section.rb', line 24
def screen
@screen
end
|
#section_styles ⇒ Object
Returns the value of attribute section_styles.
24
25
26
|
# File 'motion-prime/sections/base_section.rb', line 24
def section_styles
@section_styles
end
|
Class Method Details
.after_initialize(method_name) ⇒ Object
357
358
359
|
# File 'motion-prime/sections/base_section.rb', line 357
def after_initialize(method_name)
set_callback :initialize, :after, method_name
end
|
.after_render(method_name) ⇒ Object
351
352
353
|
# File 'motion-prime/sections/base_section.rb', line 351
def after_render(method_name)
set_callback :render, :after, method_name
end
|
.before_initialize(method_name) ⇒ Object
354
355
356
|
# File 'motion-prime/sections/base_section.rb', line 354
def before_initialize(method_name)
set_callback :initialize, :before, method_name
end
|
.before_render(method_name) ⇒ Object
348
349
350
|
# File 'motion-prime/sections/base_section.rb', line 348
def before_render(method_name)
set_callback :render, :before, method_name
end
|
.bind_keyboard_close(options) ⇒ Object
360
361
362
|
# File 'motion-prime/sections/base_section.rb', line 360
def bind_keyboard_close(options)
self.keyboard_close_bindings = options
end
|
.container(options) ⇒ Object
345
346
347
|
# File 'motion-prime/sections/base_section.rb', line 345
def container(options)
self.container_options = options
end
|
.element(name, options = {}, &block) ⇒ Object
337
338
339
340
341
342
343
344
|
# File 'motion-prime/sections/base_section.rb', line 337
def element(name, options = {}, &block)
options[:name] ||= name
options[:type] ||= :label
options[:block] = block
self.elements_options ||= {}
self.elements_options[name] = options
self.elements_options[name]
end
|
.inherited(subclass) ⇒ Object
331
332
333
334
335
|
# File 'motion-prime/sections/base_section.rb', line 331
def inherited(subclass)
subclass.elements_options = self.elements_options.try(:clone)
subclass.container_options = self.container_options.try(:clone)
subclass.keyboard_close_bindings = self.keyboard_close_bindings.try(:clone)
end
|
Instance Method Details
#add_element(key, options = {}) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'motion-prime/sections/base_section.rb', line 149
def add_element(key, options = {})
return unless render_element?(key)
opts = options.clone
index = opts.delete(:at)
options = build_options_for_element(opts)
options[:name] ||= key
element = build_element(options)
if index
new_elements_array = elements.to_a.insert(index, [key, element])
self.elements = Hash[new_elements_array]
else
self.elements[key] = element
end
end
|
#bind_keyboard_events ⇒ Object
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'motion-prime/sections/base_section.rb', line 241
def bind_keyboard_events
NSNotificationCenter.defaultCenter.addObserver self,
selector: :on_keyboard_show,
name: UIKeyboardDidShowNotification,
object: nil
NSNotificationCenter.defaultCenter.addObserver self,
selector: :on_keyboard_hide,
name: UIKeyboardDidHideNotification,
object: nil
NSNotificationCenter.defaultCenter.addObserver self,
selector: :keyboard_will_show,
name: UIKeyboardWillShowNotification,
object: nil
NSNotificationCenter.defaultCenter.addObserver self,
selector: :keyboard_will_hide,
name: UIKeyboardWillHideNotification,
object: nil
end
|
#build_element(options = {}) ⇒ Object
#cell ⇒ Object
178
179
180
181
182
183
|
# File 'motion-prime/sections/base_section.rb', line 178
def cell
container_view || begin
first_element = elements.values.first
first_element.view.superview.superview
end
end
|
#container_bounds ⇒ Object
46
47
48
|
# File 'motion-prime/sections/base_section.rb', line 46
def container_bounds
options[:container_bounds] or raise "You must pass `container bounds` option to prerender base section"
end
|
#container_height ⇒ Object
Get computed container height
69
70
71
|
# File 'motion-prime/sections/base_section.rb', line 69
def container_height
container_options[:height] || DEFAULT_CONTENT_HEIGHT
end
|
#container_options ⇒ Object
Get computed container options
53
54
55
56
|
# File 'motion-prime/sections/base_section.rb', line 53
def container_options
compute_container_options! unless @container_options
@container_options
end
|
#create_elements ⇒ Object
141
142
143
144
145
146
147
|
# File 'motion-prime/sections/base_section.rb', line 141
def create_elements
self.elements = {}
elements_options.each do |key, opts|
add_element(key, opts)
end
self.instance_eval(&@options_block) if @options_block.is_a?(Proc)
end
|
#dealloc ⇒ Object
40
41
42
43
44
|
# File 'motion-prime/sections/base_section.rb', line 40
def dealloc
Prime.logger.dealloc_message :section, self, self.name
NSNotificationCenter.defaultCenter.removeObserver self super
end
|
#default_name ⇒ Object
Get section default name, based on class name
88
89
90
|
# File 'motion-prime/sections/base_section.rb', line 88
def default_name
self.class_name_without_kvo.demodulize.underscore.gsub(/\_section$/, '')
end
|
#element(name) ⇒ Object
212
213
214
|
# File 'motion-prime/sections/base_section.rb', line 212
def element(name)
elements[name.to_sym]
end
|
#elements_options ⇒ Object
Get section elements options, where the key is element name.
95
96
97
|
# File 'motion-prime/sections/base_section.rb', line 95
def elements_options
self.class.elements_options || {}
end
|
#elements_to_draw ⇒ Object
270
271
272
|
# File 'motion-prime/sections/base_section.rb', line 270
def elements_to_draw
self.elements.select { |key, element| element.is_a?(DrawElement) }
end
|
#elements_to_render ⇒ Object
274
275
276
|
# File 'motion-prime/sections/base_section.rb', line 274
def elements_to_render
self.elements.select { |key, element| element.is_a?(BaseElement) }
end
|
#hide ⇒ Object
220
221
222
223
224
225
226
|
# File 'motion-prime/sections/base_section.rb', line 220
def hide
if container_view
container_view.hidden = true
else
elements.values.each(&:hide)
end
end
|
#hide_keyboard ⇒ Object
260
261
262
263
264
265
266
267
268
|
# File 'motion-prime/sections/base_section.rb', line 260
def hide_keyboard
elements = Array.wrap(keyboard_close_bindings_options[:elements])
views = Array.wrap(keyboard_close_bindings_options[:views])
elements.each do |el|
views << el.view if %w[text_field text_view].include?(el.view_name) && el.view
end
views.compact.each(&:resignFirstResponder)
end
|
#keyboard_will_hide ⇒ Object
239
|
# File 'motion-prime/sections/base_section.rb', line 239
def keyboard_will_hide; end
|
#keyboard_will_show ⇒ Object
238
|
# File 'motion-prime/sections/base_section.rb', line 238
def keyboard_will_show; end
|
#load_section ⇒ Object
Create elements if they are not created yet. This will not cause rendering elements, they will be rendered immediately after that or rendered async later, based on type of section.
104
105
106
107
108
109
110
111
112
113
114
|
# File 'motion-prime/sections/base_section.rb', line 104
def load_section
return if @section_loaded
if @section_loading
sleep 0.1
return @section_loaded ? false : load_section
end
@section_loading = true
create_elements
@section_loading = false
return @section_loaded = true
end
|
#load_section! ⇒ Object
119
120
121
122
|
# File 'motion-prime/sections/base_section.rb', line 119
def load_section!
@section_loaded = false
load_section
end
|
#on_keyboard_hide ⇒ Object
237
|
# File 'motion-prime/sections/base_section.rb', line 237
def on_keyboard_hide; end
|
#on_keyboard_show ⇒ Object
236
|
# File 'motion-prime/sections/base_section.rb', line 236
def on_keyboard_show; end
|
#reload_section ⇒ Object
Force reload section, will also re-render elements. For table view cells will also reload it’s table data.
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'motion-prime/sections/base_section.rb', line 126
def reload_section
self.elements_to_render.values.map(&:view).flatten.each do |view|
view.removeFromSuperview if view
end
load_section!
run_callbacks :render do
render!
end
if @table && !self.is_a?(BaseFieldSection)
cell.setNeedsDisplay
@table.reload_table_data
end
end
|
#render(container_options = {}) ⇒ Object
185
186
187
188
189
190
191
|
# File 'motion-prime/sections/base_section.rb', line 185
def render(container_options = {})
load_section
self.container_options.merge!(container_options)
run_callbacks :render do
render!
end
end
|
#render! ⇒ Object
204
205
206
207
208
209
210
|
# File 'motion-prime/sections/base_section.rb', line 204
def render!
render_container(container_options) do
elements_to_render.each do |key, element|
element.render
end
end
end
|
#render_container(options = {}, &block) ⇒ Object
193
194
195
196
197
198
199
200
201
202
|
# File 'motion-prime/sections/base_section.rb', line 193
def render_container(options = {}, &block)
if should_render_container?
element = self.container_element || self.init_container_element(options)
element.render do
block.call
end
else
block.call
end
end
|
#render_element?(element_name) ⇒ Boolean
174
175
176
|
# File 'motion-prime/sections/base_section.rb', line 174
def render_element?(element_name)
true
end
|
#show ⇒ Object
228
229
230
231
232
233
234
|
# File 'motion-prime/sections/base_section.rb', line 228
def show
if container_view
container_view.hidden = false
else
elements.values.each(&:show)
end
end
|
#view(name) ⇒ Object
216
217
218
|
# File 'motion-prime/sections/base_section.rb', line 216
def view(name)
element(name).view
end
|