source: WAeUP_SRP/trunk/skins/waeup_default/cpsdocument.js.dtml @ 2023

Last change on this file since 2023 was 2017, checked in by joachim, 17 years ago

started ajaxifying

File size: 9.0 KB
Line 
1/*
2<dtml-let last_modified="_.DateTime()-14" expires="_.DateTime()+1" >
3<dtml-call "REQUEST.RESPONSE.setHeader( 'Content-Type', 'text/javascript' )">
4<dtml-call "REQUEST.RESPONSE.setHeader( 'Last-Modified', last_modified.toZone('GMT').rfc822() )">
5<dtml-call "REQUEST.RESPONSE.setHeader( 'Cache-Control', 'max-age=36000, must-revalidate' )">
6<dtml-call "REQUEST.RESPONSE.setHeader( 'Expires', expires.toZone('GMT').rfc822() )" >
7</dtml-let>
8*/
9/*
10# (C) Copyright 2003 Nuxeo SARL <http://nuxeo.com>
11# Author: Tarek Ziadé <tz@nuxeo.com>
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License version 2 as published
15# by the Free Software Foundation.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25# 02111-1307, USA.
26#
27# $Id:$
28
29CPSDocument javascript layer
30
31XXX need to find a way to upload file, it is very
32restrictive as it does'nt allow to send simple
33types like Document, flexible, etc...
34
35*/
36
37var CPSDocumentEditor = Class.create();
38
39CPSDocumentEditor.prototype = {
40  initialize: function(form_id, form_action, rendered_id, buttons,
41                       reloadbuttons, title_id, translations, last_error,
42                       blockers) {
43    // if any blocker is found, we don't ajaxify
44    if (blockers) {
45      for (var i = 0; i < blockers.length; i += 1) {
46        var blocker = blockers[i];
47        if ($(blocker)) {
48          this.enabled = false;
49          return;
50        }
51      }
52    }
53    this.form_id = form_id;
54    this.blockers = blockers;
55    this.effects = Effect;
56    this.rendered_id = rendered_id;
57    this.title_id = title_id;
58    this.last_error = last_error;
59    this.sender = null;
60    this.translations = translations;
61
62    var edit_form = $(form_id);
63    var rendered_node = $(rendered_id);
64    if (!edit_form || !rendered_node) {
65      this.enabled = false;
66    }
67    else {
68
69      // getting the action value
70      this.edit_form = edit_form;
71
72      if (!this.canSendForm()) {
73        this.enabled = false;
74      }
75      else
76      {
77        this.rendered_node = rendered_node;
78        this.buttons = buttons;
79        this.reloadbuttons = reloadbuttons;
80        this.form_action = form_action;
81        // linking action to buttons
82        var count = 0;
83        if (buttons)
84        {
85            for (var i = 0; i < buttons.length; i += 1) {
86            var button = buttons[i];
87            var button_node = $(button);
88            if (button_node) {
89                button_node.type = 'button';
90                button_node.onclick = this.sendForm.bindAsEventListener(this);
91                count += 1;
92            }
93            }
94        }
95        if (reloadbuttons)
96        {
97            for (var i = 0; i < reloadbuttons.length; i += 1) {
98            var button = reloadbuttons[i];
99            var button_node = $(button);
100            if (button_node) {
101                button_node.type = 'button';
102                button_node.onclick = this.sendFormReload.bindAsEventListener(this);
103                count += 1;
104            }
105            }
106        }
107        if (count>0) {
108            this.enabled = true;
109            edit_form.action = ''; // disabling direct sending
110        }
111        else {
112            this.enabled = false;
113        }
114      }
115    }
116  },
117
118  onCreate: function() {
119    if ($('ajax_psm')) {
120      var elt = $('ajax_psm');
121      elt.innerHTML = this.translations.working;
122      Effect.Appear('ajax_psm', {duration:1, queue:'end'});
123    }
124  },
125
126  onComplete: function() {
127    if ($('ajax_psm')) {
128      Effect.Fade('ajax_psm', {duration:1.5, queue:'end'});
129    }
130  },
131
132  sendForm: function(evt) {
133    if (this.enabled) {
134      this.onCreate();
135      // let's serialize the content of the form
136      // and send it to the server
137      params = Form.serialize(this.edit_form);
138      if (params!='') {
139        params += '&ajax_edit=1';
140      }
141      var sendFormCompletedFn = this.sendFormCompleted.bind(this);
142      var options = {parameters: params, onComplete: sendFormCompletedFn};
143      if (!this.sender) {
144        this.sender = new Ajax.Request(this.form_action, options);
145      }
146      else {
147        this.sender.setOptions(options);
148        this.sender.request(this.form_action);
149      }
150    }
151  },
152
153  sendFormReload: function(evt) {
154    if (this.enabled) {
155      this.onCreate();
156      // let's serialize the content of the form
157      // and send it to the server
158      params = Form.serialize(this.edit_form);
159      if (params!='') {
160        params += '&cpsdocument_edit_and_view_button=1';
161        params += '&ajax_edit=1';
162      }
163      var sendFormCompletedFn = this.sendFormCompleted.bind(this);
164      var options = {parameters: params, onComplete: sendFormCompletedFn};
165      if (!this.sender) {
166        this.sender = new Ajax.Request(this.form_action, options);
167      }
168      else {
169        this.sender.setOptions(options);
170        this.sender.request(this.form_action);
171      }
172    }
173  },
174
175  sendFormCompleted: function(originalRequest) {
176    var result = originalRequest.responseXML;
177
178    // result comes in three node, under ajax-response
179    result = result.childNodes[0];
180    var action = '';
181    var layout = '';
182    var form_result = false;
183
184    for (var i = 0; i < result.childNodes.length; i++) {
185      var node = result.childNodes[i];
186      if (node.nodeName=='result') {
187        if (node.childNodes[0].nodeValue=='True')
188          form_result = true;
189      }
190      if (node.nodeName=='layout') {
191        layout = node.childNodes[0].nodeValue;
192      }
193      if (node.nodeName=='action') {
194        if (node.childNodes.length>0) {
195          action = node.childNodes[0].nodeValue;
196        }
197      }
198    }
199
200    if (form_result) {
201      psm = this.translations.content_changed;
202      if (this.last_error)
203        this.rendered_node.innerHTML = layout;
204    }
205    else {
206      psm = this.translations.content_error;
207      // need to reload form here
208      this.rendered_node.innerHTML = layout;
209      // we also want to scroll to the first error
210      this.scrollTo('error');
211    }
212
213    // feedback, in any case
214    feedback = $('ajax_psm');
215    if (feedback) {
216     feedback.innerHTML = psm;
217    }
218
219    // reload elements
220    if (!action) {
221      this.initialize(this.form_id, this.form_action, this.rendered_id,
222                      this.buttons, this.reloadbuttons, this.title_id,
223                      this.translations, !form_result, this.blockers);
224      if (form_result)
225        this.reloadTitleFromForm();
226
227    }
228    else {
229      this.gotoUrl(action);
230    }
231    this.onComplete();
232  },
233
234  gotoUrl: function(dest) {
235    self.location.href = dest;
236  },
237
238  canSendForm: function() {
239    // trying to see if there are files in the form
240    var inputs = this.edit_form.getElementsByTagName('input');
241    for (var i = 0; i < inputs.length; i++) {
242      var input = inputs[i];
243      // if (input.type == 'file' && input.value != '') {
244      if (input.type == 'file') {
245        return false;
246      }
247    }
248    return true;
249  },
250
251  reloadTitleFromForm: function() {
252    if ($('widget__Title')) {
253        this.reloadTitle($('widget__Title').value);
254      }
255  },
256
257  reloadTitle: function(new_title) {
258    // reloading title
259    title_node = $(this.title_id);
260    if (title_node)
261      title_node.innerHTML = new_title;
262    document.title = new_title;
263  },
264
265  scrollTo: function(classname) {
266    elements = document.getElementsByClassName(classname, document);
267    if (elements.length>0) {
268      var element = elements[0];
269      var x = element.x ? element.x : element.offsetLeft,
270          y = element.y ? element.y : element.offsetTop;
271      window.scrollTo(x, y);
272    }
273  }
274}
275
276/*--------------------------------------------------------------------------*/
277
278if (!window.CPSFlexibleEdit) {
279  var CPSFlexibleEdit = new Object();
280  }
281 
282Object.extend(CPSFlexibleEdit, {
283  buttonClick: function(button, message) {
284  /*
285  this function will simulate a click on a submit button, by setting a hidden
286  field with the name/value of the clicked button and then submitting the form
287  */
288    if (message) {
289      if (window.confirm(message))
290        flag = true;
291    }
292    else
293      flag = true;
294    if (flag) {
295      buttonName = button.name;
296      buttonValue = button.value;
297      myform = button.form;
298      placeholder = document.getElementById('button_placeholder');
299      if (document.all) { // what follows should work
300                          // with Opera/IE but doesn't in Firefox
301        placeholder.name = buttonName;
302        placeholder.value = buttonValue;
303      }
304      else if (document.getElementById) { // so here is the
305                                          // Firefox workaround
306        placeholder.setAttribute('name', buttonName);
307        placeholder.setAttribute('value', buttonValue);
308      }
309      myform.submit();
310    }
311  }
312});
313
Note: See TracBrowser for help on using the repository browser.