Sha256: f6521727d7ab8b8d765b09270827505d9214d53ff809e567fdda7d1aa7e8b871

Contents?: true

Size: 1.98 KB

Versions: 20

Compression:

Stored size: 1.98 KB

Contents

# Methods to retrieve a subview using the stylename as a key
# Kinda similar to jQuery-style $(el).find('stylename')
module Teacup
  module View

    # get one subview by stylename or class.  If the receiver matches, it will be
    # returned
    # my_view.viewWithStylename :button => #<UIButton..>
    # my_view.viewWithStylename UIButton => #<UIButton..>
    def viewWithStylename name_or_class
      return self if self._teacup_check_stylename(name_or_class)

      view = subviews.find { |view| view._teacup_check_stylename(name_or_class) }
      return view if view

      # found_subview will get assigned to the view we want, but the subview is
      # what is returned.
      found_subview = nil
      view = subviews.find { |subview| found_subview = subview.viewWithStylename(name_or_class) }
      return found_subview if view

      return nil  # couldn't find it
    end

    # get all subviews by stylename or class
    # my_view.viewsWithStylename :button => [#<UIButton..>, #<UIButton...>]
    # my_view.viewsWithStylename UIButton => [#<UIButton..>, #<UIButton...>]
    def viewsWithStylename name_or_class
      retval = []
      retval << self if self._teacup_check_stylename(name_or_class)

      search_views = [].concat(self.subviews)
      # ewww, a traditional for loop! the search_views array is modified in place,
      # and `each` and other methods don't like that.
      index = 0
      while index < search_views.length
        view = search_views[index]
        if view._teacup_check_stylename(name_or_class)
          retval << view
        end
        search_views.concat(view.subviews)
        index += 1
      end

      return retval
    end

    def _teacup_check_stylename(name_or_class)
      if name_or_class.is_a? Class
        return self.is_a?(name_or_class)
      elsif stylename == name_or_class
        return true
      elsif stylesheet.is_a?(Teacup::Stylesheet)
        return stylesheet.extends_style?(self.stylename, name_or_class)
      end
      return false
    end

  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
teacup-2.1.13 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.12 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.11 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.10 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.9 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.8 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.7 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.6 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.5 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.4 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.3 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.2 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.1 lib/teacup/core_extensions/view_getters.rb
teacup-2.1.0 lib/teacup/core_extensions/view_getters.rb
teacup-2.0.6 lib/teacup/core_extensions/view_getters.rb
teacup-2.0.5 lib/teacup/core_extensions/view_getters.rb
teacup-2.0.4 lib/teacup/core_extensions/view_getters.rb
teacup-2.0.3 lib/teacup/core_extensions/view_getters.rb
teacup-2.0.2 lib/teacup/core_extensions/view_getters.rb
teacup-2.0.0 lib/teacup/core_extensions/view_getters.rb