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 this.count_for_display = 1;
16 if (!this.node) throw('Could not find element ' + id);
17 switch(this.node.nodeName) {
21 throw(this.node.nodeName + ' not yet supported'); break;
22 default: throw(this.node.nodeName + ' not supported'); break;
25 JSAN.use('util.error'); this.error = new util.error();
27 JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.stash_retrieve();
29 JSAN.use('util.functional');
30 JSAN.use('util.widgets');
35 util.list.prototype = {
37 'init' : function (params) {
40 obj.scratch_data = {};
41 obj.event_listeners = new EventListenerList();
43 // If set, save and restore columns as if the tree/list id was the value of columns_saved_under
44 obj.columns_saved_under = params.columns_saved_under;
46 JSAN.use('util.widgets');
48 obj.printer_context = params.printer_context;
50 if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
51 if (typeof params.map_row_to_columns == 'function') {
52 obj.map_row_to_columns = params.map_row_to_columns;
54 obj.map_row_to_columns = obj.std_map_row_to_columns();
56 if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
59 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
61 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
65 'label' : document.getElementById('offlineStrings').getString('list.line_number'),
68 'properties' : 'ordinal', // column properties for css styling
71 'render' : function(my,scratch) {
72 // special code will handle this based on the attribute we set
73 // here. All cells for this column need to be updated whenever
74 // a list adds, removes, or sorts rows
79 for (var i = 0; i < params.columns.length; i++) {
80 if (typeof params.columns[i] == 'object') {
81 obj.columns.push( params.columns[i] );
83 var cols = obj.fm_columns( params.columns[i] );
84 for (var j = 0; j < cols.length; j++) {
85 obj.columns.push( cols[j] );
90 switch(obj.node.nodeName) {
91 case 'tree' : obj._init_tree(params); break;
92 case 'listbox' : obj._init_listbox(params); break;
93 default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
97 '_init_tree' : function (params) {
101 this.treechildren = this.node.lastChild;
104 var treecols = document.createElement('treecols');
105 this.node.appendChild(treecols);
106 this.treecols = treecols;
107 if (document.getElementById('column_sort_menu')) {
108 treecols.setAttribute('context','column_sort_menu');
111 var check_for_id_collisions = {};
112 for (var i = 0; i < this.columns.length; i++) {
113 var treecol = document.createElement('treecol');
114 for (var j in this.columns[i]) {
115 var value = this.columns[i][j];
117 if (typeof check_for_id_collisions[value] == 'undefined') {
118 check_for_id_collisions[value] = true;
120 // Column id's are important for sorting and saving list configuration. Collisions started happening because
121 // we were using field names as id's, and then later combining column definitions for multiple objects that
122 // shared field names. The downside to this sort of automatic collision prevention is that these generated
123 // id's can change as we add and remove columns, possibly breaking saved list configurations.
124 dump('Column collision with id = ' + value + ', renaming to ');
125 value = value + '_collision_' + i;
129 treecol.setAttribute(j,value);
131 treecols.appendChild(treecol);
133 if (this.columns[i].type == 'checkbox') {
134 obj.event_listeners.add(
140 var toggle = ev.target.getAttribute('toggleAll') || 'on';
141 if (toggle == 'off') toggle = 'on'; else toggle = 'off';
142 ev.target.setAttribute('toggleAll',toggle);
143 obj._toggle_checkbox_column(ev.target,toggle);
150 obj.event_listeners.add(
154 dump('sort_first_asc\n');
155 ev.target.setAttribute('sortDir','asc');
157 'target' : ev.target,
161 util.widgets.dispatch('sort',ev.target);
165 obj.event_listeners.add(
169 dump('sort_first_desc\n');
170 ev.target.setAttribute('sortDir','desc');
172 'target' : ev.target,
176 util.widgets.dispatch('sort',ev.target);
180 obj.event_listeners.add(
184 dump('sort_next_asc\n');
185 ev.target.setAttribute('sortDir','asc');
187 'target' : ev.target,
190 util.widgets.dispatch('sort',ev.target);
194 obj.event_listeners.add(
198 dump('sort_next_desc\n');
199 ev.target.setAttribute('sortDir','desc');
201 'target' : ev.target,
204 util.widgets.dispatch('sort',ev.target);
209 obj.event_listeners.add(
213 if (ev.button == 2 /* context menu click */ || ev.target.getAttribute('no_sort')) {
217 if (ev.ctrlKey) { // sub sort
222 ev.target.setAttribute('sortDir',sortDir);
224 'target' : ev.target,
227 } else { // first sort
228 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
229 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
233 ev.target.setAttribute('sortDir',sortDir);
235 'target' : ev.target,
240 util.widgets.dispatch('sort',ev.target);
245 obj.event_listeners.add(
249 if (!obj.first_sort) {
257 if (obj.row_count.total != obj.row_count.fleshed
258 && (obj.row_count.total - obj.row_count.fleshed) > 50
260 var r = window.confirm(
261 document.getElementById('offlineStrings').getFormattedString(
262 'list.row_fetch_warning',
263 [obj.row_count.fleshed,obj.row_count.total]
268 setTimeout( do_it, 0 );
272 setTimeout( do_it, 0 );
279 var splitter = document.createElement('splitter');
280 splitter.setAttribute('class','tree-splitter');
281 treecols.appendChild(splitter);
284 var treechildren = document.createElement('treechildren');
285 this.node.appendChild(treechildren);
286 this.treechildren = treechildren;
288 if (typeof params.on_sort == 'function') {
289 this.on_sort = params.on_sort;
291 if (typeof params.on_checkbox_toggle == 'function') {
292 this.on_checkbox_toggle = params.on_checkbox_toggle;
294 obj.event_listeners.add(
298 if (typeof params.on_select == 'function') {
299 params.on_select(ev);
301 var x = document.getElementById(obj.node.id + '_clipfield');
303 var sel = obj.retrieve_selection();
304 x.setAttribute('disabled', sel.length == 0);
309 if (typeof params.on_click == 'function') {
310 obj.event_listeners.add(
317 if (typeof params.on_dblclick == 'function') {
318 obj.event_listeners.add(
327 obj.event_listeners.add(
330 function(ev) { obj.detect_visible(); },
334 obj.event_listeners.add(
337 function(ev) { obj.auto_retrieve(); },
340 obj.event_listeners.add(
343 function(ev) { obj.auto_retrieve(); },
346 obj.event_listeners.add(
349 function(ev) { obj.auto_retrieve(); },
352 /* FIXME -- find events on scrollbar to trigger this */
353 obj.detect_visible_polling();
355 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
356 var slider = document.getAnonymousNodes( scrollbar )[2];
357 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
358 scrollbar.addEventListener('click',function(){alert('sb click');},false);
359 scrollbar.addEventListener('command',function(){alert('sb command');},false);
360 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
361 slider.addEventListener('click',function(){alert('slider click');},false);
362 slider.addEventListener('command',function(){alert('slider command');},false);
363 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
365 obj.event_listeners.add(this.node, 'scroll',function(){ obj.auto_retrieve(); },false);
367 this.restores_columns(params);
370 '_init_listbox' : function (params) {
373 var listhead = document.createElement('listhead');
374 this.node.appendChild(listhead);
376 var listcols = document.createElement('listcols');
377 this.node.appendChild(listcols);
379 for (var i = 0; i < this.columns.length; i++) {
380 var listheader = document.createElement('listheader');
381 listhead.appendChild(listheader);
382 var listcol = document.createElement('listcol');
383 listcols.appendChild(listcol);
384 for (var j in this.columns[i]) {
385 listheader.setAttribute(j,this.columns[i][j]);
386 listcol.setAttribute(j,this.columns[i][j]);
392 'cleanup' : function () {
394 obj.event_listeners.removeAll();
397 'save_columns' : function (params) {
399 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
400 alert(document.getElementById('offlineStrings').getString('list.column_save_disabled'));
402 switch (this.node.nodeName) {
403 case 'tree' : this._save_columns_tree(params); break;
404 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
409 '_save_columns_tree' : function (params) {
412 var id = obj.node.getAttribute('id');
413 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
415 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
419 var nl = obj.node.getElementsByTagName('treecol');
420 for (var i = 0; i < nl.length; i++) {
422 var col_id = col.getAttribute('id');
424 alert('FIXME: A column in this list does not have an id and cannot be saved');
427 var col_hidden = col.getAttribute('hidden');
428 var col_width = col.getAttribute('width');
429 var col_ordinal = col.getAttribute('ordinal');
430 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
432 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.encodeURIComponent(id));
433 file.set_object(my_cols);
435 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
437 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
441 'restores_columns' : function (params) {
443 switch (this.node.nodeName) {
444 case 'tree' : this._restores_columns_tree(params); break;
445 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
449 '_restores_columns_tree' : function (params) {
452 var id = obj.node.getAttribute('id');
453 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
455 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
460 if (! obj.data.hash.aous) { obj.data.hash.aous = {}; }
461 if (! obj.data.hash.aous['gui.disable_local_save_columns']) {
462 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.encodeURIComponent(id));
463 if (file._file.exists()) {
464 my_cols = file.get_object(); file.close();
467 /* local file will trump remote file if allowed, so save ourselves an http request if this is the case */
468 if (obj.data.hash.aous['url.remote_column_settings'] && ! my_cols ) {
470 var x = new XMLHttpRequest();
471 var url = obj.data.hash.aous['url.remote_column_settings'] + '/tree_columns_for_' + window.encodeURIComponent(id);
472 x.open("GET", url, false);
474 if (x.status == 200) {
475 my_cols = JSON2js( x.responseText );
478 // This can happen in the offline interface if you logged in previously and url.remote_column_settings is set.
479 // 1) You may be really "offline" now
480 // 2) the URL may just be a path component without a hostname (ie "/xul/column_settings/"), which won't work
481 // when appended to chrome://open_ils_staff_client/
482 dump('Error retrieving column settings from ' + url + ': ' + E + '\n');
487 var nl = obj.node.getElementsByTagName('treecol');
488 for (var i = 0; i < nl.length; i++) {
490 var col_id = col.getAttribute('id');
492 alert('FIXME: A column in this list does not have an id and cannot be saved');
495 if (typeof my_cols[col_id] != 'undefined') {
496 col.setAttribute('hidden',my_cols[col_id].hidden);
497 col.setAttribute('width',my_cols[col_id].width);
498 col.setAttribute('ordinal',my_cols[col_id].ordinal);
500 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
505 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
509 'clear' : function (params) {
511 switch (this.node.nodeName) {
512 case 'tree' : this._clear_tree(params); break;
513 case 'listbox' : this._clear_listbox(params); break;
514 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
516 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
517 this.row_count.total = 0;
518 this.row_count.fleshed = 0;
519 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
522 '_clear_tree' : function(params) {
524 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
525 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
527 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
528 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
530 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
533 '_clear_listbox' : function(params) {
536 var nl = this.node.getElementsByTagName('listitem');
537 for (var i = 0; i < nl.length; i++) {
540 for (var i = 0; i < items.length; i++) {
541 this.node.removeChild(items[i]);
545 'append' : function (params) {
548 switch (this.node.nodeName) {
549 case 'tree' : rparams = this._append_to_tree(params); break;
550 case 'listbox' : rparams = this._append_to_listbox(params); break;
551 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
553 if (rparams && params.attributes) {
554 for (var i in params.attributes) {
555 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
558 this.row_count.total++;
559 if (this.row_count.fleshed == this.row_count.total) {
560 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
562 rparams.treeitem_node.setAttribute('unique_row_counter',obj.unique_row_counter);
563 rparams.unique_row_counter = obj.unique_row_counter++;
564 if (typeof params.on_append == 'function') {
565 params.on_append(rparams);
570 'refresh_row' : function (params) {
573 switch (this.node.nodeName) {
574 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
575 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
577 if (rparams && params.attributes) {
578 for (var i in params.attributes) {
579 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
582 this.row_count.fleshed--;
587 '_append_to_tree' : function (params) {
591 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
593 var s = ('util.list.append: params = ' + (params) + '\n');
595 var treechildren_node = this.treechildren;
597 if (params.node && params.node.nodeName == 'treeitem') {
598 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
599 if (params.node.lastChild.nodeName == 'treechildren') {
600 treechildren_node = params.node.lastChild;
602 treechildren_node = document.createElement('treechildren');
603 params.node.appendChild(treechildren_node);
607 var treeitem = document.createElement('treeitem');
608 treeitem.setAttribute('retrieve_id',params.retrieve_id);
609 if (typeof params.to_bottom != 'undefined') {
610 treechildren_node.appendChild( treeitem );
611 if (typeof params.no_auto_select == 'undefined') {
612 if (!obj.auto_select_pending) {
613 obj.auto_select_pending = true;
614 setTimeout(function() {
615 dump('auto-selecting\n');
616 var idx = Number(obj.node.view.rowCount)-1;
617 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
618 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
619 obj.auto_select_pending = false;
620 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
625 if (treechildren_node.firstChild) {
626 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
628 treechildren_node.appendChild( treeitem );
630 if (typeof params.no_auto_select == 'undefined') {
631 if (!obj.auto_select_pending) {
632 obj.auto_select_pending = true;
633 setTimeout(function() {
634 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
635 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
636 obj.auto_select_pending = false;
637 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
642 var treerow = document.createElement('treerow');
643 treeitem.appendChild( treerow );
644 treerow.setAttribute('retrieve_id',params.retrieve_id);
645 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
647 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
648 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
650 obj.put_retrieving_label(treerow);
652 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
653 obj.event_listeners.add(
658 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
660 treerow.setAttribute('retrieved','true');
662 //dump('fleshing = ' + params.retrieve_id + '\n');
664 function inc_fleshed() {
665 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
666 treerow.setAttribute('fleshed','true');
667 obj.row_count.fleshed++;
668 if (obj.row_count.fleshed >= obj.row_count.total) {
669 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
673 params.treeitem_node = treeitem;
674 params.on_retrieve = function(p) {
677 obj._map_row_to_treecell(p,treerow);
679 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
680 dump('idx = ' + idx + '\n');
681 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
682 if ( obj.node.view.selection.isSelected( idx ) ) {
683 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
684 util.widgets.dispatch('select',obj.node);
687 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
688 dump('fixme2: ' + E + '\n');
692 if (typeof params.retrieve_row == 'function') {
694 params.retrieve_row( params );
696 } else if (typeof obj.retrieve_row == 'function') {
698 obj.retrieve_row( params );
704 obj.refresh_ordinals();
708 if (typeof params.flesh_immediately != 'undefined') {
709 if (params.flesh_immediately) {
712 util.widgets.dispatch('flesh',treerow);
718 obj.event_listeners.add(
722 //dump('fleshing anon\n');
723 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
724 obj._map_row_to_treecell(params,treerow);
725 treerow.setAttribute('retrieved','true');
726 treerow.setAttribute('fleshed','true');
727 obj.row_count.fleshed++;
728 if (obj.row_count.fleshed >= obj.row_count.total) {
729 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
731 obj.refresh_ordinals();
735 if (typeof params.flesh_immediately != 'undefined') {
736 if (params.flesh_immediately) {
739 util.widgets.dispatch('flesh',treerow);
745 this.error.sdump('D_LIST',s);
749 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
751 //if (typeof params.to_bottom != 'undefined')
752 if (typeof params.to_top == 'undefined') {
753 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.firstChild.getAttribute('unique_row_counter') ); }
754 treechildren_node.removeChild( treechildren_node.firstChild );
756 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.lastChild.getAttribute('unique_row_counter') ); }
757 treechildren_node.removeChild( treechildren_node.lastChild );
763 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
765 params.treeitem_node = treeitem;
769 '_refresh_row_in_tree' : function (params) {
773 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
774 if (typeof params.treeitem_node == 'undefined') throw('util.list.refresh_row: Object must contain a treeitem_node');
775 if (params.treeitem_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: treeitem_node must be a treeitem');
777 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
779 var treeitem = params.treeitem_node;
780 treeitem.setAttribute('retrieve_id',params.retrieve_id);
781 if (typeof params.to_bottom != 'undefined') {
782 if (typeof params.no_auto_select == 'undefined') {
783 if (!obj.auto_select_pending) {
784 obj.auto_select_pending = true;
785 setTimeout(function() {
786 dump('auto-selecting\n');
787 var idx = Number(obj.node.view.rowCount)-1;
788 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
789 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
790 obj.auto_select_pending = false;
791 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
796 //var delete_me = [];
797 //for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
798 //for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
799 var prev_treerow = treeitem.firstChild; /* FIXME: worry about hierarchal lists like copy_browser? */
800 var treerow = document.createElement('treerow');
801 while (prev_treerow.firstChild) {
802 treerow.appendChild( prev_treerow.removeChild( prev_treerow.firstChild ) );
804 treeitem.replaceChild( treerow, prev_treerow );
805 treerow.setAttribute('retrieve_id',params.retrieve_id);
806 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
808 s += ('tree = ' + this.node.nodeName + '\n');
809 s += ('treeitem = ' + treeitem.nodeName + ' treerow = ' + treerow.nodeName + '\n');
811 obj.put_retrieving_label(treerow);
813 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
815 s += 'found a retrieve_row function\n';
817 obj.event_listeners.add(
822 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
824 treerow.setAttribute('retrieved','true');
826 //dump('fleshing = ' + params.retrieve_id + '\n');
828 function inc_fleshed() {
829 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
830 treerow.setAttribute('fleshed','true');
831 obj.row_count.fleshed++;
832 if (obj.row_count.fleshed >= obj.row_count.total) {
833 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
837 params.treeitem_node = treeitem;
838 params.on_retrieve = function(p) {
841 obj._map_row_to_treecell(p,treerow);
843 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
844 dump('idx = ' + idx + '\n');
845 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
846 if ( obj.node.view.selection.isSelected( idx ) ) {
847 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
848 util.widgets.dispatch('select',obj.node);
851 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
852 dump('fixme2: ' + E + '\n');
856 if (typeof params.retrieve_row == 'function') {
858 params.retrieve_row( params );
860 } else if (typeof obj.retrieve_row == 'function') {
862 obj.retrieve_row( params );
868 obj.refresh_ordinals();
872 if (typeof params.flesh_immediately != 'undefined') {
873 if (params.flesh_immediately) {
876 util.widgets.dispatch('flesh',treerow);
884 s += 'did not find a retrieve_row function\n';
886 obj.event_listeners.add(
890 //dump('fleshing anon\n');
891 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
892 obj._map_row_to_treecell(params,treerow);
893 treerow.setAttribute('retrieved','true');
894 treerow.setAttribute('fleshed','true');
895 obj.row_count.fleshed++;
896 if (obj.row_count.fleshed >= obj.row_count.total) {
897 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
899 obj.refresh_ordinals();
903 if (typeof params.flesh_immediately != 'undefined') {
904 if (params.flesh_immediately) {
907 util.widgets.dispatch('flesh',treerow);
917 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
919 //if (typeof params.to_bottom != 'undefined')
920 if (typeof params.to_top == 'undefined') {
921 treechildren_node.removeChild( treechildren_node.firstChild );
923 treechildren_node.removeChild( treechildren_node.lastChild );
929 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
931 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
933 this.error.sdump('D_LIST',s);
938 'refresh_ordinals' : function() {
941 if (obj.refresh_ordinals_timeout_id) { return; }
943 function _refresh_ordinals(clear) {
944 var nl = obj.node.getElementsByAttribute('label','_');
945 for (var i = 0; i < nl.length; i++) {
950 nl[i].setAttribute( // treecell properties for css styling
955 nl = obj.node.getElementsByAttribute('ord_col','true');
956 for (var i = 0; i < nl.length; i++) {
959 // we could just use 'i' here if we trust the order of elements
960 1 + obj.node.contentView.getIndexOfItem(nl[i].parentNode.parentNode) // treeitem
963 if (clear) { obj.refresh_ordinals_timeout_id = null; }
966 // spamming this to cover race conditions
967 setTimeout(_refresh_ordinals, 500); // for speedy looking UI updates
968 setTimeout(_refresh_ordinals, 2000); // for most uses
969 obj.refresh_ordinals_timeout_id = setTimeout(
971 _refresh_ordinals(true);
973 4000 // just in case, say with a slow rendering list
977 alert('Error in list.js, refresh_ordinals(): ' + E);
981 'put_retrieving_label' : function(treerow) {
984 for (var i = 0; i < obj.columns.length; i++) {
986 if (typeof treerow.childNodes[i] == 'undefined') {
987 treecell = document.createElement('treecell');
988 treerow.appendChild(treecell);
990 treecell = treerow.childNodes[i];
992 treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
995 alert('Error in list.js, put_retrieving_label(): ' + E);
999 'detect_visible' : function() {
1002 //dump('detect_visible obj.node = ' + obj.node + '\n');
1003 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
1005 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
1008 s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n';
1009 if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
1010 var C = B.childNodes[j];
1011 s += '\t\t' + C.nodeName + '\n';
1014 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
1015 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
1016 var curpos = scrollbar.getAttribute('curpos');
1017 var maxpos = scrollbar.getAttribute('maxpos');
1018 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
1019 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
1020 if ( obj.auto_retrieve() > 0 ) {
1021 obj.curpos = curpos; obj.maxpos = maxpos;
1025 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
1027 } catch(E) { obj.error.sdump('D_ERROR',E); }
1030 'detect_visible_polling' : function() {
1032 //alert('detect_visible_polling');
1034 obj.detect_visible();
1035 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
1042 'auto_retrieve' : function(params) {
1044 switch (this.node.nodeName) {
1045 case 'tree' : obj._auto_retrieve_tree(params); break;
1046 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
1050 '_auto_retrieve_tree' : function (params) {
1052 if (!obj.auto_retrieve_in_progress) {
1053 obj.auto_retrieve_in_progress = true;
1057 //alert('auto_retrieve\n');
1059 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
1060 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
1061 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
1062 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
1063 for (var i = startpos; i < endpos + 4; i++) {
1065 //dump('trying index ' + i + '\n');
1066 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
1067 if (item && item.getAttribute('retrieved') != 'true' ) {
1068 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
1069 util.widgets.dispatch('flesh',item); count++;
1072 //dump(i + ' : ' + E + '\n');
1075 obj.auto_retrieve_in_progress = false;
1077 } catch(E) { alert(E); }
1083 'exec_on_all_fleshed' : function() {
1086 if (obj.on_all_fleshed) {
1087 if (typeof obj.on_all_fleshed == 'function') {
1088 dump('exec_on_all_fleshed == function\n');
1091 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
1094 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
1095 dump('exec_on_all_fleshed == array\n');
1099 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
1100 var f = obj.on_all_fleshed.pop();
1101 if (typeof f == 'function') {
1102 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',E); }
1104 if (obj.on_all_fleshed.length > 0) arguments.callee();
1106 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
1111 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
1115 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
1119 'full_retrieve' : function(params) {
1121 switch (this.node.nodeName) {
1122 case 'tree' : obj._full_retrieve_tree(params); break;
1123 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
1125 obj.refresh_ordinals();
1128 '_full_retrieve_tree' : function(params) {
1131 if (obj.row_count.fleshed >= obj.row_count.total) {
1132 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
1133 obj.exec_on_all_fleshed();
1135 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
1136 JSAN.use('util.widgets');
1137 var nodes = obj.treechildren.childNodes;
1138 for (var i = 0; i < nodes.length; i++) {
1139 util.widgets.dispatch('flesh',nodes[i].firstChild);
1143 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
1147 '_append_to_listbox' : function (params) {
1151 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
1153 var s = ('util.list.append: params = ' + (params) + '\n');
1155 var listitem = document.createElement('listitem');
1157 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
1159 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
1163 listitem.setAttribute('retrieve_id',params.retrieve_id);
1164 //FIXME//Make async and fire when row is visible in list
1167 params.treeitem_node = listitem;
1168 params.on_retrieve = function(row) {
1170 obj._map_row_to_listcell(params,listitem);
1171 obj.node.appendChild( listitem );
1172 util.widgets.dispatch('select',obj.node);
1175 if (typeof params.retrieve_row == 'function') {
1177 row = params.retrieve_row( params );
1181 if (typeof obj.retrieve_row == 'function') {
1183 row = obj.retrieve_row( params );
1190 this._map_row_to_listcell(params,listitem);
1191 this.node.appendChild( listitem );
1194 this.error.sdump('D_LIST',s);
1195 params.treeitem_node = listitem;
1200 '_map_row_to_treecell' : function(params,treerow) {
1204 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
1206 for (var i = 0; i < this.columns.length; i++) {
1208 if (typeof treerow.childNodes[i] == 'undefined') {
1209 treecell = document.createElement('treecell');
1210 treerow.appendChild( treecell );
1212 treecell = treerow.childNodes[i];
1215 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1217 var sort_value = '';
1219 // What skip columns is doing is rendering the treecells as blank/empty
1220 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
1221 treecell.setAttribute('label',label);
1222 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1225 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
1226 treecell.setAttribute('label',label);
1227 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1231 if (typeof params.map_row_to_column == 'function') {
1232 if (this.columns[i].id == 'lineno'){
1234 label = this.count_for_display.toString();
1235 this.count_for_display++;
1239 label = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1242 } else if (typeof this.map_row_to_column == 'function') {
1243 if (this.columns[i].id == 'lineno'){
1245 label = this.count_for_display.toString();
1246 this.count_for_display++;
1250 label = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1254 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
1255 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1257 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
1260 var sort_values = [];
1262 if (typeof params.map_row_to_columns == 'function') {
1264 var values = params.map_row_to_columns(params.row,this.columns,this.scratch_data);
1265 if (typeof values.values == 'undefined') {
1268 labels = values.values;
1269 sort_values = values.sort_values;
1272 } else if (typeof this.map_row_to_columns == 'function') {
1274 var values = this.map_row_to_columns(params.row,this.columns,this.scratch_data);
1275 if (typeof values.values == 'undefined') {
1278 labels = values.values;
1279 sort_values = values.sort_values;
1282 for (var i = 0; i < labels.length; i++) {
1284 if (typeof treerow.childNodes[i] == 'undefined') {
1285 treecell = document.createElement('treecell');
1286 treerow.appendChild(treecell);
1288 treecell = treerow.childNodes[i];
1290 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1291 if ( this.columns[i].type == 'checkbox') {
1292 treecell.setAttribute('value', labels[i]);
1294 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
1296 if (sort_values[i]) {
1297 treecell.setAttribute('sort_value',js2JSON(sort_values[i]));
1299 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
1304 throw('No row to column mapping function.');
1306 this.error.sdump('D_LIST',s);
1309 '_map_row_to_listcell' : function(params,listitem) {
1312 for (var i = 0; i < this.columns.length; i++) {
1314 if (typeof params.map_row_to_column == 'function') {
1316 value = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1320 if (typeof this.map_row_to_column == 'function') {
1322 value = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1325 if (typeof value == 'string' || typeof value == 'number') {
1326 var listcell = document.createElement('listcell');
1327 listcell.setAttribute('label',value);
1328 listitem.appendChild(listcell);
1329 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1331 listitem.appendChild(value);
1332 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1335 this.error.sdump('D_LIST',s);
1338 'select_all' : function(params) {
1340 switch(this.node.nodeName) {
1341 case 'tree' : return this._select_all_from_tree(params); break;
1342 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1346 '_select_all_from_tree' : function(params) {
1348 this.node.view.selection.selectAll();
1351 'retrieve_selection' : function(params) {
1353 switch(this.node.nodeName) {
1354 case 'tree' : return this._retrieve_selection_from_tree(params); break;
1355 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1359 '_retrieve_selection_from_tree' : function(params) {
1362 var start = new Object();
1363 var end = new Object();
1364 var numRanges = this.node.view.selection.getRangeCount();
1365 for (var t=0; t<numRanges; t++){
1366 this.node.view.selection.getRangeAt(t,start,end);
1367 for (var v=start.value; v<=end.value; v++){
1368 var i = this.node.contentView.getItemAtIndex(v);
1375 'dump' : function(params) {
1377 switch(this.node.nodeName) {
1378 case 'tree' : return this._dump_tree(params); break;
1379 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1383 '_dump_tree' : function(params) {
1386 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1388 var treeitem = this.treechildren.childNodes[i];
1389 var treerow = treeitem.firstChild;
1390 for (var j = 0; j < treerow.childNodes.length; j++) {
1391 row.push( treerow.childNodes[j].getAttribute('label') );
1398 'dump_with_keys' : function(params) {
1400 switch(this.node.nodeName) {
1401 case 'tree' : return this._dump_tree_with_keys(params); break;
1402 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1407 '_dump_tree_with_keys' : function(params) {
1411 function process_tree(treechildren) {
1412 for (var i = 0; i < treechildren.childNodes.length; i++) {
1414 var treeitem = treechildren.childNodes[i];
1415 var treerow = treeitem.firstChild;
1416 for (var j = 0; j < treerow.childNodes.length; j++) {
1417 if (typeof obj.columns[j] == 'undefined') {
1418 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1419 dump('_dump_tree_with_keys @ ' + location.href + '\n');
1420 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1423 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1424 var sort = treerow.childNodes[j].getAttribute('sort_value');
1426 row[ obj.columns[j].id + '_sort_value' ] = sort;
1431 if (treeitem.childNodes.length > 1) {
1432 process_tree(treeitem.lastChild);
1437 process_tree(this.treechildren);
1442 'dump_csv' : function(params) {
1444 switch(this.node.nodeName) {
1445 case 'tree' : return this._dump_tree_csv(params); break;
1446 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1451 '_dump_tree_csv' : function(params) {
1455 for (var j = 0; j < obj.columns.length; j++) {
1456 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1459 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1462 ord_cols.sort( function(a,b) {
1463 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1464 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1467 for (var j = 0; j < ord_cols.length; j++) {
1468 if (_dump) _dump += ',';
1469 _dump += '"' + obj.columns[ ord_cols[j][1] ].label.replace(/"/g, '""') + '"';
1473 function process_tree(treechildren) {
1474 for (var i = 0; i < treechildren.childNodes.length; i++) {
1476 var treeitem = treechildren.childNodes[i];
1477 var treerow = treeitem.firstChild;
1478 for (var j = 0; j < ord_cols.length; j++) {
1479 if (row) row += ',';
1480 row += '"' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label').replace(/"/g, '""') + '"';
1482 _dump += row + '\r\n';
1483 if (treeitem.childNodes.length > 1) {
1484 process_tree(treeitem.lastChild);
1489 process_tree(this.treechildren);
1494 'dump_extended_format' : function(params) {
1496 switch(this.node.nodeName) {
1497 case 'tree' : return this._dump_tree_extended_format(params); break;
1498 default: throw('NYI: Need .dump_extended_format() for ' + this.node.nodeName); break;
1503 '_dump_tree_extended_format' : function(params) {
1507 for (var j = 0; j < obj.columns.length; j++) {
1508 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1511 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1514 ord_cols.sort( function(a,b) {
1515 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1516 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1520 function process_tree(treechildren) {
1521 for (var i = 0; i < treechildren.childNodes.length; i++) {
1522 var row = document.getElementById('offlineStrings').getString('list.dump_extended_format.record_separator') + '\r\n';
1523 var treeitem = treechildren.childNodes[i];
1524 var treerow = treeitem.firstChild;
1525 for (var j = 0; j < ord_cols.length; j++) {
1526 row += obj.columns[ ord_cols[j][1] ].label + ': ' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label') + '\r\n';
1528 _dump += row + '\r\n';
1529 if (treeitem.childNodes.length > 1) {
1530 process_tree(treeitem.lastChild);
1535 process_tree(this.treechildren);
1540 'dump_csv_to_clipboard' : function(params) {
1542 if (typeof params == 'undefined') params = {};
1543 if (params.no_full_retrieve) {
1544 copy_to_clipboard( obj.dump_csv( params ) );
1546 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1550 'dump_csv_to_printer' : function(params) {
1552 if (typeof params == 'undefined') params = {};
1553 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1554 if (params.no_full_retrieve) {
1555 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1557 obj.wrap_in_full_retrieve(
1559 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1565 'dump_extended_format_to_printer' : function(params) {
1567 if (typeof params == 'undefined') params = {};
1568 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1569 if (params.no_full_retrieve) {
1570 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1572 obj.wrap_in_full_retrieve(
1574 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1580 'dump_csv_to_file' : function(params) {
1582 JSAN.use('util.file'); var f = new util.file();
1583 if (typeof params == 'undefined') params = {};
1584 if (params.no_full_retrieve) {
1585 params.data = obj.dump_csv( params );
1586 params.not_json = true;
1587 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1588 f.export_file( params );
1590 obj.wrap_in_full_retrieve(
1592 params.data = obj.dump_csv( params );
1593 params.not_json = true;
1594 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1595 f.export_file( params );
1601 'print' : function(params) {
1602 if (!params) params = {};
1603 switch(this.node.nodeName) {
1604 case 'tree' : return this._print_tree(params); break;
1605 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1609 '_print_tree' : function(params) {
1612 var data = obj.data; data.stash_retrieve();
1613 if (!params.staff && data.list.au && data.list.au[0]) {
1614 params.staff = data.list.au[0];
1616 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() ]) {
1617 params.lib = JSON2js( js2JSON( data.hash.aou[ data.list.au[0].ws_ou() ] ) ); // clone this sucker
1618 params.lib.children(null); // since we're modifying it
1620 if (params.template && data.print_list_templates[ params.template ]) {
1621 var template = data.print_list_templates[ params.template ];
1622 if (template.inherit) {
1623 template = data.print_list_templates[ template.inherit ];
1624 // if someone wants to implement recursion later, feel free
1626 for (var i in template) params[i] = template[i];
1628 obj.wrap_in_full_retrieve(
1631 if (!params.list) params.list = obj.dump_with_keys();
1632 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1633 print.tree_list( params );
1634 if (typeof params.callback == 'function') params.callback();
1636 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1642 obj.error.standard_unexpected_error_alert('_print_tree',E);
1646 'dump_selected_with_keys' : function(params) {
1648 switch(this.node.nodeName) {
1649 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1650 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1655 '_dump_tree_selection_with_keys' : function(params) {
1658 var list = obj._retrieve_selection_from_tree();
1659 for (var i = 0; i < list.length; i++) {
1661 var treeitem = list[i];
1662 var treerow = treeitem.firstChild;
1663 for (var j = 0; j < treerow.childNodes.length; j++) {
1664 var value = treerow.childNodes[j].getAttribute('label');
1665 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1666 if (typeof obj.columns[j] == 'undefined') {
1667 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1668 dump('_dump_tree_selection_with_keys @ ' + location.href + '\n');
1669 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1672 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1681 'clipboard' : function(params) {
1684 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1685 obj.data.stash_retrieve();
1686 obj.data.list_clipboard = dump; obj.data.stash('list_clipboard');
1687 JSAN.use('util.window'); var win = new util.window();
1688 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1689 window.focus(); // sometimes the main window will lower after a clipboard action
1691 this.error.standard_unexpected_error_alert('clipboard',E);
1695 'dump_retrieve_ids' : function(params) {
1697 switch(this.node.nodeName) {
1698 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1699 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1703 '_dump_retrieve_ids_tree' : function(params) {
1706 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1707 var treeitem = this.treechildren.childNodes[i];
1708 dump.push( treeitem.getAttribute('retrieve_id') );
1713 'wrap_in_full_retrieve' : function(f) {
1715 if (typeof obj.on_all_fleshed == 'function') { // legacy
1716 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1718 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1719 obj.on_all_fleshed.push(f);
1720 obj.full_retrieve();
1723 '_sort_tree' : function() {
1726 if (obj.node.getAttribute('no_sort')) {
1730 var sorts = [ obj.first_sort ].concat( obj.sub_sorts );
1731 var columns = util.functional.map_list(
1737 var column_positions = [];
1738 for (var i = 0; i < columns.length; i++) {
1739 for (var j = 0; j < obj.columns.length; j++) {
1740 if (obj.columns[j].id == columns[i].id) {
1741 column_positions.push( function(a){return a;}(j) );
1745 obj.wrap_in_full_retrieve(
1748 JSAN.use('util.money');
1750 var treeitems = obj.treechildren.childNodes;
1751 for (var i = 0; i < treeitems.length; i++) {
1752 var treeitem = treeitems[i];
1753 var treerow = treeitem.firstChild;
1755 function get_value(treecell) {
1758 ? treecell.getAttribute('label')
1760 'sort_value' : treecell ? treecell.hasAttribute('sort_value')
1762 treecell.getAttribute('sort_value'))
1769 for (var j = 0; j < column_positions.length; j++) {
1770 var treecell = treerow.childNodes[ column_positions[j] ];
1772 'position' : column_positions[j],
1773 'value' : get_value(treecell)
1782 rows = rows.sort( function(A,B) {
1783 function normalize(a,b,p) {
1788 // ensure that numeric values have not
1789 // morphed into strings along the way.
1790 if (obj.columns[p].sort_type == 'money' ||
1791 obj.columns[p].sort_type == 'number') {
1798 if (obj.columns[p].sort_type) {
1799 switch(obj.columns[p].sort_type) {
1801 JSAN.use('util.date'); // to pull in dojo.date.locale
1802 a = dojo.date.locale.parse(a,{});
1803 b = dojo.date.locale.parse(b,{});
1806 a = Number(a); b = Number(b);
1809 a = util.money.dollars_float_to_cents_integer(a);
1810 b = util.money.dollars_float_to_cents_integer(b);
1812 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1813 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1814 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1817 a = String( a ).toUpperCase();
1818 b = String( b ).toUpperCase();
1822 if (typeof a == 'string' || typeof b == 'string') {
1823 a = String( a ).toUpperCase();
1824 b = String( b ).toUpperCase();
1831 for (var i = 0; i < sorts.length; i++) {
1833 if (sorts[i].sortDir == 'asc') {
1835 A['values'][i]['value'],
1836 B['values'][i]['value'],
1837 A['values'][i]['position']
1841 B['values'][i]['value'],
1842 A['values'][i]['value'],
1843 A['values'][i]['position']
1846 if (values[0] < values[1] ) {
1849 if (values[0] > values[1] ) {
1855 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1856 for (var i = 0; i < rows.length; i++) {
1857 obj.treechildren.appendChild( rows[i].node );
1859 if (typeof obj.on_sort == 'function') obj.on_sort();
1861 obj.error.standard_unexpected_error_alert('sorting',E);
1863 obj.refresh_ordinals();
1867 obj.error.standard_unexpected_error_alert('pre sorting', E);
1871 '_toggle_checkbox_column' : function(col,toggle) {
1874 if (obj.node.getAttribute('no_toggle')) {
1878 for (var i = 0; i < obj.columns.length; i++) {
1879 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1881 var treeitems = obj.treechildren.childNodes;
1882 for (var i = 0; i < treeitems.length; i++) {
1883 var treeitem = treeitems[i];
1884 var treerow = treeitem.firstChild;
1885 var treecell = treerow.childNodes[ col_pos ];
1886 treecell.setAttribute('value',(toggle == 'on'));
1888 if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1890 obj.error.standard_unexpected_error_alert('pre toggle', E);
1894 'render_list_actions' : function(params) {
1896 switch(this.node.nodeName) {
1897 case 'tree' : return this._render_list_actions_for_tree(params); break;
1898 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1902 '_render_list_actions_for_tree' : function(params) {
1905 var btn = document.createElement('button');
1906 btn.setAttribute('id',obj.node.id + '_list_actions');
1907 btn.setAttribute('type','menu');
1908 btn.setAttribute('allowevents','true');
1909 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1910 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1911 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1912 var mp = document.createElement('menupopup');
1913 btn.appendChild(mp);
1914 var mi = document.createElement('menuitem');
1915 mi.setAttribute('id',obj.node.id + '_clipfield');
1916 mi.setAttribute('disabled','true');
1917 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1918 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1920 mi = document.createElement('menuitem');
1921 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1922 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1923 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1925 mi = document.createElement('menuitem');
1926 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1927 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1928 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1930 mi = document.createElement('menuitem');
1931 mi.setAttribute('id',obj.node.id + '_extended_to_printer');
1932 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.label'));
1933 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.accesskey'));
1935 mi = document.createElement('menuitem');
1936 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1937 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1938 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1940 mi = document.createElement('menuitem');
1941 mi.setAttribute('id',obj.node.id + '_save_columns');
1942 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1943 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1944 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
1945 mi.setAttribute('disabled','true');
1950 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1954 'set_list_actions' : function(params) {
1956 switch(this.node.nodeName) {
1957 case 'tree' : return this._set_list_actions_for_tree(params); break;
1958 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1962 '_set_list_actions_for_tree' : function(params) {
1963 // This should be called after the button element from render_list_actions has been appended to the DOM
1966 var x = document.getElementById(obj.node.id + '_clipfield');
1968 obj.event_listeners.add(
1972 obj.clipboard(params);
1973 if (params && typeof params.on_complete == 'function') {
1974 params.on_complete(params);
1980 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1982 obj.event_listeners.add(
1986 obj.dump_csv_to_clipboard(params);
1987 if (params && typeof params.on_complete == 'function') {
1988 params.on_complete(params);
1994 x = document.getElementById(obj.node.id + '_csv_to_printer');
1996 obj.event_listeners.add(
2000 obj.dump_csv_to_printer(params);
2001 if (params && typeof params.on_complete == 'function') {
2002 params.on_complete(params);
2008 x = document.getElementById(obj.node.id + '_extended_to_printer');
2010 obj.event_listeners.add(
2014 obj.dump_extended_format_to_printer(params);
2015 if (params && typeof params.on_complete == 'function') {
2016 params.on_complete(params);
2023 x = document.getElementById(obj.node.id + '_csv_to_file');
2025 obj.event_listeners.add(
2029 obj.dump_csv_to_file(params);
2030 if (params && typeof params.on_complete == 'function') {
2031 params.on_complete(params);
2037 x = document.getElementById(obj.node.id + '_save_columns');
2039 obj.event_listeners.add(
2043 obj.save_columns(params);
2044 if (params && typeof params.on_complete == 'function') {
2045 params.on_complete(params);
2053 obj.error.standard_unexpected_error_alert('setting list actions',E);
2057 // Takes fieldmapper class name and attempts to spit out column definitions suitable for .init
2058 'fm_columns' : function(hint,column_extras,prefix) {
2061 if (!prefix) { prefix = ''; }
2062 var sort_headers = false;
2064 // requires the dojo library fieldmapper.autoIDL
2065 if (typeof fieldmapper == 'undefined') { throw 'fieldmapper undefined'; }
2066 if (typeof fieldmapper.IDL == 'undefined') { throw 'fieldmapper.IDL undefined'; }
2067 if (typeof fieldmapper.IDL.fmclasses == 'undefined') { throw 'fieldmapper.IDL.fmclasses undefined'; }
2068 if (typeof fieldmapper.IDL.fmclasses[hint] == 'undefined') { throw 'fieldmapper.IDL.fmclasses.' + hint + ' undefined'; }
2069 var my_class = fieldmapper.IDL.fmclasses[hint];
2070 var data = obj.data; data.stash_retrieve();
2072 function col_def(my_field) {
2073 var col_id = prefix + hint + '_' + my_field.name;
2075 var datafield = my_field.name;
2076 var fleshed_display_field;
2077 if (column_extras) {
2078 if (column_extras['*']) {
2079 if (column_extras['*']['dataobj']) {
2080 dataobj = column_extras['*']['dataobj'];
2083 if (column_extras[col_id]) {
2084 if (column_extras[col_id]['dataobj']) {
2085 dataobj = column_extras[col_id]['dataobj'];
2087 if (column_extras[col_id]['datafield']) {
2088 datafield = column_extras[col_id]['datafield'];
2090 if (column_extras[col_id]['fleshed_display_field']) {
2091 fleshed_display_field = column_extras[col_id]['fleshed_display_field'];
2097 'label' : my_field.label || my_field.name,
2098 'sort_type' : [ 'int', 'float', 'id', 'number' ].indexOf(my_field.datatype) > -1 ? 'number' :
2099 ( my_field.datatype == 'money' ? 'money' :
2100 ( my_field.datatype == 'timestamp' ? 'date' : 'default')),
2101 'hidden' : my_field.virtual || my_field.datatype == 'link',
2104 // my_field.datatype => bool float id int interval link money number org_unit text timestamp
2105 if (my_field.datatype == 'link') {
2106 def.render = function(my) {
2107 // is the object fleshed?
2108 return my[dataobj][datafield]() && typeof my[dataobj][datafield]() == 'object'
2109 // yes, show the display field
2110 ? my[dataobj][datafield]()[fleshed_display_field||my_field.key]()
2111 // no, do we have its class in data.hash?
2112 : ( typeof data.hash[ my[dataobj].Structure.field_map[datafield].class ] != 'undefined'
2113 // yes, do we have this particular object cached?
2114 ? ( data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ]
2115 // yes, show the display field
2116 ? data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ][
2117 fleshed_display_field||my_field.key
2119 // no, just show the raw value
2120 : my[dataobj][datafield]()
2122 // no, just show the raw value
2123 : my[dataobj][datafield]()
2127 def.render = function(my) { return my[dataobj][datafield](); }
2129 if (my_field.datatype == 'timestamp') {
2130 JSAN.use('util.date');
2131 def.render = function(my) {
2132 return util.date.formatted_date( my[dataobj][datafield](), '%{localized}' );
2134 def.sort_value = function(my) {
2135 return util.date.db_date2Date( my[dataobj][datafield]() ).getTime();
2138 if (my_field.datatype == 'org_unit') {
2139 def.render = function(my) {
2140 return typeof my[dataobj][datafield]() == 'object' ? my[dataobj][datafield]().shortname() : data.hash.aou[ my[dataobj][datafield]() ].shortname();
2143 if (my_field.datatype == 'money') {
2144 JSAN.use('util.money');
2145 def.render = function(my) {
2146 return util.money.sanitize( my[dataobj][datafield]() );
2148 def.sort_value = function(my) {
2149 return util.money.dollars_float_to_cents_integer( my[dataobj][datafield]() );
2152 if (column_extras) {
2153 if (column_extras['*']) {
2154 for (var attr in column_extras['*']) {
2155 def[attr] = column_extras['*'][attr];
2157 if (column_extras['*']['expanded_label']) {
2158 def.label = my_class.label + ': ' + def.label;
2160 if (column_extras['*']['label_prefix']) {
2161 def.label = column_extras['*']['label_prefix'] + def.label;
2163 if (column_extras['*']['remove_virtual']) {
2164 if (my_field.virtual) {
2165 def.remove_me = true;
2168 if (column_extras['*']['sort_headers']) {
2169 sort_headers = true;
2172 if (column_extras[col_id]) {
2173 for (var attr in column_extras[col_id]) {
2174 def[attr] = column_extras[col_id][attr];
2176 if (column_extras[col_id]['keep_me']) {
2177 def.remove_me = false;
2179 if (column_extras[col_id]['remove_me']) {
2180 def.remove_me = true;
2182 if (column_extras[col_id]['label_prefix']) {
2183 def.label = column_extras[col_id]['label_prefix'] + def.label;
2187 if (def.remove_me) {
2188 dump('Skipping ' + def.label + '\n');
2191 dump('Defining ' + def.label + '\n');
2196 for (var i = 0; i < my_class.fields.length; i++) {
2197 var my_field = my_class.fields[i];
2198 var def = col_def(my_field);
2200 columns.push( def );
2207 if (a.label > b.label) { return 1; }
2208 if (a.label < b.label) { return -1; }
2215 obj.error.standard_unexpected_error_alert('fm_columns()',E);
2219 // Default for the map_row_to_columns function for .init
2220 'std_map_row_to_columns' : function(error_value) {
2221 return function(row,cols,scratch) {
2222 // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
2223 // cols contains all of the objects listed above in columns
2224 // scratch is a temporary space shared by all cells/rows (or just per row if not explicitly passed in)
2225 if (!scratch) { scratch = {}; }
2228 JSAN.use('util.error'); obj.error = new util.error();
2229 JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
2230 JSAN.use('util.network'); obj.network = new util.network();
2231 JSAN.use('util.money');
2233 // FIXME: backwards compatability with server/patron code and the old patron.util.std_map_row_to_columns.
2234 // Will remove in a separate commit and change all instances of obj.OpenILS.data to obj.data at the same time.
2235 obj.OpenILS = { 'data' : obj.data };
2239 var sort_values = [];
2242 for (var i = 0; i < cols.length; i++) {
2243 switch (typeof cols[i].render) {
2244 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;
2245 case 'string' : cmd += 'try { ' + cols[i].render + '; values['+i+'] = v; } catch(E) { values['+i+'] = error_value; }'; break;
2246 default: cmd += 'values['+i+'] = "??? '+(typeof cols[i].render)+'"; ';
2248 switch (typeof cols[i].sort_value) {
2251 sort_values[i] = cols[i].sort_value(my,scratch);
2253 sort_values[i] = error_value;
2254 obj.error.sdump('D_COLUMN_RENDER_ERROR',E);
2258 sort_values[i] = JSON2js(cols[i].sort_value);
2261 cmd += 'sort_values['+i+'] = values[' + i + '];';
2264 if (cmd) eval( cmd );
2266 obj.error.sdump('D_WARN','map_row_to_column: ' + E);
2267 if (error_value) { value = error_value; } else { value = ' ' };
2269 return { 'values' : values, 'sort_values' : sort_values };
2273 dump('exiting util.list.js\n');