]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/cat/marc_keys.js
changing legacy code.. ack :( put buckets and volume transfer support in copy browser
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / cat / marc_keys.js
1 function handle_keyup(ev) {
2         if (ev.target.tagName != 'textbox') { return; }
3         //mw.sdump('D_CAT','keyCode:' + ev.keyCode + ' charCode:' + ev.charCode + ' shift:' + ev.shiftKey + ' ctrl:' + ev.ctrlKey + ' meta:' + ev.metaKey + '\n');
4         resizeWrapper(ev.target);
5 }
6
7 function handle_keypress(ev) {
8         mw.sdump('D_CAT','keyCode:' + ev.keyCode + ' charCode:' + ev.charCode + ' shift:' + ev.shiftKey + ' ctrl:' + ev.ctrlKey + ' meta:' + ev.metaKey + '\n');
9         if (ev.target.tagName != 'textbox') { return; }
10         var rstatus = false;
11         if (ev.charCode) {
12                 switch(ev.charCode) {
13                         case 100: 
14                                 if (ev.ctrlKey) { /* control+d */
15                                         rstatus = handle_key_c_d(ev);
16                                 }
17                                 break;
18                 }
19         } else if (ev.keyCode) {
20                 switch(ev.keyCode) {
21                         case 13: /* enter */
22                         case 77: /* mac enter */
23                                 rstatus = handle_key_enter(ev);
24                                 break;
25                         case 46: 
26                                 if (ev.ctrlKey) { /* control+del */
27                                         rstatus = handle_key_c_del(ev);
28                                 }
29                                 break;
30                         case 9: 
31                                 if (ev.shiftKey) { /* shift+tab */
32                                         rstatus = navigate_col_left(ev.target,true);
33                                 } else { /* tab */
34                                         rstatus = navigate_col_right(ev.target,true);
35                                 }
36                                 break;
37                 }
38         }
39         resizeWrapper(ev.target);
40         if (rstatus) {
41                 ev.preventDefault();
42         }
43         return rstatus;
44 }
45
46 function handle_key_c_d(ev) {
47         if (ev.target.tagName != 'textbox') { return; }
48         var t = ev.target;
49         if ((ev.ctrlKey)&&(ev.charCode==100)&&(t.getAttribute('subfields')=='true')) {
50                 var n_ev = document.createEvent("KeyEvents");
51                 if (n_ev) { /* the best way... fake a keypress */
52                         n_ev.initKeyEvent("keypress", 1, 1, null, 0, 0, 0, 0, 0, 8225);
53                         t.inputField.dispatchEvent(n_ev);
54                 } else { /* this destroys the widget's undo buffer */
55                         var s_start = t.selectionStart; var s_end = t.selectionEnd;
56                         var first_half = t.value.substr(0,s_start);
57                         var second_half = t.value.substr(s_end);
58                         t.value = first_half + String.fromCharCode(8225) + second_half;
59                         t.setSelectionRange(s_start+1,s_start+1);
60                 }
61                 return true;
62         } else {
63                 return false;
64         }
65 }
66
67 function handle_key_c_del(ev) {
68         // rows (rows) -> row (r) -> wrapper (w) -> textbox (t)
69         if (ev.target.tagName != 'textbox') { return; }
70         var t = ev.target;
71         var r = t.parentNode.parentNode;
72         if ((ev.ctrlKey)&&(ev.keyCode==46)) {
73                 r.setAttribute('hidden','true');
74                 handle_tag_change(ev);
75                 if (! navigate_row_down(t,which_col_am_i(t),false) ) {
76                         navigate_row_up(t,which_col_am_i(t),false);
77                         mw.sdump('D_CAT',"let's go up\n");
78                 }
79                 return true;
80         } else {
81                 return false;
82         }
83 }
84
85 var new_row_id = -1;
86 function handle_key_enter(ev) {
87         // rows (rows) -> row (r) -> wrapper (w) -> textbox (t)
88         if (ev.target.tagName != 'textbox') { return; }
89         var t = ev.target; 
90         if ((ev.keyCode == 13) || (ev.keyCode == 77)) {
91                 if (ev.ctrlKey) { // add new row
92                         var new_r = build_data_row('data_' + new_row_id--);
93                         new_r.setAttribute('notempty','true');                  
94                         new_r.setAttribute('newnode','true');                   
95                         var w = t.parentNode;
96                         var r = w.parentNode;
97                         var rows = r.parentNode;
98                         var sibling_row;
99                         if (ev.shiftKey) {
100                                 sibling_row = r;
101                         } else {
102                                 sibling_row = r.nextSibling;
103                         }
104                         if (sibling_row) {
105                                 rows.insertBefore(new_r,sibling_row);
106                         } else {
107                                 rows.appendChild(new_r);
108                         }
109                         var c = new_r.childNodes;
110                         apply_event_listeners(c,'data');
111                         c[1].firstChild.value = ' '; // indicator 1
112                         c[2].firstChild.value = ' '; // indicator 2
113                         c[0].firstChild.focus();
114                         return true;
115                 } else { // move to next row
116                         navigate_row_down(t,which_col_am_i(t),true);
117                         return true;
118                 }
119         } else {
120                 return false;
121         }
122 }
123
124 function which_col_am_i(t) {
125         var r = t.parentNode.parentNode;
126         for (var i in r.childNodes) {
127                 if (t == r.childNodes[i].firstChild) {
128                         return i;
129                 }
130         }
131 }
132
133 function nextSibling_not_hidden(e) {
134         var s = e.nextSibling;
135         while ((s)&&(s.getAttribute('hidden')=='true')) {
136                 s = s.nextSibling;
137         }
138         return s;
139 }
140
141 function previousSibling_not_hidden(e) {
142         var s = e.previousSibling;
143         while ((s)&&(s.getAttribute('hidden')=='true')) {
144                 s = s.previousSibling;
145         }
146         return s;
147 }
148
149 function navigate_row_down(t,c,wrap) {
150         // rows -> row (r) -> wrapper -> textbox (t)
151         var r = t.parentNode.parentNode;
152         var sibling_row = nextSibling_not_hidden(r);
153         if (sibling_row) {
154                 sibling_row.childNodes[c].firstChild.focus();
155                 if (c == 0) {
156                         sibling_row.childNodes[c].firstChild.select();
157                 }
158         } else {
159                 if (wrap) {
160                         r.parentNode.firstChild.childNodes[c].firstChild.focus();
161                 } else {
162                         return false;
163                 }
164         }
165         return true;
166 }
167
168 function navigate_row_up(t,c,wrap) {
169         // rows -> row (r) -> wrapper -> textbox (t)
170         var r = t.parentNode.parentNode;
171         var sibling_row = previousSibling_not_hidden(r);
172         if (sibling_row) {
173                 sibling_row.childNodes[c].firstChild.focus();
174         } else {
175                 if (wrap) {
176                         r.parentNode.lastChild.childNodes[c].firstChild.focus();
177                 } else {
178                         return false;
179                 }
180         }
181         return true;
182 }
183
184 function navigate_col_left(t,wrap) {
185         // rows -> row -> wrapper (w) -> textbox (t)
186         var w = t.parentNode;
187         var sibling_wrapper = w.previousSibling;
188         if (sibling_wrapper) {
189                 sibling_wrapper.firstChild.focus();
190                 if (sibling_wrapper.parentNode.childNodes[3] != sibling_wrapper) {
191                         sibling_wrapper.firstChild.select();
192                 }
193         } else {
194                 if (wrap) {
195                         return navigate_row_up(t,3,false);
196                 } else {
197                         return false;
198                 }
199         }
200         return true;
201 }
202
203 function navigate_col_right(t,wrap) {
204         // rows -> row -> wrapper (w) -> textbox (t)
205         var w = t.parentNode;
206         var sibling_wrapper = w.nextSibling;
207         if (sibling_wrapper) {
208                 sibling_wrapper.firstChild.focus();
209                 if (sibling_wrapper.parentNode.childNodes[3] != sibling_wrapper) {
210                         sibling_wrapper.firstChild.select();
211                 }
212         } else {
213                 if (wrap) {
214                         return navigate_row_down(t,0);
215                 } else {
216                         return false;
217                 }
218         }
219         return true;
220 }
221