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.



1006
1007
1008
# File 'lib/trellis/trellis.rb', line 1006

def initialize()
  @logger = Application.logger
end

Instance Attribute Details

#loggerObject

the page instance containing the component



1002
1003
1004
# File 'lib/trellis/trellis.rb', line 1002

def logger
  @logger
end

#pageObject

the page instance containing the component



1002
1003
1004
# File 'lib/trellis/trellis.rb', line 1002

def page
  @page
end

Class Method Details

.add_class_scripts_to_page(page, attributes) ⇒ Object



1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'lib/trellis/trellis.rb', line 1097

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



1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
# File 'lib/trellis/trellis.rb', line 1086

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



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

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


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

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



1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/trellis/trellis.rb', line 1119

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


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

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



1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/trellis/trellis.rb', line 1108

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



1039
1040
1041
# File 'lib/trellis/trellis.rb', line 1039

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

.depends_on(*syms) ⇒ Object



1173
1174
1175
1176
1177
1178
# File 'lib/trellis/trellis.rb', line 1173

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



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
# File 'lib/trellis/trellis.rb', line 1043

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



1149
1150
1151
# File 'lib/trellis/trellis.rb', line 1149

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

.inherited(child) ⇒ Object

:nodoc:



1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/trellis/trellis.rb', line 1010

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



1169
1170
1171
# File 'lib/trellis/trellis.rb', line 1169

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

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



1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'lib/trellis/trellis.rb', line 1136

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



1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
# File 'lib/trellis/trellis.rb', line 1157

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



1153
1154
1155
# File 'lib/trellis/trellis.rb', line 1153

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

.tag_name(name) ⇒ Object



1035
1036
1037
# File 'lib/trellis/trellis.rb', line 1035

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

Instance Method Details

#load_component_session_information(page, instance_variable_name, session_data) ⇒ Object



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
# File 'lib/trellis/trellis.rb', line 1187

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



1180
1181
1182
1183
1184
1185
# File 'lib/trellis/trellis.rb', line 1180

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