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

Last change on this file since 4392 was 2025, checked in by joachim, 17 years ago

ajax now works with ie

File size: 8.9 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.onclick = this.sendForm.bindAsEventListener(this);
90                count += 1;
91            }
92            }
93        }
94        if (reloadbuttons)
95        {
96            for (var i = 0; i < reloadbuttons.length; i += 1) {
97            var button = reloadbuttons[i];
98            var button_node = $(button);
99            if (button_node) {
100                button_node.onclick = this.sendFormReload.bindAsEventListener(this);
101                count += 1;
102            }
103            }
104        }
105        if (count>0) {
106            this.enabled = true;
107            edit_form.action = ''; // disabling direct sending
108        }
109        else {
110            this.enabled = false;
111        }
112      }
113    }
114  },
115
116  onCreate: function() {
117    if ($('ajax_psm')) {
118      var elt = $('ajax_psm');
119      elt.innerHTML = this.translations.working;
120      Effect.Appear('ajax_psm', {duration:1, queue:'end'});
121    }
122  },
123
124  onComplete: function() {
125    if ($('ajax_psm')) {
126      Effect.Fade('ajax_psm', {duration:1.5, queue:'end'});
127    }
128  },
129
130  sendForm: function(evt) {
131    if (this.enabled) {
132      this.onCreate();
133      // let's serialize the content of the form
134      // and send it to the server
135      params = Form.serialize(this.edit_form);
136      if (params!='') {
137        params += '&ajax_edit=1';
138      }
139      var sendFormCompletedFn = this.sendFormCompleted.bind(this);
140      var options = {parameters: params, onComplete: sendFormCompletedFn};
141      if (!this.sender) {
142        this.sender = new Ajax.Request(this.form_action, options);
143      }
144      else {
145        this.sender.setOptions(options);
146        this.sender.request(this.form_action);
147      }
148    }
149  },
150
151  sendFormReload: function(evt) {
152    if (this.enabled) {
153      this.onCreate();
154      // let's serialize the content of the form
155      // and send it to the server
156      params = Form.serialize(this.edit_form);
157      if (params!='') {
158        params += '&cpsdocument_edit_and_view_button=1';
159        params += '&ajax_edit=1';
160      }
161      var sendFormCompletedFn = this.sendFormCompleted.bind(this);
162      var options = {parameters: params, onComplete: sendFormCompletedFn};
163      if (!this.sender) {
164        this.sender = new Ajax.Request(this.form_action, options);
165      }
166      else {
167        this.sender.setOptions(options);
168        this.sender.request(this.form_action);
169      }
170    }
171  },
172
173  sendFormCompleted: function(originalRequest) {
174    var result = originalRequest.responseXML;
175
176    // result comes in three node, under ajax-response
177    result = result.childNodes[0];
178    var action = '';
179    var layout = '';
180    var form_result = false;
181
182    for (var i = 0; i < result.childNodes.length; i++) {
183      var node = result.childNodes[i];
184      if (node.nodeName=='result') {
185        if (node.childNodes[0].nodeValue=='True')
186          form_result = true;
187      }
188      if (node.nodeName=='layout') {
189        layout = node.childNodes[0].nodeValue;
190      }
191      if (node.nodeName=='action') {
192        if (node.childNodes.length>0) {
193          action = node.childNodes[0].nodeValue;
194        }
195      }
196    }
197
198    if (form_result) {
199      psm = this.translations.content_changed;
200      if (this.last_error)
201        this.rendered_node.innerHTML = layout;
202    }
203    else {
204      psm = this.translations.content_error;
205      // need to reload form here
206      this.rendered_node.innerHTML = layout;
207      // we also want to scroll to the first error
208      this.scrollTo('error');
209    }
210
211    // feedback, in any case
212    feedback = $('ajax_psm');
213    if (feedback) {
214     feedback.innerHTML = psm;
215    }
216
217    // reload elements
218    if (!action) {
219      this.initialize(this.form_id, this.form_action, this.rendered_id,
220                      this.buttons, this.reloadbuttons, this.title_id,
221                      this.translations, !form_result, this.blockers);
222      if (form_result)
223        this.reloadTitleFromForm();
224
225    }
226    else {
227      this.gotoUrl(action);
228    }
229    this.onComplete();
230  },
231
232  gotoUrl: function(dest) {
233    self.location.href = dest;
234  },
235
236  canSendForm: function() {
237    // trying to see if there are files in the form
238    var inputs = this.edit_form.getElementsByTagName('input');
239    for (var i = 0; i < inputs.length; i++) {
240      var input = inputs[i];
241      // if (input.type == 'file' && input.value != '') {
242      if (input.type == 'file') {
243        return false;
244      }
245    }
246    return true;
247  },
248
249  reloadTitleFromForm: function() {
250    if ($('widget__Title')) {
251        this.reloadTitle($('widget__Title').value);
252      }
253  },
254
255  reloadTitle: function(new_title) {
256    // reloading title
257    title_node = $(this.title_id);
258    if (title_node)
259      title_node.innerHTML = new_title;
260    document.title = new_title;
261  },
262
263  scrollTo: function(classname) {
264    elements = document.getElementsByClassName(classname, document);
265    if (elements.length>0) {
266      var element = elements[0];
267      var x = element.x ? element.x : element.offsetLeft,
268          y = element.y ? element.y : element.offsetTop;
269      window.scrollTo(x, y);
270    }
271  }
272}
273
274/*--------------------------------------------------------------------------*/
275
276if (!window.CPSFlexibleEdit) {
277  var CPSFlexibleEdit = new Object();
278  }
279 
280Object.extend(CPSFlexibleEdit, {
281  buttonClick: function(button, message) {
282  /*
283  this function will simulate a click on a submit button, by setting a hidden
284  field with the name/value of the clicked button and then submitting the form
285  */
286    if (message) {
287      if (window.confirm(message))
288        flag = true;
289    }
290    else
291      flag = true;
292    if (flag) {
293      buttonName = button.name;
294      buttonValue = button.value;
295      myform = button.form;
296      placeholder = document.getElementById('button_placeholder');
297      if (document.all) { // what follows should work
298                          // with Opera/IE but doesn't in Firefox
299        placeholder.name = buttonName;
300        placeholder.value = buttonValue;
301      }
302      else if (document.getElementById) { // so here is the
303                                          // Firefox workaround
304        placeholder.setAttribute('name', buttonName);
305        placeholder.setAttribute('value', buttonValue);
306      }
307      myform.submit();
308    }
309  }
310});
311
Note: See TracBrowser for help on using the repository browser.