module WWW_App::TO::OLD

Public Instance Methods

css_id(*args) click to toggle source

Examples

css_id             -> current css id of element.
                      It uses the first class, if any, found.
                      #id.class     -> if #id and first class found.
                      #id           -> if class is missing and id given.
                      #id tag.class -> if class given and ancestor has id.
                      #id tag tag   -> if no class given and ancestor has id.
                      tag tag tag   -> if no ancestor has class.

css_id :my_class   -> same as 'css_id()' except
                      'my_class' overrides :class attribute of current
                      element.
# File lib/www_app/TO.rb, line 837
def css_id *args

  str_class = nil

  case args.size
  when 0
    fail "Not in a tag." unless tag!
    str_class = @css_id_override
  when 1
    str_class = args.first
  else
    fail "Unknown args: #{args.inspect}"
  end

  i        = tag![:tag_index]
  id_given = false
  classes  = []

  while !id_given && i && i > -1
    e           = @tag_arr[i]
    id          = dom_id e
    first_class = e[:attrs][:class].first

    if id
      id_given = true
      if str_class
        classes.unshift(
          str_class.is_a?(::Symbol) ?
          "##{id}.#{str_class}" :
          "##{id}#{str_class}"
        )
      else
        classes.unshift "##{id}"
      end

    else # no id given
      if str_class
        classes.unshift(
          str_class.is_a?(::Symbol) ?
          "#{e[:tag]}.#{str_class}" :
          "#{e[:tag]}#{str_class}"
        )
      elsif first_class
        classes.unshift "#{e[:tag]}.#{first_class}"
      else
        if e[:tag] != :body || (classes.empty?)
          classes.unshift "#{e[:tag]}"
        end
      end # if first_class

    end # if id

    i = e[:parent_index]
    break if i == @body[:tag_index] && !classes.empty?
  end

  classes.join SPACE
end
dom_id(*args) click to toggle source

Examples

dom_id             -> the current dom id of the current element
dom_id :default    -> if no dom it, set/get default of current element
dom_id {:element:} -> dom id of element: {:type=>:html, :tag=>...}
# File lib/www_app/TO.rb, line 754
def dom_id *args

  use_default = false

  case
  when args.empty?
    e = tag!
    # do nothing else

  when args.size == 1 && args.first == :default
    e = tag!
    use_default = true

  when args.size == 1 && args.first.is_a?(::Hash) && args.first[:type]==:html
    e = args.first

  else
    fail "Unknown args: #{args.inspect}"
  end

  id = e[:attrs][:id]
  return id if id
  return nil unless use_default

  e[:default_id] ||= begin
                       key = e[:tag]
                       @default_ids[key] ||= -1
                       @default_ids[key] += 1
                     end
end
selector_id() click to toggle source

Examples

selector_id   -> a series of ids and tags to be used as a JS selector
                 Example:
                    #id tag tag
                    tag tag
# File lib/www_app/TO.rb, line 793
def selector_id
  i        = tag![:tag_index]
  id_given = false
  classes  = []

  while !id_given && i && i > -1
    e         = @tag_arr[i]
    id        = dom_id e
    (id_given = true) if id

    if e[:tag] == :body && !classes.empty?
      # do nothing because
      # we do not want 'body tag.class tag.class'
    else
      case
      when id
        classes << "##{id}"
      else
        classes << e[:tag]
      end # === case
    end # === if

    i = e[:parent_index]
  end

  return 'body' if classes.empty?
  classes.join SPACE
end