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;
14 if (!this.node) throw('Could not find element ' + id);
15 switch(this.node.nodeName) {
19 throw(this.node.nodeName + ' not yet supported'); break;
20 default: throw(this.node.nodeName + ' not supported'); break;
23 JSAN.use('util.error'); this.error = new util.error();
25 JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.stash_retrieve();
27 JSAN.use('util.functional');
28 JSAN.use('util.widgets');
33 util.list.prototype = {
35 'init' : function (params) {
38 obj.scratch_data = {};
39 obj.event_listeners = new EventListenerList();
41 // If set, save and restore columns as if the tree/list id was the value of columns_saved_under
42 obj.columns_saved_under = params.columns_saved_under;
44 JSAN.use('util.widgets');
46 obj.printer_context = params.printer_context;
48 if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
49 if (typeof params.map_row_to_columns == 'function') {
50 obj.map_row_to_columns = params.map_row_to_columns;
52 obj.map_row_to_columns = obj.std_map_row_to_columns();
54 if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
57 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
59 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
63 'label' : document.getElementById('offlineStrings').getString('list.line_number'),
66 'properties' : 'ordinal', // column properties for css styling
69 'render' : function(my,scratch) {
70 // special code will handle this based on the attribute we set
71 // here. All cells for this column need to be updated whenever
72 // a list adds, removes, or sorts rows
77 for (var i = 0; i < params.columns.length; i++) {
78 if (typeof params.columns[i] == 'object') {
79 obj.columns.push( params.columns[i] );
81 var cols = obj.fm_columns( params.columns[i] );
82 for (var j = 0; j < cols.length; j++) {
83 obj.columns.push( cols[j] );
88 switch(obj.node.nodeName) {
89 case 'tree' : obj._init_tree(params); break;
90 case 'listbox' : obj._init_listbox(params); break;
91 default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
95 '_init_tree' : function (params) {
99 this.treechildren = this.node.lastChild;
102 var treecols = document.createElement('treecols');
103 this.node.appendChild(treecols);
104 this.treecols = treecols;
105 if (document.getElementById('column_sort_menu')) {
106 treecols.setAttribute('context','column_sort_menu');
109 var check_for_id_collisions = {};
110 for (var i = 0; i < this.columns.length; i++) {
111 var treecol = document.createElement('treecol');
112 for (var j in this.columns[i]) {
113 var value = this.columns[i][j];
115 if (typeof check_for_id_collisions[value] == 'undefined') {
116 check_for_id_collisions[value] = true;
118 // Column id's are important for sorting and saving list configuration. Collisions started happening because
119 // we were using field names as id's, and then later combining column definitions for multiple objects that
120 // shared field names. The downside to this sort of automatic collision prevention is that these generated
121 // id's can change as we add and remove columns, possibly breaking saved list configurations.
122 dump('Column collision with id = ' + value + ', renaming to ');
123 value = value + '_collision_' + i;
127 treecol.setAttribute(j,value);
129 treecols.appendChild(treecol);
131 if (this.columns[i].type == 'checkbox') {
132 obj.event_listeners.add(
138 var toggle = ev.target.getAttribute('toggleAll') || 'on';
139 if (toggle == 'off') toggle = 'on'; else toggle = 'off';
140 ev.target.setAttribute('toggleAll',toggle);
141 obj._toggle_checkbox_column(ev.target,toggle);
148 obj.event_listeners.add(
152 dump('sort_first_asc\n');
153 ev.target.setAttribute('sortDir','asc');
155 'target' : ev.target,
159 util.widgets.dispatch('sort',ev.target);
163 obj.event_listeners.add(
167 dump('sort_first_desc\n');
168 ev.target.setAttribute('sortDir','desc');
170 'target' : ev.target,
174 util.widgets.dispatch('sort',ev.target);
178 obj.event_listeners.add(
182 dump('sort_next_asc\n');
183 ev.target.setAttribute('sortDir','asc');
185 'target' : ev.target,
188 util.widgets.dispatch('sort',ev.target);
192 obj.event_listeners.add(
196 dump('sort_next_desc\n');
197 ev.target.setAttribute('sortDir','desc');
199 'target' : ev.target,
202 util.widgets.dispatch('sort',ev.target);
207 obj.event_listeners.add(
211 if (ev.button == 2 /* context menu click */ || ev.target.getAttribute('no_sort')) {
215 if (ev.ctrlKey) { // sub sort
220 ev.target.setAttribute('sortDir',sortDir);
222 'target' : ev.target,
225 } else { // first sort
226 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
227 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
231 ev.target.setAttribute('sortDir',sortDir);
233 'target' : ev.target,
238 util.widgets.dispatch('sort',ev.target);
243 obj.event_listeners.add(
247 if (!obj.first_sort) {
255 if (obj.row_count.total != obj.row_count.fleshed
256 && (obj.row_count.total - obj.row_count.fleshed) > 50
258 var r = window.confirm(
259 document.getElementById('offlineStrings').getFormattedString(
260 'list.row_fetch_warning',
261 [obj.row_count.fleshed,obj.row_count.total]
266 setTimeout( do_it, 0 );
270 setTimeout( do_it, 0 );
277 var splitter = document.createElement('splitter');
278 splitter.setAttribute('class','tree-splitter');
279 treecols.appendChild(splitter);
282 var treechildren = document.createElement('treechildren');
283 this.node.appendChild(treechildren);
284 this.treechildren = treechildren;
286 if (typeof params.on_sort == 'function') {
287 this.on_sort = params.on_sort;
289 if (typeof params.on_checkbox_toggle == 'function') {
290 this.on_checkbox_toggle = params.on_checkbox_toggle;
292 obj.event_listeners.add(
296 if (typeof params.on_select == 'function') {
297 params.on_select(ev);
299 var x = document.getElementById(obj.node.id + '_clipfield');
301 var sel = obj.retrieve_selection();
302 x.setAttribute('disabled', sel.length == 0);
307 if (typeof params.on_click == 'function') {
308 obj.event_listeners.add(
315 if (typeof params.on_dblclick == 'function') {
316 obj.event_listeners.add(
325 obj.event_listeners.add(
328 function(ev) { obj.detect_visible(); },
332 obj.event_listeners.add(
335 function(ev) { obj.auto_retrieve(); },
338 obj.event_listeners.add(
341 function(ev) { obj.auto_retrieve(); },
344 obj.event_listeners.add(
347 function(ev) { obj.auto_retrieve(); },
350 /* FIXME -- find events on scrollbar to trigger this */
351 obj.detect_visible_polling();
353 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
354 var slider = document.getAnonymousNodes( scrollbar )[2];
355 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
356 scrollbar.addEventListener('click',function(){alert('sb click');},false);
357 scrollbar.addEventListener('command',function(){alert('sb command');},false);
358 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
359 slider.addEventListener('click',function(){alert('slider click');},false);
360 slider.addEventListener('command',function(){alert('slider command');},false);
361 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
363 obj.event_listeners.add(this.node, 'scroll',function(){ obj.auto_retrieve(); },false);
365 this.restores_columns(params);
368 '_init_listbox' : function (params) {
371 var listhead = document.createElement('listhead');
372 this.node.appendChild(listhead);
374 var listcols = document.createElement('listcols');
375 this.node.appendChild(listcols);
377 for (var i = 0; i < this.columns.length; i++) {
378 var listheader = document.createElement('listheader');
379 listhead.appendChild(listheader);
380 var listcol = document.createElement('listcol');
381 listcols.appendChild(listcol);
382 for (var j in this.columns[i]) {
383 listheader.setAttribute(j,this.columns[i][j]);
384 listcol.setAttribute(j,this.columns[i][j]);
390 'cleanup' : function () {
392 obj.event_listeners.removeAll();
395 'save_columns' : function (params) {
397 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
398 alert(document.getElementById('offlineStrings').getString('list.column_save_disabled'));
400 switch (this.node.nodeName) {
401 case 'tree' : this._save_columns_tree(params); break;
402 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
407 '_save_columns_tree' : function (params) {
410 var id = obj.node.getAttribute('id');
411 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
413 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
417 var nl = obj.node.getElementsByTagName('treecol');
418 for (var i = 0; i < nl.length; i++) {
420 var col_id = col.getAttribute('id');
422 alert('FIXME: A column in this list does not have an id and cannot be saved');
425 var col_hidden = col.getAttribute('hidden');
426 var col_width = col.getAttribute('width');
427 var col_ordinal = col.getAttribute('ordinal');
428 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
430 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
431 file.set_object(my_cols);
433 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
435 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
439 'restores_columns' : function (params) {
441 switch (this.node.nodeName) {
442 case 'tree' : this._restores_columns_tree(params); break;
443 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
447 '_restores_columns_tree' : function (params) {
450 var id = obj.node.getAttribute('id');
451 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
453 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
458 if (! obj.data.hash.aous) { obj.data.hash.aous = {}; }
459 if (! obj.data.hash.aous['gui.disable_local_save_columns']) {
460 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
461 if (file._file.exists()) {
462 my_cols = file.get_object(); file.close();
465 /* local file will trump remote file if allowed, so save ourselves an http request if this is the case */
466 if (obj.data.hash.aous['url.remote_column_settings'] && ! my_cols ) {
468 var x = new XMLHttpRequest();
469 var url = obj.data.hash.aous['url.remote_column_settings'] + '/tree_columns_for_' + window.escape(id);
470 x.open("GET", url, false);
472 if (x.status == 200) {
473 my_cols = JSON2js( x.responseText );
476 // This can happen in the offline interface if you logged in previously and url.remote_column_settings is set.
477 // 1) You may be really "offline" now
478 // 2) the URL may just be a path component without a hostname (ie "/xul/column_settings/"), which won't work
479 // when appended to chrome://open_ils_staff_client/
480 dump('Error retrieving column settings from ' + url + ': ' + E + '\n');
485 var nl = obj.node.getElementsByTagName('treecol');
486 for (var i = 0; i < nl.length; i++) {
488 var col_id = col.getAttribute('id');
490 alert('FIXME: A column in this list does not have an id and cannot be saved');
493 if (typeof my_cols[col_id] != 'undefined') {
494 col.setAttribute('hidden',my_cols[col_id].hidden);
495 col.setAttribute('width',my_cols[col_id].width);
496 col.setAttribute('ordinal',my_cols[col_id].ordinal);
498 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
503 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
507 'clear' : function (params) {
509 switch (this.node.nodeName) {
510 case 'tree' : this._clear_tree(params); break;
511 case 'listbox' : this._clear_listbox(params); break;
512 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
514 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
515 this.row_count.total = 0;
516 this.row_count.fleshed = 0;
517 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
520 '_clear_tree' : function(params) {
522 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
523 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
525 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
526 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
528 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
531 '_clear_listbox' : function(params) {
534 var nl = this.node.getElementsByTagName('listitem');
535 for (var i = 0; i < nl.length; i++) {
538 for (var i = 0; i < items.length; i++) {
539 this.node.removeChild(items[i]);
543 'append' : function (params) {
546 switch (this.node.nodeName) {
547 case 'tree' : rparams = this._append_to_tree(params); break;
548 case 'listbox' : rparams = this._append_to_listbox(params); break;
549 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
551 if (rparams && params.attributes) {
552 for (var i in params.attributes) {
553 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
556 this.row_count.total++;
557 if (this.row_count.fleshed == this.row_count.total) {
558 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
560 rparams.treeitem_node.setAttribute('unique_row_counter',obj.unique_row_counter);
561 rparams.unique_row_counter = obj.unique_row_counter++;
562 if (typeof params.on_append == 'function') {
563 params.on_append(rparams);
568 'refresh_row' : function (params) {
571 switch (this.node.nodeName) {
572 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
573 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
575 if (rparams && params.attributes) {
576 for (var i in params.attributes) {
577 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
580 this.row_count.fleshed--;
585 '_append_to_tree' : function (params) {
589 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
591 var s = ('util.list.append: params = ' + (params) + '\n');
593 var treechildren_node = this.treechildren;
595 if (params.node && params.node.nodeName == 'treeitem') {
596 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
597 if (params.node.lastChild.nodeName == 'treechildren') {
598 treechildren_node = params.node.lastChild;
600 treechildren_node = document.createElement('treechildren');
601 params.node.appendChild(treechildren_node);
605 var treeitem = document.createElement('treeitem');
606 treeitem.setAttribute('retrieve_id',params.retrieve_id);
607 if (typeof params.to_bottom != 'undefined') {
608 treechildren_node.appendChild( treeitem );
609 if (typeof params.no_auto_select == 'undefined') {
610 if (!obj.auto_select_pending) {
611 obj.auto_select_pending = true;
612 setTimeout(function() {
613 dump('auto-selecting\n');
614 var idx = Number(obj.node.view.rowCount)-1;
615 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
616 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
617 obj.auto_select_pending = false;
618 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
623 if (treechildren_node.firstChild) {
624 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
626 treechildren_node.appendChild( treeitem );
628 if (typeof params.no_auto_select == 'undefined') {
629 if (!obj.auto_select_pending) {
630 obj.auto_select_pending = true;
631 setTimeout(function() {
632 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
633 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
634 obj.auto_select_pending = false;
635 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
640 var treerow = document.createElement('treerow');
641 treeitem.appendChild( treerow );
642 treerow.setAttribute('retrieve_id',params.retrieve_id);
643 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
645 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
646 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
648 obj.put_retrieving_label(treerow);
650 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
651 obj.event_listeners.add(
656 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
658 treerow.setAttribute('retrieved','true');
660 //dump('fleshing = ' + params.retrieve_id + '\n');
662 function inc_fleshed() {
663 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
664 treerow.setAttribute('fleshed','true');
665 obj.row_count.fleshed++;
666 if (obj.row_count.fleshed >= obj.row_count.total) {
667 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
671 params.treeitem_node = treeitem;
672 params.on_retrieve = function(p) {
675 obj._map_row_to_treecell(p,treerow);
677 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
678 dump('idx = ' + idx + '\n');
679 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
680 if ( obj.node.view.selection.isSelected( idx ) ) {
681 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
682 util.widgets.dispatch('select',obj.node);
685 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
686 dump('fixme2: ' + E + '\n');
690 if (typeof params.retrieve_row == 'function') {
692 params.retrieve_row( params );
694 } else if (typeof obj.retrieve_row == 'function') {
696 obj.retrieve_row( params );
702 obj.refresh_ordinals();
706 if (typeof params.flesh_immediately != 'undefined') {
707 if (params.flesh_immediately) {
710 util.widgets.dispatch('flesh',treerow);
716 obj.event_listeners.add(
720 //dump('fleshing anon\n');
721 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
722 obj._map_row_to_treecell(params,treerow);
723 treerow.setAttribute('retrieved','true');
724 treerow.setAttribute('fleshed','true');
725 obj.row_count.fleshed++;
726 if (obj.row_count.fleshed >= obj.row_count.total) {
727 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
729 obj.refresh_ordinals();
733 if (typeof params.flesh_immediately != 'undefined') {
734 if (params.flesh_immediately) {
737 util.widgets.dispatch('flesh',treerow);
743 this.error.sdump('D_LIST',s);
747 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
749 //if (typeof params.to_bottom != 'undefined')
750 if (typeof params.to_top == 'undefined') {
751 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.firstChild.getAttribute('unique_row_counter') ); }
752 treechildren_node.removeChild( treechildren_node.firstChild );
754 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.lastChild.getAttribute('unique_row_counter') ); }
755 treechildren_node.removeChild( treechildren_node.lastChild );
761 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
763 params.treeitem_node = treeitem;
767 '_refresh_row_in_tree' : function (params) {
771 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
772 if (typeof params.treeitem_node == 'undefined') throw('util.list.refresh_row: Object must contain a treeitem_node');
773 if (params.treeitem_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: treeitem_node must be a treeitem');
775 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
777 var treeitem = params.treeitem_node;
778 treeitem.setAttribute('retrieve_id',params.retrieve_id);
779 if (typeof params.to_bottom != 'undefined') {
780 if (typeof params.no_auto_select == 'undefined') {
781 if (!obj.auto_select_pending) {
782 obj.auto_select_pending = true;
783 setTimeout(function() {
784 dump('auto-selecting\n');
785 var idx = Number(obj.node.view.rowCount)-1;
786 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
787 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
788 obj.auto_select_pending = false;
789 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
794 //var delete_me = [];
795 //for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
796 //for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
797 var prev_treerow = treeitem.firstChild; /* FIXME: worry about hierarchal lists like copy_browser? */
798 var treerow = document.createElement('treerow');
799 while (prev_treerow.firstChild) {
800 treerow.appendChild( prev_treerow.removeChild( prev_treerow.firstChild ) );
802 treeitem.replaceChild( treerow, prev_treerow );
803 treerow.setAttribute('retrieve_id',params.retrieve_id);
804 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
806 s += ('tree = ' + this.node.nodeName + '\n');
807 s += ('treeitem = ' + treeitem.nodeName + ' treerow = ' + treerow.nodeName + '\n');
809 obj.put_retrieving_label(treerow);
811 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
813 s += 'found a retrieve_row function\n';
815 obj.event_listeners.add(
820 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
822 treerow.setAttribute('retrieved','true');
824 //dump('fleshing = ' + params.retrieve_id + '\n');
826 function inc_fleshed() {
827 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
828 treerow.setAttribute('fleshed','true');
829 obj.row_count.fleshed++;
830 if (obj.row_count.fleshed >= obj.row_count.total) {
831 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
835 params.treeitem_node = treeitem;
836 params.on_retrieve = function(p) {
839 obj._map_row_to_treecell(p,treerow);
841 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
842 dump('idx = ' + idx + '\n');
843 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
844 if ( obj.node.view.selection.isSelected( idx ) ) {
845 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
846 util.widgets.dispatch('select',obj.node);
849 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
850 dump('fixme2: ' + E + '\n');
854 if (typeof params.retrieve_row == 'function') {
856 params.retrieve_row( params );
858 } else if (typeof obj.retrieve_row == 'function') {
860 obj.retrieve_row( params );
866 obj.refresh_ordinals();
870 if (typeof params.flesh_immediately != 'undefined') {
871 if (params.flesh_immediately) {
874 util.widgets.dispatch('flesh',treerow);
882 s += 'did not find a retrieve_row function\n';
884 obj.event_listeners.add(
888 //dump('fleshing anon\n');
889 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
890 obj._map_row_to_treecell(params,treerow);
891 treerow.setAttribute('retrieved','true');
892 treerow.setAttribute('fleshed','true');
893 obj.row_count.fleshed++;
894 if (obj.row_count.fleshed >= obj.row_count.total) {
895 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
897 obj.refresh_ordinals();
901 if (typeof params.flesh_immediately != 'undefined') {
902 if (params.flesh_immediately) {
905 util.widgets.dispatch('flesh',treerow);
915 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
917 //if (typeof params.to_bottom != 'undefined')
918 if (typeof params.to_top == 'undefined') {
919 treechildren_node.removeChild( treechildren_node.firstChild );
921 treechildren_node.removeChild( treechildren_node.lastChild );
927 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
929 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
931 this.error.sdump('D_LIST',s);
936 'refresh_ordinals' : function() {
939 if (obj.refresh_ordinals_timeout_id) { return; }
941 function _refresh_ordinals(clear) {
942 var nl = obj.node.getElementsByAttribute('label','_');
943 for (var i = 0; i < nl.length; i++) {
948 nl[i].setAttribute( // treecell properties for css styling
953 nl = obj.node.getElementsByAttribute('ord_col','true');
954 for (var i = 0; i < nl.length; i++) {
957 // we could just use 'i' here if we trust the order of elements
958 1 + obj.node.contentView.getIndexOfItem(nl[i].parentNode.parentNode) // treeitem
961 if (clear) { obj.refresh_ordinals_timeout_id = null; }
964 // spamming this to cover race conditions
965 setTimeout(_refresh_ordinals, 500); // for speedy looking UI updates
966 setTimeout(_refresh_ordinals, 2000); // for most uses
967 obj.refresh_ordinals_timeout_id = setTimeout(
969 _refresh_ordinals(true);
971 4000 // just in case, say with a slow rendering list
975 alert('Error in list.js, refresh_ordinals(): ' + E);
979 'put_retrieving_label' : function(treerow) {
982 for (var i = 0; i < obj.columns.length; i++) {
984 if (typeof treerow.childNodes[i] == 'undefined') {
985 treecell = document.createElement('treecell');
986 treerow.appendChild(treecell);
988 treecell = treerow.childNodes[i];
990 treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
993 alert('Error in list.js, put_retrieving_label(): ' + E);
997 'detect_visible' : function() {
1000 //dump('detect_visible obj.node = ' + obj.node + '\n');
1001 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
1003 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
1006 s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n';
1007 if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
1008 var C = B.childNodes[j];
1009 s += '\t\t' + C.nodeName + '\n';
1012 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
1013 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
1014 var curpos = scrollbar.getAttribute('curpos');
1015 var maxpos = scrollbar.getAttribute('maxpos');
1016 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
1017 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
1018 if ( obj.auto_retrieve() > 0 ) {
1019 obj.curpos = curpos; obj.maxpos = maxpos;
1023 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
1025 } catch(E) { obj.error.sdump('D_ERROR',E); }
1028 'detect_visible_polling' : function() {
1030 //alert('detect_visible_polling');
1032 obj.detect_visible();
1033 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
1040 'auto_retrieve' : function(params) {
1042 switch (this.node.nodeName) {
1043 case 'tree' : obj._auto_retrieve_tree(params); break;
1044 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
1048 '_auto_retrieve_tree' : function (params) {
1050 if (!obj.auto_retrieve_in_progress) {
1051 obj.auto_retrieve_in_progress = true;
1055 //alert('auto_retrieve\n');
1057 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
1058 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
1059 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
1060 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
1061 for (var i = startpos; i < endpos + 4; i++) {
1063 //dump('trying index ' + i + '\n');
1064 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
1065 if (item && item.getAttribute('retrieved') != 'true' ) {
1066 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
1067 util.widgets.dispatch('flesh',item); count++;
1070 //dump(i + ' : ' + E + '\n');
1073 obj.auto_retrieve_in_progress = false;
1075 } catch(E) { alert(E); }
1081 'exec_on_all_fleshed' : function() {
1084 if (obj.on_all_fleshed) {
1085 if (typeof obj.on_all_fleshed == 'function') {
1086 dump('exec_on_all_fleshed == function\n');
1089 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
1092 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
1093 dump('exec_on_all_fleshed == array\n');
1097 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
1098 var f = obj.on_all_fleshed.pop();
1099 if (typeof f == 'function') {
1100 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',E); }
1102 if (obj.on_all_fleshed.length > 0) arguments.callee();
1104 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
1109 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
1113 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
1117 'full_retrieve' : function(params) {
1119 switch (this.node.nodeName) {
1120 case 'tree' : obj._full_retrieve_tree(params); break;
1121 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
1123 obj.refresh_ordinals();
1126 '_full_retrieve_tree' : function(params) {
1129 if (obj.row_count.fleshed >= obj.row_count.total) {
1130 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
1131 obj.exec_on_all_fleshed();
1133 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
1134 JSAN.use('util.widgets');
1135 var nodes = obj.treechildren.childNodes;
1136 for (var i = 0; i < nodes.length; i++) {
1137 util.widgets.dispatch('flesh',nodes[i].firstChild);
1141 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
1145 '_append_to_listbox' : function (params) {
1149 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
1151 var s = ('util.list.append: params = ' + (params) + '\n');
1153 var listitem = document.createElement('listitem');
1155 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
1157 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
1161 listitem.setAttribute('retrieve_id',params.retrieve_id);
1162 //FIXME//Make async and fire when row is visible in list
1165 params.treeitem_node = listitem;
1166 params.on_retrieve = function(row) {
1168 obj._map_row_to_listcell(params,listitem);
1169 obj.node.appendChild( listitem );
1170 util.widgets.dispatch('select',obj.node);
1173 if (typeof params.retrieve_row == 'function') {
1175 row = params.retrieve_row( params );
1179 if (typeof obj.retrieve_row == 'function') {
1181 row = obj.retrieve_row( params );
1188 this._map_row_to_listcell(params,listitem);
1189 this.node.appendChild( listitem );
1192 this.error.sdump('D_LIST',s);
1193 params.treeitem_node = listitem;
1198 '_map_row_to_treecell' : function(params,treerow) {
1202 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
1204 for (var i = 0; i < this.columns.length; i++) {
1206 if (typeof treerow.childNodes[i] == 'undefined') {
1207 treecell = document.createElement('treecell');
1208 treerow.appendChild( treecell );
1210 treecell = treerow.childNodes[i];
1213 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1215 var sort_value = '';
1217 // What skip columns is doing is rendering the treecells as blank/empty
1218 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
1219 treecell.setAttribute('label',label);
1220 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1223 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
1224 treecell.setAttribute('label',label);
1225 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1229 if (typeof params.map_row_to_column == 'function') {
1231 label = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1233 } else if (typeof this.map_row_to_column == 'function') {
1235 label = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1238 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
1239 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1241 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
1244 var sort_values = [];
1246 if (typeof params.map_row_to_columns == 'function') {
1248 var values = params.map_row_to_columns(params.row,this.columns,this.scratch_data);
1249 if (typeof values.values == 'undefined') {
1252 labels = values.values;
1253 sort_values = values.sort_values;
1256 } else if (typeof this.map_row_to_columns == 'function') {
1258 var values = this.map_row_to_columns(params.row,this.columns,this.scratch_data);
1259 if (typeof values.values == 'undefined') {
1262 labels = values.values;
1263 sort_values = values.sort_values;
1266 for (var i = 0; i < labels.length; i++) {
1268 if (typeof treerow.childNodes[i] == 'undefined') {
1269 treecell = document.createElement('treecell');
1270 treerow.appendChild(treecell);
1272 treecell = treerow.childNodes[i];
1274 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1275 if ( this.columns[i].type == 'checkbox') {
1276 treecell.setAttribute('value', labels[i]);
1278 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
1280 if (sort_values[i]) {
1281 treecell.setAttribute('sort_value',js2JSON(sort_values[i]));
1283 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
1288 throw('No row to column mapping function.');
1290 this.error.sdump('D_LIST',s);
1293 '_map_row_to_listcell' : function(params,listitem) {
1296 for (var i = 0; i < this.columns.length; i++) {
1298 if (typeof params.map_row_to_column == 'function') {
1300 value = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1304 if (typeof this.map_row_to_column == 'function') {
1306 value = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1309 if (typeof value == 'string' || typeof value == 'number') {
1310 var listcell = document.createElement('listcell');
1311 listcell.setAttribute('label',value);
1312 listitem.appendChild(listcell);
1313 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1315 listitem.appendChild(value);
1316 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1319 this.error.sdump('D_LIST',s);
1322 'select_all' : function(params) {
1324 switch(this.node.nodeName) {
1325 case 'tree' : return this._select_all_from_tree(params); break;
1326 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1330 '_select_all_from_tree' : function(params) {
1332 this.node.view.selection.selectAll();
1335 'retrieve_selection' : function(params) {
1337 switch(this.node.nodeName) {
1338 case 'tree' : return this._retrieve_selection_from_tree(params); break;
1339 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1343 '_retrieve_selection_from_tree' : function(params) {
1346 var start = new Object();
1347 var end = new Object();
1348 var numRanges = this.node.view.selection.getRangeCount();
1349 for (var t=0; t<numRanges; t++){
1350 this.node.view.selection.getRangeAt(t,start,end);
1351 for (var v=start.value; v<=end.value; v++){
1352 var i = this.node.contentView.getItemAtIndex(v);
1359 'dump' : function(params) {
1361 switch(this.node.nodeName) {
1362 case 'tree' : return this._dump_tree(params); break;
1363 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1367 '_dump_tree' : function(params) {
1370 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1372 var treeitem = this.treechildren.childNodes[i];
1373 var treerow = treeitem.firstChild;
1374 for (var j = 0; j < treerow.childNodes.length; j++) {
1375 row.push( treerow.childNodes[j].getAttribute('label') );
1382 'dump_with_keys' : function(params) {
1384 switch(this.node.nodeName) {
1385 case 'tree' : return this._dump_tree_with_keys(params); break;
1386 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1391 '_dump_tree_with_keys' : function(params) {
1395 function process_tree(treechildren) {
1396 for (var i = 0; i < treechildren.childNodes.length; i++) {
1398 var treeitem = treechildren.childNodes[i];
1399 var treerow = treeitem.firstChild;
1400 for (var j = 0; j < treerow.childNodes.length; j++) {
1401 if (typeof obj.columns[j] == 'undefined') {
1402 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1403 dump('_dump_tree_with_keys @ ' + location.href + '\n');
1404 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1407 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1408 var sort = treerow.childNodes[j].getAttribute('sort_value');
1410 row[ obj.columns[j].id + '_sort_value' ] = sort;
1415 if (treeitem.childNodes.length > 1) {
1416 process_tree(treeitem.lastChild);
1421 process_tree(this.treechildren);
1426 'dump_csv' : function(params) {
1428 switch(this.node.nodeName) {
1429 case 'tree' : return this._dump_tree_csv(params); break;
1430 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1435 '_dump_tree_csv' : function(params) {
1439 for (var j = 0; j < obj.columns.length; j++) {
1440 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1443 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1446 ord_cols.sort( function(a,b) {
1447 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1448 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1451 for (var j = 0; j < ord_cols.length; j++) {
1452 if (_dump) _dump += ',';
1453 _dump += '"' + obj.columns[ ord_cols[j][1] ].label.replace(/"/g, '""') + '"';
1457 function process_tree(treechildren) {
1458 for (var i = 0; i < treechildren.childNodes.length; i++) {
1460 var treeitem = treechildren.childNodes[i];
1461 var treerow = treeitem.firstChild;
1462 for (var j = 0; j < ord_cols.length; j++) {
1463 if (row) row += ',';
1464 row += '"' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label').replace(/"/g, '""') + '"';
1466 _dump += row + '\r\n';
1467 if (treeitem.childNodes.length > 1) {
1468 process_tree(treeitem.lastChild);
1473 process_tree(this.treechildren);
1478 'dump_extended_format' : function(params) {
1480 switch(this.node.nodeName) {
1481 case 'tree' : return this._dump_tree_extended_format(params); break;
1482 default: throw('NYI: Need .dump_extended_format() for ' + this.node.nodeName); break;
1487 '_dump_tree_extended_format' : function(params) {
1491 for (var j = 0; j < obj.columns.length; j++) {
1492 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1495 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1498 ord_cols.sort( function(a,b) {
1499 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1500 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1504 function process_tree(treechildren) {
1505 for (var i = 0; i < treechildren.childNodes.length; i++) {
1506 var row = document.getElementById('offlineStrings').getString('list.dump_extended_format.record_separator') + '\r\n';
1507 var treeitem = treechildren.childNodes[i];
1508 var treerow = treeitem.firstChild;
1509 for (var j = 0; j < ord_cols.length; j++) {
1510 row += obj.columns[ ord_cols[j][1] ].label + ': ' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label') + '\r\n';
1512 _dump += row + '\r\n';
1513 if (treeitem.childNodes.length > 1) {
1514 process_tree(treeitem.lastChild);
1519 process_tree(this.treechildren);
1524 'dump_csv_to_clipboard' : function(params) {
1526 if (typeof params == 'undefined') params = {};
1527 if (params.no_full_retrieve) {
1528 copy_to_clipboard( obj.dump_csv( params ) );
1530 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1534 'dump_csv_to_printer' : function(params) {
1536 if (typeof params == 'undefined') params = {};
1537 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1538 if (params.no_full_retrieve) {
1539 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1541 obj.wrap_in_full_retrieve(
1543 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1549 'dump_extended_format_to_printer' : function(params) {
1551 if (typeof params == 'undefined') params = {};
1552 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1553 if (params.no_full_retrieve) {
1554 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1556 obj.wrap_in_full_retrieve(
1558 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1564 'dump_csv_to_file' : function(params) {
1566 JSAN.use('util.file'); var f = new util.file();
1567 if (typeof params == 'undefined') params = {};
1568 if (params.no_full_retrieve) {
1569 params.data = obj.dump_csv( params );
1570 params.not_json = true;
1571 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1572 f.export_file( params );
1574 obj.wrap_in_full_retrieve(
1576 params.data = obj.dump_csv( params );
1577 params.not_json = true;
1578 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1579 f.export_file( params );
1585 'print' : function(params) {
1586 if (!params) params = {};
1587 switch(this.node.nodeName) {
1588 case 'tree' : return this._print_tree(params); break;
1589 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1593 '_print_tree' : function(params) {
1596 var data = obj.data; data.stash_retrieve();
1597 if (!params.staff && data.list.au && data.list.au[0]) {
1598 params.staff = data.list.au[0];
1600 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() ]) {
1601 params.lib = JSON2js( js2JSON( data.hash.aou[ data.list.au[0].ws_ou() ] ) ); // clone this sucker
1602 params.lib.children(null); // since we're modifying it
1604 if (params.template && data.print_list_templates[ params.template ]) {
1605 var template = data.print_list_templates[ params.template ];
1606 if (template.inherit) {
1607 template = data.print_list_templates[ template.inherit ];
1608 // if someone wants to implement recursion later, feel free
1610 for (var i in template) params[i] = template[i];
1612 obj.wrap_in_full_retrieve(
1615 if (!params.list) params.list = obj.dump_with_keys();
1616 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1617 print.tree_list( params );
1618 if (typeof params.callback == 'function') params.callback();
1620 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1626 obj.error.standard_unexpected_error_alert('_print_tree',E);
1630 'dump_selected_with_keys' : function(params) {
1632 switch(this.node.nodeName) {
1633 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1634 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1639 '_dump_tree_selection_with_keys' : function(params) {
1642 var list = obj._retrieve_selection_from_tree();
1643 for (var i = 0; i < list.length; i++) {
1645 var treeitem = list[i];
1646 var treerow = treeitem.firstChild;
1647 for (var j = 0; j < treerow.childNodes.length; j++) {
1648 var value = treerow.childNodes[j].getAttribute('label');
1649 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1650 if (typeof obj.columns[j] == 'undefined') {
1651 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1652 dump('_dump_tree_selection_with_keys @ ' + location.href + '\n');
1653 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1656 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1665 'clipboard' : function(params) {
1668 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1669 obj.data.stash_retrieve();
1670 obj.data.list_clipboard = dump; obj.data.stash('list_clipboard');
1671 JSAN.use('util.window'); var win = new util.window();
1672 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1673 window.focus(); // sometimes the main window will lower after a clipboard action
1675 this.error.standard_unexpected_error_alert('clipboard',E);
1679 'dump_retrieve_ids' : function(params) {
1681 switch(this.node.nodeName) {
1682 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1683 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1687 '_dump_retrieve_ids_tree' : function(params) {
1690 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1691 var treeitem = this.treechildren.childNodes[i];
1692 dump.push( treeitem.getAttribute('retrieve_id') );
1697 'wrap_in_full_retrieve' : function(f) {
1699 if (typeof obj.on_all_fleshed == 'function') { // legacy
1700 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1702 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1703 obj.on_all_fleshed.push(f);
1704 obj.full_retrieve();
1707 '_sort_tree' : function() {
1710 if (obj.node.getAttribute('no_sort')) {
1714 var sorts = [ obj.first_sort ].concat( obj.sub_sorts );
1715 var columns = util.functional.map_list(
1721 var column_positions = [];
1722 for (var i = 0; i < columns.length; i++) {
1723 for (var j = 0; j < obj.columns.length; j++) {
1724 if (obj.columns[j].id == columns[i].id) {
1725 column_positions.push( function(a){return a;}(j) );
1729 obj.wrap_in_full_retrieve(
1732 JSAN.use('util.money');
1734 var treeitems = obj.treechildren.childNodes;
1735 for (var i = 0; i < treeitems.length; i++) {
1736 var treeitem = treeitems[i];
1737 var treerow = treeitem.firstChild;
1739 function get_value(treecell) {
1742 ? treecell.getAttribute('label')
1744 'sort_value' : treecell ? treecell.hasAttribute('sort_value')
1746 treecell.getAttribute('sort_value'))
1753 for (var j = 0; j < column_positions.length; j++) {
1754 var treecell = treerow.childNodes[ column_positions[j] ];
1756 'position' : column_positions[j],
1757 'value' : get_value(treecell)
1766 rows = rows.sort( function(A,B) {
1767 function normalize(a,b,p) {
1774 if (obj.columns[p].sort_type) {
1775 switch(obj.columns[p].sort_type) {
1777 JSAN.use('util.date'); // to pull in dojo.date.locale
1778 a = dojo.date.locale.parse(a,{});
1779 b = dojo.date.locale.parse(b,{});
1782 a = Number(a); b = Number(b);
1785 a = util.money.dollars_float_to_cents_integer(a);
1786 b = util.money.dollars_float_to_cents_integer(b);
1788 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1789 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1790 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1793 a = String( a ).toUpperCase();
1794 b = String( b ).toUpperCase();
1798 if (typeof a == 'string' || typeof b == 'string') {
1799 a = String( a ).toUpperCase();
1800 b = String( b ).toUpperCase();
1807 for (var i = 0; i < sorts.length; i++) {
1809 if (sorts[i].sortDir == 'asc') {
1811 A['values'][i]['value'],
1812 B['values'][i]['value'],
1813 A['values'][i]['position']
1817 B['values'][i]['value'],
1818 A['values'][i]['value'],
1819 A['values'][i]['position']
1822 if (values[0] < values[1] ) {
1825 if (values[0] > values[1] ) {
1831 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1832 for (var i = 0; i < rows.length; i++) {
1833 obj.treechildren.appendChild( rows[i].node );
1835 if (typeof obj.on_sort == 'function') obj.on_sort();
1837 obj.error.standard_unexpected_error_alert('sorting',E);
1839 obj.refresh_ordinals();
1843 obj.error.standard_unexpected_error_alert('pre sorting', E);
1847 '_toggle_checkbox_column' : function(col,toggle) {
1850 if (obj.node.getAttribute('no_toggle')) {
1854 for (var i = 0; i < obj.columns.length; i++) {
1855 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1857 var treeitems = obj.treechildren.childNodes;
1858 for (var i = 0; i < treeitems.length; i++) {
1859 var treeitem = treeitems[i];
1860 var treerow = treeitem.firstChild;
1861 var treecell = treerow.childNodes[ col_pos ];
1862 treecell.setAttribute('value',(toggle == 'on'));
1864 if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1866 obj.error.standard_unexpected_error_alert('pre toggle', E);
1870 'render_list_actions' : function(params) {
1872 switch(this.node.nodeName) {
1873 case 'tree' : return this._render_list_actions_for_tree(params); break;
1874 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1878 '_render_list_actions_for_tree' : function(params) {
1881 var btn = document.createElement('button');
1882 btn.setAttribute('id',obj.node.id + '_list_actions');
1883 btn.setAttribute('type','menu');
1884 btn.setAttribute('allowevents','true');
1885 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1886 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1887 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1888 var mp = document.createElement('menupopup');
1889 btn.appendChild(mp);
1890 var mi = document.createElement('menuitem');
1891 mi.setAttribute('id',obj.node.id + '_clipfield');
1892 mi.setAttribute('disabled','true');
1893 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1894 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1896 mi = document.createElement('menuitem');
1897 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1898 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1899 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1901 mi = document.createElement('menuitem');
1902 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1903 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1904 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1906 mi = document.createElement('menuitem');
1907 mi.setAttribute('id',obj.node.id + '_extended_to_printer');
1908 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.label'));
1909 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.accesskey'));
1911 mi = document.createElement('menuitem');
1912 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1913 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1914 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1916 mi = document.createElement('menuitem');
1917 mi.setAttribute('id',obj.node.id + '_save_columns');
1918 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1919 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1920 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
1921 mi.setAttribute('disabled','true');
1926 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1930 'set_list_actions' : function(params) {
1932 switch(this.node.nodeName) {
1933 case 'tree' : return this._set_list_actions_for_tree(params); break;
1934 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1938 '_set_list_actions_for_tree' : function(params) {
1939 // This should be called after the button element from render_list_actions has been appended to the DOM
1942 var x = document.getElementById(obj.node.id + '_clipfield');
1944 obj.event_listeners.add(
1948 obj.clipboard(params);
1949 if (params && typeof params.on_complete == 'function') {
1950 params.on_complete(params);
1956 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1958 obj.event_listeners.add(
1962 obj.dump_csv_to_clipboard(params);
1963 if (params && typeof params.on_complete == 'function') {
1964 params.on_complete(params);
1970 x = document.getElementById(obj.node.id + '_csv_to_printer');
1972 obj.event_listeners.add(
1976 obj.dump_csv_to_printer(params);
1977 if (params && typeof params.on_complete == 'function') {
1978 params.on_complete(params);
1984 x = document.getElementById(obj.node.id + '_extended_to_printer');
1986 obj.event_listeners.add(
1990 obj.dump_extended_format_to_printer(params);
1991 if (params && typeof params.on_complete == 'function') {
1992 params.on_complete(params);
1999 x = document.getElementById(obj.node.id + '_csv_to_file');
2001 obj.event_listeners.add(
2005 obj.dump_csv_to_file(params);
2006 if (params && typeof params.on_complete == 'function') {
2007 params.on_complete(params);
2013 x = document.getElementById(obj.node.id + '_save_columns');
2015 obj.event_listeners.add(
2019 obj.save_columns(params);
2020 if (params && typeof params.on_complete == 'function') {
2021 params.on_complete(params);
2029 obj.error.standard_unexpected_error_alert('setting list actions',E);
2033 // Takes fieldmapper class name and attempts to spit out column definitions suitable for .init
2034 'fm_columns' : function(hint,column_extras,prefix) {
2037 if (!prefix) { prefix = ''; }
2039 // requires the dojo library fieldmapper.autoIDL
2040 if (typeof fieldmapper == 'undefined') { throw 'fieldmapper undefined'; }
2041 if (typeof fieldmapper.IDL == 'undefined') { throw 'fieldmapper.IDL undefined'; }
2042 if (typeof fieldmapper.IDL.fmclasses == 'undefined') { throw 'fieldmapper.IDL.fmclasses undefined'; }
2043 if (typeof fieldmapper.IDL.fmclasses[hint] == 'undefined') { throw 'fieldmapper.IDL.fmclasses.' + hint + ' undefined'; }
2044 var my_class = fieldmapper.IDL.fmclasses[hint];
2045 var data = obj.data; data.stash_retrieve();
2047 function col_def(my_field) {
2048 var col_id = prefix + hint + '_' + my_field.name;
2050 var datafield = my_field.name;
2051 var fleshed_display_field;
2052 if (column_extras) {
2053 if (column_extras['*']) {
2054 if (column_extras['*']['dataobj']) {
2055 dataobj = column_extras['*']['dataobj'];
2058 if (column_extras[col_id]) {
2059 if (column_extras[col_id]['dataobj']) {
2060 dataobj = column_extras[col_id]['dataobj'];
2062 if (column_extras[col_id]['datafield']) {
2063 datafield = column_extras[col_id]['datafield'];
2065 if (column_extras[col_id]['fleshed_display_field']) {
2066 fleshed_display_field = column_extras[col_id]['fleshed_display_field'];
2072 'label' : my_field.label || my_field.name,
2073 'sort_type' : [ 'int', 'float', 'id', 'number' ].indexOf(my_field.datatype) > -1 ? 'number' :
2074 ( my_field.datatype == 'money' ? 'money' :
2075 ( my_field.datatype == 'timestamp' ? 'date' : 'default')),
2076 'hidden' : my_field.virtual || my_field.datatype == 'link',
2079 // my_field.datatype => bool float id int interval link money number org_unit text timestamp
2080 if (my_field.datatype == 'link') {
2081 def.render = function(my) {
2082 // is the object fleshed?
2083 return my[dataobj][datafield]() && typeof my[dataobj][datafield]() == 'object'
2084 // yes, show the display field
2085 ? my[dataobj][datafield]()[fleshed_display_field||my_field.key]()
2086 // no, do we have its class in data.hash?
2087 : ( typeof data.hash[ my[dataobj].Structure.field_map[datafield].class ] != 'undefined'
2088 // yes, do we have this particular object cached?
2089 ? ( data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ]
2090 // yes, show the display field
2091 ? data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ][
2092 fleshed_display_field||my_field.key
2094 // no, just show the raw value
2095 : my[dataobj][datafield]()
2097 // no, just show the raw value
2098 : my[dataobj][datafield]()
2102 def.render = function(my) { return my[dataobj][datafield](); }
2104 if (my_field.datatype == 'timestamp') {
2105 JSAN.use('util.date');
2106 def.render = function(my) {
2107 return util.date.formatted_date( my[dataobj][datafield](), '%{localized}' );
2109 def.sort_value = function(my) {
2110 return util.date.db_date2Date( my[dataobj][datafield]() ).getTime();
2113 if (my_field.datatype == 'org_unit') {
2114 def.render = function(my) {
2115 return typeof my[dataobj][datafield]() == 'object' ? my[dataobj][datafield]().shortname() : data.hash.aou[ my[dataobj][datafield]() ].shortname();
2118 if (my_field.datatype == 'money') {
2119 JSAN.use('util.money');
2120 def.render = function(my) {
2121 return util.money.sanitize( my[dataobj][datafield]() );
2123 def.sort_value = function(my) {
2124 return util.money.dollars_float_to_cents_integer( my[dataobj][datafield]() );
2127 if (column_extras) {
2128 if (column_extras['*']) {
2129 for (var attr in column_extras['*']) {
2130 def[attr] = column_extras['*'][attr];
2132 if (column_extras['*']['expanded_label']) {
2133 def.label = my_class.label + ': ' + def.label;
2135 if (column_extras['*']['label_prefix']) {
2136 def.label = column_extras['*']['label_prefix'] + def.label;
2138 if (column_extras['*']['remove_virtual']) {
2139 if (my_field.virtual) {
2140 def.remove_me = true;
2144 if (column_extras[col_id]) {
2145 for (var attr in column_extras[col_id]) {
2146 def[attr] = column_extras[col_id][attr];
2148 if (column_extras[col_id]['keep_me']) {
2149 def.remove_me = false;
2151 if (column_extras[col_id]['label_prefix']) {
2152 def.label = column_extras[col_id]['label_prefix'] + def.label;
2156 if (def.remove_me) {
2157 dump('Skipping ' + def.label + '\n');
2160 dump('Defining ' + def.label + '\n');
2165 for (var i = 0; i < my_class.fields.length; i++) {
2166 var my_field = my_class.fields[i];
2167 var def = col_def(my_field);
2169 columns.push( def );
2174 obj.error.standard_unexpected_error_alert('fm_columns()',E);
2178 // Default for the map_row_to_columns function for .init
2179 'std_map_row_to_columns' : function(error_value) {
2180 return function(row,cols,scratch) {
2181 // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
2182 // cols contains all of the objects listed above in columns
2183 // scratch is a temporary space shared by all cells/rows (or just per row if not explicitly passed in)
2184 if (!scratch) { scratch = {}; }
2187 JSAN.use('util.error'); obj.error = new util.error();
2188 JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
2189 JSAN.use('util.network'); obj.network = new util.network();
2190 JSAN.use('util.money');
2192 // FIXME: backwards compatability with server/patron code and the old patron.util.std_map_row_to_columns.
2193 // Will remove in a separate commit and change all instances of obj.OpenILS.data to obj.data at the same time.
2194 obj.OpenILS = { 'data' : obj.data };
2198 var sort_values = [];
2201 for (var i = 0; i < cols.length; i++) {
2202 switch (typeof cols[i].render) {
2203 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;
2204 case 'string' : cmd += 'try { ' + cols[i].render + '; values['+i+'] = v; } catch(E) { values['+i+'] = error_value; }'; break;
2205 default: cmd += 'values['+i+'] = "??? '+(typeof cols[i].render)+'"; ';
2207 switch (typeof cols[i].sort_value) {
2210 sort_values[i] = cols[i].sort_value(my,scratch);
2212 sort_values[i] = error_value;
2213 obj.error.sdump('D_COLUMN_RENDER_ERROR',E);
2217 sort_values[i] = JSON2js(cols[i].sort_value);
2220 cmd += 'sort_values['+i+'] = values[' + i + '];';
2223 if (cmd) eval( cmd );
2225 obj.error.sdump('D_WARN','map_row_to_column: ' + E);
2226 if (error_value) { value = error_value; } else { value = ' ' };
2228 return { 'values' : values, 'sort_values' : sort_values };
2232 dump('exiting util.list.js\n');