Dotclear

source: admin/js/confirm-close.js @ 3445:26274861f3f6

Revision 3445:26274861f3f6, 2.7 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Add button type to non submit button, add meta-helper class to meta editor buttons, cope with helper buttons in confirm-close.js, addresses #2226

The

chainHandler(window,'onload',function() {

confirmClosePage.getCurrentForms();

});

seems to be run before all DOM ready (with all js done), CKEditor or LegacyEditor? add some stuff to current form after that ?!?
a 1 second delay to run again confirmClosePage.getCurrentForms() is not enough…

Line 
1function confirmClose() {
2
3     if (arguments.length > 0) {
4          for (var i=0; i<arguments.length; i++) {
5               this.forms_id.push(arguments[i]);
6          }
7     }
8};
9
10confirmClose.prototype = {
11     prompt: 'You have unsaved changes.',
12     forms_id: new Array(),
13     forms: new Array(),
14     formSubmit: false,
15
16     getCurrentForms: function() {
17          var formsInPage = this.getForms();
18          var f;
19          var This = this;
20          this.forms=new Array();
21          for (var i=0; i<formsInPage.length; i++) {
22               f = formsInPage[i];
23               var tmpForm = new Array();
24               for (var j=0; j<f.elements.length; j++) {
25                    tmpForm.push(this.getFormElementValue(f[j]));
26               }
27               this.forms.push(tmpForm);
28
29               chainHandler(f,'onsubmit',function() {
30                    This.formSubmit = true;
31               });
32          }
33     },
34
35     compareForms: function() {
36          if (this.forms.length == 0) {
37               return true;
38          }
39
40          var formsInPage = this.getForms();
41          var f;
42          for (var i=0; i<formsInPage.length; i++) {
43               f = formsInPage[i];
44               for (var j=0; j < Math.min(f.elements.length,this.forms[i].length); j++) {
45                    if (this.forms[i][j] != this.getFormElementValue(f[j])) {
46                         return false;
47                    }
48               }
49          }
50
51          return true;
52     },
53
54     getForms: function() {
55          if (!document.getElementsByTagName || !document.getElementById) {
56               return new Array();
57          }
58
59          if (this.forms_id.length > 0) {
60               var res = new Array();
61               var f;
62               for (var i=0; i<this.forms_id.length; i++) {
63                    f = document.getElementById(this.forms_id[i]);
64                    if (f != undefined) {
65                         res.push(f);
66                    }
67               }
68               return res;
69          } else {
70               return document.getElementsByTagName('form');
71          }
72
73          return new Array();
74     },
75
76     getFormElementValue: function(e) {
77          if (e == undefined || e.classList.contains('meta-helper') || e.classList.contains('checkbox-helper')) {
78               return null;
79          }
80          if (e.type != undefined && e.type == 'radio') {
81               return this.getFormRadioValue(e);
82          } else if (e.type != undefined && e.type == 'checkbox') {
83               return this.getFormCheckValue(e);
84          } else if (e.type == 'password') {
85               return null;
86          } else if (e.value != undefined) {
87               return e.value;
88          } else {
89               return null;
90          }
91     },
92
93     getFormCheckValue: function(e) {
94          if (e.checked) {
95               return e.value;
96          }
97          return null;
98     },
99
100     getFormRadioValue: function(e) {
101          for (var i=0; i <e.length; i++) {
102               if (e[i].checked) {
103                    return e[i].value;
104               } else {
105                    return null;
106               }
107          }
108          return null;
109     }
110};
111
112var confirmClosePage = new confirmClose();
113
114chainHandler(window,'onload',function() {
115     confirmClosePage.getCurrentForms();
116});
117
118chainHandler(window,'onbeforeunload',function(event_) {
119     if (event_ == undefined && window.event) {
120          event_ = window.event;
121     }
122
123     if (!confirmClosePage.formSubmit && !confirmClosePage.compareForms()) {
124          event_.returnValue = confirmClosePage.prompt;
125          return confirmClosePage.prompt;
126     }
127     return false;
128});
Note: See TracBrowser for help on using the repository browser.

Sites map