Fixed hotkeys on multiform pages.
[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 debug(msg) {
18     box = document.getElementById('msgbox')
19         box.innerHTML= box.innerHTML+'<br>'+msg
20 }
21
22 function progbar() {
23         box = document.getElementById('msgbox');
24     box.innerHTML= "<center><table width='98%' border='1' cellpadding=3 "
25         +"bordercolor='#007700' style='border-collapse: collapse'>"
26         +"<tr><td align='center' bgcolor='#ccffcc' >"
27                 +"<img src='"+user.theme+"images/progressbar.gif' alt='"
28                 +user.loadtxt+"' /></td></tr></table></center><br>";
29         box.style.display = 'block';
30 }
31
32 function save_focus(e) {
33   _focus = e.name||e.id;
34   var h = document.getElementById('hints');
35   if (h) {
36         h.style.display = e.title && e.title.length ? 'inline' : 'none';
37         h.innerHTML = e.title ? e.title : '';
38   }
39 }
40
41 function _expand(tabobj) {
42
43   var ul = tabobj.parentNode.parentNode;
44   var alltabs=ul.getElementsByTagName("input");
45   var frm = tabobj.form;
46
47   if (ul.getAttribute("rel")){
48         for (var i=0; i<alltabs.length; i++){
49           alltabs[i].className = "ajaxbutton"  //deselect all tabs
50         }
51         tabobj.className = "current";
52         JsHttpRequest.request(tabobj)
53   }
54 }
55
56 //interface for selecting a tab (plus expand corresponding content)
57 function expandtab(tabcontentid, tabnumber) {
58   var tabs = document.getElementById(tabcontentid);
59  _expand(tabs.getElementsByTagName("input")[tabnumber]);
60 }
61
62 function _set_combo_input(e) {
63                 e.setAttribute('_last', e.value);
64                 e.onblur=function() { 
65                   var but_name = this.name.substring(0, this.name.length-4)+'button';
66                   var button = document.getElementsByName(but_name)[0];
67                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
68                   save_focus(select);
69 // submit request if there is submit_on_change option set and 
70 // search field has changed.
71                   if (button && (this.value != this.getAttribute('_last'))) {
72                         JsHttpRequest.request(button);
73                   } else if(this.className=='combo2') {
74                                 this.style.display = 'none';
75                                 select.style.display = 'inline';
76                                 setFocus(select);
77                   }
78                   return false;
79                 };
80                 e.onkeyup = function(ev) {
81                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
82                         if(select && select.selectedIndex>=0) {
83                           var len = select.length;
84                           var byid = this.className=='combo';
85                           var ac = this.value.toUpperCase();
86                           select.options[select.selectedIndex].selected = false;
87                           for (i = 0; i < len; i++) {
88                                 var txt = byid ? select.options[i].value : select.options[i].text;
89                                 if (txt.toUpperCase().indexOf(ac) >= 0) {
90                                   select.options[i].selected = true;
91                                   break;
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';
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                                   box.value = byid ? opt.value : opt.text;
114                                   box.setAttribute('_last', box.value);
115                                 }
116                 }
117 }
118
119 function _set_combo_select(e) {
120                 // When combo position is changed via js (eg from searchbox)
121                 // no onchange event is generated. To ensure proper change 
122                 // signaling we must track selectedIndex in onblur handler.
123                 e.setAttribute('_last', e.selectedIndex);
124                 e.onblur = function() {
125                         if(this.className=='combo')
126                             _update_box(this);
127                         if (this.selectedIndex != this.getAttribute('_last'))
128                                 this.onchange();
129                 }
130                 e.onchange = function() {
131                         var s = this;
132                         this.setAttribute('_last', this.selectedIndex);                 
133                         if(s.className=='combo')
134                             _update_box(s);
135                         if(s.selectedIndex>=0) {
136                                  var sname = '_'+s.name+'_update';
137                                  var update = document.getElementsByName(sname)[0];
138                                  if(update) {
139                                             JsHttpRequest.request(update);
140                                 } 
141                         }
142                         return true;
143                 }
144                 e.onkeydown = function(event) {
145                     event = event||window.event;
146                     key = event.keyCode||event.which;
147                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
148                     if (box && key == 32 && this.className == 'combo2') {
149                             this.style.display = 'none';
150                             box.style.display = 'inline';
151                                 box.value='';
152                                 setFocus(box);
153                             return false;
154                          }
155                         if (this.getAttribute('aspect') == 'editable' && key==115) {
156                                 // F4: call related database editor - not available in non-js fallback mode
157                                 JsHttpRequest.request('_'+this.name+'_editor', this.form);
158                                 return false; // prevent default binding
159                                 // TODO: stopPropagation when needed
160                         }
161                 }
162 }               
163
164 /*
165  Behaviour definitions
166 */
167 var inserts = {
168         'input': function(e) {
169                 if(e.onfocus==undefined) {
170                         e.onfocus = function() {
171                             save_focus(this);
172                                 if (this.className == 'combo') 
173                                         this.select();
174                         };
175                 }
176                 if (e.className == 'combo' || e.className == 'combo2') {
177                                 _set_combo_input(e);
178                 } 
179                 else
180                 if(e.type == 'text' ) {
181                                 e.onkeydown = function(ev) { 
182                                         ev = ev||window.event;
183                                         key = ev.keyCode||ev.which;
184                                         if(key == 13) {
185                                                 if(e.className == 'searchbox') e.onblur();
186                                                 return false;
187                                         } 
188                                         return true;
189                                 }
190                         }
191         },
192         'input.combo2,input[aspect="fallback"]': 
193         function(e) {
194             // this hides search button for js enabled browsers
195             e.style.display = 'none';
196         },
197         'div.js_only': 
198         function(e) {
199             // this shows divs for js enabled browsers only
200             e.style.display = 'block';
201         },
202 //      '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
203         'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.navibutton': 
204         function(e) {
205             e.onclick = function() {
206                         if (this.getAttribute('aspect') == 'process')
207                                 progbar();
208                     save_focus(this);
209                         JsHttpRequest.request(this);
210                         return false;
211             }
212         },
213     '.amount': function(e) {
214                 if(e.onblur==undefined) {
215                   e.onblur = function() {
216                         var dec = this.getAttribute("dec");
217                         price_format(this.name, get_amount(this.name), dec);
218                   };
219                 }
220         },
221         '.searchbox': // emulated onchange event handling for text inputs
222                 function(e) {
223                         e.setAttribute('_last_val', e.value);
224                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
225                         e.onblur = function() {
226                                 var val = this.getAttribute('_last_val');
227                                 if (val != this.value) {
228                                         this.setAttribute('_last_val', this.value);
229                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
230                                 }
231                         }
232                 },
233         'select': function(e) {
234                 if(e.onfocus==undefined) {
235                         e.onfocus = function() {
236                             save_focus(this);
237                         };
238                   var c = e.className;
239                   if (c == 'combo' || c == 'combo2')
240                         _set_combo_select(e);
241                 }
242         },
243         'textarea,a': function(e) {
244                 if(e.onfocus==undefined) {
245                         e.onfocus = function() {
246                             save_focus(this);
247                         };
248                         e.onmouseover = function(e) {
249                         setFocus(this);
250                                 return false;
251                         }
252                 }
253         },
254         'a.printlink':  function(l) {
255                 l.onclick = function() {
256                     save_focus(this);
257                         JsHttpRequest.request(this);
258                         return false;
259                 }
260         },
261         'a': function(e) { // traverse menu
262                 e.onkeydown = function(ev) { 
263                         ev = ev||window.event;
264                         key = ev.keyCode||ev.which;
265                         if(key==37 || key==38 || key==39 || key==40) {
266                                         move_focus(key, e, document.links);
267                                         ev.returnValue = false;
268                                         return false;
269                         }
270                 }
271         },
272         'ul.ajaxtabs':  function(ul) {
273             var ulist=ul.getElementsByTagName("li");
274             for (var x=0; x<ulist.length; x++){ //loop through each LI e
275                 var ulistlink=ulist[x].getElementsByTagName("input")[0];
276                 if(ulistlink.onclick==undefined) {
277 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
278                     var url = ulistlink.form.action
279                     ulistlink.onclick=function(){
280                         _expand(this);
281                         return false;
282                     }
283                 }
284             }
285         },
286 /*      'tr.editrow': function(e) {
287                         e.onkeydown = function(ev) { 
288                         ev = ev||window.event;
289                         key = ev.keyCode||ev.which;
290                         if(key == 13) {
291                           // Find & click additem/update button
292                           
293                         } else  if(key == 27) {
294                           return false;
295                         }
296                 }
297
298         },
299 */      '#msgbox': function(e) {
300         // this is to avoid changing div height after ajax update in IE7
301           e.style.display = e.innerHTML.length ? 'block' : 'none';
302         }
303 /* TODO
304         'a.date_picker':  function(e) {
305             // this un-hides data picker for js enabled browsers
306             e.href = date_picker(this.getAttribute('rel'));
307             e.style.display = '';
308             e.tabindex = -1; // skip in tabbing order
309         }
310 */
311 };
312 function stopEv(ev) {
313                         ev.returnValue = false;
314                         ev.cancelBubble = true;
315                         if(ev.preventDefault) ev.preventDefault();
316                         return false;
317 }
318 /*
319         Modified accesskey system. While Alt key is pressed letter keys moves 
320         focus to next marked link. Alt key release activates focused link.
321 */
322 function setHotKeys() {
323         document.onkeydown = function(ev) {
324                 ev = ev||window.event;
325                 key = ev.keyCode||ev.which;
326                 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
327                         _hotkeys.alt = true;
328                         _hotkeys.focus = -1;
329                         return stopEv(ev);
330                 }
331                 else if (_hotkeys.alt && ((key>47 && key<58) || (key>64 && key<91))) {
332                         var n = _hotkeys.focus;
333                         var l = document.links;
334                         var cnt = l.length;
335                         key = String.fromCharCode(key);
336                         for (var i=0; i<cnt; i++) { 
337                                 n = (n+1)%cnt;
338                                 // check also if the link is visible
339                                 if (l[n].accessKey==key && l[n].scrollWidth) {
340                                         _hotkeys.focus = n;
341             // The timeout is needed to prevent unpredictable behaviour on IE.
342                                         var tmp = function() {document.links[_hotkeys.focus].focus();};
343                                         setTimeout(tmp, 0);
344                                         break;
345                                 }
346                         }
347                         return stopEv(ev);
348                 }
349                 if((ev.ctrlKey && key == 13) || key == 27) {
350                         _hotkeys.alt = false; // cancel link selection
351                         _hotkeys.focus = -1;
352                         ev.cancelBubble = true;
353                         if(ev.stopPropagation) ev.stopPropagation();
354                         // activate submit/escape form
355                         for(var j=0; j<this.forms.length; j++) {
356                                 var form = this.forms[j];
357                                 for (var i=0; i<form.elements.length; i++){
358                                         var el = form.elements[i];
359                                         var asp = el.getAttribute('aspect');
360                                         if ((asp=='default' && key==13)||(asp=='cancel' && key==27)) {
361                                                 JsHttpRequest.request(el);
362                                                 ev.returnValue = false;
363                                                 return false;
364                                         }
365                                 }
366                         }
367                         ev.returnValue = false;
368                         return false;
369                 } 
370                 return true;
371         };
372         document.onkeyup = function(ev) {
373                 if (_hotkeys.alt==true) {
374                         ev = ev||window.event;
375                         key = ev.keyCode||ev.which;
376
377                         if (key == 18) {
378                                 _hotkeys.alt = false;
379                                 if (_hotkeys.focus>=0) {
380                                         var link = document.links[_hotkeys.focus];
381                                         if (link.target=='_blank') {
382 //                                              window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
383                                                 openWindow(link.href,'_blank');
384                                         } else
385                                                 window.location = link.href;
386                                 }
387                         } 
388                         return stopEv(ev);
389                 }
390                 return true;
391         }
392 }
393
394 Behaviour.register(inserts);
395
396 Behaviour.addLoadEvent(setFocus);
397 Behaviour.addLoadEvent(setHotKeys);