Module: LiveTree::LiveTreeHelper
- Defined in:
- app/helpers/live_tree.rb
Class Method Summary collapse
-
.live_tree_js_name(name) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#_get_live_tree_data(item, options, params) ⇒ Object
:nodoc:.
-
#_recurse_live_tree_data(item, depth, get_item_name_proc, special_child_id = nil, special_child_data = nil) ⇒ Object
:nodoc:.
-
#construct_live_tree_data(item, options = {}) ⇒ Object
Returns data in format appropriate for use with the
:initial_data
parameter of #construct_live_tree_function . -
#construct_live_tree_function(name, options = {}) ⇒ Object
Returns JavaScript code that constructs a JavaScript LiveTree object.
-
#live_tree(name, options = {}) ⇒ Object
Embeds a LiveTree at the current location in the document.
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, , params) #:nodoc: ||= {} get_item_name_proc = Proc.new { |x| x.name } if [:get_item_name_proc] != nil get_item_name_proc = [: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 isnil
, 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, = {}) return _get_live_tree_data(item, , ) 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
becomesonClickItem
).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 (defaultfalse
, 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, = {}) = .dup; if [:id] != nil tree_id = [:id] else tree_id = name end for k in [:on_click_item, :on_expand_item, :on_collapse_item, :on_load_item] if [k] != nil [k] = "function(item){" + [k] + "}" end end if [:data_url] == nil if [:data_action] == nil act = name.to_s + "_live_tree_data" else act = [:data_action] end if [:data_controller] == nil [:data_url] = { :action => act } else [:data_url] = { :controller => [:data_controller], :action => act } end end [:data_url] = '"' + escape_javascript(url_for([:data_url])) + '"' for k in [:css_class, :css_style] if [k] != nil [k] = '"' + escape_javascript([k]) + '"' end end if [:initial_data_root] != nil item = [:initial_data_root] if ([:initial_data_whole_tree]) depth = nil elsif ([:expand_root_item] == nil || [:expand_root_item] || [:hide_root_item]) depth = 2 else depth = 1 end if [:initial_data_options] == nil = controller.method("#{name}_live_tree_options") if = .call else = {} end else = [:initial_data_options] end = .dup [:depth] = depth; [:initial_data] = construct_live_tree_data(item, ) end .delete :id .delete :data_action .delete :data_controller .delete :initial_data_root .delete :initial_data_options .delete :initial_data_whole_tree = "{\n" + .map {|k, v| LiveTreeHelper.live_tree_js_name(k) + ":#{v}"}.join(",\n") + "\n}" "new LiveTree(\"" + tree_id.to_s + "\"," + + ")" 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, = {}) = .dup; if [:js_variable_name] != nil var_name = [:js_variable_name] else var_name = LiveTreeHelper.live_tree_js_name(name) end .delete :js_variable_name js = "var " + var_name + "=" + construct_live_tree_function(name, ) + ";" js += var_name + ".render();" return javascript_tag(js); end |