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