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 = {};
40 // If set, save and restore columns as if the tree/list id was the value of columns_saved_under
41 obj.columns_saved_under = params.columns_saved_under;
43 JSAN.use('util.widgets');
45 obj.printer_context = params.printer_context;
47 if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
48 if (typeof params.map_row_to_columns == 'function') {
49 obj.map_row_to_columns = params.map_row_to_columns;
51 obj.map_row_to_columns = obj.std_map_row_to_columns();
53 if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
56 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
58 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
62 'label' : document.getElementById('offlineStrings').getString('list.line_number'),
65 'properties' : 'ordinal', // column properties for css styling
68 'render' : function(my,scratch) {
69 // special code will handle this based on the attribute we set
70 // here. All cells for this column need to be updated whenever
71 // a list adds, removes, or sorts rows
76 for (var i = 0; i < params.columns.length; i++) {
77 if (typeof params.columns[i] == 'object') {
78 obj.columns.push( params.columns[i] );
80 var cols = obj.fm_columns( params.columns[i] );
81 for (var j = 0; j < cols.length; j++) {
82 obj.columns.push( cols[j] );
87 switch(obj.node.nodeName) {
88 case 'tree' : obj._init_tree(params); break;
89 case 'listbox' : obj._init_listbox(params); break;
90 default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
94 '_init_tree' : function (params) {
98 this.treechildren = this.node.lastChild;
101 var treecols = document.createElement('treecols');
102 this.node.appendChild(treecols);
103 this.treecols = treecols;
104 if (document.getElementById('column_sort_menu')) {
105 treecols.setAttribute('context','column_sort_menu');
108 var check_for_id_collisions = {};
109 for (var i = 0; i < this.columns.length; i++) {
110 var treecol = document.createElement('treecol');
111 for (var j in this.columns[i]) {
112 var value = this.columns[i][j];
114 if (typeof check_for_id_collisions[value] == 'undefined') {
115 check_for_id_collisions[value] = true;
117 // Column id's are important for sorting and saving list configuration. Collisions started happening because
118 // we were using field names as id's, and then later combining column definitions for multiple objects that
119 // shared field names. The downside to this sort of automatic collision prevention is that these generated
120 // id's can change as we add and remove columns, possibly breaking saved list configurations.
121 dump('Column collision with id = ' + value + ', renaming to ');
122 value = value + '_collision_' + i;
126 treecol.setAttribute(j,value);
128 treecols.appendChild(treecol);
130 if (this.columns[i].type == 'checkbox') {
131 treecol.addEventListener(
136 var toggle = ev.target.getAttribute('toggleAll') || 'on';
137 if (toggle == 'off') toggle = 'on'; else toggle = 'off';
138 ev.target.setAttribute('toggleAll',toggle);
139 obj._toggle_checkbox_column(ev.target,toggle);
146 treecol.addEventListener(
149 dump('sort_first_asc\n');
150 ev.target.setAttribute('sortDir','asc');
152 'target' : ev.target,
156 util.widgets.dispatch('sort',ev.target);
160 treecol.addEventListener(
163 dump('sort_first_desc\n');
164 ev.target.setAttribute('sortDir','desc');
166 'target' : ev.target,
170 util.widgets.dispatch('sort',ev.target);
174 treecol.addEventListener(
177 dump('sort_next_asc\n');
178 ev.target.setAttribute('sortDir','asc');
180 'target' : ev.target,
183 util.widgets.dispatch('sort',ev.target);
187 treecol.addEventListener(
190 dump('sort_next_desc\n');
191 ev.target.setAttribute('sortDir','desc');
193 'target' : ev.target,
196 util.widgets.dispatch('sort',ev.target);
201 treecol.addEventListener(
205 if (ev.button == 2 /* context menu click */ || ev.target.getAttribute('no_sort')) {
208 if (document.popupNode
209 && document.popupNode.nodeName == 'treecol'
210 && document.popupNode.hasAttribute('locked')
216 if (ev.ctrlKey) { // sub sort
222 ev.target.setAttribute('sortDir',sortDir);
224 'target' : ev.target,
227 } else { // first sort
229 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
230 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
234 ev.target.setAttribute('sortDir',sortDir);
236 'target' : ev.target,
241 util.widgets.dispatch('sort',ev.target);
246 treecol.addEventListener(
250 if (!obj.first_sort) {
260 if (obj.row_count.total != obj.row_count.fleshed
261 && (obj.row_count.total - obj.row_count.fleshed) > 50
263 var r = window.confirm(
264 document.getElementById('offlineStrings').getFormattedString(
265 'list.row_fetch_warning',
266 [obj.row_count.fleshed,obj.row_count.total]
271 setTimeout( do_it, 0 );
275 setTimeout( do_it, 0 );
282 var splitter = document.createElement('splitter');
283 splitter.setAttribute('class','tree-splitter');
284 treecols.appendChild(splitter);
287 var treechildren = document.createElement('treechildren');
288 this.node.appendChild(treechildren);
289 this.treechildren = treechildren;
291 if (typeof params.on_sort == 'function') {
292 this.on_sort = params.on_sort;
294 if (typeof params.on_checkbox_toggle == 'function') {
295 this.on_checkbox_toggle = params.on_checkbox_toggle;
297 this.node.addEventListener(
300 if (typeof params.on_select == 'function') {
301 params.on_select(ev);
303 var x = document.getElementById(obj.node.id + '_clipfield');
305 var sel = obj.retrieve_selection();
306 x.setAttribute('disabled', sel.length == 0);
311 if (typeof params.on_click == 'function') {
312 this.node.addEventListener(
318 if (typeof params.on_dblclick == 'function') {
319 this.node.addEventListener(
327 this.node.addEventListener(
329 function(ev) { obj.detect_visible(); },
333 this.node.addEventListener(
335 function(ev) { obj.auto_retrieve(); },
338 this.node.addEventListener(
340 function(ev) { obj.auto_retrieve(); },
343 window.addEventListener(
345 function(ev) { obj.auto_retrieve(); },
348 /* FIXME -- find events on scrollbar to trigger this */
349 obj.detect_visible_polling();
351 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
352 var slider = document.getAnonymousNodes( scrollbar )[2];
353 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
354 scrollbar.addEventListener('click',function(){alert('sb click');},false);
355 scrollbar.addEventListener('command',function(){alert('sb command');},false);
356 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
357 slider.addEventListener('click',function(){alert('slider click');},false);
358 slider.addEventListener('command',function(){alert('slider command');},false);
359 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
361 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
363 this.restores_columns(params);
366 '_init_listbox' : function (params) {
369 var listhead = document.createElement('listhead');
370 this.node.appendChild(listhead);
372 var listcols = document.createElement('listcols');
373 this.node.appendChild(listcols);
375 for (var i = 0; i < this.columns.length; i++) {
376 var listheader = document.createElement('listheader');
377 listhead.appendChild(listheader);
378 var listcol = document.createElement('listcol');
379 listcols.appendChild(listcol);
380 for (var j in this.columns[i]) {
381 listheader.setAttribute(j,this.columns[i][j]);
382 listcol.setAttribute(j,this.columns[i][j]);
388 'save_columns' : function (params) {
390 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
391 alert(document.getElementById('offlineStrings').getString('list.column_save_disabled'));
393 switch (this.node.nodeName) {
394 case 'tree' : this._save_columns_tree(params); break;
395 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
400 '_save_columns_tree' : function (params) {
403 var id = obj.node.getAttribute('id');
404 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
406 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
410 var nl = obj.node.getElementsByTagName('treecol');
411 for (var i = 0; i < nl.length; i++) {
413 var col_id = col.getAttribute('id');
415 alert('FIXME: A column in this list does not have an id and cannot be saved');
418 var col_hidden = col.getAttribute('hidden');
419 var col_width = col.getAttribute('width');
420 var col_ordinal = col.getAttribute('ordinal');
421 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
423 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
424 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
425 file.set_object(my_cols);
427 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
429 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
433 'restores_columns' : function (params) {
435 switch (this.node.nodeName) {
436 case 'tree' : this._restores_columns_tree(params); break;
437 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
441 '_restores_columns_tree' : function (params) {
444 var id = obj.node.getAttribute('id');
445 if (obj.columns_saved_under) { id = obj.columns_saved_under; }
447 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
452 if (! obj.data.hash.aous) { obj.data.hash.aous = {}; }
453 if (! obj.data.hash.aous['gui.disable_local_save_columns']) {
454 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
455 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
456 if (file._file.exists()) {
457 my_cols = file.get_object(); file.close();
460 /* local file will trump remote file if allowed, so save ourselves an http request if this is the case */
461 if (obj.data.hash.aous['url.remote_column_settings'] && ! my_cols ) {
463 var x = new XMLHttpRequest();
464 var url = obj.data.hash.aous['url.remote_column_settings'] + '/tree_columns_for_' + window.escape(id);
465 x.open("GET", url, false);
467 if (x.status == 200) {
468 my_cols = JSON2js( x.responseText );
471 // This can happen in the offline interface if you logged in previously and url.remote_column_settings is set.
472 // 1) You may be really "offline" now
473 // 2) the URL may just be a path component without a hostname (ie "/xul/column_settings/"), which won't work
474 // when appended to chrome://open_ils_staff_client/
475 dump('Error retrieving column settings from ' + url + ': ' + E + '\n');
480 var nl = obj.node.getElementsByTagName('treecol');
481 for (var i = 0; i < nl.length; i++) {
483 var col_id = col.getAttribute('id');
485 alert('FIXME: A column in this list does not have an id and cannot be saved');
488 if (typeof my_cols[col_id] != 'undefined') {
489 col.setAttribute('hidden',my_cols[col_id].hidden);
490 col.setAttribute('width',my_cols[col_id].width);
491 col.setAttribute('ordinal',my_cols[col_id].ordinal);
493 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
498 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
502 'clear' : function (params) {
504 switch (this.node.nodeName) {
505 case 'tree' : this._clear_tree(params); break;
506 case 'listbox' : this._clear_listbox(params); break;
507 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
509 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
510 this.row_count.total = 0;
511 this.row_count.fleshed = 0;
512 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
515 '_clear_tree' : function(params) {
517 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
518 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
520 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
521 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
523 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
526 '_clear_listbox' : function(params) {
529 var nl = this.node.getElementsByTagName('listitem');
530 for (var i = 0; i < nl.length; i++) {
533 for (var i = 0; i < items.length; i++) {
534 this.node.removeChild(items[i]);
538 'append' : function (params) {
541 switch (this.node.nodeName) {
542 case 'tree' : rparams = this._append_to_tree(params); break;
543 case 'listbox' : rparams = this._append_to_listbox(params); break;
544 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
546 if (rparams && params.attributes) {
547 for (var i in params.attributes) {
548 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
551 this.row_count.total++;
552 if (this.row_count.fleshed == this.row_count.total) {
553 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
555 rparams.treeitem_node.setAttribute('unique_row_counter',obj.unique_row_counter);
556 rparams.unique_row_counter = obj.unique_row_counter++;
557 if (typeof params.on_append == 'function') {
558 params.on_append(rparams);
563 'refresh_row' : function (params) {
566 switch (this.node.nodeName) {
567 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
568 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
570 if (rparams && params.attributes) {
571 for (var i in params.attributes) {
572 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
575 this.row_count.fleshed--;
580 '_append_to_tree' : function (params) {
584 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
586 var s = ('util.list.append: params = ' + (params) + '\n');
588 var treechildren_node = this.treechildren;
590 if (params.node && params.node.nodeName == 'treeitem') {
591 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
592 if (params.node.lastChild.nodeName == 'treechildren') {
593 treechildren_node = params.node.lastChild;
595 treechildren_node = document.createElement('treechildren');
596 params.node.appendChild(treechildren_node);
600 var treeitem = document.createElement('treeitem');
601 treeitem.setAttribute('retrieve_id',params.retrieve_id);
602 if (typeof params.to_bottom != 'undefined') {
603 treechildren_node.appendChild( treeitem );
604 if (typeof params.no_auto_select == 'undefined') {
605 if (!obj.auto_select_pending) {
606 obj.auto_select_pending = true;
607 setTimeout(function() {
608 dump('auto-selecting\n');
609 var idx = Number(obj.node.view.rowCount)-1;
610 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
611 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
612 obj.auto_select_pending = false;
613 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
618 if (treechildren_node.firstChild) {
619 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
621 treechildren_node.appendChild( treeitem );
623 if (typeof params.no_auto_select == 'undefined') {
624 if (!obj.auto_select_pending) {
625 obj.auto_select_pending = true;
626 setTimeout(function() {
627 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
628 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
629 obj.auto_select_pending = false;
630 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
635 var treerow = document.createElement('treerow');
636 treeitem.appendChild( treerow );
637 treerow.setAttribute('retrieve_id',params.retrieve_id);
638 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
640 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
641 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
643 obj.put_retrieving_label(treerow);
645 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
646 treerow.addEventListener(
650 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
652 treerow.setAttribute('retrieved','true');
654 //dump('fleshing = ' + params.retrieve_id + '\n');
656 function inc_fleshed() {
657 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
658 treerow.setAttribute('fleshed','true');
659 obj.row_count.fleshed++;
660 if (obj.row_count.fleshed >= obj.row_count.total) {
661 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
665 params.treeitem_node = treeitem;
666 params.on_retrieve = function(p) {
669 obj._map_row_to_treecell(p,treerow);
671 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
672 dump('idx = ' + idx + '\n');
673 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
674 if ( obj.node.view.selection.isSelected( idx ) ) {
675 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
676 util.widgets.dispatch('select',obj.node);
679 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
680 dump('fixme2: ' + E + '\n');
684 if (typeof params.retrieve_row == 'function') {
686 params.retrieve_row( params );
688 } else if (typeof obj.retrieve_row == 'function') {
690 obj.retrieve_row( params );
696 obj.refresh_ordinals();
700 if (typeof params.flesh_immediately != 'undefined') {
701 if (params.flesh_immediately) {
704 util.widgets.dispatch('flesh',treerow);
710 treerow.addEventListener(
713 //dump('fleshing anon\n');
714 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
715 obj._map_row_to_treecell(params,treerow);
716 treerow.setAttribute('retrieved','true');
717 treerow.setAttribute('fleshed','true');
718 obj.row_count.fleshed++;
719 if (obj.row_count.fleshed >= obj.row_count.total) {
720 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
722 obj.refresh_ordinals();
726 if (typeof params.flesh_immediately != 'undefined') {
727 if (params.flesh_immediately) {
730 util.widgets.dispatch('flesh',treerow);
736 this.error.sdump('D_LIST',s);
740 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
742 //if (typeof params.to_bottom != 'undefined')
743 if (typeof params.to_top == 'undefined') {
744 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.firstChild.getAttribute('unique_row_counter') ); }
745 treechildren_node.removeChild( treechildren_node.firstChild );
747 if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.lastChild.getAttribute('unique_row_counter') ); }
748 treechildren_node.removeChild( treechildren_node.lastChild );
754 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
756 params.treeitem_node = treeitem;
760 '_refresh_row_in_tree' : function (params) {
764 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
765 if (typeof params.treeitem_node == 'undefined') throw('util.list.refresh_row: Object must contain a treeitem_node');
766 if (params.treeitem_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: treeitem_node must be a treeitem');
768 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
770 var treeitem = params.treeitem_node;
771 treeitem.setAttribute('retrieve_id',params.retrieve_id);
772 if (typeof params.to_bottom != 'undefined') {
773 if (typeof params.no_auto_select == 'undefined') {
774 if (!obj.auto_select_pending) {
775 obj.auto_select_pending = true;
776 setTimeout(function() {
777 dump('auto-selecting\n');
778 var idx = Number(obj.node.view.rowCount)-1;
779 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
780 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
781 obj.auto_select_pending = false;
782 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
787 //var delete_me = [];
788 //for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
789 //for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
790 var prev_treerow = treeitem.firstChild; /* FIXME: worry about hierarchal lists like copy_browser? */
791 var treerow = document.createElement('treerow');
792 while (prev_treerow.firstChild) {
793 treerow.appendChild( prev_treerow.removeChild( prev_treerow.firstChild ) );
795 treeitem.replaceChild( treerow, prev_treerow );
796 treerow.setAttribute('retrieve_id',params.retrieve_id);
797 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
799 s += ('tree = ' + this.node.nodeName + '\n');
800 s += ('treeitem = ' + treeitem.nodeName + ' treerow = ' + treerow.nodeName + '\n');
802 obj.put_retrieving_label(treerow);
804 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
806 s += 'found a retrieve_row function\n';
808 treerow.addEventListener(
812 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
814 treerow.setAttribute('retrieved','true');
816 //dump('fleshing = ' + params.retrieve_id + '\n');
818 function inc_fleshed() {
819 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
820 treerow.setAttribute('fleshed','true');
821 obj.row_count.fleshed++;
822 if (obj.row_count.fleshed >= obj.row_count.total) {
823 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
827 params.treeitem_node = treeitem;
828 params.on_retrieve = function(p) {
831 obj._map_row_to_treecell(p,treerow);
833 var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
834 dump('idx = ' + idx + '\n');
835 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
836 if ( obj.node.view.selection.isSelected( idx ) ) {
837 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
838 util.widgets.dispatch('select',obj.node);
841 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
842 dump('fixme2: ' + E + '\n');
846 if (typeof params.retrieve_row == 'function') {
848 params.retrieve_row( params );
850 } else if (typeof obj.retrieve_row == 'function') {
852 obj.retrieve_row( params );
858 obj.refresh_ordinals();
862 if (typeof params.flesh_immediately != 'undefined') {
863 if (params.flesh_immediately) {
866 util.widgets.dispatch('flesh',treerow);
874 s += 'did not find a retrieve_row function\n';
876 treerow.addEventListener(
879 //dump('fleshing anon\n');
880 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
881 obj._map_row_to_treecell(params,treerow);
882 treerow.setAttribute('retrieved','true');
883 treerow.setAttribute('fleshed','true');
884 obj.row_count.fleshed++;
885 if (obj.row_count.fleshed >= obj.row_count.total) {
886 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
888 obj.refresh_ordinals();
892 if (typeof params.flesh_immediately != 'undefined') {
893 if (params.flesh_immediately) {
896 util.widgets.dispatch('flesh',treerow);
906 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
908 //if (typeof params.to_bottom != 'undefined')
909 if (typeof params.to_top == 'undefined') {
910 treechildren_node.removeChild( treechildren_node.firstChild );
912 treechildren_node.removeChild( treechildren_node.lastChild );
918 setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
920 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
922 this.error.sdump('D_LIST',s);
927 'refresh_ordinals' : function() {
930 if (obj.refresh_ordinals_timeout_id) { return; }
932 function _refresh_ordinals(clear) {
933 var nl = obj.node.getElementsByAttribute('label','_');
934 for (var i = 0; i < nl.length; i++) {
939 nl[i].setAttribute( // treecell properties for css styling
944 nl = obj.node.getElementsByAttribute('ord_col','true');
945 for (var i = 0; i < nl.length; i++) {
948 // we could just use 'i' here if we trust the order of elements
949 1 + obj.node.contentView.getIndexOfItem(nl[i].parentNode.parentNode) // treeitem
952 if (clear) { obj.refresh_ordinals_timeout_id = null; }
955 // spamming this to cover race conditions
956 setTimeout(_refresh_ordinals, 500); // for speedy looking UI updates
957 setTimeout(_refresh_ordinals, 2000); // for most uses
958 obj.refresh_ordinals_timeout_id = setTimeout(
960 _refresh_ordinals(true);
962 4000 // just in case, say with a slow rendering list
966 alert('Error in list.js, refresh_ordinals(): ' + E);
970 'put_retrieving_label' : function(treerow) {
973 for (var i = 0; i < obj.columns.length; i++) {
975 if (typeof treerow.childNodes[i] == 'undefined') {
976 treecell = document.createElement('treecell');
977 treerow.appendChild(treecell);
979 treecell = treerow.childNodes[i];
981 treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
984 alert('Error in list.js, put_retrieving_label(): ' + E);
988 'detect_visible' : function() {
991 //dump('detect_visible obj.node = ' + obj.node + '\n');
992 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
994 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
997 s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n';
998 if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
999 var C = B.childNodes[j];
1000 s += '\t\t' + C.nodeName + '\n';
1003 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
1004 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
1005 var curpos = scrollbar.getAttribute('curpos');
1006 var maxpos = scrollbar.getAttribute('maxpos');
1007 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
1008 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
1009 if ( obj.auto_retrieve() > 0 ) {
1010 obj.curpos = curpos; obj.maxpos = maxpos;
1014 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
1016 } catch(E) { obj.error.sdump('D_ERROR',E); }
1019 'detect_visible_polling' : function() {
1021 //alert('detect_visible_polling');
1023 obj.detect_visible();
1024 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
1031 'auto_retrieve' : function(params) {
1033 switch (this.node.nodeName) {
1034 case 'tree' : obj._auto_retrieve_tree(params); break;
1035 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
1039 '_auto_retrieve_tree' : function (params) {
1041 if (!obj.auto_retrieve_in_progress) {
1042 obj.auto_retrieve_in_progress = true;
1046 //alert('auto_retrieve\n');
1048 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
1049 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
1050 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
1051 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
1052 for (var i = startpos; i < endpos + 4; i++) {
1054 //dump('trying index ' + i + '\n');
1055 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
1056 if (item && item.getAttribute('retrieved') != 'true' ) {
1057 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
1058 util.widgets.dispatch('flesh',item); count++;
1061 //dump(i + ' : ' + E + '\n');
1064 obj.auto_retrieve_in_progress = false;
1066 } catch(E) { alert(E); }
1072 'exec_on_all_fleshed' : function() {
1075 if (obj.on_all_fleshed) {
1076 if (typeof obj.on_all_fleshed == 'function') {
1077 dump('exec_on_all_fleshed == function\n');
1080 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
1083 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
1084 dump('exec_on_all_fleshed == array\n');
1088 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
1089 var f = obj.on_all_fleshed.pop();
1090 if (typeof f == 'function') {
1091 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); }
1093 if (obj.on_all_fleshed.length > 0) arguments.callee();
1095 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
1100 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
1104 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
1108 'full_retrieve' : function(params) {
1110 switch (this.node.nodeName) {
1111 case 'tree' : obj._full_retrieve_tree(params); break;
1112 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
1114 obj.refresh_ordinals();
1117 '_full_retrieve_tree' : function(params) {
1120 if (obj.row_count.fleshed >= obj.row_count.total) {
1121 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
1122 obj.exec_on_all_fleshed();
1124 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
1125 JSAN.use('util.widgets');
1126 var nodes = obj.treechildren.childNodes;
1127 for (var i = 0; i < nodes.length; i++) {
1128 util.widgets.dispatch('flesh',nodes[i].firstChild);
1132 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
1136 '_append_to_listbox' : function (params) {
1140 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
1142 var s = ('util.list.append: params = ' + (params) + '\n');
1144 var listitem = document.createElement('listitem');
1146 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
1148 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
1152 listitem.setAttribute('retrieve_id',params.retrieve_id);
1153 //FIXME//Make async and fire when row is visible in list
1156 params.treeitem_node = listitem;
1157 params.on_retrieve = function(row) {
1159 obj._map_row_to_listcell(params,listitem);
1160 obj.node.appendChild( listitem );
1161 util.widgets.dispatch('select',obj.node);
1164 if (typeof params.retrieve_row == 'function') {
1166 row = params.retrieve_row( params );
1170 if (typeof obj.retrieve_row == 'function') {
1172 row = obj.retrieve_row( params );
1179 this._map_row_to_listcell(params,listitem);
1180 this.node.appendChild( listitem );
1183 this.error.sdump('D_LIST',s);
1184 params.treeitem_node = listitem;
1189 '_map_row_to_treecell' : function(params,treerow) {
1193 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
1195 for (var i = 0; i < this.columns.length; i++) {
1197 if (typeof treerow.childNodes[i] == 'undefined') {
1198 treecell = document.createElement('treecell');
1199 treerow.appendChild( treecell );
1201 treecell = treerow.childNodes[i];
1204 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1206 var sort_value = '';
1208 // What skip columns is doing is rendering the treecells as blank/empty
1209 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
1210 treecell.setAttribute('label',label);
1211 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1214 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
1215 treecell.setAttribute('label',label);
1216 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1220 if (typeof params.map_row_to_column == 'function') {
1222 label = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1224 } else if (typeof this.map_row_to_column == 'function') {
1226 label = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1229 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
1230 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1232 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
1235 var sort_values = [];
1237 if (typeof params.map_row_to_columns == 'function') {
1239 var values = params.map_row_to_columns(params.row,this.columns,this.scratch_data);
1240 if (typeof values.values == 'undefined') {
1243 labels = values.values;
1244 sort_values = values.sort_values;
1247 } else if (typeof this.map_row_to_columns == 'function') {
1249 var values = this.map_row_to_columns(params.row,this.columns,this.scratch_data);
1250 if (typeof values.values == 'undefined') {
1253 labels = values.values;
1254 sort_values = values.sort_values;
1257 for (var i = 0; i < labels.length; i++) {
1259 if (typeof treerow.childNodes[i] == 'undefined') {
1260 treecell = document.createElement('treecell');
1261 treerow.appendChild(treecell);
1263 treecell = treerow.childNodes[i];
1265 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1266 if ( this.columns[i].type == 'checkbox') {
1267 treecell.setAttribute('value', labels[i]);
1269 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
1271 if (sort_values[i]) {
1272 treecell.setAttribute('sort_value',js2JSON(sort_values[i]));
1274 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
1279 throw('No row to column mapping function.');
1281 this.error.sdump('D_LIST',s);
1284 '_map_row_to_listcell' : function(params,listitem) {
1287 for (var i = 0; i < this.columns.length; i++) {
1289 if (typeof params.map_row_to_column == 'function') {
1291 value = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1295 if (typeof this.map_row_to_column == 'function') {
1297 value = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1300 if (typeof value == 'string' || typeof value == 'number') {
1301 var listcell = document.createElement('listcell');
1302 listcell.setAttribute('label',value);
1303 listitem.appendChild(listcell);
1304 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1306 listitem.appendChild(value);
1307 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1310 this.error.sdump('D_LIST',s);
1313 'select_all' : function(params) {
1315 switch(this.node.nodeName) {
1316 case 'tree' : return this._select_all_from_tree(params); break;
1317 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1321 '_select_all_from_tree' : function(params) {
1323 this.node.view.selection.selectAll();
1326 'retrieve_selection' : function(params) {
1328 switch(this.node.nodeName) {
1329 case 'tree' : return this._retrieve_selection_from_tree(params); break;
1330 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1334 '_retrieve_selection_from_tree' : function(params) {
1337 var start = new Object();
1338 var end = new Object();
1339 var numRanges = this.node.view.selection.getRangeCount();
1340 for (var t=0; t<numRanges; t++){
1341 this.node.view.selection.getRangeAt(t,start,end);
1342 for (var v=start.value; v<=end.value; v++){
1343 var i = this.node.contentView.getItemAtIndex(v);
1350 'dump' : function(params) {
1352 switch(this.node.nodeName) {
1353 case 'tree' : return this._dump_tree(params); break;
1354 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1358 '_dump_tree' : function(params) {
1361 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1363 var treeitem = this.treechildren.childNodes[i];
1364 var treerow = treeitem.firstChild;
1365 for (var j = 0; j < treerow.childNodes.length; j++) {
1366 row.push( treerow.childNodes[j].getAttribute('label') );
1373 'dump_with_keys' : function(params) {
1375 switch(this.node.nodeName) {
1376 case 'tree' : return this._dump_tree_with_keys(params); break;
1377 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1382 '_dump_tree_with_keys' : function(params) {
1386 function process_tree(treechildren) {
1387 for (var i = 0; i < treechildren.childNodes.length; i++) {
1389 var treeitem = treechildren.childNodes[i];
1390 var treerow = treeitem.firstChild;
1391 for (var j = 0; j < treerow.childNodes.length; j++) {
1392 if (typeof obj.columns[j] == 'undefined') {
1393 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1394 dump('_dump_tree_with_keys @ ' + location.href + '\n');
1395 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1398 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1399 var sort = treerow.childNodes[j].getAttribute('sort_value');
1401 row[ obj.columns[j].id + '_sort_value' ] = sort;
1406 if (treeitem.childNodes.length > 1) {
1407 process_tree(treeitem.lastChild);
1412 process_tree(this.treechildren);
1417 'dump_csv' : function(params) {
1419 switch(this.node.nodeName) {
1420 case 'tree' : return this._dump_tree_csv(params); break;
1421 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1426 '_dump_tree_csv' : function(params) {
1430 for (var j = 0; j < obj.columns.length; j++) {
1431 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1434 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1437 ord_cols.sort( function(a,b) {
1438 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1439 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1442 for (var j = 0; j < ord_cols.length; j++) {
1443 if (_dump) _dump += ',';
1444 _dump += '"' + obj.columns[ ord_cols[j][1] ].label.replace(/"/g, '""') + '"';
1448 function process_tree(treechildren) {
1449 for (var i = 0; i < treechildren.childNodes.length; i++) {
1451 var treeitem = treechildren.childNodes[i];
1452 var treerow = treeitem.firstChild;
1453 for (var j = 0; j < ord_cols.length; j++) {
1454 if (row) row += ',';
1455 row += '"' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label').replace(/"/g, '""') + '"';
1457 _dump += row + '\r\n';
1458 if (treeitem.childNodes.length > 1) {
1459 process_tree(treeitem.lastChild);
1464 process_tree(this.treechildren);
1469 'dump_extended_format' : function(params) {
1471 switch(this.node.nodeName) {
1472 case 'tree' : return this._dump_tree_extended_format(params); break;
1473 default: throw('NYI: Need .dump_extended_format() for ' + this.node.nodeName); break;
1478 '_dump_tree_extended_format' : function(params) {
1482 for (var j = 0; j < obj.columns.length; j++) {
1483 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1486 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1489 ord_cols.sort( function(a,b) {
1490 if ( Number( a[0] ) < Number( b[0] ) ) return -1;
1491 if ( Number( a[0] ) > Number( b[0] ) ) return 1;
1495 function process_tree(treechildren) {
1496 for (var i = 0; i < treechildren.childNodes.length; i++) {
1497 var row = document.getElementById('offlineStrings').getString('list.dump_extended_format.record_separator') + '\r\n';
1498 var treeitem = treechildren.childNodes[i];
1499 var treerow = treeitem.firstChild;
1500 for (var j = 0; j < ord_cols.length; j++) {
1501 row += obj.columns[ ord_cols[j][1] ].label + ': ' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label') + '\r\n';
1503 _dump += row + '\r\n';
1504 if (treeitem.childNodes.length > 1) {
1505 process_tree(treeitem.lastChild);
1510 process_tree(this.treechildren);
1515 'dump_csv_to_clipboard' : function(params) {
1517 if (typeof params == 'undefined') params = {};
1518 if (params.no_full_retrieve) {
1519 copy_to_clipboard( obj.dump_csv( params ) );
1521 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1525 'dump_csv_to_printer' : function(params) {
1527 if (typeof params == 'undefined') params = {};
1528 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1529 if (params.no_full_retrieve) {
1530 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1532 obj.wrap_in_full_retrieve(
1534 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1540 'dump_extended_format_to_printer' : function(params) {
1542 if (typeof params == 'undefined') params = {};
1543 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1544 if (params.no_full_retrieve) {
1545 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1547 obj.wrap_in_full_retrieve(
1549 print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1555 'dump_csv_to_file' : function(params) {
1557 JSAN.use('util.file'); var f = new util.file();
1558 if (typeof params == 'undefined') params = {};
1559 if (params.no_full_retrieve) {
1560 params.data = obj.dump_csv( params );
1561 params.not_json = true;
1562 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1563 f.export_file( params );
1565 obj.wrap_in_full_retrieve(
1567 params.data = obj.dump_csv( params );
1568 params.not_json = true;
1569 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1570 f.export_file( params );
1576 'print' : function(params) {
1577 if (!params) params = {};
1578 switch(this.node.nodeName) {
1579 case 'tree' : return this._print_tree(params); break;
1580 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1584 '_print_tree' : function(params) {
1587 var data = obj.data; data.stash_retrieve();
1588 if (!params.staff && data.list.au && data.list.au[0]) {
1589 params.staff = data.list.au[0];
1591 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() ]) {
1592 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1593 params.lib.children(null);
1595 if (params.template && data.print_list_templates[ params.template ]) {
1596 var template = data.print_list_templates[ params.template ];
1597 if (template.inherit) {
1598 template = data.print_list_templates[ template.inherit ];
1599 // if someone wants to implement recursion later, feel free
1601 for (var i in template) params[i] = template[i];
1603 obj.wrap_in_full_retrieve(
1606 if (!params.list) params.list = obj.dump_with_keys();
1607 JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1608 print.tree_list( params );
1609 if (typeof params.callback == 'function') params.callback();
1611 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1617 obj.error.standard_unexpected_error_alert('_print_tree',E);
1621 'dump_selected_with_keys' : function(params) {
1623 switch(this.node.nodeName) {
1624 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1625 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1630 '_dump_tree_selection_with_keys' : function(params) {
1633 var list = obj._retrieve_selection_from_tree();
1634 for (var i = 0; i < list.length; i++) {
1636 var treeitem = list[i];
1637 var treerow = treeitem.firstChild;
1638 for (var j = 0; j < treerow.childNodes.length; j++) {
1639 var value = treerow.childNodes[j].getAttribute('label');
1640 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1641 if (typeof obj.columns[j] == 'undefined') {
1642 dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1643 dump('_dump_tree_selection_with_keys @ ' + location.href + '\n');
1644 dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1647 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1656 'clipboard' : function(params) {
1659 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1660 obj.data.stash_retrieve();
1661 obj.data.list_clipboard = dump; obj.data.stash('list_clipboard');
1662 JSAN.use('util.window'); var win = new util.window();
1663 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1664 window.focus(); // sometimes the main window will lower after a clipboard action
1666 this.error.standard_unexpected_error_alert('clipboard',E);
1670 'dump_retrieve_ids' : function(params) {
1672 switch(this.node.nodeName) {
1673 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1674 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1678 '_dump_retrieve_ids_tree' : function(params) {
1681 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1682 var treeitem = this.treechildren.childNodes[i];
1683 dump.push( treeitem.getAttribute('retrieve_id') );
1688 'wrap_in_full_retrieve' : function(f) {
1690 if (typeof obj.on_all_fleshed == 'function') { // legacy
1691 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1693 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1694 obj.on_all_fleshed.push(f);
1695 obj.full_retrieve();
1698 '_sort_tree' : function() {
1701 if (obj.node.getAttribute('no_sort')) {
1705 var sorts = [ obj.first_sort ].concat( obj.sub_sorts );
1706 var columns = util.functional.map_list(
1712 var column_positions = [];
1713 for (var i = 0; i < columns.length; i++) {
1714 for (var j = 0; j < obj.columns.length; j++) {
1715 if (obj.columns[j].id == columns[i].id) {
1716 column_positions.push( function(a){return a;}(j) );
1720 obj.wrap_in_full_retrieve(
1723 JSAN.use('util.money');
1725 var treeitems = obj.treechildren.childNodes;
1726 for (var i = 0; i < treeitems.length; i++) {
1727 var treeitem = treeitems[i];
1728 var treerow = treeitem.firstChild;
1730 function get_value(treecell) {
1733 ? treecell.getAttribute('label')
1735 'sort_value' : treecell ? treecell.hasAttribute('sort_value')
1737 treecell.getAttribute('sort_value'))
1744 for (var j = 0; j < column_positions.length; j++) {
1745 var treecell = treerow.childNodes[ column_positions[j] ];
1747 'position' : column_positions[j],
1748 'value' : get_value(treecell)
1757 rows = rows.sort( function(A,B) {
1758 function normalize(a,b,p) {
1765 if (obj.columns[p].getAttribute('sort_type')) {
1766 switch(obj.columns[p].getAttribute('sort_type')) {
1768 JSAN.use('util.date'); // to pull in dojo.date.locale
1769 a = dojo.date.locale.parse(a,{});
1770 b = dojo.date.locale.parse(b,{});
1773 a = Number(a); b = Number(b);
1776 a = util.money.dollars_float_to_cents_integer(a);
1777 b = util.money.dollars_float_to_cents_integer(b);
1779 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1780 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1781 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1784 a = String( a ).toUpperCase();
1785 b = String( b ).toUpperCase();
1789 if (typeof a == 'string' || typeof b == 'string') {
1790 a = String( a ).toUpperCase();
1791 b = String( b ).toUpperCase();
1798 for (var i = 0; i < sorts.length; i++) {
1800 if (sorts[i].sortDir == 'asc') {
1802 A['values'][i]['value'],
1803 B['values'][i]['value'],
1804 A['values'][i]['position']
1808 B['values'][i]['value'],
1809 A['values'][i]['value'],
1810 A['values'][i]['position']
1813 if (values[0] < values[1] ) {
1816 if (values[0] > values[1] ) {
1822 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1823 for (var i = 0; i < rows.length; i++) {
1824 obj.treechildren.appendChild( rows[i].node );
1826 if (typeof obj.on_sort == 'function') obj.on_sort();
1828 obj.error.standard_unexpected_error_alert('sorting',E);
1830 obj.refresh_ordinals();
1834 obj.error.standard_unexpected_error_alert('pre sorting', E);
1838 '_toggle_checkbox_column' : function(col,toggle) {
1841 if (obj.node.getAttribute('no_toggle')) {
1845 for (var i = 0; i < obj.columns.length; i++) {
1846 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1848 var treeitems = obj.treechildren.childNodes;
1849 for (var i = 0; i < treeitems.length; i++) {
1850 var treeitem = treeitems[i];
1851 var treerow = treeitem.firstChild;
1852 var treecell = treerow.childNodes[ col_pos ];
1853 treecell.setAttribute('value',(toggle == 'on'));
1855 if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1857 obj.error.standard_unexpected_error_alert('pre toggle', E);
1861 'render_list_actions' : function(params) {
1863 switch(this.node.nodeName) {
1864 case 'tree' : return this._render_list_actions_for_tree(params); break;
1865 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1869 '_render_list_actions_for_tree' : function(params) {
1872 var btn = document.createElement('button');
1873 btn.setAttribute('id',obj.node.id + '_list_actions');
1874 btn.setAttribute('type','menu');
1875 btn.setAttribute('allowevents','true');
1876 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1877 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1878 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1879 var mp = document.createElement('menupopup');
1880 btn.appendChild(mp);
1881 var mi = document.createElement('menuitem');
1882 mi.setAttribute('id',obj.node.id + '_clipfield');
1883 mi.setAttribute('disabled','true');
1884 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1885 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1887 mi = document.createElement('menuitem');
1888 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1889 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1890 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1892 mi = document.createElement('menuitem');
1893 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1894 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1895 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1897 mi = document.createElement('menuitem');
1898 mi.setAttribute('id',obj.node.id + '_extended_to_printer');
1899 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.label'));
1900 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.accesskey'));
1902 mi = document.createElement('menuitem');
1903 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1904 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1905 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1907 mi = document.createElement('menuitem');
1908 mi.setAttribute('id',obj.node.id + '_save_columns');
1909 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1910 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1911 if (obj.data.hash.aous['gui.disable_local_save_columns']) {
1912 mi.setAttribute('disabled','true');
1917 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1921 'set_list_actions' : function(params) {
1923 switch(this.node.nodeName) {
1924 case 'tree' : return this._set_list_actions_for_tree(params); break;
1925 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1929 '_set_list_actions_for_tree' : function(params) {
1930 // This should be called after the button element from render_list_actions has been appended to the DOM
1933 var x = document.getElementById(obj.node.id + '_clipfield');
1938 obj.clipboard(params);
1939 if (params && typeof params.on_complete == 'function') {
1940 params.on_complete(params);
1946 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1951 obj.dump_csv_to_clipboard(params);
1952 if (params && typeof params.on_complete == 'function') {
1953 params.on_complete(params);
1959 x = document.getElementById(obj.node.id + '_csv_to_printer');
1964 obj.dump_csv_to_printer(params);
1965 if (params && typeof params.on_complete == 'function') {
1966 params.on_complete(params);
1972 x = document.getElementById(obj.node.id + '_extended_to_printer');
1977 obj.dump_extended_format_to_printer(params);
1978 if (params && typeof params.on_complete == 'function') {
1979 params.on_complete(params);
1986 x = document.getElementById(obj.node.id + '_csv_to_file');
1991 obj.dump_csv_to_file(params);
1992 if (params && typeof params.on_complete == 'function') {
1993 params.on_complete(params);
1999 x = document.getElementById(obj.node.id + '_save_columns');
2004 obj.save_columns(params);
2005 if (params && typeof params.on_complete == 'function') {
2006 params.on_complete(params);
2014 obj.error.standard_unexpected_error_alert('setting list actions',E);
2018 // Takes fieldmapper class name and attempts to spit out column definitions suitable for .init
2019 'fm_columns' : function(hint,column_extras,prefix) {
2022 if (!prefix) { prefix = ''; }
2024 // requires the dojo library fieldmapper.autoIDL
2025 if (typeof fieldmapper == 'undefined') { throw 'fieldmapper undefined'; }
2026 if (typeof fieldmapper.IDL == 'undefined') { throw 'fieldmapper.IDL undefined'; }
2027 if (typeof fieldmapper.IDL.fmclasses == 'undefined') { throw 'fieldmapper.IDL.fmclasses undefined'; }
2028 if (typeof fieldmapper.IDL.fmclasses[hint] == 'undefined') { throw 'fieldmapper.IDL.fmclasses.' + hint + ' undefined'; }
2029 var my_class = fieldmapper.IDL.fmclasses[hint];
2030 var data = obj.data; data.stash_retrieve();
2032 function col_def(my_field) {
2033 var col_id = prefix + hint + '_' + my_field.name;
2035 var datafield = my_field.name;
2036 var fleshed_display_field;
2037 if (column_extras) {
2038 if (column_extras['*']) {
2039 if (column_extras['*']['dataobj']) {
2040 dataobj = column_extras['*']['dataobj'];
2043 if (column_extras[col_id]) {
2044 if (column_extras[col_id]['dataobj']) {
2045 dataobj = column_extras[col_id]['dataobj'];
2047 if (column_extras[col_id]['datafield']) {
2048 datafield = column_extras[col_id]['datafield'];
2050 if (column_extras[col_id]['fleshed_display_field']) {
2051 fleshed_display_field = column_extras[col_id]['fleshed_display_field'];
2057 'label' : my_field.label || my_field.name,
2058 'sort_type' : [ 'int', 'float', 'id', 'number' ].indexOf(my_field.datatype) > -1 ? 'number' :
2059 ( my_field.datatype == 'money' ? 'money' :
2060 ( my_field.datatype == 'timestamp' ? 'date' : 'default')),
2061 'hidden' : my_field.virtual || my_field.datatype == 'link',
2064 // my_field.datatype => bool float id int interval link money number org_unit text timestamp
2065 if (my_field.datatype == 'link') {
2066 def.render = function(my) {
2067 // is the object fleshed?
2068 return my[dataobj][datafield]() && typeof my[dataobj][datafield]() == 'object'
2069 // yes, show the display field
2070 ? my[dataobj][datafield]()[fleshed_display_field||my_field.key]()
2071 // no, do we have its class in data.hash?
2072 : ( typeof data.hash[ my[dataobj].Structure.field_map[datafield].class ] != 'undefined'
2073 // yes, do we have this particular object cached?
2074 ? ( data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ]
2075 // yes, show the display field
2076 ? data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ][
2077 fleshed_display_field||my_field.key
2079 // no, just show the raw value
2080 : my[dataobj][datafield]()
2082 // no, just show the raw value
2083 : my[dataobj][datafield]()
2087 def.render = function(my) { return my[dataobj][datafield](); }
2089 if (my_field.datatype == 'timestamp') {
2090 JSAN.use('util.date');
2091 def.render = function(my) {
2092 return util.date.formatted_date( my[dataobj][datafield](), '%{localized}' );
2094 def.sort_value = function(my) {
2095 return util.date.db_date2Date( my[dataobj][datafield]() ).getTime();
2098 if (my_field.datatype == 'org_unit') {
2099 def.render = function(my) {
2100 return typeof my[dataobj][datafield]() == 'object' ? my[dataobj][datafield]().shortname() : data.hash.aou[ my[dataobj][datafield]() ].shortname();
2103 if (my_field.datatype == 'money') {
2104 JSAN.use('util.money');
2105 def.render = function(my) {
2106 return util.money.sanitize( my[dataobj][datafield]() );
2108 def.sort_value = function(my) {
2109 return util.money.dollars_float_to_cents_integer( my[dataobj][datafield]() );
2112 if (column_extras) {
2113 if (column_extras['*']) {
2114 for (var attr in column_extras['*']) {
2115 def[attr] = column_extras['*'][attr];
2117 if (column_extras['*']['expanded_label']) {
2118 def.label = my_class.label + ': ' + def.label;
2120 if (column_extras['*']['label_prefix']) {
2121 def.label = column_extras['*']['label_prefix'] + def.label;
2123 if (column_extras['*']['remove_virtual']) {
2124 if (my_field.virtual) {
2125 def.remove_me = true;
2129 if (column_extras[col_id]) {
2130 for (var attr in column_extras[col_id]) {
2131 def[attr] = column_extras[col_id][attr];
2133 if (column_extras[col_id]['keep_me']) {
2134 def.remove_me = false;
2136 if (column_extras[col_id]['label_prefix']) {
2137 def.label = column_extras[col_id]['label_prefix'] + def.label;
2141 if (def.remove_me) {
2142 dump('Skipping ' + def.label + '\n');
2145 dump('Defining ' + def.label + '\n');
2150 for (var i = 0; i < my_class.fields.length; i++) {
2151 var my_field = my_class.fields[i];
2152 var def = col_def(my_field);
2154 columns.push( def );
2159 obj.error.standard_unexpected_error_alert('fm_columns()',E);
2163 // Default for the map_row_to_columns function for .init
2164 'std_map_row_to_columns' : function(error_value) {
2165 return function(row,cols,scratch) {
2166 // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
2167 // cols contains all of the objects listed above in columns
2168 // scratch is a temporary space shared by all cells/rows (or just per row if not explicitly passed in)
2169 if (!scratch) { scratch = {}; }
2172 JSAN.use('util.error'); obj.error = new util.error();
2173 JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
2174 JSAN.use('util.network'); obj.network = new util.network();
2175 JSAN.use('util.money');
2179 var sort_values = [];
2182 for (var i = 0; i < cols.length; i++) {
2183 switch (typeof cols[i].render) {
2184 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;
2185 case 'string' : cmd += 'try { ' + cols[i].render + '; values['+i+'] = v; } catch(E) { values['+i+'] = error_value; }'; break;
2186 default: cmd += 'values['+i+'] = "??? '+(typeof cols[i].render)+'"; ';
2188 switch (typeof cols[i].sort_value) {
2191 sort_values[i] = cols[i].sort_value(my,scratch);
2193 sort_values[i] = error_value;
2194 obj.error.sdump('D_COLUMN_RENDER_ERROR',E);
2199 + cols[i].sort_value
2202 +'] = v; } catch(E) { sort_values['
2204 + '] = error_value; }';
2207 cmd += 'sort_values['+i+'] = values[' + i + '];';
2210 if (cmd) eval( cmd );
2212 obj.error.sdump('D_WARN','map_row_to_column: ' + E);
2213 if (error_value) { value = error_value; } else { value = ' ' };
2215 return { 'values' : values, 'sort_values' : sort_values };
2219 dump('exiting util.list.js\n');