Class: Trellis::Component

Inherits:
Object show all
Defined in:
lib/trellis/trellis.rb

Overview

– Component – The component represents a stateless (tag) or a stateful components. Trellis components can provide contributions to the page. The contributions can be javascript, css stylesheets either at the class level or on a per instance basis. Components contain parameters that can be coerced or casted to a particular type before being handed to the event handling code

Constant Summary collapse

@@components =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComponent

Returns a new instance of Component.



662
663
664
# File 'lib/trellis/trellis.rb', line 662

def initialize()
  @logger = Application.logger
end

Instance Attribute Details

#loggerObject

the page instance containing the component



658
659
660
# File 'lib/trellis/trellis.rb', line 658

def logger
  @logger
end

#pageObject

the page instance containing the component



658
659
660
# File 'lib/trellis/trellis.rb', line 658

def page
  @page
end

Class Method Details

.add_class_scripts_to_page(page, attributes) ⇒ Object



753
754
755
756
757
758
759
760
761
762
# File 'lib/trellis/trellis.rb', line 753

def self.add_class_scripts_to_page(page, attributes)
  class_scripts.each do |body|  
    body = body.replace_ant_style_properties(attributes) if attributes
    builder = Builder::XmlMarkup.new
    script = builder.script(:type => "text/javascript") do |builder|
      builder << body
    end
    page.parsed_template.at("html/body").containers.last.after("\n#{script}")
  end      
end

.add_class_styles_to_page(page, attributes) ⇒ Object



742
743
744
745
746
747
748
749
750
751
# File 'lib/trellis/trellis.rb', line 742

def self.add_class_styles_to_page(page, attributes)
  class_styles.each do |body|  
    body = body.replace_ant_style_properties(attributes) if attributes
    builder = Builder::XmlMarkup.new
    style = builder.style(:type => "text/css") do |builder|
      builder << body
    end
    page.parsed_template.at("html/head").containers.last.after("\n#{style}")
  end      
end

.add_document_modifications_to_page(page) ⇒ Object



786
787
788
789
790
# File 'lib/trellis/trellis.rb', line 786

def self.add_document_modifications_to_page(page)
  document_modifications.each do |block| 
    page.parsed_template.instance_eval(&block)
  end
end


733
734
735
736
737
738
739
740
# File 'lib/trellis/trellis.rb', line 733

def self.add_script_links_to_page(page, attributes)
  script_links.each do |src|  
    src = src.replace_ant_style_properties(attributes) if attributes
    builder = Builder::XmlMarkup.new
    script = builder.script('', :type => "text/javascript", :src => src)
    page.parsed_template.at("html/head").containers.last.after("\n#{script}")
  end      
end

.add_scripts_to_page(page, attributes) ⇒ Object



775
776
777
778
779
780
781
782
783
784
# File 'lib/trellis/trellis.rb', line 775

def self.add_scripts_to_page(page, attributes)
  scripts.each do |body|  
    body = body.replace_ant_style_properties(attributes) if attributes
    builder = Builder::XmlMarkup.new
    script = builder.script(:type => "text/javascript") do |builder|
      builder << body
    end
    page.parsed_template.at("html/body").containers.last.after("\n#{script}")
  end      
end


724
725
726
727
728
729
730
731
# File 'lib/trellis/trellis.rb', line 724

def self.add_style_links_to_page(page, attributes)
  style_links.each do |href|  
    href = href.replace_ant_style_properties(attributes) if attributes
    builder = Builder::XmlMarkup.new
    link = builder.link(:rel => "stylesheet", :type => "text/css", :href => href)
    page.parsed_template.at("html/head").containers.last.after("\n#{link}")
  end
end

.add_styles_to_page(page, attributes) ⇒ Object



764
765
766
767
768
769
770
771
772
773
# File 'lib/trellis/trellis.rb', line 764

def self.add_styles_to_page(page, attributes)
  styles.each do |body|  
    body = body.replace_ant_style_properties(attributes) if attributes
    builder = Builder::XmlMarkup.new
    style = builder.style(:type => "text/css") do |builder|
      builder << body
    end
    page.parsed_template.at("html/head").containers.last.after("\n#{style}")
  end      
end

.contained_in(*args) ⇒ Object



695
696
697
# File 'lib/trellis/trellis.rb', line 695

def self.contained_in(*args)
  @containers = @containers | args
end

.depends_on(*syms) ⇒ Object



829
830
831
832
833
834
# File 'lib/trellis/trellis.rb', line 829

def self.depends_on(*syms)
  syms.each do |sym|
    component = Component.get_component(sym)
    dependencies << component if component
  end
end

.field(sym, options = nil) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/trellis/trellis.rb', line 699

def self.field(sym, options=nil)
  # extract options
  coherce_to = options[:coherce_to] if options
  default_value = options[:defaults_to] if options
  persistent = options[:persistent] if options
  
  @fields << sym
  
  # add an instance field to the component
  attr_accessor sym
  
  # store in array of persistent fields
  persistents << sym if persistent
  
  # castings
  if coherce_to
    meta_def("#{sym}=") do |value| 
      Application.logger.debug "casting value #{sym} to #{coherce_to}"
      self.instance_variable_set("@#{sym}", value)
    end
  end
 
  send("#{sym}=", default_value) if default_value
end

.get_component(sym) ⇒ Object



805
806
807
# File 'lib/trellis/trellis.rb', line 805

def self.get_component(sym)
  @@components[sym]
end

.inherited(child) ⇒ Object

:nodoc:



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/trellis/trellis.rb', line 666

def self.inherited(child) #:nodoc:     
  # component registration
  @@components[child.class_to_sym] = child

  child.class_attr_accessor(:body)
  child.class_attr_accessor(:cname)
  child.cname = child.underscore_class_name
  child.meta_def(:stateful?) { @stateful }
  
  child.attr_array(:fields, :create_accessor => false)
  child.attr_array(:style_links)
  child.attr_array(:script_links)
  child.attr_array(:scripts)
  child.attr_array(:class_scripts)
  child.attr_array(:styles)
  child.attr_array(:class_styles)      
  child.attr_array(:persistents)
  child.attr_array(:dependencies)
  child.attr_array(:document_modifications)
  child.attr_array(:containers)
  
  Application.logger.debug "registered component for tag #{child.cname} => class #{child}"
  super
end

.is_statefulObject



825
826
827
# File 'lib/trellis/trellis.rb', line 825

def self.is_stateful
  instance_variable_set "@stateful".to_sym, true
end

.page_contribution(sym, contribution = nil, options = nil, &block) ⇒ Object



792
793
794
795
796
797
798
799
800
801
802
803
# File 'lib/trellis/trellis.rb', line 792

def self.page_contribution(sym, contribution=nil, options=nil, &block)
  unless (sym == :dom && block_given?)
    # add the contribution to the appropriate array of contributions
    # scripts, class_scripts, styles, class_styles, script_links, style_links
    scope = options[:scope] || :class if options
    receiver = sym.to_s.plural
    receiver = "class_#{receiver}" if scope == :class
    instance_variable_get("@#{receiver}").send(:<<, contribution)
  else
    @document_modifications << block
  end
end

.register_with_tag_context(context) ⇒ Object



813
814
815
816
817
818
819
820
821
822
823
# File 'lib/trellis/trellis.rb', line 813

def self.register_with_tag_context(context)
  Application.logger.debug "registering #{self} with tag context"
  if @containers.empty?
    context.define_tag("#{@cname}", {}, &@body)
  else
    @containers.each do |container| 
      Application.logger.debug "=> registering tag name #{container}:#{@cname}"
      context.define_tag("#{container}:#{@cname}", {}, &@body)
    end
  end
end

.render(&body) ⇒ Object



809
810
811
# File 'lib/trellis/trellis.rb', line 809

def self.render(&body) 
  @body = body
end

.tag_name(name) ⇒ Object



691
692
693
# File 'lib/trellis/trellis.rb', line 691

def self.tag_name(name)
  @cname = name
end

Instance Method Details

#load_component_session_information(page, instance_variable_name, session_data) ⇒ Object



843
844
845
846
847
848
849
850
851
852
# File 'lib/trellis/trellis.rb', line 843

def load_component_session_information(page, instance_variable_name, session_data)
  self.class.persistents.each do |field|
    field_sym = "@#{field}".to_sym
    current_value = instance_variable_get(field_sym)
    new_value = session_data["#{page.class}_#{self.class}_#{instance_variable_name}_#{field}"] if session_data
    if current_value != new_value && new_value != nil
      instance_variable_set(field_sym, new_value)
    end      
  end
end

#save_component_session_information(page, instance_variable_name, session_data) ⇒ Object



836
837
838
839
840
841
# File 'lib/trellis/trellis.rb', line 836

def save_component_session_information(page, instance_variable_name, session_data)
  self.class.persistents.each do |field|
    key = "#{page.class}_#{self.class}_#{instance_variable_name}_#{field}"
    session_data[key] = instance_variable_get("@#{field}".to_sym) if session_data
  end 
end