Module: WWW_App::TO::OLD

Defined in:
lib/www_app/TO.rb

Overview

to_html

Instance Method Summary collapse

Instance Method Details

#css_id(*args) ⇒ Object

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.


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# 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) ⇒ Object

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=>...}


754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# 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_idObject

Examples

selector_id   -> a series of ids and tags to be used as a JS selector
                 Example:
                    #id tag tag
                    tag tag


793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# 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