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 'focus': -1 // currently selected indeks of document.links
17 function save_focus(e) {
18 _focus = e.name||e.id;
19 var h = document.getElementById('hints');
21 h.style.display = e.title && e.title.length ? 'inline' : 'none';
22 h.innerHTML = e.title ? e.title : '';
26 function _expand(tabobj) {
28 var ul = tabobj.parentNode.parentNode;
29 var alltabs=ul.getElementsByTagName("input");
30 var frm = tabobj.form;
32 if (ul.getAttribute("rel")){
33 for (var i=0; i<alltabs.length; i++){
34 alltabs[i].className = "ajaxbutton" //deselect all tabs
36 tabobj.className = "current";
37 JsHttpRequest.request(tabobj)
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]);
47 function _set_combo_input(e) {
48 e.setAttribute('_last', e.value);
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];
54 // submit request if there is submit_on_change option set and
55 // search field has changed.
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';
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;
82 e.onkeydown = function(ev) {
83 ev = ev||window.event;
84 key = ev.keyCode||ev.which;
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];
100 box.value = byid ? opt.value : opt.text;
101 box.setAttribute('_last', box.value);
102 return old != box.value
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))
121 e.onchange = function() {
123 this.setAttribute('_last', this.selectedIndex);
124 if(s.className=='combo')
126 if(s.selectedIndex>=0) {
127 var sname = '_'+s.name+'_update';
128 var update = document.getElementsByName(sname)[0];
130 JsHttpRequest.request(update);
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';
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)
158 editors._call = key; // store call point for passBack
162 function passBack(value) {
165 var back = o.editors[o.editors._call]; // form input bindings
166 var to = o.document.getElementsByName(back[1])[0];
168 if (to[0] != undefined)
169 to[0].value = value; // ugly hack to set selector to any value
171 // update page after item selection
172 o.JsHttpRequest.request('_'+to.name+'_update', to.form);
180 Behaviour definitions
183 'input': function(e) {
184 if(e.onfocus==undefined) {
185 e.onfocus = function() {
187 if (this.className == 'combo')
191 if (e.className == 'combo' || e.className == 'combo2') {
195 if(e.type == 'text' ) {
196 e.onkeydown = function(ev) {
197 ev = ev||window.event;
198 key = ev.keyCode||ev.which;
200 if(e.className == 'searchbox') e.onblur();
207 'input.combo2,input[aspect="fallback"]':
209 // this hides search button for js enabled browsers
210 e.style.display = 'none';
214 // this shows divs for js enabled browsers only
215 e.style.display = 'block';
217 // '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
218 'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton':
220 e.onclick = function() {
222 if (e.getAttribute('aspect') == 'process')
223 JsHttpRequest.request(this, null, 60000);
225 JsHttpRequest.request(this);
229 'button': function(e) {
231 var func = _validate[e.name];
234 if (typeof old != 'function' || old == func) { // prevent multiply binding on ajax update
237 e.onclick = function() {
239 { old(); return true;}
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);
255 '.searchbox': // emulated onchange event handling for text inputs
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);
267 'button[aspect=selector], button[aspect=abort], input[aspect=selector]': function(e) {
268 e.onclick = function() {
269 passBack(this.getAttribute('rel'));
273 'button[aspect=popup]': function(e) {
275 e.onclick = function() {
276 // this.form.target = '_blank';
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)
284 // editors._call = key; // store call point for passBack
285 // _w.moveTo(50, 50);
290 'select': function(e) {
291 if(e.onfocus==undefined) {
292 e.onfocus = function() {
296 if (c == 'combo' || c == 'combo2')
297 _set_combo_select(e);
300 'a.printlink': function(l) {
301 l.onclick = function() {
303 JsHttpRequest.request(this);
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;
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(){
332 /* 'tr.editrow': function(e) {
333 e.onkeydown = function(ev) {
334 ev = ev||window.event;
335 key = ev.keyCode||ev.which;
337 // Find & click additem/update button
339 } else if(key == 27) {
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';
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
359 function stopEv(ev) {
360 if(ev.preventDefault) {
362 ev.stopPropagation();
364 ev.returnValue = false;
365 ev.cancelBubble = true;
371 Modified accesskey system. While Alt key is pressed letter keys moves
372 focus to next marked link. Alt key release activates focused link.
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
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;
387 key = String.fromCharCode(key);
388 for (var i=0; i<cnt; i++) {
390 // check also if the link is visible
391 if (l[n].accessKey==key && l[n].scrollWidth) {
393 // The timeout is needed to prevent unpredictable behaviour on IE.
394 var tmp = function() {document.links[_hotkeys.focus].focus();};
401 if((ev.ctrlKey && key == 13) || (key == 27 && ev.altKey)) {
402 _hotkeys.alt = false; // cancel link selection
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;
417 if ((asp=='default' && key==13)||(asp=='cancel' && key==27)) {
418 JsHttpRequest.request(el);
419 ev.returnValue = false;
424 ev.returnValue = false;
427 if (editors && editors[key]) {
429 return stopEv(ev); // prevent default binding
433 document.onkeyup = function(ev) {
434 ev = ev||window.event;
435 key = ev.keyCode||ev.which;
437 if (_hotkeys.alt==true) {
439 _hotkeys.alt = false;
440 if (_hotkeys.focus>=0) {
441 var link = document.links[_hotkeys.focus];
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');
449 window.location = link.href;
458 Behaviour.register(inserts);
460 Behaviour.addLoadEvent(setFocus);
461 Behaviour.addLoadEvent(setHotKeys);