Module: LiveTree::LiveTreeHelper

Defined in:
app/helpers/live_tree.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.live_tree_js_name(name) ⇒ Object

:nodoc:



102
103
104
# File 'app/helpers/live_tree.rb', line 102

def LiveTreeHelper.live_tree_js_name(name) #:nodoc:
	Inflector.camelize(name).sub(/^(.)/) { |s| $1.downcase }
end

Instance Method Details

#_get_live_tree_data(item, options, params) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/live_tree.rb', line 82

def _get_live_tree_data(item, options, params) #:nodoc:
	options ||= {}
	get_item_name_proc = Proc.new { |x| x.name }
	if options[:get_item_name_proc] != nil
		get_item_name_proc = options[:get_item_name_proc]
	end
	depth = params[:depth] == nil ? nil : params[:depth].to_i
	include_parents = params[:include_parents]
	root_item_id = params[:root_item_id] == nil ? nil : params[:root_item_id].to_i
	
	result = _recurse_live_tree_data(item, depth, get_item_name_proc)
	if include_parents
		while item.parent != nil && (root_item_id == nil || item.id != root_item_id)
			result = _recurse_live_tree_data(item.parent, 2, get_item_name_proc, item.id, result)
			item = item.parent
		end	
	end			
	return result;
end

#_recurse_live_tree_data(item, depth, get_item_name_proc, special_child_id = nil, special_child_data = nil) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/live_tree.rb', line 60

def _recurse_live_tree_data(item, depth, get_item_name_proc, special_child_id = nil, special_child_data = nil) #:nodoc:
	result = "{id:" + item.id.to_s + ",name:'" + escape_javascript(get_item_name_proc.call(item).to_s) + "'"
	if item.children.size == 0
		result += ",children:[]"
	elsif depth == nil || depth > 1
		result += ",children:[\n"
		first = true
		for child in item.children
			result += ",\n" unless first
			first = false
			if child.id == special_child_id
				result += special_child_data
			else
				result += _recurse_live_tree_data(child, depth == nil ? nil : depth - 1, get_item_name_proc, special_child_id, special_child_data)
			end
		end
		result += "]"
	end
	result += "}"
	result
end

#construct_live_tree_data(item, options = {}) ⇒ Object

Returns data in format appropriate for use with the :initial_data parameter of #construct_live_tree_function .

Arguments:

  • item - Root item of the data.

  • options - Same options as LiveTree.get_live_tree_data, as well as:

    • :depth - How many level deep to recurse (default is nil, which means to recurse to the ends of all branches).



215
216
217
# File 'app/helpers/live_tree.rb', line 215

def construct_live_tree_data(item, options = {})
	return _get_live_tree_data(item, options, options)
end

#construct_live_tree_function(name, options = {}) ⇒ Object

Returns JavaScript code that constructs a JavaScript LiveTree object.

Arguments:

  • name - name of the tree

  • options - most of these options are passed to the JavaScript LiveTree object as-is, but their names are converted to camelcase-with-lowercase-first-letter (e.g. :on_click_item becomes onClickItem).

    See LiveTreeClient for details details about those options. In addition, the following may be specified:

    • :id - HTML ID to give the tree’s top HTML element (default is the tree’s name).

    • :data_action - Action used by the tree to read data (default is “#name_live_tree_data”).

    • :data_controller - Controller used by the tree to read data (default is the current controller).

    • :initial_data_root - Root item to construct initial tree data (optional, but you almost certainly want to include this).

    • :initial_data_whole_tree - Specifies whether the entire tree should be sent in the initial data, in which case the client will never have to contact the server for more data (default false, only used if :initial_data_root is specified).

    • :initial_data_options - Specifies options for constructing the initial data. See #construct_live_tree_data for details. If you used LiveTree::ClassMethods.live_tree to setup your controller, you don’t need to specify this (the same values passed to live_tree are used). (default {}, only used if :initial_data_root).



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/helpers/live_tree.rb', line 145

def construct_live_tree_function(name, options = {})
	options = options.dup;
	if options[:id] != nil
		tree_id = options[:id]
	else
		tree_id = name
	end
	for k in [:on_click_item, :on_expand_item, :on_collapse_item, :on_load_item]
		if options[k] != nil
			options[k] = "function(item){" + options[k] + "}"
		end
	end
	if options[:data_url] == nil				
		if options[:data_action] == nil
			act = name.to_s + "_live_tree_data"
		else
			act = options[:data_action]
		end
		if options[:data_controller] == nil
			options[:data_url] = { :action => act }
		else
			options[:data_url] = { :controller => options[:data_controller], :action => act }
		end
	end
	options[:data_url] = '"' + escape_javascript(url_for(options[:data_url])) + '"'
	for k in [:css_class, :css_style]
		if options[k] != nil
			options[k] = '"' + escape_javascript(options[k]) + '"'
		end
	end
	if options[:initial_data_root] != nil
		item = options[:initial_data_root]
		if (options[:initial_data_whole_tree])
			depth = nil
		elsif (options[:expand_root_item] == nil || options[:expand_root_item] || options[:hide_root_item])
			depth = 2
		else
			depth = 1
		end
		
		if options[:initial_data_options] == nil
			data_options_method = controller.method("#{name}_live_tree_options")
			if data_options_method
				data_options = data_options_method.call
			else
				data_options = {}
			end
		else
			data_options = options[:initial_data_options]
		end
		data_options = data_options.dup
		data_options[:depth] = depth;
		options[:initial_data] = construct_live_tree_data(item, data_options)
	end
	options.delete :id
	options.delete :data_action
	options.delete :data_controller
	options.delete :initial_data_root
	options.delete :initial_data_options
	options.delete :initial_data_whole_tree
	options_js = "{\n" + options.map {|k, v| LiveTreeHelper.live_tree_js_name(k) + ":#{v}"}.join(",\n") + "\n}"			
	"new LiveTree(\"" + tree_id.to_s + "\"," + options_js + ")"
end

#live_tree(name, options = {}) ⇒ Object

Embeds a LiveTree at the current location in the document.

Arguments:

  • name - name of the tree

  • options - may contain any of the options documented in #construct_live_tree_function, as well as:

    • :js_variable_name - JavaScript variable name to assign the JavaScript LiveTree object to (optional).

If :js_variable_name is not specified, the tree is assigned to a variable with the name of the tree converted to camelcase with a lowercase first letter (e.g. if the tree is named family_tree, it will be assigned to familyTree).



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/live_tree.rb', line 115

def live_tree(name, options = {})
	options = options.dup;
	if options[:js_variable_name] != nil
		var_name = options[:js_variable_name]
	else
		var_name = LiveTreeHelper.live_tree_js_name(name)
	end
	options.delete :js_variable_name
	js = "var " + var_name + "=" + construct_live_tree_function(name, options) + ";"
	js += var_name + ".render();"
	return javascript_tag(js);
end