motion-prime/elements/base_element.rb in motion-prime-1.0.3 vs motion-prime/elements/base_element.rb in motion-prime-1.0.4

- old
+ new

@@ -1,17 +1,19 @@ motion_require '../helpers/has_normalizer' motion_require '../helpers/has_style_chain_builder' motion_require '../helpers/has_class_factory' +motion_require '../helpers/has_style_options' module MotionPrime class BaseElement # MotionPrime::BaseElement is container for UIView class elements with options. # Elements are located inside Sections include ::MotionSupport::Callbacks include HasNormalizer include HasStyleChainBuilder include HasClassFactory + include HasStyleOptions extend HasClassFactory attr_accessor :options, :section, :name, :view_class, :view, :view_name, :styles, :screen delegate :observing_errors?, :has_errors?, :errors_observer_fields, :observing_errors_for, to: :section, allow_nil: true @@ -52,10 +54,18 @@ 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 + def notify_section_before_render + section.try(:before_element_render, self) + end + + def notify_section_after_render + section.try(:after_element_render, self) + end + def render(options = {}, &block) run_callbacks :render do render!(options, &block) end end @@ -80,14 +90,14 @@ 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) + raw_options = Styles.for(styles).deep_merge(raw_options) @computed_options = raw_options normalize_options(@computed_options, section.try(:elements_eval_object), %w[ - font text placeholder title_label + font font_name font_size text placeholder title_label padding padding_left padding_right padding_top padding_bottom left right min_width min_outer_width max_width max_outer_width width top bottom min_height min_outer_height max_height max_outer_height height]) end @@ -102,22 +112,31 @@ render(render_target: render_target) if (changed_options & [:text, :size_to_fit]).any? && respond_to?(:size_to_fit) size_to_fit end - section.try(:on_element_render, self) end def update_with_options(new_options = {}) - options.merge!(new_options) + options.deep_merge!(new_options) reload! + computed_options.deep_merge!(new_options) rerender!(new_options.keys) end def update_options(new_options) - options.merge!(new_options) + options.deep_merge!(new_options) return unless view + + required_options = if new_options.slice(:width, :height, :top, :left, :right, :bottom).any? + new_options[:calculate_frame] = true + [:width, :height, :top, :left, :right, :bottom] + elsif new_options.slice(:text, :title).any? + [:line_spacing, :line_height, :underline, :fragment_color, :text_alignment, :font, :font_name, :font_size, :line_break_mode, :number_of_lines] + end + new_options = computed_options.slice(*Array.wrap(required_options)).merge(new_options) + ViewStyler.new(view, view.superview.try(:bounds), new_options).apply end def update update_with_options({}) @@ -159,15 +178,14 @@ def compute_block_options normalize_value(@block, section) if @block end def compute_style_options(*style_sources) - has_errors = section.respond_to?(:observing_errors?) && observing_errors? && has_errors? - @styles = [] if cell_section? - @styles += compute_cell_style_options(style_sources, has_errors) + suffixes = section.style_suffixes if section.respond_to?(:style_suffixes) + @styles += compute_cell_style_options(style_sources, Array.wrap(suffixes)) end mixins = [] custom_styles = [] style_sources.each do |source| @@ -187,15 +205,15 @@ @styles << [section.name, name].join('_').to_sym end # custom style (from options or block options), using for TableViews as well @styles += custom_styles - # pp @view_class.to_s + @styles.inspect; puts() + # pp(@view_class.to_s + @styles.inspect); puts() @styles end - def compute_cell_style_options(style_sources, has_errors) + def compute_cell_style_options(style_sources, additional_suffixes) base_styles = {common: [], specific: []} suffixes = {common: [], specific: []} all_styles = [] # following example in Prime::TableSection#cell_section_styles @@ -206,17 +224,21 @@ end if @view_name != 'base' && !cell_element? # form element: _input # table element: _image suffixes[:common] << @view_name.to_sym - suffixes[:common] << :"#{@view_name}_with_errors" if has_errors + additional_suffixes.each do |additional_suffix| + suffixes[:common] << [@view_name, additional_suffix].join('_').to_sym + end end if name && name.to_s != @view_name # form element: _input # table element: _icon suffixes[:specific] << name.to_sym - suffixes[:specific] << :"#{name}_with_errors" if has_errors + additional_suffixes.each do |additional_suffix| + suffixes[:specific] << [name, additional_suffix].join('_').to_sym + end end # form cell: base_form_field, base_form_string_field # form element: base_form_field_string_field, base_form_string_field_text_field, base_form_string_field_input # table cell: base_table_cell # table element: base_table_cell_image @@ -257,7 +279,10 @@ end def after_render(method_name) set_callback :render, :after, method_name end end + + before_render :notify_section_before_render + after_render :notify_section_after_render end end \ No newline at end of file