Module: Zena::Use::Ajax::ViewMethods
- Includes:
- RubyLess
- Defined in:
- lib/zena/use/ajax.rb
Instance Method Summary collapse
-
#add_drag_id(dom_id, js_options = nil) ⇒ Object
Used by zafu to set dom_id that need to be made draggable.
-
#add_drop_id(dom_id, options) ⇒ Object
Used by zafu to transform a dom_id into a droppable element.
- #add_toggle_id(dom_id, group_name, role, opts = {}) ⇒ Object
- #filter_form(node, dom_id, loading, upd) ⇒ Object
-
#ndom_id(node = @node, append_form = true) ⇒ Object
Return the DOM id for a node.
-
#preview_node(node) ⇒ Object
Load parameters in node before rendering.
-
#render_js(in_html = true) ⇒ Object
Include draggable ids in bottom of page Javascript.
-
#update_page_content(page, obj) ⇒ Object
RJS to update a page after create/update/destroy.
Instance Method Details
#add_drag_id(dom_id, js_options = nil) ⇒ Object
Used by zafu to set dom_id that need to be made draggable.
161 162 163 164 |
# File 'lib/zena/use/ajax.rb', line 161 def add_drag_id(dom_id, = nil) @drag_ids ||= {} (@drag_ids[] ||= []) << dom_id end |
#add_drop_id(dom_id, options) ⇒ Object
Used by zafu to transform a dom_id into a droppable element.
167 168 169 170 171 |
# File 'lib/zena/use/ajax.rb', line 167 def add_drop_id(dom_id, ) js_data << "Droppables.add('#{dom_id}', {hoverclass:'#{[:hover] || 'drop_hover'}', onDrop:function(element){ new Ajax.Request('#{[:url]}', {asynchronous:true, evalScripts:true, method:'put', parameters:'drop=' + encodeURIComponent(element.id)}); }});" end |
#add_toggle_id(dom_id, group_name, role, opts = {}) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/zena/use/ajax.rb', line 173 def add_toggle_id(dom_id, group_name, role, opts = {}) arity = opts[:arity] || 'many' if js = opts[:js] js = ", js:function(e) { #{js} }" end @toggle_ids ||= {} unless list = @toggle_ids[group_name] list = @toggle_ids[group_name] = [] if other = yield found = other.rel[role].other_zips else found = [] end url = "/nodes/#{other.zip}" js_data << "#{group_name} = {list:#{found.inspect}, url:#{url.inspect}, role:#{role.inspect}, arity:#{arity.inspect}#{js}};" end list << dom_id end |
#filter_form(node, dom_id, loading, upd) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/zena/use/ajax.rb', line 194 def filter_form(node, dom_id, loading, upd) if loading loading = "\n#{loading}($('#{upd}'));" else loading = '' end # Disable 'redir' parameter during preview or filter. js_data << %Q{new Form.Observer('#{dom_id}', 0.3, function(element, value) {#{loading} var data = Form.serialize('#{dom_id}').gsub(/&redir=/,'&no_redir=') new Ajax.Request('#{zafu_node_path(node)}', {asynchronous:true, evalScripts:true, method:'post', parameters:data}) });} end |
#ndom_id(node = @node, append_form = true) ⇒ Object
Return the DOM id for a node. We had to name this method ‘ndom_id’ because we want to avoid the clash with Rails’ dom_id method.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/zena/use/ajax.rb', line 10 def ndom_id(node = @node, append_form = true) if node.kind_of?(Node) && !node.new_record? if params[:action] == 'create' && !params[:udom_id] && !params[:s] # !params[:s] is to not use this when executing Zena.post # We cannot filter with params[:zadd] because this breaks when editing in lists. return "#{params[:dom_id]}_#{node.zip}" end elsif append_form && node.kind_of?(Node) && params[:zadd] return "#{params[:dom_id]}_#{node.zip.to_i}" end @dom_id || params[:udom_id] || params[:dom_id] end |
#preview_node(node) ⇒ Object
Load parameters in node before rendering. SECURITY: There may be a security threat here if the node attributes are used for queries or relations.
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/zena/use/ajax.rb', line 209 def preview_node(node) return nil if !node.kind_of?(Node) if attrs = params[:node] # Return a copy new_node = node.dup new_node.version = node.version.dup new_node.attributes = Node.transform_attributes(params[:node], node, true) return new_node else return node end end |
#render_js(in_html = true) ⇒ Object
Include draggable ids in bottom of page Javascript.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/zena/use/ajax.rb', line 223 def render_js(in_html = true) if @drag_ids @drag_ids.each do |, list| if .nil? js_data << %Q{#{list.inspect}.each(Zena.draggable);} else js_data << %Q{#{list.inspect}.each(function(item) { Zena.draggable(item, #{})});} end end end if @toggle_ids @toggle_ids.each do |group_name, list| js_data << %Q{#{list.inspect}.each(function(item) { Zena.set_toggle(item, #{group_name})});} end end # Super is in Zena::Use::Rendering super end |
#update_page_content(page, obj) ⇒ Object
RJS to update a page after create/update/destroy
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/zena/use/ajax.rb', line 26 def update_page_content(page, obj) unless params[:dom_id] # simply reply with failure or success if !obj.errors.empty? page << "alert(#{obj.errors.first.join(': ')});" page << "return false;" # How to avoid 'onSuccess' ? elsif params[:udom_id] == '_page' # reload page page << "document.location.href = document.location.href;" else # ? end return end if params[:t_id] && obj.errors.empty? obj = secure(Node) { Node.find_by_zip(params[:t_id])} @node = obj end base_class = obj.kind_of?(Node) ? Node : obj.class if obj.new_record? # A. could not create object: show form with errors page.replace ndom_id, :file => template_path_from_template_url('_form') elsif @errors || !obj.errors.empty? # B. could not update/delete: show errors form_file = template_path_from_template_url('_form') if File.exist?(form_file) page.replace ndom_id, :file => form_file else page.insert_html :top, params[:dom_id], :inline => render_errors end elsif params[:udom_id] if params[:udom_id] == '_page' # reload page page << "document.location.href = document.location.href;" else # C. update another part of the page if node_id = params[:u_id] if node_id.to_i != obj.zip if base_class == Node instance_variable_set("@#{base_class.to_s.underscore}", secure(base_class) { base_class.find_by_zip(node_id) }) else instance_variable_set("@#{base_class.to_s.underscore}", secure(base_class) { base_class.find_by_id(node_id) }) end end end page.replace params[:udom_id], :file => template_path_from_template_url('', params[:u_url] || params[:t_url]) if params[:upd_both] @dom_id = params[:dom_id] page.replace params[:dom_id], :file => template_path_from_template_url end if params[:done] && params[:zadd] page.toggle "#{params[:dom_id]}_0", "#{params[:dom_id]}_add" page << params[:done] elsif params[:done] page << params[:done] end end else # D. normal update #if params[:dom_id] == '_page' # # reload page # page << "document.location.href = document.location.href;" # case params[:action] when 'edit' page.replace params[:dom_id], :file => template_path_from_template_url page << "$('#{params[:dom_id]}_form_t').focusFirstElement();" when 'create' if params[:zadd] # ADD WITH FORM... pos = params[:position] || :before ref = params[:reference] || "#{params[:dom_id]}_add" page.insert_html pos.to_sym, ref, :file => template_path_from_template_url if obj.kind_of?(Node) @node = obj.parent.new_child(:class => obj.class) else instance_variable_set("@#{base_class.to_s.underscore}", obj.clone) end page.replace ndom_id, :file => template_path_from_template_url('_form') if params[:done] page << params[:done] elsif params[:zadd] page.toggle "#{params[:dom_id]}_0", "#{params[:dom_id]}_add" end else # Zena.post operation page.replace ndom_id, :file => template_path_from_template_url page << params[:done] if params[:done] end when 'update' page.replace ndom_id, :file => template_path_from_template_url page << params[:done] if params[:done] when 'destroy' page << %Q{ new Effect.Highlight('#{ndom_id}', { duration: 0.3, afterFinish: function() { new Effect.Fade('#{ndom_id}', { duration: 0.5, afterFinish: function() { $('#{ndom_id}').remove(); } }); } }); } when 'drop' case params[:done] when 'remove' page.visual_effect :highlight, params[:drop], :duration => 0.3 page.visual_effect :fade, params[:drop], :duration => 0.3 end page.replace params[:dom_id], :file => template_path_from_template_url else if position = params[:insert] page.call 'Zena.insert_inner', params[:dom_id], position, render(:file => template_path_from_template_url) else page.replace params[:dom_id], :file => template_path_from_template_url end end end if params[:redir] page << "window.location.href = '#{params[:redir]}';" end if params[:reload] page << "Zena.reload(#{params[:reload].inspect});" end page << render_js(false) end |