Class: MotionPrime::BaseElement

Inherits:
Object
  • Object
show all
Extended by:
HasClassFactory
Includes:
HasClassFactory, HasNormalizer, HasStyleChainBuilder, MotionSupport::Callbacks
Defined in:
motion-prime/elements/base_element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasClassFactory

camelize_factory, class_factory, low_camelize_factory

Methods included from HasStyleChainBuilder

#build_styles_chain

Methods included from HasNormalizer

#normalize_object, #normalize_options

Constructor Details

#initialize(options = {}) ⇒ BaseElement

Returns a new instance of BaseElement.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'motion-prime/elements/base_element.rb', line 20

def initialize(options = {})
  options[:screen] = options[:screen].try(:weak_ref)
  @options = options
  @screen = options[:screen]
  @section = options[:section]

  @view_class = options[:view_class] || 'UIView'
  @name = options[:name]
  @block = options[:block]
  @view_name = self.class_name_without_kvo.demodulize.underscore.gsub(/(_draw)?_element/, '')

  if Prime.env.development?
    info = []
    info << @name
    info << view_name
    info << section.try(:name)
    info << screen.class
    @_element_info = info.join(' ')
    @@_allocated_elements ||= []
    @@_allocated_elements << @_element_info
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def name
  @name
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def options
  @options
end

#screenObject

Returns the value of attribute screen.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def screen
  @screen
end

#sectionObject

Returns the value of attribute section.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def section
  @section
end

#stylesObject

Returns the value of attribute styles.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def styles
  @styles
end

#viewObject

Returns the value of attribute view.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def view
  @view
end

#view_classObject

Returns the value of attribute view_class.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def view_class
  @view_class
end

#view_nameObject

Returns the value of attribute view_name.



15
16
17
# File 'motion-prime/elements/base_element.rb', line 15

def view_name
  @view_name
end

Class Method Details

.after_render(method_name) ⇒ Object



223
224
225
# File 'motion-prime/elements/base_element.rb', line 223

def after_render(method_name)
  set_callback :render, :after, method_name
end

.before_render(method_name) ⇒ Object



220
221
222
# File 'motion-prime/elements/base_element.rb', line 220

def before_render(method_name)
  set_callback :render, :before, method_name
end

.factory(type, options = {}) ⇒ Object



213
214
215
216
217
218
219
# File 'motion-prime/elements/base_element.rb', line 213

def factory(type, options = {})
  element_class = class_factory("#{type}_element", true) || self
  view_class_name = camelize_factory("ui_#{type}")

  options.merge!(view_class: view_class_name)
  element_class.new(options)
end

Instance Method Details

#add_target(target = nil, action = 'on_click:', event = :touch) ⇒ Object



52
53
54
55
# File 'motion-prime/elements/base_element.rb', line 52

def add_target(target = nil, action = 'on_click:', event = :touch)
  return false unless self.view
  self.view.addTarget(target || section, action: action, forControlEvents: event.uicontrolevent)
end

#bind_gesture(action, receiver = nil) ⇒ Object



109
110
111
112
113
114
# File 'motion-prime/elements/base_element.rb', line 109

def bind_gesture(action, receiver = nil)
  receiver ||= self
  single_tap = UITapGestureRecognizer.alloc.initWithTarget(receiver, action: action)
  view.addGestureRecognizer single_tap
  view.setUserInteractionEnabled true
end

#compute_options!Object



81
82
83
84
85
86
87
88
# File 'motion-prime/elements/base_element.rb', line 81

def compute_options!
  block_options = compute_block_options || {}
  raw_options = self.options.except(:screen, :name, :block, :view_class).merge(block_options)
  compute_style_options(raw_options)
  raw_options = Styles.for(styles).merge(raw_options)
  @computed_options = raw_options
  normalize_options(@computed_options, section, %w[text placeholder font title_label padding padding_left padding_right min_width min_outer_width max_width max_outer_width width left right])
end

#computed_optionsObject

Lazy-computing options



76
77
78
79
# File 'motion-prime/elements/base_element.rb', line 76

def computed_options
  compute_options! unless @computed_options
  @computed_options
end

#deallocObject



43
44
45
46
47
48
49
50
# File 'motion-prime/elements/base_element.rb', line 43

def dealloc
  if Prime.env.development?
    index = @@_allocated_elements.index(@_element_info)
    @@_allocated_elements.delete_at(index) if index
  end
  Prime.logger.dealloc_message :element, self, self.name
  super
end

#hideObject



101
102
103
# File 'motion-prime/elements/base_element.rb', line 101

def hide
  view.hidden = true
end

#reload!Object



90
91
92
# File 'motion-prime/elements/base_element.rb', line 90

def reload!
  compute_options!
end

#render(options = {}, &block) ⇒ Object



57
58
59
60
61
# File 'motion-prime/elements/base_element.rb', line 57

def render(options = {}, &block)
  run_callbacks :render do
    render!(options, &block)
  end
end

#render!(options = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'motion-prime/elements/base_element.rb', line 63

def render!(options = {}, &block)
  view = screen.add_view class_factory(view_class), computed_options.merge(options) do |view|
    @view = view
    block.try(:call, view, self)
  end

  if computed_options.has_key?(:delegate) && computed_options[:delegate].respond_to?(:delegated_by)
    computed_options[:delegate].delegated_by(view)
  end
  view
end

#showObject



105
106
107
# File 'motion-prime/elements/base_element.rb', line 105

def show
  view.hidden = false
end

#update_with_options(new_options = {}) ⇒ Object



94
95
96
97
98
99
# File 'motion-prime/elements/base_element.rb', line 94

def update_with_options(new_options = {})
  options.merge!(new_options)
  reload!
  view.try(:removeFromSuperview)
  render
end