Module: Zena::Use::Ajax::ViewMethods

Includes:
RubyLess
Defined in:
lib/zena/use/ajax.rb

Instance Method Summary collapse

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.



145
146
147
148
# File 'lib/zena/use/ajax.rb', line 145

def add_drag_id(dom_id, js_options = nil)
  @drag_ids ||= {}
  (@drag_ids[js_options] ||= []) << dom_id
end

#add_drop_id(dom_id, options) ⇒ Object

Used by zafu to transform a dom_id into a droppable element.



151
152
153
154
155
# File 'lib/zena/use/ajax.rb', line 151

def add_drop_id(dom_id, options)
  js_data << "Droppables.add('#{dom_id}', {hoverclass:'#{options[:hover] || 'drop_hover'}', onDrop:function(element){
  new Ajax.Request('#{options[:url]}', {asynchronous:true, evalScripts:true, method:'put', parameters:'drop=' + encodeURIComponent(element.id)});
}});"
end

#add_toggle_id(dom_id, group_name, role) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/zena/use/ajax.rb', line 157

def add_toggle_id(dom_id, group_name, role)
  @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}};"
  end
  list << dom_id
end

#filter_form(node, dom_id) ⇒ Object



173
174
175
# File 'lib/zena/use/ajax.rb', line 173

def filter_form(node, dom_id)
  js_data << %Q{new Form.Observer('#{dom_id}', 0.3, function(element, value) {new Ajax.Request('#{zafu_node_path(node)}', {asynchronous:true, evalScripts:true, method:'get', parameters:Form.serialize('#{dom_id}')})});}
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
# 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]
      return "#{params[:dom_id]}_#{node.zip}"
    end
  elsif append_form && node.kind_of?(Node) && params[:zadd]
    return "#{params[:dom_id]}_form"
  end

  @dom_id || params[:udom_id] || params[:dom_id]
end

#render_js(in_html = true) ⇒ Object

Include draggable ids in bottom of page Javascript.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/zena/use/ajax.rb', line 178

def render_js(in_html = true)
  if @drag_ids
    @drag_ids.each do |js_options, list|
      if js_options.nil?
        js_data << %Q{#{list.inspect}.each(Zena.draggable);}
      else
        js_data << %Q{#{list.inspect}.each(function(item) { Zena.draggable(item, #{js_options})});}
      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



23
24
25
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
# File 'lib/zena/use/ajax.rb', line 23

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.erb"
  elsif @errors || !obj.errors.empty?
    # B. could not update/delete: show errors
    form_file = template_path_from_template_url + "_form.erb"
    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]) + ".erb"
      if params[:upd_both]
        @dom_id = params[:dom_id]
        page.replace params[:dom_id], :file => template_path_from_template_url + ".erb"
      end
      if params[:done] && params[:zadd]
        page.toggle "#{params[:dom_id]}_form", "#{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 + ".erb"
      page << "$('#{params[:dom_id]}_form_t').focusFirstElement();"
    when 'create'
      pos = params[:position]  || :before
      ref = params[:reference] || "#{params[:dom_id]}_add"
      page.insert_html pos.to_sym, ref, :file => template_path_from_template_url + ".erb"
      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.erb"
      if params[:done]
        page << params[:done]
      elsif params[:zadd]
        page.toggle "#{params[:dom_id]}_form", "#{params[:dom_id]}_add"
      end
    when 'update'
      page.replace ndom_id, :file => template_path_from_template_url + ".erb"
      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 + ".erb"
    else
      if position = params[:insert]
        page.call 'Zena.insert_inner', params[:dom_id], position, render(:file => template_path_from_template_url + ".erb")
      else
        page.replace params[:dom_id], :file => template_path_from_template_url + ".erb"
      end
    end
  end
  page << render_js(false)
end