]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/hold_pull_list_classic.js
show Age Protection in classic style hold pull list
[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         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
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
58         switch( hold.hold_type() ) {
59                 case 'C' : unHideMe($n(row, 'copy_hold')); break;
60                 case 'V' : unHideMe($n(row, 'volume_hold')); break;
61                 case 'T' : unHideMe($n(row, 'title_hold')); break;
62                 case 'M' : unHideMe($n(row, 'mr_hold')); break;
63         }
64         
65         var treq = new Request( FETCH_MODS_FROM_COPY, hold.current_copy() );
66         treq.callback(
67                 function(r) {
68                         pullListDrawTitle( tbody, row, hold, idx, r.getResultObject() );        });
69         treq.send();
70
71         var creq = new Request( FETCH_COPY, hold.current_copy(), ['location'] );
72         creq.callback(
73                 function(r) {
74                         pullListDrawCopy( tbody, row, hold, idx, r.getResultObject() ); });
75         creq.send();
76
77         var ureq = new Request( FETCH_USER, SESSION, hold.usr(), ['card'] );
78         ureq.callback(
79                 function(r) {
80                         pullListDrawUser( tbody, row, hold, idx, r.getResultObject() ); });
81         ureq.send();
82
83 }
84
85
86 function pullListDrawTitle( tbody, row, hold, idx, record ) {
87         $n(row, 'title').appendChild(text(record.title()));
88         $n(row, 'author').appendChild(text(record.author()));
89
90         var type = modsFormatToMARC(record.types_of_resource()[0]);
91         unHideMe($n(row, 'format_' + type));
92         if( (parseInt(idx) +1) == numHolds ) update_ready('title');
93 }
94
95
96 function pullListDrawCopy( tbody, row, hold, idx, copy ) {
97
98         $n(row, 'barcode').appendChild(text(copy.barcode()));
99         $n(row, 'copy_location').appendChild(text(copy.location().name()));
100         $n(row, 'copy_number').appendChild(text(copy.copy_number()));
101         try {
102                 if (copy.age_protect()) {
103                         $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() )) ));  
104                         unHideMe($n(row, 'age_protect_span'));
105                 }
106         } catch(E) { alert(E); }
107
108         var vreq = new Request(FETCH_VOLUME, copy.call_number());
109         vreq.callback(
110                 function(r) { pullListDrawVolume( tbody, row, hold, idx, r.getResultObject() ); } );
111         vreq.send();
112 }
113
114
115 function pullListDrawUser( tbody, row, hold, idx, user ) {
116         $n(row, 'patron').appendChild(text(user.card().barcode()));
117         if( (parseInt(idx) +1) == numHolds ) update_ready('patron');
118 }
119
120 var callNumbers = [];
121 function pullListDrawVolume( tbody, row, hold, idx, volume ) {
122         $n(row, 'call_number').appendChild(text(volume.label()));
123         callNumbers.push(volume.label());
124
125         if( (parseInt(idx) +1) == numHolds ) update_ready('call_number');
126 }
127
128
129 function ts_getInnerText(el) {
130         try {
131                 if (el == null) { alert('null'); return ''; }
132                 if (typeof el == "string") return el;
133                 if (typeof el == "undefined") { return el };
134                 if (el.innerText) return el.innerText;  //Not needed but it is faster
135                 var str = "";
136         
137                 var cs = el.childNodes;
138                 var l = cs.length;
139                 for (var i = 0; i < l; i++) {
140                         switch (cs[i].nodeType) {
141                                 case 1: //ELEMENT_NODE
142                         str += ts_getInnerText(cs[i]);
143                                 break;
144                                 case 3: //TEXT_NODE
145                                         str += cs[i].nodeValue;
146                                 break;
147                         }
148                 }
149                 return str;
150         } catch(E) {
151                 try { 
152                         alert('el = ' + el + '\nel.nodeName = ' + el.nodeName + '  el.nodeType = ' + el.nodeType + '\nE = ' + E);
153                 } catch(F) {
154                         alert('el = ' + el + '\nF = ' + F + '\nE = ' + E);
155                 }
156         }
157 }
158
159 function get_unhidden_span(node) {
160         var nl = node.childNodes;
161         var s = '';
162         for (var i = 0; i < nl.length; i++) {
163                 if (nl[i].nodeName != 'span') continue;
164                 if (nl[i].getAttribute('class') != 'hide_me') s += ts_getInnerText(nl[i]);
165         }
166         return s;
167 }
168
169 function $f(parent,name) {
170         var nl = parent.childNodes;
171         for (var i = 0; i < nl.length; i++) {
172                 if (typeof nl[i].getAttribute != 'undefined' && nl[i].getAttribute('name') == name) {
173                         return nl[i];
174                 }
175         }
176 }
177
178 function update_ready(which_update) {
179         g[which_update] = true;
180         if (typeof g.title != 'undefined' && typeof g.patron != 'undefined' && typeof g.call_number != 'undefined') {
181                 setTimeout( function() { update_ready_do_it(); }, 1000);
182         }
183 }
184
185 function update_ready_do_it() {
186         unHideMe($('pull_list_tbody')); hideMe($('inprogress'));
187         var rows = [];
188         var div = $('pull_list_tbody');
189         var div_children = div.childNodes;
190         for (var i = 0; i < div_children.length; i++) {
191                 var pre = div_children[i];
192                 if (pre.nodeName != 'pre') continue;
193                 value = ( 
194                         { 
195                                 'call_number' : ts_getInnerText($f(pre,'call_number')), 
196                                 'title' : ts_getInnerText($f(pre,'title')),
197                                 'author' : ts_getInnerText($f(pre,'author')),
198                                 'location' : ts_getInnerText($f(pre,'copy_location')),
199                                 'copy_number' : ts_getInnerText($f(pre,'copy_number')),
200                                 'item_type' : get_unhidden_span($f(pre,'item_type')),
201                                 'node' : pre 
202                         } 
203                 );
204                 rows.push( value );
205         }
206         rows = rows.sort( function(a,b) { 
207                 function inner_sort(sort_type,a,b) {
208                         switch(sort_type) {
209                                 case 'number' :
210                                         a = Number(a); b = Number(b);
211                                 break;
212                                 case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
213                                         a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
214                                         b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
215                                 break;
216                                 default:
217                                         a = String( a ).toUpperCase();
218                                         b = String( b ).toUpperCase();
219                                 break;
220                         }
221                                 
222                         if (a < b) return -1; 
223                         if (a > b) return 1; 
224                         return 0; 
225                 }
226                 var value = inner_sort('string',a.call_number,b.call_number);
227                 if (value == 0) value = inner_sort('title',a.title,b.title);
228                 if (value == 0) value = inner_sort('string',a.author,b.author);
229                 if (value == 0) value = inner_sort('string',a.location,b.location);
230                 if (value == 0) value = inner_sort('number',a.copy_number,b.copy_number);
231                 if (value == 0) value = inner_sort('string',a.item_type,b.item_type);
232                 return value;
233         } );
234         while(div.lastChild) div.removeChild( div.lastChild );
235         for (var i = 0; i < rows.length; i++) {
236                 div.appendChild( rows[i].node );
237         }
238 }