class SproutCore::ViewHelperSupport::RenderContext
Attributes
options passed in from the view helper
Public Class Methods
# File lib/sproutcore/deprecated/view_helper.rb, line 108 def initialize(view_helper_id, item_id, opts={}, client_builder = nil, render_source=nil) @_options = opts.dup @bindings = (@_options[:bind] || {}).dup @outlets = [] @prototypes = {} @view_helper_id = view_helper_id @item_id = item_id @outlet = opts[:outlet] @define = opts[:define] @client_builder = client_builder @render_source = render_source @parent_context = SproutCore::PageHelper.current_render_context @parent_context.child_contexts << self if @parent_context @child_contexts = [] @attributes = (@_options[:attributes] || {}).dup @_properties = {} if @_options[:properties] @_options[:properties].each do | key, value | @_properties[key.to_s.camelize(:lower)] = prepare_for_javascript(value) end end end
Public Instance Methods
# File lib/sproutcore/deprecated/view_helper.rb, line 381 def _partial_properties(keys,join = ",\n") ret = keys.map do |key| value = @_properties[key] next if value.nil? %(#{key}: #{value}) end ret * join end
Call this method in your view helper definition to map an option to an attribute. This attribute can then be rendered with attributes. This method takes the same options as var
# File lib/sproutcore/deprecated/view_helper.rb, line 341 def attribute(option_key, default_value=:__UNDEFINED__, opts={}, &block) ret = _pair(option_key, default_value, opts, &block) return if ret[2] # ignore @attributes[ret[0]] = ret[1] end
returns the standard attributes for the HTML. This will automatically include the item id. You can also declare added attributes using the attribute param.
# File lib/sproutcore/deprecated/view_helper.rb, line 269 def attributes final_class_names = css_class_names final_styles = css_styles ret = @attributes.map do |key,value| # if the css class or css style is declared, replace the current # set coming from the view_helper if key.to_sym == :class && value final_class_names = value nil elsif key.to_sym == :style && value final_styles = value nil else value ? %(#{key}="#{value}") : nil end end # add in class names final_class_names = [final_class_names].flatten final_class_names << @item_id final_class_names.compact! unless final_class_names.empty? ret << %(class="#{final_class_names.uniq * ' '}") end # add in styles unless final_styles.nil? final_styles = [final_styles].flatten final_styles.compact! ret << %(style="#{final_styles.uniq * ' '}") unless final_styles.empty? end ret.compact * ' ' end
Call this method to make a binding available or to set a default binding. You can use this for properties you want to allow a binding for but don't want to take as a fully property.
# File lib/sproutcore/deprecated/view_helper.rb, line 393 def bind(option_key, default_value=:__UNDEFINED__, opts={}) key, v, ignore = _pair(option_key, default_value, opts, false) # always look for the option key in the bindings passed by the user. # if present, this should override whatever we set if found = @bindings[option_key.to_sym] || @bindings[option_key.to_s] v = found ignore = false @bindings.delete option_key.to_sym @bindings.delete option_key.to_s end # finally, set the binding value. unless ignore v = v.include?('(') ? v : prepare_for_javascript(v) @_properties["#{key.camelize(:lower)}Binding"] = v end end
# File lib/sproutcore/deprecated/view_helper.rb, line 226 def blank_url static_url('blank.gif') end
# File lib/sproutcore/deprecated/view_helper.rb, line 333 def close_tag %{</#{@tag}>} end
this method must be called to configure the HTML. also captures the client builder in use at the time it is called.
# File lib/sproutcore/deprecated/view_helper.rb, line 210 def content(text = nil, &block) @content_render_client_builder = self.client_builder @content_render = text || block if (text || block) end
Your view helper can add css classes to be appended to the classes attribute by adding to this array.
# File lib/sproutcore/deprecated/view_helper.rb, line 318 def css_class_names @css_class_names ||= [] end
# File lib/sproutcore/deprecated/view_helper.rb, line 322 def css_class_names=(new_ary) @css_class_names = new_ary end
Your view helper can add text to by appended to the styles attribute by adding to this array.
# File lib/sproutcore/deprecated/view_helper.rb, line 308 def css_styles @css_styles ||= [] end
# File lib/sproutcore/deprecated/view_helper.rb, line 312 def css_styles=(new_ary) @css_styles = new_ary end
This does the standard open tag with the default tag and attributes. Usually you can use this.
# File lib/sproutcore/deprecated/view_helper.rb, line 328 def open_tag %{<#{@tag} #{attributes}>} end
# File lib/sproutcore/deprecated/view_helper.rb, line 137 def options @_options end
# File lib/sproutcore/deprecated/view_helper.rb, line 167 def parent_helper(opts = {}) if @current_helper && @current_helper.parent_helper @_options.merge! opts @current_helper.parent_helper.prepare_context(self) end end
# File lib/sproutcore/deprecated/view_helper.rb, line 145 def prepare_bindings @bindings.each do | k,v | key = k.to_s.camelize(:lower) + 'Binding' @_properties[key] = v.include?('(') ? v : prepare_for_javascript(v) end end
# File lib/sproutcore/deprecated/view_helper.rb, line 439 def prepare_for_javascript(value) return 'null' if value.nil? case value when Array "[#{value.map { |v| prepare_for_javascript(v) } * ','}]" when Hash items = value.map do |k,v| [prepare_for_javascript(k),prepare_for_javascript(v)] * ': ' end "{ #{items * ', '} }" when FalseClass "false" when TrueClass "true" else %("#{ value.to_s.gsub('"','\"').gsub("\n",'\n') }") end end
# File lib/sproutcore/deprecated/view_helper.rb, line 152 def prepare_outlets return if @outlets.size == 0 outlets = [] @outlets.each do | key, opts | outlet_key = key.to_s.camelize(:lower) outlets << outlet_key unless opts[:lazy] outlet_path = opts[:outlet_path] || ".#{opts[:id] || key }?" str = %{#{opts[:class] || 'SC.View'}.extend({\n#{ opts[:properties] }\n}).outletFor("#{outlet_path}")} @_properties[outlet_key] = str end @_properties['outlets'] = outlets end
returns all the JS properties specified by the property method.
# File lib/sproutcore/deprecated/view_helper.rb, line 348 def properties keys = @_properties.keys ret = [] # example element, if there is one if @define @_properties['emptyElement'] = %($sel("#resources? .#{@item_id}:1:1")) ret << _partial_properties(['emptyElement']) end # outlets first if keys.include?('outlets') outlets = @_properties['outlets'] @_properties['outlets'] = '["' + (outlets * '","') + '"]' ret << _partial_properties(['outlets']) ret << _partial_properties(outlets,",\n\n") keys.reject! { |k| outlets.include?(k) || (k == 'outlets') } end bindings = keys.reject { |k| !k.match(/Binding$/) } if bindings.size > 0 ret << _partial_properties(bindings) keys.reject! { |k| bindings.include?(k) } end if keys.size > 0 ret << _partial_properties(keys) end ret = ret * ",\n\n" ' ' + ret.gsub("\n","\n ") end
Call this method in your view helper to specify a property you want added to the javascript declaration. This methos take the same options as var. Note that normally the type of value returned here will be marshalled into the proper type for JavaScript. If you provide a block to compute the property, however, the value will be inserted directly.
# File lib/sproutcore/deprecated/view_helper.rb, line 419 def property(option_key, default_value=:__UNDEFINED__, opts={}, &block) ret = _pair(option_key, default_value, opts, &block) key = ret[0].camelize(:lower) unless ret[2] # ignore value = ret[1] value = prepare_for_javascript(value) unless block_given? @_properties[key] = value end # also look for a matching binding and set it needed. if v = @bindings[option_key.to_sym] || @bindings[option_key.to_s] v = v.include?('(') ? v : prepare_for_javascript(v) @_properties["#{key}Binding"] = v @bindings.delete option_key.to_sym @bindings.delete option_key.to_s end end
RENDER METHODS
# File lib/sproutcore/deprecated/view_helper.rb, line 175 def render_content @attributes[:id] = @item_id if @item_id && !(@outlet || @define) old_client_builder = self.client_builder self.client_builder = @content_render_client_builder unless @content_render_client_builder.nil? ret = _do_render(@content_render) self.client_builder = old_client_builder return ret end
# File lib/sproutcore/deprecated/view_helper.rb, line 195 def render_styles _do_render(@styles_render) end
# File lib/sproutcore/deprecated/view_helper.rb, line 185 def render_view prepare_bindings prepare_outlets _do_render(@view_render) end
# File lib/sproutcore/deprecated/view_helper.rb, line 141 def set_outlet(key,opts = {}) @outlets << [key, opts] end
# File lib/sproutcore/deprecated/view_helper.rb, line 220 def static_url(resource_name, opts = {}) opts[:language] ||= @language entry = @client_builder.find_resource_entry(resource_name, opts) entry.nil? ? '' : entry.cacheable_url end
this method may be called to add CSS styles
# File lib/sproutcore/deprecated/view_helper.rb, line 216 def styles(text = nil, &block) @styles_render = text || block if (text || block) end
# File lib/sproutcore/deprecated/view_helper.rb, line 133 def to_s "RenderContext #{view_helper_id}[#{item_id}]" end
This will extract the specified value and put it into an ivar you can access later during rendering. For example:
var :label, 'Default label'
will now be accessible in your code via @label
Parameters: option_key: (req) the option to map. default_value: (opt) if passed, this will be used as the default value if the option is not passed in.
:key => (opt) the name of the resulting ivar. defaults to the option key.
:optional => (opt) if true, then the attribute will not be included if it is not explicitly passed in the options. if no default value is specified, then this will default to true, otherwise defaults to false.
:constant => (opt) if true, then any passed in options will be ignored for this key and the default you specify will be used instead. Defaults to false
you may also pass a block that will be used to compute the value at render time. Expect a single parameter which is the initial value.
# File lib/sproutcore/deprecated/view_helper.rb, line 259 def var(option_key, default_value=:__UNDEFINED__, opts={}, &block) ret = _pair(option_key, default_value, opts, &block) return if ret[2] # ignore instance_variable_set("@#{ret[0]}".to_sym, ret[1]) ret[1] end
This method must be called to configure the view.
# File lib/sproutcore/deprecated/view_helper.rb, line 203 def view(view_class,text = nil, &block) @view_class = view_class @view_render = text || block if (text || block) end
# File lib/sproutcore/deprecated/view_helper.rb, line 191 def view_class @view_class end
Private Instance Methods
INTERNAL SUPPORT
# File lib/sproutcore/deprecated/view_helper.rb, line 461 def _do_render(render_item) if render_item.nil? '' elsif render_item.instance_of?(Proc) render_item.call else render_item end end
# File lib/sproutcore/deprecated/view_helper.rb, line 471 def _pair(option_key, default_value, opts, look_for_key = true) if default_value.instance_of?(Hash) opts = default_value default_value = :__UNDEFINED__ end # get the attribute value. possibly return if no value and optional. optional = opts.has_key?(:optional) ? opts[:optional] : (default_value == :__UNDEFINED__) if opts[:constant] == true value = default_value elsif look_for_key && options.has_key?(option_key.to_sym) value = options[option_key.to_sym] elsif look_for_key && options.has_key?(option_key.to_s) value = options[option_key.to_s] else value = default_value end if (optional==true) && value == :__UNDEFINED__ ignore = true value = nil else ignore = false value = nil if value == :__UNDEFINED__ value = yield(value) if block_given? end attr_key = (opts[:key] || option_key).to_s [attr_key, value, ignore] end