Class: SproutCore::ViewHelperSupport::RenderContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sproutcore/deprecated/view_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_helper_id, item_id, opts = {}, client_builder = nil, render_source = nil) ⇒ RenderContext

Returns a new instance of RenderContext.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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

Instance Attribute Details

#child_contextsObject (readonly)

Returns the value of attribute child_contexts.



106
107
108
# File 'lib/sproutcore/deprecated/view_helper.rb', line 106

def child_contexts
  @child_contexts
end

#client_builderObject

Returns the value of attribute client_builder.



103
104
105
# File 'lib/sproutcore/deprecated/view_helper.rb', line 103

def client_builder
  @client_builder
end

#current_helperObject

Returns the value of attribute current_helper.



102
103
104
# File 'lib/sproutcore/deprecated/view_helper.rb', line 102

def current_helper
  @current_helper
end

#defineObject

Returns the value of attribute define.



101
102
103
# File 'lib/sproutcore/deprecated/view_helper.rb', line 101

def define
  @define
end

#item_idObject (readonly)

Returns the value of attribute item_id.



99
100
101
# File 'lib/sproutcore/deprecated/view_helper.rb', line 99

def item_id
  @item_id
end

#outletObject

Returns the value of attribute outlet.



100
101
102
# File 'lib/sproutcore/deprecated/view_helper.rb', line 100

def outlet
  @outlet
end

#parent_contextObject (readonly)

Returns the value of attribute parent_context.



105
106
107
# File 'lib/sproutcore/deprecated/view_helper.rb', line 105

def parent_context
  @parent_context
end

#render_sourceObject (readonly)

Returns the value of attribute render_source.



104
105
106
# File 'lib/sproutcore/deprecated/view_helper.rb', line 104

def render_source
  @render_source
end

#view_helper_idObject (readonly)

options passed in from the view helper



98
99
100
# File 'lib/sproutcore/deprecated/view_helper.rb', line 98

def view_helper_id
  @view_helper_id
end

Instance Method Details

#_partial_properties(keys, join = ",\n") ⇒ Object



381
382
383
384
385
386
387
388
# 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

#attribute(option_key, default_value = :__UNDEFINED__, opts = {}, &block) ⇒ Object

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



341
342
343
344
345
# 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

#attributesObject

returns the standard attributes for the HTML. This will automatically include the item id. You can also declare added attributes using the attribute param.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# 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

#bind(option_key, default_value = :__UNDEFINED__, opts = {}) ⇒ Object

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.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# 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

#blank_urlObject



226
227
228
# File 'lib/sproutcore/deprecated/view_helper.rb', line 226

def blank_url
  static_url('blank.gif')
end

#close_tagObject Also known as: ct



333
334
335
# File 'lib/sproutcore/deprecated/view_helper.rb', line 333

def close_tag
  %{</#{@tag}>}
end

#content(text = nil, &block) ⇒ Object

this method must be called to configure the HTML. also captures the client builder in use at the time it is called.



210
211
212
213
# 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

#css_class_namesObject

Your view helper can add css classes to be appended to the classes attribute by adding to this array.



318
319
320
# File 'lib/sproutcore/deprecated/view_helper.rb', line 318

def css_class_names
  @css_class_names ||= []
end

#css_class_names=(new_ary) ⇒ Object



322
323
324
# File 'lib/sproutcore/deprecated/view_helper.rb', line 322

def css_class_names=(new_ary)
  @css_class_names = new_ary
end

#css_stylesObject

Your view helper can add text to by appended to the styles attribute by adding to this array.



308
309
310
# File 'lib/sproutcore/deprecated/view_helper.rb', line 308

def css_styles
  @css_styles ||= []
end

#css_styles=(new_ary) ⇒ Object



312
313
314
# File 'lib/sproutcore/deprecated/view_helper.rb', line 312

def css_styles=(new_ary)
  @css_styles = new_ary
end

#open_tagObject Also known as: ot

This does the standard open tag with the default tag and attributes. Usually you can use this.



328
329
330
# File 'lib/sproutcore/deprecated/view_helper.rb', line 328

def open_tag
  %{<#{@tag} #{attributes}>}
end

#optionsObject



137
138
139
# File 'lib/sproutcore/deprecated/view_helper.rb', line 137

def options
  @_options
end

#parent_helper(opts = {}) ⇒ Object



167
168
169
170
171
172
# 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

#prepare_bindingsObject



145
146
147
148
149
150
# 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

#prepare_for_javascript(value) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# 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

#prepare_outletsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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

#propertiesObject

returns all the JS properties specified by the property method.



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# 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

#property(option_key, default_value = :__UNDEFINED__, opts = {}, &block) ⇒ Object

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.



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# 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_contentObject

RENDER METHODS



175
176
177
178
179
180
181
182
183
# 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

#render_stylesObject



195
196
197
# File 'lib/sproutcore/deprecated/view_helper.rb', line 195

def render_styles
  _do_render(@styles_render)
end

#render_viewObject



185
186
187
188
189
# File 'lib/sproutcore/deprecated/view_helper.rb', line 185

def render_view
  prepare_bindings
  prepare_outlets
  _do_render(@view_render)
end

#set_outlet(key, opts = {}) ⇒ Object



141
142
143
# File 'lib/sproutcore/deprecated/view_helper.rb', line 141

def set_outlet(key,opts = {})
  @outlets << [key, opts]
end

#static_url(resource_name, opts = {}) ⇒ Object



220
221
222
223
224
# 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

#styles(text = nil, &block) ⇒ Object

this method may be called to add CSS styles



216
217
218
# File 'lib/sproutcore/deprecated/view_helper.rb', line 216

def styles(text = nil, &block)
  @styles_render = text || block if (text || block)
end

#to_sObject



133
134
135
# File 'lib/sproutcore/deprecated/view_helper.rb', line 133

def to_s
  "RenderContext #{view_helper_id}[#{item_id}]"
end

#var(option_key, default_value = :__UNDEFINED__, opts = {}, &block) ⇒ Object

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.



259
260
261
262
263
264
# 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

#view(view_class, text = nil, &block) ⇒ Object

This method must be called to configure the view.



203
204
205
206
# 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

#view_classObject



191
192
193
# File 'lib/sproutcore/deprecated/view_helper.rb', line 191

def view_class
  @view_class
end