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.



963
964
965
# File 'lib/trellis/trellis.rb', line 963

def initialize()
  @logger = Application.logger
end

Instance Attribute Details

#loggerObject

the page instance containing the component



959
960
961
# File 'lib/trellis/trellis.rb', line 959

def logger
  @logger
end

#pageObject

the page instance containing the component



959
960
961
# File 'lib/trellis/trellis.rb', line 959

def page
  @page
end

Class Method Details

.add_class_scripts_to_page(page, attributes) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/trellis/trellis.rb', line 1054

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.dom.at_css("html/body").children.last.after("\n#{script}")
  end      
end

.add_class_styles_to_page(page, attributes) ⇒ Object



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/trellis/trellis.rb', line 1043

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.dom.at_css("html/head").children.last.after("\n#{style}")
  end      
end

.add_document_modifications_to_page(page) ⇒ Object



1087
1088
1089
1090
1091
# File 'lib/trellis/trellis.rb', line 1087

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


1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/trellis/trellis.rb', line 1034

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.dom.at_css("html/head").children.last.after("\n#{script}")
  end      
end

.add_scripts_to_page(page, attributes) ⇒ Object



1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/trellis/trellis.rb', line 1076

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.dom.at_css("html/body").children.last.after("\n#{script}")
  end      
end


1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/trellis/trellis.rb', line 1025

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.dom.at_css("html/head").children.last.after("\n#{link}")
  end
end

.add_styles_to_page(page, attributes) ⇒ Object



1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# File 'lib/trellis/trellis.rb', line 1065

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.dom.at_css("html/head").children.last.after("\n#{style}")
  end      
end

.contained_in(*args) ⇒ Object



996
997
998
# File 'lib/trellis/trellis.rb', line 996

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

.depends_on(*syms) ⇒ Object



1130
1131
1132
1133
1134
1135
# File 'lib/trellis/trellis.rb', line 1130

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



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/trellis/trellis.rb', line 1000

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



1106
1107
1108
# File 'lib/trellis/trellis.rb', line 1106

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

.inherited(child) ⇒ Object

:nodoc:



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/trellis/trellis.rb', line 967

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



1126
1127
1128
# File 'lib/trellis/trellis.rb', line 1126

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

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



1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/trellis/trellis.rb', line 1093

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



1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'lib/trellis/trellis.rb', line 1114

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



1110
1111
1112
# File 'lib/trellis/trellis.rb', line 1110

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

.tag_name(name) ⇒ Object



992
993
994
# File 'lib/trellis/trellis.rb', line 992

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

Instance Method Details

#load_component_session_information(page, instance_variable_name, session_data) ⇒ Object



1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/trellis/trellis.rb', line 1144

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



1137
1138
1139
1140
1141
1142
# File 'lib/trellis/trellis.rb', line 1137

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