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
16 var _validate = {}; // validation functions
19 box = document.getElementById('msgbox')
20 box.innerHTML= box.innerHTML+'<br>'+msg
23 function save_focus(e) {
24 _focus = e.name||e.id;
25 var h = document.getElementById('hints');
27 h.style.display = e.title && e.title.length ? 'inline' : 'none';
28 h.innerHTML = e.title ? e.title : '';
32 function _expand(tabobj) {
34 var ul = tabobj.parentNode.parentNode;
35 var alltabs=ul.getElementsByTagName("input");
36 var frm = tabobj.form;
38 if (ul.getAttribute("rel")){
39 for (var i=0; i<alltabs.length; i++){
40 alltabs[i].className = "ajaxbutton" //deselect all tabs
42 tabobj.className = "current";
43 JsHttpRequest.request(tabobj)
47 //interface for selecting a tab (plus expand corresponding content)
48 function expandtab(tabcontentid, tabnumber) {
49 var tabs = document.getElementById(tabcontentid);
50 _expand(tabs.getElementsByTagName("input")[tabnumber]);
53 function _set_combo_input(e) {
54 e.setAttribute('_last', e.value);
56 var but_name = this.name.substring(0, this.name.length-4)+'button';
57 var button = document.getElementsByName(but_name)[0];
58 var select = document.getElementsByName(this.getAttribute('rel'))[0];
60 // submit request if there is submit_on_change option set and
61 // search field has changed.
62 if (button && (this.value != this.getAttribute('_last'))) {
63 JsHttpRequest.request(button);
64 } else if(this.className=='combo2') {
65 this.style.display = 'none';
66 select.style.display = 'inline';
67 setFocus(select.name);
71 e.onkeyup = function(ev) {
72 var select = document.getElementsByName(this.getAttribute('rel'))[0];
73 if(select && select.selectedIndex>=0) {
74 var len = select.length;
75 var byid = this.className=='combo';
76 var ac = this.value.toUpperCase();
77 select.options[select.selectedIndex].selected = false;
78 for (i = 0; i < len; i++) {
79 var txt = byid ? select.options[i].value : select.options[i].text;
80 if (txt.toUpperCase().indexOf(ac) >= 0) {
81 select.options[i].selected = true;
87 e.onkeydown = function(ev) {
88 ev = ev||window.event;
89 key = ev.keyCode||ev.which;
97 function _update_box(s) {
98 var byid = s.className=='combo';
99 var rel = s.getAttribute('rel');
100 var box = document.getElementsByName(rel)[0];
101 if(box && s.selectedIndex>=0) {
102 var opt = s.options[s.selectedIndex];
104 box.value = byid ? opt.value : opt.text;
105 box.setAttribute('_last', box.value);
110 function _set_combo_select(e) {
111 // When combo position is changed via js (eg from searchbox)
112 // no onchange event is generated. To ensure proper change
113 // signaling we must track selectedIndex in onblur handler.
114 e.setAttribute('_last', e.selectedIndex);
115 e.onblur = function() {
116 if(this.className=='combo')
118 if (this.selectedIndex != this.getAttribute('_last'))
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';
146 if (this.getAttribute('aspect') == 'editable' && key==115) {
147 // F4: call related database editor - not available in non-js fallback mode
148 JsHttpRequest.request('_'+this.name+'_editor', this.form);
149 return false; // prevent default binding
150 // TODO: stopPropagation when needed
156 Behaviour definitions
159 'form': function(e) {
160 e.onkeydown = function(ev) {
161 ev = ev||window.event;
162 key = ev.keyCode||ev.which;
163 if((ev.ctrlKey && key == 13) || key == 27) {
164 _hotkeys.alt = false;
165 ev.cancelBubble = true;
166 if(ev.stopPropagation) ev.stopPropagation();
167 // here ctrl-enter/escape support
168 ev.returnValue = false;
174 'input': function(e) {
175 if(e.onfocus==undefined) {
176 e.onfocus = function() {
178 if (this.className == 'combo')
182 if (e.className == 'combo' || e.className == 'combo2') {
186 if(e.type == 'text' ) {
187 e.onkeydown = function(ev) {
188 ev = ev||window.event;
189 key = ev.keyCode||ev.which;
191 if(e.className == 'searchbox') e.onblur();
198 'input.combo2,input[aspect="fallback"]':
200 // this hides search button for js enabled browsers
201 e.style.display = 'none';
205 // this shows divs for js enabled browsers only
206 e.style.display = 'block';
208 // '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
209 'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton':
211 e.onclick = function() {
213 if (e.getAttribute('aspect') == 'process')
214 JsHttpRequest.request(this, null, 30000);
216 JsHttpRequest.request(this);
220 'button': function(e) {
222 var func = eval("_validate."+e.name);
225 if (typeof old != 'function') {
228 e.onclick = function() {
230 { old(); return true;}
238 '.amount': function(e) {
239 if(e.onblur==undefined) {
240 e.onblur = function() {
241 var dec = this.getAttribute("dec");
242 price_format(this.name, get_amount(this.name), dec);
246 '.searchbox': // emulated onchange event handling for text inputs
248 e.setAttribute('_last_val', e.value);
249 e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
250 e.onblur = function() {
251 var val = this.getAttribute('_last_val');
252 if (val != this.value) {
253 this.setAttribute('_last_val', this.value);
254 JsHttpRequest.request('_'+this.name+'_changed', this.form);
257 /* e.onkeydown = function(ev) {
258 ev = ev||window.event;
259 key = ev.keyCode||ev.which;
260 if (key == 13 && (this.value != this.getAttribute('_last_val'))) {
266 'select': function(e) {
267 if(e.onfocus==undefined) {
268 e.onfocus = function() {
272 if (c == 'combo' || c == 'combo2')
273 _set_combo_select(e);
276 'textarea,a': function(e) {
277 if(e.onfocus==undefined) {
278 e.onfocus = function() {
283 'a.printlink': function(l) {
284 l.onclick = function() {
286 JsHttpRequest.request(this);
290 'ul.ajaxtabs': function(ul) {
291 var ulist=ul.getElementsByTagName("li");
292 for (var x=0; x<ulist.length; x++){ //loop through each LI e
293 var ulistlink=ulist[x].getElementsByTagName("input")[0];
294 if(ulistlink.onclick==undefined) {
295 // ? var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
296 var url = ulistlink.form.action
297 ulistlink.onclick=function(){
304 '#msgbox': function(e) {
305 // this is to avoid changing div height after ajax update in IE7
306 e.style.display = e.innerHTML.length ? 'block' : 'none';
309 'a.date_picker': function(e) {
310 // this un-hides data picker for js enabled browsers
311 e.href = date_picker(this.getAttribute('rel'));
312 e.style.display = '';
313 e.tabindex = -1; // skip in tabbing order
317 function stopEv(ev) {
318 ev.returnValue = false;
319 ev.cancelBubble = true;
320 if(ev.preventDefault) ev.preventDefault();
324 Modified accesskey system. While Alt key is pressed letter keys moves
325 focus to next marked link. Alt key release activates focused link.
327 function setHotKeys() {
328 document.onkeydown = function(ev) {
329 ev = ev||window.event;
330 key = ev.keyCode||ev.which;
332 if (key == 27 && ev.altKey) { // cancel selection
333 _hotkeys.alt = false;
338 if (ev.altKey && !ev.ctrlKey && ((key>47 && key<58) || (key>64 && key<91))) {
340 var n = _hotkeys.focus;
341 var l = document.links;
343 key = String.fromCharCode(key);
344 for (var i=0; i<cnt; i++) {
346 // check also if the link is visible
347 if (l[n].accessKey==key && l[n].scrollWidth) {
349 // The timeout is needed to prevent unpredictable behaviour on IE.
350 var tmp = function() {document.links[_hotkeys.focus].focus();};
359 document.onkeyup = function(ev) {
360 if (_hotkeys.alt==true) {
361 ev = ev||window.event;
362 key = ev.keyCode||ev.which;
365 _hotkeys.alt = false;
366 if (_hotkeys.focus>=0) {
367 var link = document.links[_hotkeys.focus];
371 if (link.target=='_blank') {
372 window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
373 openWindow(link.href,'_blank');
375 window.location = link.href;
384 Behaviour.register(inserts);
386 Behaviour.addLoadEvent(setFocus);
387 Behaviour.addLoadEvent(setHotKeys);