1 dump('entering util.list.js\n');
3 if (typeof main == 'undefined') main = {};
4 util.list = function (id) {
6 this.node = document.getElementById(id);
8 this.row_count = { 'total' : 0, 'fleshed' : 0 };
10 this.unique_row_counter = 0;
12 if (!this.node) throw('Could not find element ' + id);
13 switch(this.node.nodeName) {
17 throw(this.node.nodeName + ' not yet supported'); break;
18 default: throw(this.node.nodeName + ' not supported'); break;
21 JSAN.use('util.error'); this.error = new util.error();
23 JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.stash_retrieve();
28 util.list.prototype = {
30 'init' : function (params) {
33 obj.scratch_data = {};
35 // If set, save and restore columns as if the tree/list id was the value of columns_saved_under
36 obj.columns_saved_under = params.columns_saved_under;
38 JSAN.use('util.widgets');
40 obj.printer_context = params.printer_context;
42 if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
43 if (typeof params.map_row_to_columns == 'function') {
44 obj.map_row_to_columns = params.map_row_to_columns;
46 obj.map_row_to_columns = obj.std_map_row_to_columns();
48 if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
51 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
53 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
57 'label' : document.getElementById('offlineStrings').getString('list.line_number'),
60 'properties' : 'ordinal', // column properties for css styling
63 'render' : function(my,scratch) {
64 // special code will handle this based on the attribute we set
65 // here. All cells for this column need to be updated whenever
66 // a list adds, removes, or sorts rows
71 for (var i = 0; i < params.columns.length; i++) {
72 if (typeof params.columns[i] == 'object') {
73 obj.columns.push( params.columns[i] );
75 var cols = obj.fm_columns( params.columns[i] );
76 for (var j = 0; j < cols.length; j++) {
77 obj.columns.push( cols[j] );
82 switch(obj.node.nodeName) {
83 case 'tree' : obj._init_tree(params); break;
84 case 'listbox' : obj._init_listbox(params); break;
85 default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
89 '_init_tree' : function (params) {
93 this.treechildren = this.node.lastChild;
96 var treecols = document.createElement('treecols');
97 this.node.appendChild(treecols);
98 this.treecols = treecols;
100 var check_for_id_collisions = {};
101 for (var i = 0; i < this.columns.length; i++) {
102 var treecol = document.createElement('treecol');
103 for (var j in this.columns[i]) {
104 var value = this.columns[i][j];
106 if (typeof check_for_id_collisions[value] == 'undefined') {
107 check_for_id_collisions[value] = true;
109 // Column id's are important for sorting and saving list configuration. Collisions started happening because
110 // we were using field names as id's, and then later combining column definitions for multiple objects that
111 // shared field names. The downside to this sort of automatic collision prevention is that these generated
112 // id's can change as we add and remove columns, possibly breaking saved list configurations.
113 dump('Column collision with id = ' + value + ', renaming to ');
114 value = value + '_collision_' + i;
118 treecol.setAttribute(j,value);
120 treecols.appendChild(treecol);
121 if (this.columns[i].type == 'checkbox') {
122 treecol.addEventListener(
127 var toggle = ev.target.getAttribute('toggleAll') || 'on';
128 if (toggle == 'off') toggle = 'on'; else toggle = 'off';
129 ev.target.setAttribute('toggleAll',toggle);
130 obj._toggle_checkbox_column(ev.target,toggle);
137 treecol.addEventListener(
141 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
142 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
143 ev.target.setAttribute('sortDir',sortDir);
144 obj._sort_tree(ev.target,sortDir);
147 if (obj.row_count.total != obj.row_count.fleshed && (obj.row_count.total - obj.row_count.fleshed) > 50) {
148 var r = window.confirm(document.getElementById('offlineStrings').getFormattedString('list.row_fetch_warning',[obj.row_count.fleshed,obj.row_count.total]));
151 setTimeout( do_it, 0 );
154 setTimeout( do_it, 0 );
160 var splitter = document.createElement('splitter');
161 splitter.setAttribute('class','tree-splitter');
162 treecols.appendChild(splitter);
165 var treechildren = document.createElement('treechildren');
166 this.node.appendChild(treechildren);
167 this.treechildren = treechildren;
169 if (typeof params.on_sort == 'function') {
170 this.on_sort = params.on_sort;
172 if (typeof params.on_checkbox_toggle == 'function') {
173 this.on_checkbox_toggle = params.on_checkbox_toggle;
175 this.node.addEventListener(
178 if (typeof params.on_select == 'function') {
179 params.on_select(ev);
181 var x = document.getElementById(obj.node.id + '_clipfield');
183 var sel = obj.retrieve_selection();
184 x.setAttribute('disabled', sel.length == 0);
189 if (typeof params.on_click == 'function') {
190 this.node.addEventListener(
196 if (typeof params.on_dblclick == 'function') {
197 this.node.addEventListener(
205 this.node.addEventListener(
207 function(ev) { obj.detect_visible(); },
211 this.node.addEventListener(
213 function(ev) { obj.auto_retrieve(); },
216 this.node.addEventListener(
218 function(ev) { obj.auto_retrieve(); },
221 window.addEventListener(
223 function(ev) { obj.auto_retrieve(); },
226 /* FIXME -- find events on scrollbar to trigger this */
227 obj.detect_visible_polling();
229 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
230 var slider = document.getAnonymousNodes( scrollbar )[2];
231 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
232 scrollbar.addEventListener('click',function(){alert('sb click');},false);
233 scrollbar.addEventListener('command',function(){alert('sb command');},false);
234 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
235 slider.addEventListener('click',function(){alert('slider click');},false);
236 slider.addEventListener('command',function(){alert('slider command');},false);
237 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
239 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
241 this.restores_columns(params);
244 '_init_listbox' : function (params) {
247 var listhead = document.createElement('listhead');
248 this.node.appendChild(listhead);
250 var listcols = document.createElement('listcols');
251 this.node.appendChild(listcols);
253 for (var i = 0; i < this.columns.length; i++) {
254 var listheader = document.createElement('listheader');
255 listhead.appendChild(listheader);
256 var listcol = document.createElement('listcol');
257 listcols.appendChild(listcol);
258 for (var j in this.columns[i]) {
259 listheader.setAttribute(j,this.columns[i][j]);
260 listcol.setAttribute(j,this.columns[i][j]);
266 'save_columns' : function (params) {
268 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
269 alert(document.getElementById('offlineStrings').getString('list.column_save_disabled'));
271 switch (this.node.nodeName) {
272 case 'tree' : this._save_columns_tree(params); break;
273 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
278 '_save_columns_tree' : function (params) {
281 var id = obj.node.getAttribute('id');
282 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
284 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
288 var nl = obj.node.getElementsByTagName('treecol');
289 for (var i = 0; i < nl.length; i++) {
291 var col_id = col.getAttribute('id');
293 alert('FIXME: A column in this list does not have an id and cannot be saved');
296 var col_hidden = col.getAttribute('hidden');
297 var col_width = col.getAttribute('width');
298 var col_ordinal = col.getAttribute('ordinal');
299 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
301 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
302 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
303 file.set_object(my_cols);
305 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
307 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
311 'restores_columns' : function (params) {
313 switch (this.node.nodeName) {
314 case 'tree' : this._restores_columns_tree(params); break;
315 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
319 '_restores_columns_tree' : function (params) {
322 var id = obj.node.getAttribute('id');
323 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
325 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
330 if (! obj.data.hash.aous) { obj.data.hash.aous = {}; }
331 if (! obj.data.hash.aous['gui.disable_local_save_columns']) {
332 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
333 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
334 if (file._file.exists()) {
335 my_cols = file.get_object(); file.close();
338 /* local file will trump remote file if allowed, so save ourselves an http request if this is the case */
339 if (obj.data.hash.aous['url.remote_column_settings'] && ! my_cols ) {
341 var x = new XMLHttpRequest();
342 var url = obj.data.hash.aous['url.remote_column_settings'] + '/tree_columns_for_' + window.escape(id);
343 x.open("GET", url, false);
345 if (x.status == 200) {
346 my_cols = JSON2js( x.responseText );
349 // This can happen in the offline interface if you logged in previously and url.remote_column_settings is set.
350 // 1) You may be really "offline" now
351 // 2) the URL may just be a path component without a hostname (ie "/xul/column_settings/"), which won't work
352 // when appended to chrome://open_ils_staff_client/
353 dump('Error retrieving column settings from ' + url + ': ' + E + '\n');
358 var nl = obj.node.getElementsByTagName('treecol');
359 for (var i = 0; i < nl.length; i++) {
361 var col_id = col.getAttribute('id');
363 alert('FIXME: A column in this list does not have an id and cannot be saved');
366 if (typeof my_cols[col_id] != 'undefined') {
367 col.setAttribute('hidden',my_cols[col_id].hidden);
368 col.setAttribute('width',my_cols[col_id].width);
369 col.setAttribute('ordinal',my_cols[col_id].ordinal);
371 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
376 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
380 'clear' : function (params) {
382 switch (this.node.nodeName) {
383 case 'tree' : this._clear_tree(params); break;
384 case 'listbox' : this._clear_listbox(params); break;
385 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
387 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
388 this.row_count.total = 0;
389 this.row_count.fleshed = 0;
390 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
393 '_clear_tree' : function(params) {
395 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
396 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
398 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
399 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
401 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
404 '_clear_listbox' : function(params) {
407 var nl = this.node.getElementsByTagName('listitem');
408 for (var i = 0; i < nl.length; i++) {
411 for (var i = 0; i < items.length; i++) {
412 this.node.removeChild(items[i]);
416 'append' : function (params) {
419 switch (this.node.nodeName) {
420 case 'tree' : rparams = this._append_to_tree(params); break;
421 case 'listbox' : rparams = this._append_to_listbox(params); break;
422 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
424 if (rparams && params.attributes) {
425 for (var i in params.attributes) {
426 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
429 this.row_count.total++;
430 if (this.row_count.fleshed == this.row_count.total) {
431 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
433 rparams.treeitem_node.setAttribute('unique_row_counter',obj.unique_row_counter);
434 rparams.unique_row_counter = obj.unique_row_counter++;
435 if (typeof params.on_append == 'function') {
436 params.on_append(rparams);
441 'refresh_row' : function (params) {
444 switch (this.node.nodeName) {
445 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
446 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
448 if (rparams && params.attributes) {
449 for (var i in params.attributes) {
450 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
453 this.row_count.fleshed--;
458 '_append_to_tree' : function (params) {
462 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
464 var s = ('util.list.append: params = ' + (params) + '\n');
466 var treechildren_node = this.treechildren;
468 if (params.node && params.node.nodeName == 'treeitem') {
469 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
470 if (params.node.lastChild.nodeName == 'treechildren') {
471 treechildren_node = params.node.lastChild;
473 treechildren_node = document.createElement('treechildren');
474 params.node.appendChild(treechildren_node);
478 var treeitem = document.createElement('treeitem');
479 treeitem.setAttribute('retrieve_id',params.retrieve_id);
480 if (typeof params.to_bottom != 'undefined') {
481 treechildren_node.appendChild( treeitem );
482 if (typeof params.no_auto_select == 'undefined') {
483 if (!obj.auto_select_pending) {
484 obj.auto_select_pending = true;
485 setTimeout(function() {
486 dump('auto-selecting\n');
487 var idx = Number(obj.node.view.rowCount)-1;
488 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
489 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
490 obj.auto_select_pending = false;
491 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
496 if (treechildren_node.firstChild) {
497 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
499 treechildren_node.appendChild( treeitem );
501 if (typeof params.no_auto_select == 'undefined') {
502 if (!obj.auto_select_pending) {
503 obj.auto_select_pending = true;
504 setTimeout(function() {
505 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
506 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
507 obj.auto_select_pending = false;
508 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
513 var treerow = document.createElement('treerow');
514 treeitem.appendChild( treerow );
515 treerow.setAttribute('retrieve_id',params.retrieve_id);
516 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
518 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
519 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
521 obj.put_retrieving_label(treerow);
523 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
524 treerow.addEventListener(
528 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
530 treerow.setAttribute('retrieved','true');
532 //dump('fleshing = ' + params.retrieve_id + '\n');
534 function inc_fleshed() {
535 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
536 treerow.setAttribute('fleshed','true');
537 obj.row_count.fleshed++;
538 if (obj.row_count.fleshed >= obj.row_count.total) {
539 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
543 params.treeitem_node = treeitem;
544 params.on_retrieve = function(p) {
547 obj._map_row_to_treecell(p,treerow);
549 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
550 dump('idx = ' + idx + '\n');
551 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
552 if ( obj.node.view.selection.isSelected( idx ) ) {
553 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
554 util.widgets.dispatch('select',obj.node);
557 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
558 dump('fixme2: ' + E + '\n');
562 if (typeof params.retrieve_row == 'function') {
564 params.retrieve_row( params );
566 } else if (typeof obj.retrieve_row == 'function') {
568 obj.retrieve_row( params );
577 if (typeof params.flesh_immediately != 'undefined') {
578 if (params.flesh_immediately) {
581 util.widgets.dispatch('flesh',treerow);
587 treerow.addEventListener(
590 //dump('fleshing anon\n');
591 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
592 obj._map_row_to_treecell(params,treerow);
593 treerow.setAttribute('retrieved','true');
594 treerow.setAttribute('fleshed','true');
595 obj.row_count.fleshed++;
596 if (obj.row_count.fleshed >= obj.row_count.total) {
597 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
602 if (typeof params.flesh_immediately != 'undefined') {
603 if (params.flesh_immediately) {
606 util.widgets.dispatch('flesh',treerow);
612 this.error.sdump('D_LIST',s);
616 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
618 //if (typeof params.to_bottom != 'undefined')
619 if (typeof params.to_top == 'undefined') {
620 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.firstChild.getAttribute('unique_row_counter') ); }
621 treechildren_node.removeChild( treechildren_node.firstChild );
623 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.lastChild.getAttribute('unique_row_counter') ); }
624 treechildren_node.removeChild( treechildren_node.lastChild );
630 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
632 params.treeitem_node = treeitem;
636 '_refresh_row_in_tree' : function (params) {
640 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
641 if (typeof params.treeitem_node == 'undefined') throw('util.list.refresh_row: Object must contain a treeitem_node');
642 if (params.treeitem_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: treeitem_node must be a treeitem');
644 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
646 var treeitem = params.treeitem_node;
647 treeitem.setAttribute('retrieve_id',params.retrieve_id);
648 if (typeof params.to_bottom != 'undefined') {
649 if (typeof params.no_auto_select == 'undefined') {
650 if (!obj.auto_select_pending) {
651 obj.auto_select_pending = true;
652 setTimeout(function() {
653 dump('auto-selecting\n');
654 var idx = Number(obj.node.view.rowCount)-1;
655 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
656 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
657 obj.auto_select_pending = false;
658 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
663 //var delete_me = [];
664 //for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
665 //for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
666 var prev_treerow = treeitem.firstChild; /* FIXME: worry about hierarchal lists like copy_browser? */
667 var treerow = document.createElement('treerow');
668 while (prev_treerow.firstChild) {
669 treerow.appendChild( prev_treerow.removeChild( prev_treerow.firstChild ) );
671 treeitem.replaceChild( treerow, prev_treerow );
672 treerow.setAttribute('retrieve_id',params.retrieve_id);
673 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
675 s += ('tree = ' + this.node.nodeName + '\n');
676 s += ('treeitem = ' + treeitem.nodeName + ' treerow = ' + treerow.nodeName + '\n');
678 obj.put_retrieving_label(treerow);
680 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
682 s += 'found a retrieve_row function\n';
684 treerow.addEventListener(
688 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
690 treerow.setAttribute('retrieved','true');
692 //dump('fleshing = ' + params.retrieve_id + '\n');
694 function inc_fleshed() {
695 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
696 treerow.setAttribute('fleshed','true');
697 obj.row_count.fleshed++;
698 if (obj.row_count.fleshed >= obj.row_count.total) {
699 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
703 params.treeitem_node = treeitem;
704 params.on_retrieve = function(p) {
707 obj._map_row_to_treecell(p,treerow);
709 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
710 dump('idx = ' + idx + '\n');
711 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
712 if ( obj.node.view.selection.isSelected( idx ) ) {
713 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
714 util.widgets.dispatch('select',obj.node);
717 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
718 dump('fixme2: ' + E + '\n');
722 if (typeof params.retrieve_row == 'function') {
724 params.retrieve_row( params );
726 } else if (typeof obj.retrieve_row == 'function') {
728 obj.retrieve_row( params );
737 if (typeof params.flesh_immediately != 'undefined') {
738 if (params.flesh_immediately) {
741 util.widgets.dispatch('flesh',treerow);
749 s += 'did not find a retrieve_row function\n';
751 treerow.addEventListener(
754 //dump('fleshing anon\n');
755 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
756 obj._map_row_to_treecell(params,treerow);
757 treerow.setAttribute('retrieved','true');
758 treerow.setAttribute('fleshed','true');
759 obj.row_count.fleshed++;
760 if (obj.row_count.fleshed >= obj.row_count.total) {
761 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
766 if (typeof params.flesh_immediately != 'undefined') {
767 if (params.flesh_immediately) {
770 util.widgets.dispatch('flesh',treerow);
780 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
782 //if (typeof params.to_bottom != 'undefined')
783 if (typeof params.to_top == 'undefined') {
784 treechildren_node.removeChild( treechildren_node.firstChild );
786 treechildren_node.removeChild( treechildren_node.lastChild );
792 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
794 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
796 this.error.sdump('D_LIST',s);
801 'refresh_ordinals' : function() {
804 setTimeout( // Otherwise we can miss a row just added
806 var nl = obj.node.getElementsByAttribute('label','_');
807 for (var i = 0; i < nl.length; i++) {
812 nl[i].setAttribute( // treecell properties for css styling
817 nl = obj.node.getElementsByAttribute('ord_col','true');
818 for (var i = 0; i < nl.length; i++) {
821 // we could just use 'i' here if we trust the order of elements
822 1 + obj.node.contentView.getIndexOfItem(nl[i].parentNode.parentNode) // treeitem
828 alert('Error in list.js, refresh_ordinals(): ' + E);
832 'put_retrieving_label' : function(treerow) {
835 for (var i = 0; i < obj.columns.length; i++) {
837 if (typeof treerow.childNodes[i] == 'undefined') {
838 treecell = document.createElement('treecell');
839 treerow.appendChild(treecell);
841 treecell = treerow.childNodes[i];
843 treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
846 alert('Error in list.js, put_retrieving_label(): ' + E);
850 'detect_visible' : function() {
853 //dump('detect_visible obj.node = ' + obj.node + '\n');
854 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
856 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
859 s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n';
860 if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
861 var C = B.childNodes[j];
862 s += '\t\t' + C.nodeName + '\n';
865 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
866 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
867 var curpos = scrollbar.getAttribute('curpos');
868 var maxpos = scrollbar.getAttribute('maxpos');
869 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
870 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
871 if ( obj.auto_retrieve() > 0 ) {
872 obj.curpos = curpos; obj.maxpos = maxpos;
876 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
878 } catch(E) { obj.error.sdump('D_ERROR',E); }
881 'detect_visible_polling' : function() {
883 //alert('detect_visible_polling');
885 obj.detect_visible();
886 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
893 'auto_retrieve' : function(params) {
895 switch (this.node.nodeName) {
896 case 'tree' : obj._auto_retrieve_tree(params); break;
897 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
901 '_auto_retrieve_tree' : function (params) {
903 if (!obj.auto_retrieve_in_progress) {
904 obj.auto_retrieve_in_progress = true;
908 //alert('auto_retrieve\n');
910 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
911 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
912 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
913 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
914 for (var i = startpos; i < endpos + 4; i++) {
916 //dump('trying index ' + i + '\n');
917 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
918 if (item && item.getAttribute('retrieved') != 'true' ) {
919 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
920 util.widgets.dispatch('flesh',item); count++;
923 //dump(i + ' : ' + E + '\n');
926 obj.auto_retrieve_in_progress = false;
928 } catch(E) { alert(E); }
934 'exec_on_all_fleshed' : function() {
937 if (obj.on_all_fleshed) {
938 if (typeof obj.on_all_fleshed == 'function') {
939 dump('exec_on_all_fleshed == function\n');
942 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
945 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
946 dump('exec_on_all_fleshed == array\n');
950 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
951 var f = obj.on_all_fleshed.pop();
952 if (typeof f == 'function') {
953 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); }
955 if (obj.on_all_fleshed.length > 0) arguments.callee();
957 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
962 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
966 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
970 'full_retrieve' : function(params) {
972 switch (this.node.nodeName) {
973 case 'tree' : obj._full_retrieve_tree(params); break;
974 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
978 '_full_retrieve_tree' : function(params) {
981 if (obj.row_count.fleshed >= obj.row_count.total) {
982 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
983 obj.exec_on_all_fleshed();
985 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
986 JSAN.use('util.widgets');
987 var nodes = obj.treechildren.childNodes;
988 for (var i = 0; i < nodes.length; i++) {
989 util.widgets.dispatch('flesh',nodes[i].firstChild);
993 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
997 '_append_to_listbox' : function (params) {
1001 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
1003 var s = ('util.list.append: params = ' + (params) + '\n');
1005 var listitem = document.createElement('listitem');
1007 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
1009 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
1013 listitem.setAttribute('retrieve_id',params.retrieve_id);
1014 //FIXME//Make async and fire when row is visible in list
1017 params.treeitem_node = listitem;
1018 params.on_retrieve = function(row) {
1020 obj._map_row_to_listcell(params,listitem);
1021 obj.node.appendChild( listitem );
1022 util.widgets.dispatch('select',obj.node);
1025 if (typeof params.retrieve_row == 'function') {
1027 row = params.retrieve_row( params );
1031 if (typeof obj.retrieve_row == 'function') {
1033 row = obj.retrieve_row( params );
1040 this._map_row_to_listcell(params,listitem);
1041 this.node.appendChild( listitem );
1044 this.error.sdump('D_LIST',s);
1045 params.treeitem_node = listitem;
1050 '_map_row_to_treecell' : function(params,treerow) {
1054 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
1056 for (var i = 0; i < this.columns.length; i++) {
1058 if (typeof treerow.childNodes[i] == 'undefined') {
1059 treecell = document.createElement('treecell');
1060 treerow.appendChild( treecell );
1062 treecell = treerow.childNodes[i];
1065 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1067 var sort_value = '';
1069 // What skip columns is doing is rendering the treecells as blank/empty
1070 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
1071 treecell.setAttribute('label',label);
1072 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1075 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
1076 treecell.setAttribute('label',label);
1077 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1081 if (typeof params.map_row_to_column == 'function') {
1083 label = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1085 } else if (typeof this.map_row_to_column == 'function') {
1087 label = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1090 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
1091 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1093 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
1096 var sort_values = [];
1098 if (typeof params.map_row_to_columns == 'function') {
1100 var values = params.map_row_to_columns(params.row,this.columns,this.scratch_data);
1101 if (typeof values.values == 'undefined') {
1104 labels = values.values;
1105 sort_values = values.sort_values;
1108 } else if (typeof this.map_row_to_columns == 'function') {
1110 var values = this.map_row_to_columns(params.row,this.columns,this.scratch_data);
1111 if (typeof values.values == 'undefined') {
1114 labels = values.values;
1115 sort_values = values.sort_values;
1118 for (var i = 0; i < labels.length; i++) {
1120 if (typeof treerow.childNodes[i] == 'undefined') {
1121 treecell = document.createElement('treecell');
1122 treerow.appendChild(treecell);
1124 treecell = treerow.childNodes[i];
1126 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1127 if ( this.columns[i].type == 'checkbox') {
1128 treecell.setAttribute('value', labels[i]);
1130 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
1132 if (sort_values[i]) {
1133 treecell.setAttribute('sort_value',js2JSON(sort_values[i]));
1135 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
1140 throw('No row to column mapping function.');
1142 this.error.sdump('D_LIST',s);
1145 '_map_row_to_listcell' : function(params,listitem) {
1148 for (var i = 0; i < this.columns.length; i++) {
1150 if (typeof params.map_row_to_column == 'function') {
1152 value = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1156 if (typeof this.map_row_to_column == 'function') {
1158 value = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1161 if (typeof value == 'string' || typeof value == 'number') {
1162 var listcell = document.createElement('listcell');
1163 listcell.setAttribute('label',value);
1164 listitem.appendChild(listcell);
1165 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1167 listitem.appendChild(value);
1168 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1171 this.error.sdump('D_LIST',s);
1174 'select_all' : function(params) {
1176 switch(this.node.nodeName) {
1177 case 'tree' : return this._select_all_from_tree(params); break;
1178 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1182 '_select_all_from_tree' : function(params) {
1184 this.node.view.selection.selectAll();
1187 'retrieve_selection' : function(params) {
1189 switch(this.node.nodeName) {
1190 case 'tree' : return this._retrieve_selection_from_tree(params); break;
1191 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1195 '_retrieve_selection_from_tree' : function(params) {
1198 var start = new Object();
1199 var end = new Object();
1200 var numRanges = this.node.view.selection.getRangeCount();
1201 for (var t=0; t<numRanges; t++){
1202 this.node.view.selection.getRangeAt(t,start,end);
1203 for (var v=start.value; v<=end.value; v++){
1204 var i = this.node.contentView.getItemAtIndex(v);
1211 'dump' : function(params) {
1213 switch(this.node.nodeName) {
1214 case 'tree' : return this._dump_tree(params); break;
1215 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1219 '_dump_tree' : function(params) {
1222 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1224 var treeitem = this.treechildren.childNodes[i];
1225 var treerow = treeitem.firstChild;
1226 for (var j = 0; j < treerow.childNodes.length; j++) {
1227 row.push( treerow.childNodes[j].getAttribute('label') );
1234 'dump_with_keys' : function(params) {
1236 switch(this.node.nodeName) {
1237 case 'tree' : return this._dump_tree_with_keys(params); break;
1238 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1243 '_dump_tree_with_keys' : function(params) {
1247 function process_tree(treechildren) {
1248 for (var i = 0; i < treechildren.childNodes.length; i++) {
1250 var treeitem = treechildren.childNodes[i];
1251 var treerow = treeitem.firstChild;
1252 for (var j = 0; j < treerow.childNodes.length; j++) {
1253 if (typeof obj.columns[j] == 'undefined') {
1254 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1255 dump('_dump_tree_with_keys @ ' + location.href + '\n');
1256 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1259 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1260 var sort = treerow.childNodes[j].getAttribute('sort_value');
1262 row[ obj.columns[j].id + '_sort_value' ] = sort;
1267 if (treeitem.childNodes.length > 1) {
1268 process_tree(treeitem.lastChild);
1273 process_tree(this.treechildren);
1278 'dump_csv' : function(params) {
1280 switch(this.node.nodeName) {
1281 case 'tree' : return this._dump_tree_csv(params); break;
1282 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1287 '_dump_tree_csv' : function(params) {
1291 for (var j = 0; j < obj.columns.length; j++) {
1292 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1295 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1298 ord_cols.sort( function(a,b) {
1299 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1300 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1303 for (var j = 0; j < ord_cols.length; j++) {
1304 if (_dump) _dump += ',';
1305 _dump += '"' + obj.columns[ ord_cols[j][1] ].label.replace(/"/g, '""') + '"';
1309 function process_tree(treechildren) {
1310 for (var i = 0; i < treechildren.childNodes.length; i++) {
1312 var treeitem = treechildren.childNodes[i];
1313 var treerow = treeitem.firstChild;
1314 for (var j = 0; j < ord_cols.length; j++) {
1315 if (row) row += ',';
1316 row += '"' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label').replace(/"/g, '""') + '"';
1318 _dump += row + '\r\n';
1319 if (treeitem.childNodes.length > 1) {
1320 process_tree(treeitem.lastChild);
1325 process_tree(this.treechildren);
1330 'dump_extended_format' : function(params) {
1332 switch(this.node.nodeName) {
1333 case 'tree' : return this._dump_tree_extended_format(params); break;
1334 default: throw('NYI: Need .dump_extended_format() for ' + this.node.nodeName); break;
1339 '_dump_tree_extended_format' : function(params) {
1343 for (var j = 0; j < obj.columns.length; j++) {
1344 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1347 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1350 ord_cols.sort( function(a,b) {
1351 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1352 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1356 function process_tree(treechildren) {
1357 for (var i = 0; i < treechildren.childNodes.length; i++) {
1358 var row = document.getElementById('offlineStrings').getString('list.dump_extended_format.record_separator') + '\r\n';
1359 var treeitem = treechildren.childNodes[i];
1360 var treerow = treeitem.firstChild;
1361 for (var j = 0; j < ord_cols.length; j++) {
1362 row += obj.columns[ ord_cols[j][1] ].label + ': ' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label') + '\r\n';
1364 _dump += row + '\r\n';
1365 if (treeitem.childNodes.length > 1) {
1366 process_tree(treeitem.lastChild);
1371 process_tree(this.treechildren);
1376 'dump_csv_to_clipboard' : function(params) {
1378 if (typeof params == 'undefined') params = {};
1379 if (params.no_full_retrieve) {
1380 copy_to_clipboard( obj.dump_csv( params ) );
1382 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1386 'dump_csv_to_printer' : function(params) {
1388 if (typeof params == 'undefined') params = {};
1389 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1390 if (params.no_full_retrieve) {
1391 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1393 obj.wrap_in_full_retrieve(
1395 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1401 'dump_extended_format_to_printer' : function(params) {
1403 if (typeof params == 'undefined') params = {};
1404 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1405 if (params.no_full_retrieve) {
1406 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1408 obj.wrap_in_full_retrieve(
1410 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1416 'dump_csv_to_file' : function(params) {
1418 JSAN.use('util.file'); var f = new util.file();
1419 if (typeof params == 'undefined') params = {};
1420 if (params.no_full_retrieve) {
1421 params.data = obj.dump_csv( params );
1422 params.not_json = true;
1423 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1424 f.export_file( params );
1426 obj.wrap_in_full_retrieve(
1428 params.data = obj.dump_csv( params );
1429 params.not_json = true;
1430 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1431 f.export_file( params );
1437 'print' : function(params) {
1438 if (!params) params = {};
1439 switch(this.node.nodeName) {
1440 case 'tree' : return this._print_tree(params); break;
1441 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1445 '_print_tree' : function(params) {
1448 var data = obj.data; data.stash_retrieve();
1449 if (!params.staff && data.list.au && data.list.au[0]) {
1450 params.staff = data.list.au[0];
1452 if (!params.lib && data.list.au && data.list.au[0] && data.list.au[0].ws_ou() && data.hash.aou && data.hash.aou[ data.list.au[0].ws_ou() ]) {
1453 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1454 params.lib.children(null);
1456 if (params.template && data.print_list_templates[ params.template ]) {
1457 var template = data.print_list_templates[ params.template ];
1458 if (template.inherit) {
1459 template = data.print_list_templates[ template.inherit ];
1460 // if someone wants to implement recursion later, feel free
1462 for (var i in template) params[i] = template[i];
1464 obj.wrap_in_full_retrieve(
1467 if (!params.list) params.list = obj.dump_with_keys();
1468 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1469 print.tree_list( params );
1470 if (typeof params.callback == 'function') params.callback();
1472 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1478 obj.error.standard_unexpected_error_alert('_print_tree',E);
1482 'dump_selected_with_keys' : function(params) {
1484 switch(this.node.nodeName) {
1485 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1486 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1491 '_dump_tree_selection_with_keys' : function(params) {
1494 var list = obj._retrieve_selection_from_tree();
1495 for (var i = 0; i < list.length; i++) {
1497 var treeitem = list[i];
1498 var treerow = treeitem.firstChild;
1499 for (var j = 0; j < treerow.childNodes.length; j++) {
1500 var value = treerow.childNodes[j].getAttribute('label');
1501 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1502 if (typeof obj.columns[j] == 'undefined') {
1503 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1504 dump('_dump_tree_selection_with_keys @ ' + location.href + '\n');
1505 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1508 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1517 'clipboard' : function(params) {
1520 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1521 obj.data.stash_retrieve();
1522 obj.data.list_clipboard = dump; obj.data.stash('list_clipboard');
1523 JSAN.use('util.window'); var win = new util.window();
1524 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1525 window.focus(); // sometimes the main window will lower after a clipboard action
1527 this.error.standard_unexpected_error_alert('clipboard',E);
1531 'dump_retrieve_ids' : function(params) {
1533 switch(this.node.nodeName) {
1534 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1535 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1539 '_dump_retrieve_ids_tree' : function(params) {
1542 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1543 var treeitem = this.treechildren.childNodes[i];
1544 dump.push( treeitem.getAttribute('retrieve_id') );
1549 'wrap_in_full_retrieve' : function(f) {
1551 if (typeof obj.on_all_fleshed == 'function') { // legacy
1552 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1554 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1555 obj.on_all_fleshed.push(f);
1556 obj.full_retrieve();
1559 '_sort_tree' : function(col,sortDir) {
1562 if (obj.node.getAttribute('no_sort') || col.getAttribute('no_sort')) {
1566 for (var i = 0; i < obj.columns.length; i++) {
1567 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1569 obj.wrap_in_full_retrieve(
1572 JSAN.use('util.money');
1574 var treeitems = obj.treechildren.childNodes;
1575 for (var i = 0; i < treeitems.length; i++) {
1576 var treeitem = treeitems[i];
1577 var treerow = treeitem.firstChild;
1578 var treecell = treerow.childNodes[ col_pos ];
1581 ? treecell.getAttribute('label')
1583 'sort_value' : treecell ? treecell.hasAttribute('sort_value')
1585 treecell.getAttribute('sort_value'))
1591 rows = rows.sort( function(a,b) {
1598 if (col.getAttribute('sort_type')) {
1599 switch(col.getAttribute('sort_type')) {
1601 JSAN.use('util.date'); // to pull in dojo.date.locale
1602 a = dojo.date.locale.parse(a,{});
1603 b = dojo.date.locale.parse(b,{});
1606 a = Number(a); b = Number(b);
1609 a = util.money.dollars_float_to_cents_integer(a);
1610 b = util.money.dollars_float_to_cents_integer(b);
1612 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1613 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1614 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1617 a = String( a ).toUpperCase();
1618 b = String( b ).toUpperCase();
1622 if (typeof a == 'string' || typeof b == 'string') {
1623 a = String( a ).toUpperCase();
1624 b = String( b ).toUpperCase();
1628 //dump('sorting: type = ' + col.getAttribute('sort_type') + ' a = ' + a + ' b = ' + b + ' a<b= ' + (a<b) + ' a>b= ' + (a>b) + '\n');
1629 if (a < b) return -1;
1630 if (a > b) return 1;
1633 if (sortDir == 'asc') rows = rows.reverse();
1634 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1635 for (var i = 0; i < rows.length; i++) {
1636 obj.treechildren.appendChild( rows[i].node );
1638 if (typeof obj.on_sort == 'function') obj.on_sort();
1640 obj.error.standard_unexpected_error_alert('sorting',E);
1642 obj.refresh_ordinals();
1646 obj.error.standard_unexpected_error_alert('pre sorting', E);
1650 '_toggle_checkbox_column' : function(col,toggle) {
1653 if (obj.node.getAttribute('no_toggle')) {
1657 for (var i = 0; i < obj.columns.length; i++) {
1658 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1660 var treeitems = obj.treechildren.childNodes;
1661 for (var i = 0; i < treeitems.length; i++) {
1662 var treeitem = treeitems[i];
1663 var treerow = treeitem.firstChild;
1664 var treecell = treerow.childNodes[ col_pos ];
1665 treecell.setAttribute('value',(toggle == 'on'));
1667 if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1669 obj.error.standard_unexpected_error_alert('pre toggle', E);
1673 'render_list_actions' : function(params) {
1675 switch(this.node.nodeName) {
1676 case 'tree' : return this._render_list_actions_for_tree(params); break;
1677 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1681 '_render_list_actions_for_tree' : function(params) {
1684 var btn = document.createElement('button');
1685 btn.setAttribute('id',obj.node.id + '_list_actions');
1686 btn.setAttribute('type','menu');
1687 btn.setAttribute('allowevents','true');
1688 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1689 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1690 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1691 var mp = document.createElement('menupopup');
1692 btn.appendChild(mp);
1693 var mi = document.createElement('menuitem');
1694 mi.setAttribute('id',obj.node.id + '_clipfield');
1695 mi.setAttribute('disabled','true');
1696 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1697 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1699 mi = document.createElement('menuitem');
1700 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1701 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1702 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1704 mi = document.createElement('menuitem');
1705 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1706 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1707 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1709 mi = document.createElement('menuitem');
1710 mi.setAttribute('id',obj.node.id + '_extended_to_printer');
1711 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.label'));
1712 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.accesskey'));
1714 mi = document.createElement('menuitem');
1715 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1716 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1717 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1719 mi = document.createElement('menuitem');
1720 mi.setAttribute('id',obj.node.id + '_save_columns');
1721 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1722 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1723 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
1724 mi.setAttribute('disabled','true');
1729 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1733 'set_list_actions' : function(params) {
1735 switch(this.node.nodeName) {
1736 case 'tree' : return this._set_list_actions_for_tree(params); break;
1737 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1741 '_set_list_actions_for_tree' : function(params) {
1742 // This should be called after the button element from render_list_actions has been appended to the DOM
1745 var x = document.getElementById(obj.node.id + '_clipfield');
1750 obj.clipboard(params);
1751 if (params && typeof params.on_complete == 'function') {
1752 params.on_complete(params);
1758 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1763 obj.dump_csv_to_clipboard(params);
1764 if (params && typeof params.on_complete == 'function') {
1765 params.on_complete(params);
1771 x = document.getElementById(obj.node.id + '_csv_to_printer');
1776 obj.dump_csv_to_printer(params);
1777 if (params && typeof params.on_complete == 'function') {
1778 params.on_complete(params);
1784 x = document.getElementById(obj.node.id + '_extended_to_printer');
1789 obj.dump_extended_format_to_printer(params);
1790 if (params && typeof params.on_complete == 'function') {
1791 params.on_complete(params);
1798 x = document.getElementById(obj.node.id + '_csv_to_file');
1803 obj.dump_csv_to_file(params);
1804 if (params && typeof params.on_complete == 'function') {
1805 params.on_complete(params);
1811 x = document.getElementById(obj.node.id + '_save_columns');
1816 obj.save_columns(params);
1817 if (params && typeof params.on_complete == 'function') {
1818 params.on_complete(params);
1826 obj.error.standard_unexpected_error_alert('setting list actions',E);
1830 // Takes fieldmapper class name and attempts to spit out column definitions suitable for .init
1831 'fm_columns' : function(hint,column_extras,prefix) {
1834 if (!prefix) { prefix = ''; }
1836 // requires the dojo library fieldmapper.autoIDL
1837 if (typeof fieldmapper == 'undefined') { throw 'fieldmapper undefined'; }
1838 if (typeof fieldmapper.IDL == 'undefined') { throw 'fieldmapper.IDL undefined'; }
1839 if (typeof fieldmapper.IDL.fmclasses == 'undefined') { throw 'fieldmapper.IDL.fmclasses undefined'; }
1840 if (typeof fieldmapper.IDL.fmclasses[hint] == 'undefined') { throw 'fieldmapper.IDL.fmclasses.' + hint + ' undefined'; }
1841 var my_class = fieldmapper.IDL.fmclasses[hint];
1842 var data = obj.data; data.stash_retrieve();
1844 function col_def(my_field) {
1845 var col_id = prefix + hint + '_' + my_field.name;
1847 var datafield = my_field.name;
1848 var fleshed_display_field;
1849 if (column_extras) {
1850 if (column_extras['*']) {
1851 if (column_extras['*']['dataobj']) {
1852 dataobj = column_extras['*']['dataobj'];
1855 if (column_extras[col_id]) {
1856 if (column_extras[col_id]['dataobj']) {
1857 dataobj = column_extras[col_id]['dataobj'];
1859 if (column_extras[col_id]['datafield']) {
1860 datafield = column_extras[col_id]['datafield'];
1862 if (column_extras[col_id]['fleshed_display_field']) {
1863 fleshed_display_field = column_extras[col_id]['fleshed_display_field'];
1869 'label' : my_field.label || my_field.name,
1870 'sort_type' : [ 'int', 'float', 'id', 'number' ].indexOf(my_field.datatype) > -1 ? 'number' :
1871 ( my_field.datatype == 'money' ? 'money' :
1872 ( my_field.datatype == 'timestamp' ? 'date' : 'default')),
1873 'hidden' : my_field.virtual || my_field.datatype == 'link',
1876 // my_field.datatype => bool float id int interval link money number org_unit text timestamp
1877 if (my_field.datatype == 'link') {
1878 def.render = function(my) {
1879 // is the object fleshed?
1880 return my[dataobj][datafield]() && typeof my[dataobj][datafield]() == 'object'
1881 // yes, show the display field
1882 ? my[dataobj][datafield]()[fleshed_display_field||my_field.key]()
1883 // no, do we have its class in data.hash?
1884 : ( typeof data.hash[ my[dataobj].Structure.field_map[datafield].class ] != 'undefined'
1885 // yes, do we have this particular object cached?
1886 ? ( data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ]
1887 // yes, show the display field
1888 ? data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ][
1889 fleshed_display_field||my_field.key
1891 // no, just show the raw value
1892 : my[dataobj][datafield]()
1894 // no, just show the raw value
1895 : my[dataobj][datafield]()
1899 def.render = function(my) { return my[dataobj][datafield](); }
1901 if (my_field.datatype == 'timestamp') {
1902 JSAN.use('util.date');
1903 def.render = function(my) {
1904 return util.date.formatted_date( my[dataobj][datafield](), '%{localized}' );
1906 def.sort_value = function(my) {
1907 return util.date.db_date2Date( my[dataobj][datafield]() ).getTime();
1910 if (my_field.datatype == 'org_unit') {
1911 def.render = function(my) {
1912 return typeof my[dataobj][datafield]() == 'object' ? my[dataobj][datafield]().shortname() : data.hash.aou[ my[dataobj][datafield]() ].shortname();
1915 if (my_field.datatype == 'money') {
1916 JSAN.use('util.money');
1917 def.render = function(my) {
1918 return util.money.sanitize( my[dataobj][datafield]() );
1920 def.sort_value = function(my) {
1921 return util.money.dollars_float_to_cents_integer( my[dataobj][datafield]() );
1924 if (column_extras) {
1925 if (column_extras['*']) {
1926 for (var attr in column_extras['*']) {
1927 def[attr] = column_extras['*'][attr];
1929 if (column_extras['*']['expanded_label']) {
1930 def.label = my_class.label + ': ' + def.label;
1932 if (column_extras['*']['label_prefix']) {
1933 def.label = column_extras['*']['label_prefix'] + def.label;
1935 if (column_extras['*']['remove_virtual']) {
1936 if (my_field.virtual) {
1937 def.remove_me = true;
1941 if (column_extras[col_id]) {
1942 for (var attr in column_extras[col_id]) {
1943 def[attr] = column_extras[col_id][attr];
1945 if (column_extras[col_id]['keep_me']) {
1946 def.remove_me = false;
1948 if (column_extras[col_id]['label_prefix']) {
1949 def.label = column_extras[col_id]['label_prefix'] + def.label;
1953 if (def.remove_me) {
1954 dump('Skipping ' + def.label + '\n');
1957 dump('Defining ' + def.label + '\n');
1962 for (var i = 0; i < my_class.fields.length; i++) {
1963 var my_field = my_class.fields[i];
1964 var def = col_def(my_field);
1966 columns.push( def );
1971 obj.error.standard_unexpected_error_alert('fm_columns()',E);
1975 // Default for the map_row_to_columns function for .init
1976 'std_map_row_to_columns' : function(error_value) {
1977 return function(row,cols,scratch) {
1978 // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
1979 // cols contains all of the objects listed above in columns
1980 // scratch is a temporary space shared by all cells/rows (or just per row if not explicitly passed in)
1981 if (!scratch) { scratch = {}; }
1984 JSAN.use('util.error'); obj.error = new util.error();
1985 JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
1986 JSAN.use('util.network'); obj.network = new util.network();
1987 JSAN.use('util.money');
1991 var sort_values = [];
1994 for (var i = 0; i < cols.length; i++) {
1995 switch (typeof cols[i].render) {
1996 case 'function': try { values[i] = cols[i].render(my,scratch); } catch(E) { values[i] = error_value; obj.error.sdump('D_COLUMN_RENDER_ERROR',E); } break;
1997 case 'string' : cmd += 'try { ' + cols[i].render + '; values['+i+'] = v; } catch(E) { values['+i+'] = error_value; }'; break;
1998 default: cmd += 'values['+i+'] = "??? '+(typeof cols[i].render)+'"; ';
2000 switch (typeof cols[i].sort_value) {
2003 sort_values[i] = cols[i].sort_value(my,scratch);
2005 sort_values[i] = error_value;
2006 obj.error.sdump('D_COLUMN_RENDER_ERROR',E);
2011 + cols[i].sort_value
2014 +'] = v; } catch(E) { sort_values['
2016 + '] = error_value; }';
2019 cmd += 'sort_values['+i+'] = values[' + i + '];';
2022 if (cmd) eval( cmd );
2024 obj.error.sdump('D_WARN','map_row_to_column: ' + E);
2025 if (error_value) { value = error_value; } else { value = ' ' };
2027 return { 'values' : values, 'sort_values' : sort_values };
2031 dump('exiting util.list.js\n');