]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/hold_pull_list_classic.js
Change each tab to 4 spaces in the staff client javascript files.
[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
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, 'barcode').appendChild(text(copy.barcode()));
101     $n(row, 'copy_location').appendChild(text(copy.location().name()));
102     $n(row, 'copy_number').appendChild(text(copy.copy_number()));
103     try {
104         if (copy.age_protect()) {
105             $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() )) + ' (' + copy.create_date().substr(0,10) + ')' ));    
106             unHideMe($n(row, 'age_protect_span'));
107         }
108     } catch(E) { alert(E); }
109
110     var vreq = new Request(FETCH_VOLUME, copy.call_number());
111     vreq.callback(
112         function(r) { pullListDrawVolume( tbody, row, hold, idx, r.getResultObject() ); } );
113     vreq.send();
114 }
115
116
117 function pullListDrawUser( tbody, row, hold, idx, user ) {
118     $n(row, 'patron').appendChild(text(user.card().barcode()));
119     if( (parseInt(idx) +1) == numHolds ) update_ready('patron');
120 }
121
122 var callNumbers = [];
123 function pullListDrawVolume( tbody, row, hold, idx, volume ) {
124     $n(row, 'call_number').appendChild(text(volume.label()));
125     callNumbers.push(volume.label());
126
127     if( (parseInt(idx) +1) == numHolds ) update_ready('call_number');
128 }
129
130
131 function ts_getInnerText(el) {
132     try {
133         if (el == null) { alert('null'); return ''; }
134         if (typeof el == "string") return el;
135         if (typeof el == "undefined") { return el };
136         if (el.innerText) return el.innerText;  //Not needed but it is faster
137         var str = "";
138     
139         var cs = el.childNodes;
140         var l = cs.length;
141         for (var i = 0; i < l; i++) {
142             switch (cs[i].nodeType) {
143                 case 1: //ELEMENT_NODE
144                     str += ts_getInnerText(cs[i]);
145                 break;
146                 case 3: //TEXT_NODE
147                     str += cs[i].nodeValue;
148                 break;
149             }
150         }
151         return str;
152     } catch(E) {
153         try { 
154             alert('el = ' + el + '\nel.nodeName = ' + el.nodeName + '  el.nodeType = ' + el.nodeType + '\nE = ' + E);
155         } catch(F) {
156             alert('el = ' + el + '\nF = ' + F + '\nE = ' + E);
157         }
158     }
159 }
160
161 function get_unhidden_span(node) {
162     var nl = node.childNodes;
163     var s = '';
164     for (var i = 0; i < nl.length; i++) {
165         if (nl[i].nodeName != 'span') continue;
166         if (nl[i].getAttribute('class') != 'hide_me') s += ts_getInnerText(nl[i]);
167     }
168     return s;
169 }
170
171 function $f(parent,name) {
172     var nl = parent.childNodes;
173     for (var i = 0; i < nl.length; i++) {
174         if (typeof nl[i].getAttribute != 'undefined' && nl[i].getAttribute('name') == name) {
175             return nl[i];
176         }
177     }
178 }
179
180 function update_ready(which_update) {
181     g[which_update] = true;
182     if (typeof g.title != 'undefined' && typeof g.patron != 'undefined' && typeof g.call_number != 'undefined') {
183         setTimeout( function() { update_ready_do_it(); }, 1000);
184     }
185 }
186
187 function update_ready_do_it() {
188     unHideMe($('pull_list_tbody')); hideMe($('inprogress'));
189     var rows = [];
190     var div = $('pull_list_tbody');
191     var div_children = div.childNodes;
192     for (var i = 0; i < div_children.length; i++) {
193         var pre = div_children[i];
194         if (pre.nodeName != 'pre') continue;
195         value = ( 
196             { 
197                 'call_number' : ts_getInnerText($f(pre,'call_number')), 
198                 'title' : ts_getInnerText($f(pre,'title')),
199                 'author' : ts_getInnerText($f(pre,'author')),
200                 'location' : ts_getInnerText($f(pre,'copy_location')),
201                 'copy_number' : ts_getInnerText($f(pre,'copy_number')),
202                 'item_type' : get_unhidden_span($f(pre,'item_type')),
203                 'node' : pre 
204             } 
205         );
206         rows.push( value );
207     }
208     rows = rows.sort( function(a,b) { 
209         function inner_sort(sort_type,a,b) {
210             switch(sort_type) {
211                 case 'number' :
212                     a = Number(a); b = Number(b);
213                 break;
214                 case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
215                     a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
216                     b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
217                 break;
218                 default:
219                     a = String( a ).toUpperCase();
220                     b = String( b ).toUpperCase();
221                 break;
222             }
223                 
224             if (a < b) return -1; 
225             if (a > b) return 1; 
226             return 0; 
227         }
228         var value = inner_sort('string',a.call_number,b.call_number);
229         if (value == 0) value = inner_sort('title',a.title,b.title);
230         if (value == 0) value = inner_sort('string',a.author,b.author);
231         if (value == 0) value = inner_sort('string',a.location,b.location);
232         if (value == 0) value = inner_sort('number',a.copy_number,b.copy_number);
233         if (value == 0) value = inner_sort('string',a.item_type,b.item_type);
234         return value;
235     } );
236     while(div.lastChild) div.removeChild( div.lastChild );
237     for (var i = 0; i < rows.length; i++) {
238         div.appendChild( rows[i].node );
239     }
240 }