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