Added validation hook on buttons
[fa-stable.git] / js / inserts.js
1 /**********************************************************************
2     Copyright (C) FrontAccounting, LLC.
3         Released under the terms of the GNU General Public License, GPL, 
4         as published by the Free Software Foundation, either version 3 
5         of the License, or (at your option) any later version.
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
9     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ***********************************************************************/
11 var _focus;
12 var _hotkeys = {
13         'alt': false,   // whether is the Alt key pressed
14         'list': false, // list of all elements with hotkey used recently
15         'focus': -1             // currently selected list element
16 };
17
18 function validate(e) {
19         if (e.name && (typeof _validate[e.name] == 'function'))
20                 return _validate[e.name](e);
21         return true;
22 }
23
24 function save_focus(e) {
25   _focus = e.name||e.id;
26   var h = document.getElementById('hints');
27   if (h) {
28         h.style.display = e.title && e.title.length ? 'inline' : 'none';
29         h.innerHTML = e.title ? e.title : '';
30   }
31 }
32
33 function _expand(tabobj) {
34
35   var ul = tabobj.parentNode.parentNode;
36   var alltabs=ul.getElementsByTagName("button");
37   var frm = tabobj.form;
38
39   if (ul.getAttribute("rel")){
40         for (var i=0; i<alltabs.length; i++){
41           alltabs[i].className = "ajaxbutton"  //deselect all tabs
42         }
43         tabobj.className = "current";
44         JsHttpRequest.request(tabobj)
45   }
46 }
47
48 //interface for selecting a tab (plus expand corresponding content)
49 function expandtab(tabcontentid, tabnumber) {
50   var tabs = document.getElementById(tabcontentid);
51  _expand(tabs.getElementsByTagName("input")[tabnumber]);
52 }
53
54 function _set_combo_input(e) {
55                 e.setAttribute('_last', e.value);
56                 e.onblur=function() { 
57                   var but_name = this.name.substring(0, this.name.length-4)+'button';
58                   var button = document.getElementsByName(but_name)[0];
59                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
60                   save_focus(select);
61 // submit request if there is submit_on_change option set and 
62 // search field has changed.
63                 
64                   if (button && (this.value != this.getAttribute('_last'))) {
65                         JsHttpRequest.request(button);
66                   } else if(this.className=='combo2') {
67                                 this.style.display = 'none';
68                                 select.style.display = 'inline';
69                                 setFocus(select);
70                   }
71                   return false;
72                 };
73                 e.onkeyup = function(ev) {
74                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
75                         if(select && select.selectedIndex>=0) {
76                           var len = select.length;
77                           var byid = this.className=='combo' || this.className=='combo3';
78                           var ac = this.value.toUpperCase();
79                           select.options[select.selectedIndex].selected = false;
80                           for (i = 0; i < len; i++) {
81                                 var txt = byid ? select.options[i].value : select.options[i].text;
82                                 if (this.className=='combo3') {
83                                   if(txt.toUpperCase().indexOf(ac) == 0) {
84                                         select.options[i].selected = true;
85                                         break;
86                                   }
87                                 } else {
88                                   if(txt.toUpperCase().indexOf(ac) >= 0) {
89                                         select.options[i].selected = true;
90                                         break;
91                                   }
92                                 }
93                           }
94                         }
95                 };
96         e.onkeydown = function(ev) { 
97                         ev = ev||window.event;
98                         key = ev.keyCode||ev.which;
99                         if(key == 13) {
100                           this.blur();
101                           return false;
102                         }
103                 }
104 }
105
106 function _update_box(s) {
107         var byid = s.className=='combo' || s.className=='combo3';
108         var rel = s.getAttribute('rel');
109         var box = document.getElementsByName(rel)[0];
110                 if(box && s.selectedIndex>=0) {
111                           var opt = s.options[s.selectedIndex];
112                                 if(box) {
113                                   var old = box.value;
114                                   box.value = byid ? opt.value : opt.text;
115                                   box.setAttribute('_last', box.value);
116                                   return old != box.value
117                                 }
118                 }
119 }
120
121 function _set_combo_select(e) {
122                 // When combo position is changed via js (eg from searchbox)
123                 // no onchange event is generated. To ensure proper change 
124                 // signaling we must track selectedIndex in onblur handler.
125                 e.setAttribute('_last', e.selectedIndex);
126                 e.onblur = function() {
127                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
128 //                      if(this.className=='combo')
129 //                          _update_box(this);
130                         if ((this.selectedIndex != this.getAttribute('_last'))
131                                 ||((this.className=='combo' || this.className=='combo3') && _update_box(this))
132                                 )
133                                         this.onchange();
134                 }
135                 e.onchange = function() {
136                         var s = this;
137                         this.setAttribute('_last', this.selectedIndex);
138                         if(s.className=='combo' || this.className=='combo3')
139                             _update_box(s);
140                         if(s.selectedIndex>=0) {
141                                  var sname = '_'+s.name+'_update';
142                                  var update = document.getElementsByName(sname)[0];
143                                  if(update) {
144                                             JsHttpRequest.request(update);
145                                 } 
146                         }
147                         return true;
148                 }
149                 e.onkeydown = function(event) {
150                     event = event||window.event;
151                     key = event.keyCode||event.which;
152                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
153                     if (box && key == 32 && this.className == 'combo2') {
154                             this.style.display = 'none';
155                             box.style.display = 'inline';
156                                 box.value='';
157                                 setFocus(box);
158                             return false;
159                          }
160                 }
161 }               
162
163 var _w;
164
165 function callEditor(key) {
166   var el = document.getElementsByName(editors[key][1])[0]; 
167   if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
168   _w = open(editors[key][0]+el.value+'&popup=1',
169           "edit","Scrollbars=0,resizable=0,width=800,height=600");
170   if (_w.opener == null)
171           _w.opener = self;
172   editors._call = key; // store call point for passBack 
173   _w.focus();
174 }
175
176 function passBack(value) {
177         var o = opener;
178         if(value != false) {
179                 var back = o.editors[o.editors._call]; // form input bindings
180                 var to = o.document.getElementsByName(back[1])[0];
181                 if (to) {
182                         if (to[0] != undefined) 
183                                 to[0].value = value; // ugly hack to set selector to any value
184                         to.value = value;
185                         // update page after item selection
186                         o.JsHttpRequest.request('_'+to.name+'_update', to.form);
187                         o.setFocus(to.name);
188                 }
189         }
190         close();
191 }
192
193 /*
194  Behaviour definitions
195 */
196 var inserts = {
197         'input': function(e) {
198                 if(e.onfocus==undefined) {
199                         e.onfocus = function() {
200                             save_focus(this);
201                                 if (this.className == 'combo' || this.className == 'combo3') 
202                                         this.select();
203                         };
204                 }
205                 if (e.className == 'combo' || e.className == 'combo2' || e.className == 'combo3') {
206                                 _set_combo_input(e);
207                 } 
208                 else
209                 if(e.type == 'text' ) {
210                                 e.onkeydown = function(ev) { 
211                                         ev = ev||window.event;
212                                         key = ev.keyCode||ev.which;
213                                         if(key == 13) {
214                                                 if(e.className == 'searchbox') e.onblur();
215                                                 return false;
216                                         } 
217                                         return true;
218                                 }
219                         }
220         },
221         'input.combo2,input[aspect="fallback"]': 
222         function(e) {
223             // this hides search button for js enabled browsers
224             e.style.display = 'none';
225         },
226         'div.js_only': 
227         function(e) {
228             // this shows divs for js enabled browsers only
229             e.style.display = 'block';
230         },
231         'button': function(e) {
232                 e.onclick = function(){ return validate(e); }
233         },
234 //      '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
235         'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton': 
236         function(e) {
237                         e.onclick = function() {
238                                 if (validate(e)) {
239                                         save_focus(e);
240                                         var asp = e.getAttribute('aspect')
241                                         if (asp && asp.indexOf('process') !== -1)
242                                                 JsHttpRequest.request(this, null, 60000);
243                                         else
244                                                 JsHttpRequest.request(this);
245                                 }
246                                 return false;
247                         }
248         },
249     '.amount': function(e) {
250                 if(e.onblur==undefined) {
251                   e.onblur = function() {
252                         var dec = this.getAttribute("dec");
253                         price_format(this.name, get_amount(this.name), dec);
254                   };
255                 }
256         },
257         '.searchbox': // emulated onchange event handling for text inputs
258                 function(e) {
259                         e.setAttribute('_last_val', e.value);
260                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
261                         e.onblur = function() {
262                                 var val = this.getAttribute('_last_val');
263                                 if (val != this.value) {
264                                         this.setAttribute('_last_val', this.value);
265                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
266                                 }
267                         }
268         },
269         'button[aspect*selector], button[aspect*abort], input[aspect*selector]': function(e) {
270                 e.onclick = function() {
271                         passBack(this.getAttribute('rel'));
272                         return false;
273                 }
274         },
275         'button[aspect=popup]': function(e) {
276                 e.onclick = function() {
277                         if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
278                           _w = open(document.location+'popup=1',
279                                   "edit","Scrollbars=0,resizable=0,width=800,height=600, top=50,left=50");
280                           if (_w.opener == null)
281                                   _w.opener = self;
282                         //  editors._call = key; // store call point for passBack 
283 //                        _w.moveTo(50, 50);
284                           _w.focus();
285                         return false;
286                 }
287         },
288         'select': function(e) {
289                 if(e.onfocus==undefined) {
290                         e.onfocus = function() {
291                             save_focus(this);
292                         };
293                   var c = e.className;
294                   if (c == 'combo' || c == 'combo2' || c == 'combo3')
295                         _set_combo_select(e);
296                 }
297         },
298         'a.printlink':  function(l) {
299                 l.onclick = function() {
300                     save_focus(this);
301                         JsHttpRequest.request(this, null, 60000);
302                         return false;
303                 }
304         },
305         'a': function(e) { // traverse menu
306                 e.onkeydown = function(ev) { 
307                         ev = ev||window.event;
308                         key = ev.keyCode||ev.which;
309                         if(key==37 || key==38 || key==39 || key==40) {
310                                         move_focus(key, e, document.links);
311                                         ev.returnValue = false;
312                                         return false;
313                         }
314                 }
315                 // prevent unneeded transaction entry abortion
316                 if (e.className == 'shortcut' 
317                  || e.className == 'menu_option' 
318                  || e.className == 'menu_tab'
319                  || e.className == 'selected')
320                         e.onclick = function(ev) {
321                                 if (_validate._processing 
322                                  && _validate._modified
323                                  && !confirm(_validate._processing)) {
324                                         ev.returnValue = false;
325                                         return false;
326                                 }
327                         }
328         },
329         'ul.ajaxtabs':  function(ul) {
330             var ulist=ul.getElementsByTagName("li");
331             for (var x=0; x<ulist.length; x++){ //loop through each LI e
332                 var tab=ulist[x].getElementsByTagName("button")[0];
333                 if(tab.onclick==undefined) {
334 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
335                     var url = tab.form.action
336                     tab.onclick=function(){
337                         _expand(this);
338                         return false;
339                     }
340                 }
341             }
342         }
343 /*      'tr.editrow': function(e) {
344                         e.onkeydown = function(ev) { 
345                         ev = ev||window.event;
346                         key = ev.keyCode||ev.which;
347                         if(key == 13) {
348                           // Find & click additem/update button
349                           
350                         } else  if(key == 27) {
351                           return false;
352                         }
353                 }
354
355         },
356 *//*    '#msgbox': function(e) {
357         // this is to avoid changing div height after ajax update in IE7
358           e.style.display = e.innerHTML.length ? 'block' : 'none';
359         }
360 *//* TODO
361         'a.date_picker':  function(e) {
362             // this un-hides data picker for js enabled browsers
363             e.href = date_picker(this.getAttribute('rel'));
364             e.style.display = '';
365             e.tabindex = -1; // skip in tabbing order
366         }
367 */
368 };
369
370 function stopEv(ev) {
371                         if(ev.preventDefault) {
372                                 ev.preventDefault();
373                                 ev.stopPropagation();
374                         } else {
375                                 ev.returnValue = false;
376                                 ev.cancelBubble = true;
377                                 window.keycode = 0;
378                         }
379                         return false;
380 }
381 /*
382         Modified accesskey system. While Alt key is pressed letter keys moves 
383         focus to next marked link. Alt key release activates focused link.
384 */
385 function setHotKeys() {
386         document.onkeydown = function(ev) {
387                 ev = ev||window.event;
388                 key = ev.keyCode||ev.which;
389                 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
390                         _hotkeys.alt = true;
391                         _hotkeys.focus = -1;
392                         return stopEv(ev);
393                 }
394                 else if (ev.altKey && !ev.ctrlKey && ((key>47 && key<58) || (key>64 && key<91))) {
395                         key = String.fromCharCode(key);
396                         var n = _hotkeys.focus;
397                         var l = document.getElementsBySelector('[accesskey='+key+']');
398                         var cnt = l.length;
399                         _hotkeys.list = l;
400                         for (var i=0; i<cnt; i++) { 
401                                 n = (n+1)%cnt;
402                                 // check also if the link is visible
403                                 if (l[n].accessKey==key && l[n].scrollWidth) {
404                                         _hotkeys.focus = n;
405             // The timeout is needed to prevent unpredictable behaviour on IE.
406                                         var tmp = function() {l[_hotkeys.focus].focus();};
407                                         setTimeout(tmp, 0);
408                                         break;
409                                 }
410                         }
411                         return stopEv(ev);
412                 }
413                 if((ev.ctrlKey && key == 13) || key == 27) {
414                         _hotkeys.alt = false; // cancel link selection
415                         _hotkeys.focus = -1;
416                         ev.cancelBubble = true;
417                         if(ev.stopPropagation) ev.stopPropagation();
418                         // activate submit/escape form
419                         for(var j=0; j<this.forms.length; j++) {
420                                 var form = this.forms[j];
421                                 for (var i=0; i<form.elements.length; i++){
422                                         var el = form.elements[i];
423                                         var asp = el.getAttribute('aspect');
424                                         
425
426                                         if (el.className!='editbutton' && (asp && asp.indexOf('selector') !== -1) && (key==13 || key==27)) {
427                                                 passBack(key==13 ? el.getAttribute('rel') : false);
428                                                 ev.returnValue = false;
429                                                 return false;
430                                         }
431                                         if (((asp && asp.indexOf('default') !== -1) && key==13)||((asp && asp.indexOf('cancel') !== -1) && key==27)) {
432                                                 if (validate(el)) {
433                                                         if (asp.indexOf('process') !== -1)
434                                                                 JsHttpRequest.request(el, null, 60000);
435                                                         else
436                                                                 JsHttpRequest.request(el);
437                                                 }
438                                                 ev.returnValue = false;
439                                                 return false;
440                                         }
441                                 }
442                         }
443                         ev.returnValue = false;
444                         return false;
445                 }
446                 if (editors && editors[key]) {
447                         callEditor(key);
448                         return stopEv(ev); // prevent default binding
449                 } 
450                 return true;
451         };
452         document.onkeyup = function(ev) {
453                 ev = ev||window.event;
454                 key = ev.keyCode||ev.which;
455
456                 if (_hotkeys.alt==true) {
457                         if (key == 18) {
458                                 _hotkeys.alt = false;
459                                 if (_hotkeys.focus>=0) {
460                                         var link = _hotkeys.list[_hotkeys.focus];
461                                         if(link.onclick) 
462                                                 link.onclick();
463                                         else
464                                                 if (link.target=='_blank') {
465                                                         window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
466                                                         openWindow(link.href,'_blank');
467                                                 } else
468                                                         window.location = link.href;
469                                 }
470                         return stopEv(ev);
471                         } 
472                 }
473                 return true;
474         }
475 }
476
477 Behaviour.register(inserts);
478
479 Behaviour.addLoadEvent(setFocus);
480 Behaviour.addLoadEvent(setHotKeys);