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 ***********************************************************************/
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
18 function validate(e) {
19 if (e.name && (typeof _validate[e.name] == 'function'))
20 return _validate[e.name](e);
24 function save_focus(e) {
25 _focus = e.name||e.id;
26 var h = document.getElementById('hints');
28 h.style.display = e.title && e.title.length ? 'inline' : 'none';
29 h.innerHTML = e.title ? e.title : '';
33 function _expand(tabobj) {
35 var ul = tabobj.parentNode.parentNode;
36 var alltabs=ul.getElementsByTagName("button");
37 var frm = tabobj.form;
39 if (ul.getAttribute("rel")){
40 for (var i=0; i<alltabs.length; i++){
41 alltabs[i].className = "ajaxbutton" //deselect all tabs
43 tabobj.className = "current";
44 JsHttpRequest.request(tabobj)
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]);
54 function _set_combo_input(e) {
55 e.setAttribute('_last', e.value);
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];
61 // submit request if there is submit_on_change option set and
62 // search field has changed.
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';
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;
88 if(txt.toUpperCase().indexOf(ac) >= 0) {
89 select.options[i].selected = true;
96 e.onkeydown = function(ev) {
97 ev = ev||window.event;
98 key = ev.keyCode||ev.which;
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];
114 box.value = byid ? opt.value : opt.text;
115 box.setAttribute('_last', box.value);
116 return old != box.value
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))
135 e.onchange = function() {
137 this.setAttribute('_last', this.selectedIndex);
138 if(s.className=='combo' || this.className=='combo3')
140 if(s.selectedIndex>=0) {
141 var sname = '_'+s.name+'_update';
142 var update = document.getElementsByName(sname)[0];
144 JsHttpRequest.request(update);
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';
160 if (key == 13 && !e.length) // prevent chrome issue (blocked cursor after CR on empty selector)
168 function callEditor(key) {
169 var el = document.getElementsByName(editors[key][1])[0];
170 if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
171 var left = (screen.width - editors[key][2]) / 2;
172 var top = (screen.height - editors[key][3]) / 2;
173 _w = open(editors[key][0]+el.value+'&popup=1',
174 "edit","scrollbars=yes,resizable=0,width="+editors[key][2]+",height="+editors[key][3]+",left="+left+",top="+top+",screenX="+left+",screenY="+top);
175 if (_w.opener == null)
177 editors._call = key; // store call point for passBack
181 function passBack(value) {
184 var back = o.editors[o.editors._call]; // form input bindings
185 var to = o.document.getElementsByName(back[1])[0];
187 if (to[0] != undefined)
188 to[0].value = value; // ugly hack to set selector to any value
190 // update page after item selection
191 o.JsHttpRequest.request('_'+to.name+'_update', to.form);
199 Behaviour definitions
202 'input': function(e) {
203 if(e.onfocus==undefined) {
204 e.onfocus = function() {
206 if (this.className == 'combo' || this.className == 'combo3')
210 if (e.className == 'combo' || e.className == 'combo2' || e.className == 'combo3') {
214 if(e.type == 'text' ) {
215 e.onkeydown = function(ev) {
216 ev = ev||window.event;
217 key = ev.keyCode||ev.which;
219 if(e.className == 'searchbox') e.onblur();
226 'input.combo2,input[aspect="fallback"]':
228 // this hides search button for js enabled browsers
229 e.style.display = 'none';
233 // this shows divs for js enabled browsers only
234 e.style.display = 'block';
237 'button': function(e) {
238 e.onclick = function(){ return validate(e); }
240 // '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
241 'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton':
243 e.onclick = function() {
246 var asp = e.getAttribute('aspect')
247 if (asp && asp.indexOf('process') !== -1)
248 JsHttpRequest.request(this, null, 600000); // ten minutes for backup
250 JsHttpRequest.request(this);
255 '.amount': function(e) {
256 if(e.onblur==undefined) {
257 e.onblur = function() {
258 var dec = this.getAttribute("dec");
259 price_format(this.name, get_amount(this.name), dec);
263 '.searchbox': // emulated onchange event handling for text inputs
265 e.setAttribute('_last_val', e.value);
266 e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
267 e.onblur = function() {
268 var val = this.getAttribute('_last_val');
269 if (val != this.value) {
270 this.setAttribute('_last_val', this.value);
271 JsHttpRequest.request('_'+this.name+'_changed', this.form);
275 'button[aspect*selector], button[aspect*abort], input[aspect*selector]': function(e) {
276 e.onclick = function() {
277 passBack(this.getAttribute('rel'));
281 'button[aspect=popup]': function(e) {
282 e.onclick = function() {
283 if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
284 var left = (screen.width - 800)/2;
285 var top = (screen.height - 600)/2;
286 _w = open(document.location+'popup=1',
287 "edit","Scrollbars=0,resizable=0,width=800,height=600, top="+top+",left="+left+",screenX="+left+",screenY="+top);
288 if (_w.opener == null)
290 // editors._call = key; // store call point for passBack
291 // _w.moveTo(50, 50);
296 'select': function(e) {
297 if(e.onfocus==undefined) {
298 e.onfocus = function() {
302 if (c == 'combo' || c == 'combo2' || c == 'combo3')
303 _set_combo_select(e);
306 'a.printlink': function(l) {
307 l.onclick = function() {
309 JsHttpRequest.request(this, null, 60000);
313 'a.repopts_link': function(l) {
314 l.onclick = function() {
316 var replinks = document.getElementsBySelector('a.repopts_link');
317 for(var i in replinks)
318 replinks[i].style.fontWeight = replinks[i]==this ? 'bold' : 'normal';
319 JsHttpRequest.request(this, null);
323 'a': function(e) { // traverse menu
324 e.onkeydown = function(ev) {
325 ev = ev||window.event;
326 key = ev.keyCode||ev.which;
327 if(key==37 || key==38 || key==39 || key==40) {
328 move_focus(key, e, document.links);
329 ev.returnValue = false;
333 // prevent unneeded transaction entry abortion
334 if (e.className == 'shortcut'
335 || e.className == 'menu_option'
336 || e.className == 'menu_tab'
337 || e.className == 'selected')
338 e.onclick = function(ev) {
339 if (_validate._processing
340 && _validate._modified
341 && !confirm(_validate._processing)) {
342 ev.returnValue = false;
345 window.location = e.href;
348 'ul.ajaxtabs': function(ul) {
349 var ulist=ul.getElementsByTagName("li");
350 for (var x=0; x<ulist.length; x++){ //loop through each LI e
351 var tab=ulist[x].getElementsByTagName("button")[0];
352 // if(tab.onclick==undefined) {
353 // ? var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
354 var url = tab.form.action
355 tab.onclick=function(){
363 /* 'tr.editrow': function(e) {
364 e.onkeydown = function(ev) {
365 ev = ev||window.event;
366 key = ev.keyCode||ev.which;
368 // Find & click additem/update button
370 } else if(key == 27) {
376 *//* '#msgbox': function(e) {
377 // this is to avoid changing div height after ajax update in IE7
378 e.style.display = e.innerHTML.length ? 'block' : 'none';
381 'a.date_picker': function(e) {
382 // this un-hides data picker for js enabled browsers
383 e.href = date_picker(this.getAttribute('rel'));
384 e.style.display = '';
385 e.tabindex = -1; // skip in tabbing order
390 function stopEv(ev) {
391 if(ev.preventDefault) {
393 ev.stopPropagation();
395 ev.returnValue = false;
396 ev.cancelBubble = true;
402 Modified accesskey system. While Alt key is pressed letter keys moves
403 focus to next marked link. Alt key release activates focused link.
405 function setHotKeys() {
406 document.onkeydown = function(ev) {
407 ev = ev||window.event;
408 key = ev.keyCode||ev.which;
409 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
414 else if (ev.altKey && !ev.ctrlKey && ((key>47 && key<58) || (key>64 && key<91))) {
415 key = String.fromCharCode(key);
416 var n = _hotkeys.focus;
417 var l = document.getElementsBySelector('[accesskey='+key+']');
420 for (var i=0; i<cnt; i++) {
422 // check also if the link is visible
423 if (l[n].accessKey==key && l[n].scrollWidth) {
425 // The timeout is needed to prevent unpredictable behaviour on IE.
426 var tmp = function() {l[_hotkeys.focus].focus();};
433 if((ev.ctrlKey && key == 13) || key == 27) {
434 _hotkeys.alt = false; // cancel link selection
436 ev.cancelBubble = true;
437 if(ev.stopPropagation) ev.stopPropagation();
438 // activate submit/escape form
439 for(var j=0; j<this.forms.length; j++) {
440 var form = this.forms[j];
441 for (var i=0; i<form.elements.length; i++){
442 var el = form.elements[i];
443 var asp = el.getAttribute('aspect');
446 if (el.className!='editbutton' && (asp && asp.indexOf('selector') !== -1) && (key==13 || key==27)) {
447 passBack(key==13 ? el.getAttribute('rel') : false);
448 ev.returnValue = false;
451 if (((asp && asp.indexOf('default') !== -1) && key==13)||((asp && asp.indexOf('cancel') !== -1) && key==27)) {
453 if (asp.indexOf('process') !== -1)
454 JsHttpRequest.request(el, null, 600000);
456 JsHttpRequest.request(el);
458 ev.returnValue = false;
463 ev.returnValue = false;
466 if (editors && editors[key]) {
468 return stopEv(ev); // prevent default binding
472 document.onkeyup = function(ev) {
473 ev = ev||window.event;
474 key = ev.keyCode||ev.which;
476 if (_hotkeys.alt==true) {
478 _hotkeys.alt = false;
479 if (_hotkeys.focus >= 0) {
480 var link = _hotkeys.list[_hotkeys.focus];
484 if (link.target=='_blank') {
485 window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
486 openWindow(link.href,'_blank');
488 window.location = link.href;
497 Behaviour.register(inserts);
499 Behaviour.addLoadEvent(setFocus);
500 Behaviour.addLoadEvent(setHotKeys);