new and improved classic. Pull lists and hold lists should probably include filters...
[Evergreen.git] / Open-ILS / xul / staff_client / server / admin / hold_pull_list_classic.js
1 var g = {};
2
3 var FETCH_HOLD_LIST                     = 'open-ils.circ:open-ils.circ.hold_pull_list.retrieve';
4 var FETCH_COPY                                  = 'open-ils.search:open-ils.search.asset.copy.fleshed.custom';
5 var FETCH_USER                                  = 'open-ils.actor:open-ils.actor.user.fleshed.retrieve';
6 var FETCH_VOLUME                                = 'open-ils.search:open-ils.search.callnumber.retrieve';
7
8 var myPerms                             = [ 'VIEW_HOLD' ];
9 var HOLD_LIST_LIMIT     = 50;
10 var numHolds                    = 0;
11
12 var listOffset                  = 0;
13
14 function pullListInit() {
15         fetchUser();
16         $('pl_user').appendChild(text(USER.usrname()));
17         $('pl_org').appendChild(text(findOrgUnit(USER.ws_ou()).name()));
18         setTimeout( function() { 
19                 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
20                 pullListFetchHolds();
21         }, 20 );
22 }
23
24 function pullListFetchHolds() {
25         var req = new Request(FETCH_HOLD_LIST, SESSION, HOLD_LIST_LIMIT, listOffset );
26         req.callback(pullListDrawHolds);
27         req.send();
28 }
29
30 var holdRowTemplate;
31 function pullListDrawHolds(r) {
32         var holds = r.getResultObject();
33
34         var tbody = $('pull_list_tbody');
35         if(!holdRowTemplate) 
36                 holdRowTemplate = tbody.removeChild($('pull_list_row'));
37         numHolds = holds.length;
38
39         for( var h in holds ) {
40                 var hold = holds[h];
41                 var row = holdRowTemplate.cloneNode(true);
42                 tbody.appendChild(row);
43                 pullListDrawHold( tbody, row, hold, h );
44         }
45
46 }
47
48 function pullListDrawHold( tbody, row, hold, idx ) {
49
50         $n(row, 'date').appendChild(text(hold.request_time().replace(/\ .*/, "")));
51
52         switch( hold.hold_type() ) {
53                 case 'C' : unHideMe($n(row, 'copy_hold')); break;
54                 case 'V' : unHideMe($n(row, 'volume_hold')); break;
55                 case 'T' : unHideMe($n(row, 'title_hold')); break;
56                 case 'M' : unHideMe($n(row, 'mr_hold')); break;
57         }
58         
59         var treq = new Request( FETCH_MODS_FROM_COPY, hold.current_copy() );
60         treq.callback(
61                 function(r) {
62                         pullListDrawTitle( tbody, row, hold, idx, r.getResultObject() );        });
63         treq.send();
64
65         var creq = new Request( FETCH_COPY, hold.current_copy(), ['location'] );
66         creq.callback(
67                 function(r) {
68                         pullListDrawCopy( tbody, row, hold, idx, r.getResultObject() ); });
69         creq.send();
70
71         var ureq = new Request( FETCH_USER, SESSION, hold.usr(), ['card'] );
72         ureq.callback(
73                 function(r) {
74                         pullListDrawUser( tbody, row, hold, idx, r.getResultObject() ); });
75         ureq.send();
76
77 }
78
79
80 function pullListDrawTitle( tbody, row, hold, idx, record ) {
81         $n(row, 'title').appendChild(text(record.title()));
82         $n(row, 'author').appendChild(text(record.author()));
83
84         var type = modsFormatToMARC(record.types_of_resource()[0]);
85         unHideMe($n(row, 'format_' + type));
86         if( (parseInt(idx) +1) == numHolds ) update_ready('title');
87 }
88
89
90 function pullListDrawCopy( tbody, row, hold, idx, copy ) {
91
92         $n(row, 'barcode').appendChild(text(copy.barcode()));
93         $n(row, 'copy_location').appendChild(text(copy.location().name()));
94         $n(row, 'copy_number').appendChild(text(copy.copy_number()));
95
96         var vreq = new Request(FETCH_VOLUME, copy.call_number());
97         vreq.callback(
98                 function(r) { pullListDrawVolume( tbody, row, hold, idx, r.getResultObject() ); } );
99         vreq.send();
100 }
101
102
103 function pullListDrawUser( tbody, row, hold, idx, user ) {
104         $n(row, 'patron').appendChild(text(user.card().barcode()));
105         if( (parseInt(idx) +1) == numHolds ) update_ready('patron');
106 }
107
108 var callNumbers = [];
109 function pullListDrawVolume( tbody, row, hold, idx, volume ) {
110         $n(row, 'call_number').appendChild(text(volume.label()));
111         callNumbers.push(volume.label());
112
113         if( (parseInt(idx) +1) == numHolds ) update_ready('call_number');
114 }
115
116
117 function ts_getInnerText(el) {
118         try {
119                 if (el == null) { alert('null'); return ''; }
120                 if (typeof el == "string") return el;
121                 if (typeof el == "undefined") { return el };
122                 if (el.innerText) return el.innerText;  //Not needed but it is faster
123                 var str = "";
124         
125                 var cs = el.childNodes;
126                 var l = cs.length;
127                 for (var i = 0; i < l; i++) {
128                         switch (cs[i].nodeType) {
129                                 case 1: //ELEMENT_NODE
130                         str += ts_getInnerText(cs[i]);
131                                 break;
132                                 case 3: //TEXT_NODE
133                                         str += cs[i].nodeValue;
134                                 break;
135                         }
136                 }
137                 return str;
138         } catch(E) {
139                 try { 
140                         alert('el = ' + el + '\nel.nodeName = ' + el.nodeName + '  el.nodeType = ' + el.nodeType + '\nE = ' + E);
141                 } catch(F) {
142                         alert('el = ' + el + '\nF = ' + F + '\nE = ' + E);
143                 }
144         }
145 }
146
147 function get_unhidden_span(node) {
148         var nl = node.childNodes;
149         var s = '';
150         for (var i = 0; i < nl.length; i++) {
151                 if (nl[i].nodeName != 'span') continue;
152                 if (nl[i].getAttribute('class') != 'hide_me') s += ts_getInnerText(nl[i]);
153         }
154         return s;
155 }
156
157 function $f(parent,name) {
158         var nl = parent.childNodes;
159         for (var i = 0; i < nl.length; i++) {
160                 if (typeof nl[i].getAttribute != 'undefined' && nl[i].getAttribute('name') == name) {
161                         return nl[i];
162                 }
163         }
164 }
165
166 function update_ready(which_update) {
167         g[which_update] = true;
168         if (typeof g.title != 'undefined' && typeof g.patron != 'undefined' && typeof g.call_number != 'undefined') {
169                 unHideMe($('pull_list_tbody')); hideMe($('inprogress'));
170                 var rows = [];
171                 var div = $('pull_list_tbody');
172                 var div_children = div.childNodes;
173                 for (var i = 0; i < div_children.length; i++) {
174                         var pre = div_children[i];
175                         if (pre.nodeName != 'pre') continue;
176                         value = ( 
177                                 { 
178                                         'call_number' : ts_getInnerText($f(pre,'call_number')), 
179                                         'title' : ts_getInnerText($f(pre,'title')),
180                                         'author' : ts_getInnerText($f(pre,'author')),
181                                         'location' : ts_getInnerText($f(pre,'copy_location')),
182                                         'copy_number' : ts_getInnerText($f(pre,'copy_number')),
183                                         'item_type' : get_unhidden_span($f(pre,'item_type')),
184                                         'node' : pre 
185                                 } 
186                         );
187                         rows.push( value );
188                 }
189                 rows = rows.sort( function(a,b) { 
190                         function inner_sort(sort_type,a,b) {
191                                 switch(sort_type) {
192                                         case 'number' :
193                                                 a = Number(a); b = Number(b);
194                                         break;
195                                         case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
196                                                 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
197                                                 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
198                                         break;
199                                         default:
200                                                 a = String( a ).toUpperCase();
201                                                 b = String( b ).toUpperCase();
202                                         break;
203                                 }
204                         
205                                 if (a < b) return -1; 
206                                 if (a > b) return 1; 
207                                 return 0; 
208                         }
209                         var value = inner_sort('string',a.call_number,b.call_number);
210                         if (value == 0) value = inner_sort('title',a.title,b.title);
211                         if (value == 0) value = inner_sort('string',a.author,b.author);
212                         if (value == 0) value = inner_sort('string',a.location,b.location);
213                         if (value == 0) value = inner_sort('number',a.copy_number,b.copy_number);
214                         if (value == 0) value = inner_sort('string',a.item_type,b.item_type);
215                         return value;
216                 } );
217                 while(div.lastChild) div.removeChild( div.lastChild );
218                 for (var i = 0; i < rows.length; i++) {
219                         div.appendChild( rows[i].node );
220                 }
221         }
222 }