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