epic-s6ts
[kivitendo-erp.git] / js / common.js
1 function centerParms(width,height,extra) {
2   xPos = (screen.width - width) / 2;
3   yPos = (screen.height - height) / 2;
4
5   string = "left=" + xPos + ",top=" + yPos;
6
7   if (extra)
8     string += "width=" + width + ",height=" + height;
9
10   return string;
11 }
12
13 function get_input_value(input_name) {
14   var the_input = document.getElementsByName(input_name);
15   if (the_input && the_input[0])
16     return the_input[0].value;
17   return '';
18 }
19
20 function set_cursor_position(n) {
21   $('[name=' + n + ']').focus();
22 }
23
24 function focussable(e) {
25   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
26 }
27
28 function set_cursor_to_first_element(){
29   var df = document.forms;
30   for (var f = 0; f < df.length; f++)
31     for (var i = 0; i < df[f].length; i++)
32       if (focussable(df[f][i]))
33         try { df[f][i].focus(); return } catch (er) { }
34 }
35
36 function getElementByIndirectName(name){
37   var e = document.getElementsByName(name)[0];
38   if (e) return document.getElementsByName(e.value)[0];
39 }
40
41 function focus_by_name(name){
42   var f = getElementByIndirectName(name);
43   if (focussable(f)) {
44     set_cursor_position(f.name);
45     return true;
46   }
47   return false;
48 }
49
50 $(function () {
51   $('input').focus(function(){
52     if (focussable(this)) window.focused_element = this;
53   });
54
55   // setting focus inside a tabbed area fails if this is encountered before the tabbing is complete
56   // in that case the elements count as hidden and jquery aborts .focus()
57   setTimeout(function(){
58     // Lowest priority: first focussable element in form.
59     set_cursor_to_first_element();
60
61     // Medium priority: class set in template
62     var initial_focus = $(".initial_focus").filter(':visible')[0];
63     if (initial_focus)
64       $(initial_focus).focus();
65
66     // special: honour focus_position
67     // if no higher priority applies set focus to the appropriate element
68     if ($("#display_row")[0] && kivi.myconfig.focus_position) {
69       switch(kivi.myconfig.focus_position) {
70         case 'last_partnumber'  : $('#display_row tr.row:gt(-3):lt(-1) input[name*="partnumber"]').focus(); break;
71         case 'last_description' : $('#display_row tr.row:gt(-3):lt(-1) input[name*="description"]').focus(); break;
72         case 'last_qty'         : $('#display_row tr.row:gt(-3):lt(-1) input[name*="qty"]').focus(); break;
73         case 'new_partnumber'   : $('#display_row tr:gt(1) input[name*="partnumber"]').focus(); break;
74         case 'new_description'  : $('#display_row tr:gt(1) input[name*="description"]').focus(); break;
75         case 'new_qty'          : $('#display_row tr:gt(1) input[name*="qty"]').focus(); break;
76       }
77     }
78
79     // all of this screws with the native location.hash focus, so reimplement this as well
80     if (location.hash) {
81       var hash_name = location.hash.substr(1);
82       var $hash_by_id = $(location.hash + ':visible');
83       if ($hash_by_id.length > 0) {
84         $hash_by_id.get(0).focus();
85       } else {
86         var $by_name = $('[name=' + hash_name + ']:visible');
87         if ($by_name.length > 0) {
88           $by_name.get(0).focus();
89         }
90       }
91     }
92
93     // legacy. some forms install these
94     if (typeof fokus == 'function') { fokus(); return; }
95     if (focus_by_name('cursor_fokus')) return;
96   }, 0);
97 });
98
99 $('form').submit(function(){
100   if (window.focused_element)
101     document.forms[0].cursor_fokus.value = window.focused_element.name;
102 });