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