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 if (!this.node) throw('Could not find element ' + id);
11 switch(this.node.nodeName) {
15 throw(this.node.nodeName + ' not yet supported'); break;
16 default: throw(this.node.nodeName + ' not supported'); break;
19 JSAN.use('util.error'); this.error = new util.error();
24 util.list.prototype = {
26 'init' : function (params) {
30 JSAN.use('util.widgets');
32 if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
33 if (typeof params.map_row_to_columns == 'function') obj.map_row_to_columns = params.map_row_to_columns;
34 if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
37 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
39 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
40 obj.columns = params.columns;
42 switch(obj.node.nodeName) {
43 case 'tree' : obj._init_tree(params); break;
44 case 'listbox' : obj._init_listbox(params); break;
45 default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
49 '_init_tree' : function (params) {
53 this.treechildren = this.node.lastChild;
56 var treecols = document.createElement('treecols');
57 this.node.appendChild(treecols);
58 this.treecols = treecols;
60 for (var i = 0; i < this.columns.length; i++) {
61 var treecol = document.createElement('treecol');
62 for (var j in this.columns[i]) {
63 treecol.setAttribute(j,this.columns[i][j]);
65 treecols.appendChild(treecol);
66 treecol.addEventListener(
70 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
71 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
72 ev.target.setAttribute('sortDir',sortDir);
73 obj._sort_tree(ev.target,sortDir);
76 if (obj.row_count.total != obj.row_count.fleshed && (obj.row_count.total - obj.row_count.fleshed) > 50) {
77 var r = window.confirm(document.getElementById('offlineStrings').getFormattedString('list.row_fetch_warning',[obj.row_count.fleshed,obj.row_count.total]));
80 setTimeout( do_it, 0 );
83 setTimeout( do_it, 0 );
88 var splitter = document.createElement('splitter');
89 splitter.setAttribute('class','tree-splitter');
90 treecols.appendChild(splitter);
93 var treechildren = document.createElement('treechildren');
94 this.node.appendChild(treechildren);
95 this.treechildren = treechildren;
97 this.node.addEventListener(
100 if (typeof params.on_select == 'function') {
101 params.on_select(ev);
103 var x = document.getElementById(obj.node.id + '_clipfield');
105 var sel = obj.retrieve_selection();
106 x.setAttribute('disabled', sel.length == 0);
111 if (typeof params.on_click == 'function') {
112 this.node.addEventListener(
119 this.node.addEventListener(
121 function(ev) { obj.detect_visible(); },
125 this.node.addEventListener(
127 function(ev) { obj.auto_retrieve(); },
130 this.node.addEventListener(
132 function(ev) { obj.auto_retrieve(); },
135 window.addEventListener(
137 function(ev) { obj.auto_retrieve(); },
140 /* FIXME -- find events on scrollbar to trigger this */
141 obj.detect_visible_polling();
143 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
144 var slider = document.getAnonymousNodes( scrollbar )[2];
145 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
146 scrollbar.addEventListener('click',function(){alert('sb click');},false);
147 scrollbar.addEventListener('command',function(){alert('sb command');},false);
148 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
149 slider.addEventListener('click',function(){alert('slider click');},false);
150 slider.addEventListener('command',function(){alert('slider command');},false);
151 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
153 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
155 this.restores_columns(params);
158 '_init_listbox' : function (params) {
161 var listhead = document.createElement('listhead');
162 this.node.appendChild(listhead);
164 var listcols = document.createElement('listcols');
165 this.node.appendChild(listcols);
167 for (var i = 0; i < this.columns.length; i++) {
168 var listheader = document.createElement('listheader');
169 listhead.appendChild(listheader);
170 var listcol = document.createElement('listcol');
171 listcols.appendChild(listcol);
172 for (var j in this.columns[i]) {
173 listheader.setAttribute(j,this.columns[i][j]);
174 listcol.setAttribute(j,this.columns[i][j]);
180 'save_columns' : function (params) {
182 switch (this.node.nodeName) {
183 case 'tree' : this._save_columns_tree(params); break;
184 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
188 '_save_columns_tree' : function (params) {
191 var id = obj.node.getAttribute('id'); if (!id) {
192 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
196 var nl = obj.node.getElementsByTagName('treecol');
197 for (var i = 0; i < nl.length; i++) {
199 var col_id = col.getAttribute('id');
201 alert('FIXME: A column in this list does not have an id and cannot be saved');
204 var col_hidden = col.getAttribute('hidden');
205 var col_width = col.getAttribute('width');
206 var col_ordinal = col.getAttribute('ordinal');
207 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
209 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
210 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
211 file.set_object(my_cols);
213 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
215 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
219 'restores_columns' : function (params) {
221 switch (this.node.nodeName) {
222 case 'tree' : this._restores_columns_tree(params); break;
223 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
227 '_restores_columns_tree' : function (params) {
230 var id = obj.node.getAttribute('id'); if (!id) {
231 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
235 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
236 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
237 if (file._file.exists()) {
238 var my_cols = file.get_object(); file.close();
239 var nl = obj.node.getElementsByTagName('treecol');
240 for (var i = 0; i < nl.length; i++) {
242 var col_id = col.getAttribute('id');
244 alert('FIXME: A column in this list does not have an id and cannot be saved');
247 if (typeof my_cols[col_id] != 'undefined') {
248 col.setAttribute('hidden',my_cols[col_id].hidden);
249 col.setAttribute('width',my_cols[col_id].width);
250 col.setAttribute('ordinal',my_cols[col_id].ordinal);
252 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
257 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
261 'clear' : function (params) {
263 switch (this.node.nodeName) {
264 case 'tree' : this._clear_tree(params); break;
265 case 'listbox' : this._clear_listbox(params); break;
266 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
268 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
269 this.row_count.total = 0;
270 this.row_count.fleshed = 0;
271 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
274 '_clear_tree' : function(params) {
276 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
277 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
279 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
280 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
282 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
285 '_clear_listbox' : function(params) {
288 var nl = this.node.getElementsByTagName('listitem');
289 for (var i = 0; i < nl.length; i++) {
292 for (var i = 0; i < items.length; i++) {
293 this.node.removeChild(items[i]);
297 'append' : function (params) {
300 switch (this.node.nodeName) {
301 case 'tree' : rparams = this._append_to_tree(params); break;
302 case 'listbox' : rparams = this._append_to_listbox(params); break;
303 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
305 if (rparams && params.attributes) {
306 for (var i in params.attributes) {
307 rparams.my_node.setAttribute(i,params.attributes[i]);
310 this.row_count.total++;
311 if (this.row_count.fleshed == this.row_count.total) {
312 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
317 'refresh_row' : function (params) {
320 switch (this.node.nodeName) {
321 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
322 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
324 if (rparams && params.attributes) {
325 for (var i in params.attributes) {
326 rparams.my_node.setAttribute(i,params.attributes[i]);
329 this.row_count.fleshed--;
334 '_append_to_tree' : function (params) {
338 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
340 var s = ('util.list.append: params = ' + (params) + '\n');
342 var treechildren_node = this.treechildren;
344 if (params.node && params.node.nodeName == 'treeitem') {
345 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
346 if (params.node.lastChild.nodeName == 'treechildren') {
347 treechildren_node = params.node.lastChild;
349 treechildren_node = document.createElement('treechildren');
350 params.node.appendChild(treechildren_node);
354 var treeitem = document.createElement('treeitem');
355 treeitem.setAttribute('retrieve_id',params.retrieve_id);
356 if (typeof params.to_bottom != 'undefined') {
357 treechildren_node.appendChild( treeitem );
358 if (typeof params.no_auto_select == 'undefined') {
359 if (!obj.auto_select_pending) {
360 obj.auto_select_pending = true;
361 setTimeout(function() {
362 dump('auto-selecting\n');
363 var idx = Number(obj.node.view.rowCount)-1;
364 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
365 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
366 obj.auto_select_pending = false;
367 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
372 if (treechildren_node.firstChild) {
373 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
375 treechildren_node.appendChild( treeitem );
377 if (typeof params.no_auto_select == 'undefined') {
378 if (!obj.auto_select_pending) {
379 obj.auto_select_pending = true;
380 setTimeout(function() {
381 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
382 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
383 obj.auto_select_pending = false;
384 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
389 var treerow = document.createElement('treerow');
390 treeitem.appendChild( treerow );
391 treerow.setAttribute('retrieve_id',params.retrieve_id);
393 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
394 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
396 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
398 obj.put_retrieving_label(treerow);
399 treerow.addEventListener(
403 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
405 treerow.setAttribute('retrieved','true');
407 //dump('fleshing = ' + params.retrieve_id + '\n');
409 function inc_fleshed() {
410 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
411 treerow.setAttribute('fleshed','true');
412 obj.row_count.fleshed++;
413 if (obj.row_count.fleshed >= obj.row_count.total) {
414 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
418 params.row_node = treeitem;
419 params.on_retrieve = function(p) {
422 obj._map_row_to_treecell(p,treerow);
424 var idx = obj.node.contentView.getIndexOfItem( params.row_node );
425 dump('idx = ' + idx + '\n');
426 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
427 if ( obj.node.view.selection.isSelected( idx ) ) {
428 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
429 util.widgets.dispatch('select',obj.node);
432 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
433 dump('fixme2: ' + E + '\n');
437 if (typeof params.retrieve_row == 'function') {
439 params.retrieve_row( params );
441 } else if (typeof obj.retrieve_row == 'function') {
443 obj.retrieve_row( params );
455 util.widgets.dispatch('flesh',treerow);
460 obj.put_retrieving_label(treerow);
461 treerow.addEventListener(
464 //dump('fleshing anon\n');
465 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
466 obj._map_row_to_treecell(params,treerow);
467 treerow.setAttribute('retrieved','true');
468 treerow.setAttribute('fleshed','true');
469 obj.row_count.fleshed++;
470 if (obj.row_count.fleshed >= obj.row_count.total) {
471 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
479 util.widgets.dispatch('flesh',treerow);
484 this.error.sdump('D_LIST',s);
488 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
490 //if (typeof params.to_bottom != 'undefined')
491 if (typeof params.to_top == 'undefined') {
492 treechildren_node.removeChild( treechildren_node.firstChild );
494 treechildren_node.removeChild( treechildren_node.lastChild );
500 setTimeout( function() { obj.auto_retrieve(); }, 0 );
502 params.my_node = treeitem;
506 '_refresh_row_in_tree' : function (params) {
510 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
511 if (typeof params.my_node == 'undefined') throw('util.list.refresh_row: Object must contain a my_node');
512 if (params.my_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: my_node must be a treeitem');
514 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
516 var treeitem = params.my_node;
517 treeitem.setAttribute('retrieve_id',params.retrieve_id);
518 if (typeof params.to_bottom != 'undefined') {
519 if (typeof params.no_auto_select == 'undefined') {
520 if (!obj.auto_select_pending) {
521 obj.auto_select_pending = true;
522 setTimeout(function() {
523 dump('auto-selecting\n');
524 var idx = Number(obj.node.view.rowCount)-1;
525 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
526 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
527 obj.auto_select_pending = false;
528 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
534 for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
535 for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
536 var treerow = document.createElement('treerow');
537 treeitem.appendChild( treerow );
538 treerow.setAttribute('retrieve_id',params.retrieve_id);
540 s += ('tree = ' + this.node + '\n');
541 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
543 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
545 obj.put_retrieving_label(treerow);
546 treerow.addEventListener(
550 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
552 treerow.setAttribute('retrieved','true');
554 //dump('fleshing = ' + params.retrieve_id + '\n');
556 function inc_fleshed() {
557 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
558 treerow.setAttribute('fleshed','true');
559 obj.row_count.fleshed++;
560 if (obj.row_count.fleshed >= obj.row_count.total) {
561 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
565 params.row_node = treeitem;
566 params.on_retrieve = function(p) {
569 obj._map_row_to_treecell(p,treerow);
571 var idx = obj.node.contentView.getIndexOfItem( params.row_node );
572 dump('idx = ' + idx + '\n');
573 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
574 if ( obj.node.view.selection.isSelected( idx ) ) {
575 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
576 util.widgets.dispatch('select',obj.node);
579 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
580 dump('fixme2: ' + E + '\n');
584 if (typeof params.retrieve_row == 'function') {
586 params.retrieve_row( params );
588 } else if (typeof obj.retrieve_row == 'function') {
590 obj.retrieve_row( params );
602 util.widgets.dispatch('flesh',treerow);
607 obj.put_retrieving_label(treerow);
608 treerow.addEventListener(
611 //dump('fleshing anon\n');
612 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
613 obj._map_row_to_treecell(params,treerow);
614 treerow.setAttribute('retrieved','true');
615 treerow.setAttribute('fleshed','true');
616 obj.row_count.fleshed++;
617 if (obj.row_count.fleshed >= obj.row_count.total) {
618 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
626 util.widgets.dispatch('flesh',treerow);
631 this.error.sdump('D_LIST',s);
635 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
637 //if (typeof params.to_bottom != 'undefined')
638 if (typeof params.to_top == 'undefined') {
639 treechildren_node.removeChild( treechildren_node.firstChild );
641 treechildren_node.removeChild( treechildren_node.lastChild );
647 setTimeout( function() { obj.auto_retrieve(); }, 0 );
649 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
654 'put_retrieving_label' : function(treerow) {
659 dump('put_retrieving_label. columns = ' + js2JSON(obj.columns) + '\n');
660 while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
661 dump('\t' + cols_idx);
662 var treecell = document.createElement('treecell');
663 treerow.appendChild(treecell);
667 for (var i = 0; i < obj.columns.length; i++) {
668 var treecell = document.createElement('treecell'); treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
669 treerow.appendChild(treecell);
672 dump('\t' + cols_idx + '\n');
679 'detect_visible' : function() {
682 //dump('detect_visible obj.node = ' + obj.node + '\n');
683 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
685 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
686 var curpos = scrollbar.getAttribute('curpos');
687 var maxpos = scrollbar.getAttribute('maxpos');
688 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
689 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
690 if ( obj.auto_retrieve() > 0 ) {
691 obj.curpos = curpos; obj.maxpos = maxpos;
695 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
697 } catch(E) { obj.error.sdump('D_ERROR',E); }
700 'detect_visible_polling' : function() {
702 //alert('detect_visible_polling');
704 obj.detect_visible();
705 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
712 'auto_retrieve' : function(params) {
714 switch (this.node.nodeName) {
715 case 'tree' : obj._auto_retrieve_tree(params); break;
716 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
720 '_auto_retrieve_tree' : function (params) {
722 if (!obj.auto_retrieve_in_progress) {
723 obj.auto_retrieve_in_progress = true;
727 //alert('auto_retrieve\n');
729 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
730 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
731 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
732 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
733 for (var i = startpos; i < endpos + 4; i++) {
735 //dump('trying index ' + i + '\n');
736 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
737 if (item && item.getAttribute('retrieved') != 'true' ) {
738 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
739 util.widgets.dispatch('flesh',item); count++;
742 //dump(i + ' : ' + E + '\n');
745 obj.auto_retrieve_in_progress = false;
747 } catch(E) { alert(E); }
753 'exec_on_all_fleshed' : function() {
756 if (obj.on_all_fleshed) {
757 if (typeof obj.on_all_fleshed == 'function') {
758 dump('exec_on_all_fleshed == function\n');
761 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
764 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
765 dump('exec_on_all_fleshed == array\n');
769 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
770 var f = obj.on_all_fleshed.pop();
771 if (typeof f == 'function') {
772 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); }
774 if (obj.on_all_fleshed.length > 0) arguments.callee();
776 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
781 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
785 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
789 'full_retrieve' : function(params) {
791 switch (this.node.nodeName) {
792 case 'tree' : obj._full_retrieve_tree(params); break;
793 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
797 '_full_retrieve_tree' : function(params) {
800 if (obj.row_count.fleshed >= obj.row_count.total) {
801 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
802 obj.exec_on_all_fleshed();
804 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
805 JSAN.use('util.widgets');
806 var nodes = obj.treechildren.childNodes;
807 for (var i = 0; i < nodes.length; i++) {
808 util.widgets.dispatch('flesh',nodes[i].firstChild);
812 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
816 '_append_to_listbox' : function (params) {
820 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
822 var s = ('util.list.append: params = ' + (params) + '\n');
824 var listitem = document.createElement('listitem');
826 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
828 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
832 listitem.setAttribute('retrieve_id',params.retrieve_id);
833 //FIXME//Make async and fire when row is visible in list
836 params.row_node = listitem;
837 params.on_retrieve = function(row) {
839 obj._map_row_to_listcell(params,listitem);
840 obj.node.appendChild( listitem );
841 util.widgets.dispatch('select',obj.node);
844 if (typeof params.retrieve_row == 'function') {
846 row = params.retrieve_row( params );
850 if (typeof obj.retrieve_row == 'function') {
852 row = obj.retrieve_row( params );
859 this._map_row_to_listcell(params,listitem);
860 this.node.appendChild( listitem );
863 this.error.sdump('D_LIST',s);
864 params.my_node = listitem;
869 '_map_row_to_treecell' : function(params,treerow) {
872 util.widgets.remove_children(treerow);
874 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
876 for (var i = 0; i < this.columns.length; i++) {
877 var treecell = document.createElement('treecell');
878 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
880 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
881 treecell.setAttribute('label',label);
882 treerow.appendChild( treecell );
883 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
886 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
887 treecell.setAttribute('label',label);
888 treerow.appendChild( treecell );
889 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
893 if (typeof params.map_row_to_column == 'function') {
895 label = params.map_row_to_column(params.row,this.columns[i]);
897 } else if (typeof this.map_row_to_column == 'function') {
899 label = this.map_row_to_column(params.row,this.columns[i]);
902 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
903 treerow.appendChild( treecell );
904 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
906 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
910 if (typeof params.map_row_to_columns == 'function') {
912 labels = params.map_row_to_columns(params.row,this.columns);
914 } else if (typeof this.map_row_to_columns == 'function') {
916 labels = this.map_row_to_columns(params.row,this.columns);
919 for (var i = 0; i < labels.length; i++) {
920 var treecell = document.createElement('treecell');
921 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
922 if ( this.columns[i].type == 'checkbox') {
923 treecell.setAttribute('value', labels[i]);
925 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
927 treerow.appendChild( treecell );
928 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
933 throw('No row to column mapping function.');
935 this.error.sdump('D_LIST',s);
938 '_map_row_to_listcell' : function(params,listitem) {
941 for (var i = 0; i < this.columns.length; i++) {
943 if (typeof params.map_row_to_column == 'function') {
945 value = params.map_row_to_column(params.row,this.columns[i]);
949 if (typeof this.map_row_to_column == 'function') {
951 value = this.map_row_to_column(params.row,this.columns[i]);
954 if (typeof value == 'string' || typeof value == 'number') {
955 var listcell = document.createElement('listcell');
956 listcell.setAttribute('label',value);
957 listitem.appendChild(listcell);
958 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
960 listitem.appendChild(value);
961 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
964 this.error.sdump('D_LIST',s);
967 'select_all' : function(params) {
969 switch(this.node.nodeName) {
970 case 'tree' : return this._select_all_from_tree(params); break;
971 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
975 '_select_all_from_tree' : function(params) {
977 this.node.view.selection.selectAll();
980 'retrieve_selection' : function(params) {
982 switch(this.node.nodeName) {
983 case 'tree' : return this._retrieve_selection_from_tree(params); break;
984 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
988 '_retrieve_selection_from_tree' : function(params) {
991 var start = new Object();
992 var end = new Object();
993 var numRanges = this.node.view.selection.getRangeCount();
994 for (var t=0; t<numRanges; t++){
995 this.node.view.selection.getRangeAt(t,start,end);
996 for (var v=start.value; v<=end.value; v++){
997 var i = this.node.contentView.getItemAtIndex(v);
1004 'dump' : function(params) {
1006 switch(this.node.nodeName) {
1007 case 'tree' : return this._dump_tree(params); break;
1008 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1012 '_dump_tree' : function(params) {
1015 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1017 var treeitem = this.treechildren.childNodes[i];
1018 var treerow = treeitem.firstChild;
1019 for (var j = 0; j < treerow.childNodes.length; j++) {
1020 row.push( treerow.childNodes[j].getAttribute('label') );
1027 'dump_with_keys' : function(params) {
1029 switch(this.node.nodeName) {
1030 case 'tree' : return this._dump_tree_with_keys(params); break;
1031 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1036 '_dump_tree_with_keys' : function(params) {
1039 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1041 var treeitem = this.treechildren.childNodes[i];
1042 var treerow = treeitem.firstChild;
1043 for (var j = 0; j < treerow.childNodes.length; j++) {
1044 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1051 'dump_csv' : function(params) {
1053 switch(this.node.nodeName) {
1054 case 'tree' : return this._dump_tree_csv(params); break;
1055 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1060 '_dump_tree_csv' : function(params) {
1063 for (var j = 0; j < obj.columns.length; j++) {
1064 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1067 if (dump) dump += ',';
1068 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1072 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1074 var treeitem = this.treechildren.childNodes[i];
1075 var treerow = treeitem.firstChild;
1076 for (var j = 0; j < treerow.childNodes.length; j++) {
1077 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1080 if (row) row += ',';
1081 row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1084 dump += row + '\r\n';
1089 'dump_csv_to_clipboard' : function(params) {
1091 if (typeof params == 'undefined') params = {};
1092 if (params.no_full_retrieve) {
1093 copy_to_clipboard( obj.dump_csv( params ) );
1095 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1099 'dump_csv_to_printer' : function(params) {
1101 JSAN.use('util.print'); var print = new util.print();
1102 if (typeof params == 'undefined') params = {};
1103 if (params.no_full_retrieve) {
1104 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1106 obj.wrap_in_full_retrieve(
1108 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1114 'dump_csv_to_file' : function(params) {
1116 JSAN.use('util.file'); var f = new util.file();
1117 if (typeof params == 'undefined') params = {};
1118 if (params.no_full_retrieve) {
1119 params.data = obj.dump_csv( params );
1120 params.not_json = true;
1121 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1122 f.export_file( params );
1124 obj.wrap_in_full_retrieve(
1126 params.data = obj.dump_csv( params );
1127 params.not_json = true;
1128 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1129 f.export_file( params );
1135 'print' : function(params) {
1136 if (!params) params = {};
1137 switch(this.node.nodeName) {
1138 case 'tree' : return this._print_tree(params); break;
1139 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1143 '_print_tree' : function(params) {
1146 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1147 if (!params.staff && data.list.au && data.list.au[0]) {
1148 params.staff = data.list.au[0];
1150 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() ]) {
1151 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1152 params.lib.children(null);
1154 if (params.template && data.print_list_templates[ params.template ]) {
1155 var template = data.print_list_templates[ params.template ];
1156 for (var i in template) params[i] = template[i];
1158 obj.wrap_in_full_retrieve(
1161 if (!params.list) params.list = obj.dump_with_keys();
1162 JSAN.use('util.print'); var print = new util.print();
1163 print.tree_list( params );
1164 if (typeof params.callback == 'function') params.callback();
1166 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1172 obj.error.standard_unexpected_error_alert('_print_tree',E);
1176 'dump_selected_with_keys' : function(params) {
1178 switch(this.node.nodeName) {
1179 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1180 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1185 '_dump_tree_selection_with_keys' : function(params) {
1188 var list = obj._retrieve_selection_from_tree();
1189 for (var i = 0; i < list.length; i++) {
1191 var treeitem = list[i];
1192 var treerow = treeitem.firstChild;
1193 for (var j = 0; j < treerow.childNodes.length; j++) {
1194 var value = treerow.childNodes[j].getAttribute('label');
1195 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1196 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1204 'clipboard' : function(params) {
1207 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1208 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1209 data.list_clipboard = dump; data.stash('list_clipboard');
1210 JSAN.use('util.window'); var win = new util.window();
1211 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1212 window.focus(); // sometimes the main window will lower after a clipboard action
1214 this.error.standard_unexpected_error_alert('clipboard',E);
1218 'dump_retrieve_ids' : function(params) {
1220 switch(this.node.nodeName) {
1221 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1222 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1226 '_dump_retrieve_ids_tree' : function(params) {
1229 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1230 var treeitem = this.treechildren.childNodes[i];
1231 dump.push( treeitem.getAttribute('retrieve_id') );
1236 'wrap_in_full_retrieve' : function(f) {
1238 if (typeof obj.on_all_fleshed == 'function') { // legacy
1239 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1241 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1242 obj.on_all_fleshed.push(f);
1243 obj.full_retrieve();
1246 '_sort_tree' : function(col,sortDir) {
1249 if (obj.node.getAttribute('no_sort')) {
1253 for (var i = 0; i < obj.columns.length; i++) {
1254 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1256 obj.wrap_in_full_retrieve(
1259 JSAN.use('util.money');
1261 var treeitems = obj.treechildren.childNodes;
1262 for (var i = 0; i < treeitems.length; i++) {
1263 var treeitem = treeitems[i];
1264 var treerow = treeitem.firstChild;
1265 var treecell = treerow.childNodes[ col_pos ];
1266 value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1269 rows = rows.sort( function(a,b) {
1270 a = a.value; b = b.value;
1271 if (col.getAttribute('sort_type')) {
1272 switch(col.getAttribute('sort_type')) {
1274 a = Number(a); b = Number(b);
1277 a = util.money.dollars_float_to_cents_integer(a);
1278 b = util.money.dollars_float_to_cents_integer(b);
1280 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1281 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1282 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1285 a = String( a ).toUpperCase();
1286 b = String( b ).toUpperCase();
1290 if (typeof a == 'string' || typeof b == 'string') {
1291 a = String( a ).toUpperCase();
1292 b = String( b ).toUpperCase();
1295 if (a < b) return -1;
1296 if (a > b) return 1;
1299 if (sortDir == 'asc') rows = rows.reverse();
1300 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1301 for (var i = 0; i < rows.length; i++) {
1302 obj.treechildren.appendChild( rows[i].node );
1305 obj.error.standard_unexpected_error_alert('sorting',E);
1310 obj.error.standard_unexpected_error_alert('pre sorting', E);
1314 'render_list_actions' : function(params) {
1316 switch(this.node.nodeName) {
1317 case 'tree' : return this._render_list_actions_for_tree(params); break;
1318 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1322 '_render_list_actions_for_tree' : function(params) {
1325 var btn = document.createElement('button');
1326 btn.setAttribute('id',obj.node.id + '_list_actions');
1327 btn.setAttribute('type','menu');
1328 btn.setAttribute('allowevents','true');
1329 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1330 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1331 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1332 var mp = document.createElement('menupopup');
1333 btn.appendChild(mp);
1334 var mi = document.createElement('menuitem');
1335 mi.setAttribute('id',obj.node.id + '_clipfield');
1336 mi.setAttribute('disabled','true');
1337 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1338 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1340 mi = document.createElement('menuitem');
1341 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1342 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1343 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1345 mi = document.createElement('menuitem');
1346 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1347 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1348 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1350 mi = document.createElement('menuitem');
1351 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1352 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1353 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1355 mi = document.createElement('menuitem');
1356 mi.setAttribute('id',obj.node.id + '_save_columns');
1357 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1358 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1362 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1366 'set_list_actions' : function(params) {
1368 switch(this.node.nodeName) {
1369 case 'tree' : return this._set_list_actions_for_tree(params); break;
1370 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1374 '_set_list_actions_for_tree' : function(params) {
1375 // This should be called after the button element from render_list_actions has been appended to the DOM
1378 var x = document.getElementById(obj.node.id + '_clipfield');
1383 obj.clipboard(params);
1384 if (params && typeof params.on_complete == 'function') {
1385 params.on_complete(params);
1391 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1396 obj.dump_csv_to_clipboard(params);
1397 if (params && typeof params.on_complete == 'function') {
1398 params.on_complete(params);
1404 x = document.getElementById(obj.node.id + '_csv_to_printer');
1409 obj.dump_csv_to_printer(params);
1410 if (params && typeof params.on_complete == 'function') {
1411 params.on_complete(params);
1417 x = document.getElementById(obj.node.id + '_csv_to_file');
1422 obj.dump_csv_to_file(params);
1423 if (params && typeof params.on_complete == 'function') {
1424 params.on_complete(params);
1430 x = document.getElementById(obj.node.id + '_save_columns');
1435 obj.save_columns(params);
1436 if (params && typeof params.on_complete == 'function') {
1437 params.on_complete(params);
1445 obj.error.standard_unexpected_error_alert('setting list actions',E);
1450 dump('exiting util.list.js\n');