Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / js / inserts.js
1 var _focus;
2
3 function debug(msg) {
4     box = document.getElementById('msgbox')
5         box.innerHTML= box.innerHTML+'<br>'+msg
6 }
7
8 function progbar() {
9         box = document.getElementById('msgbox');
10     box.innerHTML= "<center><table width='98%' border='1' cellpadding=3 "
11         +"bordercolor='#007700' style='border-collapse: collapse'>"
12         +"<tr><td align='center' bgcolor='#ccffcc' >"
13                 +"<img src='"+user.theme+"images/progressbar.gif' alt='"
14                 +user.loadtxt+"' /></td></tr></table></center><br>";
15         box.style.display = 'block';
16 }
17
18 function save_focus(e) {
19   _focus = e.name||e.id;
20   var h = document.getElementById('hints');
21   if (h) {
22         h.style.display = e.title && e.title.length ? 'inline' : 'none';
23         h.innerHTML = e.title ? e.title : '';
24   }
25 }
26
27 function _expand(tabobj) {
28
29   var ul = tabobj.parentNode.parentNode;
30   var alltabs=ul.getElementsByTagName("input");
31   var frm = tabobj.form;
32
33   if (ul.getAttribute("rel")){
34         for (var i=0; i<alltabs.length; i++){
35           alltabs[i].className = "ajaxbutton"  //deselect all tabs
36         }
37         tabobj.className = "current";
38         JsHttpRequest.request(tabobj)
39   }
40 }
41
42 //interface for selecting a tab (plus expand corresponding content)
43 function expandtab(tabcontentid, tabnumber) {
44   var tabs = document.getElementById(tabcontentid);
45  _expand(tabs.getElementsByTagName("input")[tabnumber]);
46 }
47
48 function _set_combo_input(e) {
49                 e.setAttribute('_last', e.value);
50                 e.onblur=function() { 
51                   var but_name = this.name.substring(0, this.name.length-4)+'button';
52                   var button = document.getElementsByName(but_name)[0];
53                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
54                   save_focus(select);
55 // submit request if there is submit_on_change option set and 
56 // 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';
62                                 setFocus(select.name);
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                                   box.value = byid ? opt.value : opt.text;
100                                   box.setAttribute('_last', box.value);
101                                 }
102                 }
103 }
104
105 function _set_combo_select(e) {
106                 // When combo position is changed via js (eg from searchbox)
107                 // no onchange event is generated. To ensure proper change 
108                 // signaling we must track selectedIndex in onblur handler.
109                 e.setAttribute('_last', e.selectedIndex);
110                 e.onblur = function() {
111                         if(this.className=='combo')
112                             _update_box(this);
113                         if (this.selectedIndex != this.getAttribute('_last'))
114                                 this.onchange();
115                 }
116                 e.onchange = function() {
117                         var s = this;
118                         this.setAttribute('_last', this.selectedIndex);                 
119                         if(s.className=='combo')
120                             _update_box(s);
121                         if(s.selectedIndex>=0) {
122                                  var sname = '_'+s.name+'_update';
123                                  var update = document.getElementsByName(sname)[0];
124                                  if(update) {
125                                             JsHttpRequest.request(update);
126                                 } 
127                         }
128                         return true;
129                 }
130                 e.onkeydown = function(event) {
131                     event = event||window.event;
132                     key = event.keyCode||event.which;
133                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
134                     if (box && key == 32 && this.className == 'combo2') {
135                             this.style.display = 'none';
136                             box.style.display = 'inline';
137                                 box.value='';
138                                 setFocus(box.name);
139                             return false;
140                          }
141                         if (this.getAttribute('aspect') == 'editable' && key==115) {
142                                 // F4: call related database editor - not available in non-js fallback mode
143                                 JsHttpRequest.request('_'+this.name+'_editor', this.form);
144                                 return false; // prevent default binding
145                                 // TODO: preventDefault, stopPropagation when needed
146                         }
147                 }
148 }               
149
150 /*
151  Behaviour definitions
152 */
153 var inserts = {
154         'input': function(e) {
155                 if(e.onfocus==undefined) {
156                         e.onfocus = function() {
157                             save_focus(this);
158                                 if (this.className == 'combo') 
159                                         this.select();
160                         };
161                 }
162                 if (e.className == 'combo' || e.className == 'combo2') {
163                                 _set_combo_input(e);
164                 } 
165                 else
166                 if(e.type == 'text' ) {
167                                 e.onkeydown = function(ev) { 
168                                         ev = ev||window.event;
169                                         key = ev.keyCode||ev.which;
170                                         if(key == 13) {
171                                                 if(e.className == 'searchbox') e.onblur();
172                                                 return false;
173                                         } 
174                                         return true;
175                                 }
176                         }
177         },
178         'input.combo2,input[aspect="fallback"]': 
179         function(e) {
180             // this hides search button for js enabled browsers
181             e.style.display = 'none';
182         },
183         'div.js_only': 
184         function(e) {
185             // this shows divs for js enabled browsers only
186             e.style.display = 'block';
187         },
188         'input.ajaxsubmit,input.editbutton,input.navibutton': 
189         function(e) {
190             e.onclick = function() {
191                         if (this.getAttribute('aspect') == 'process')
192                                 progbar();
193                         JsHttpRequest.request(this);
194                         return false;
195             }
196         },
197     '.amount': function(e) {
198                 if(e.onblur==undefined) {
199                   e.onblur = function() {
200                         var dec = this.getAttribute("dec");
201                         price_format(this.name, get_amount(this.name), dec);
202                   };
203                 }
204         },
205         '.searchbox': // emulated onchange event handling for text inputs
206                 function(e) {
207                         e.setAttribute('_last_val', e.value);
208                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
209                         e.onblur = function() {
210                                 var val = this.getAttribute('_last_val');
211                                 if (val != this.value) {
212                                         this.setAttribute('_last_val', this.value);
213                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
214                                 }
215                         }
216 /*              e.onkeydown = function(ev) { 
217                                 ev = ev||window.event;
218                                 key = ev.keyCode||ev.which;
219                                 if (key == 13 && (this.value != this.getAttribute('_last_val'))) {
220                                         this.blur();
221                                         return false;
222                                 }
223                         }
224 */              },
225         'select': function(e) {
226                 if(e.onfocus==undefined) {
227                         e.onfocus = function() {
228                             save_focus(this);
229                         };
230                   var c = e.className;
231                   if (c == 'combo' || c == 'combo2')
232                         _set_combo_select(e);
233                 }
234         },
235         'textarea,a': function(e) {
236                 if(e.onfocus==undefined) {
237                         e.onfocus = function() {
238                             save_focus(this);
239                         };
240                 }
241         },
242         'ul.ajaxtabs':  function(ul) {
243             var ulist=ul.getElementsByTagName("li");
244             for (var x=0; x<ulist.length; x++){ //loop through each LI e
245                 var ulistlink=ulist[x].getElementsByTagName("input")[0];
246                 if(ulistlink.onclick==undefined) {
247 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
248                     var url = ulistlink.form.action
249                     ulistlink.onclick=function(){
250                         _expand(this);
251                         return false;
252                     }
253                 }
254             }
255         },
256         '#msgbox': function(e) {
257         // this is to avoid changing div height after ajax update in IE7
258           e.style.display = e.innerHTML.length ? 'block' : 'none';
259         }
260 /* TODO
261         'a.date_picker':  function(e) {
262             // this un-hides data picker for js enabled browsers
263             e.href = date_picker(this.getAttribute('rel'));
264             e.style.display = '';
265             e.tabindex = -1; // skip in tabbing order
266         }
267 */
268 };
269
270 Behaviour.register(inserts);
271
272 Behaviour.addLoadEvent(setFocus);