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 if (this.columns[i].type == 'checkbox') {
67 treecol.addEventListener(
72 var toggle = ev.target.getAttribute('toggleAll') || 'on';
73 if (toggle == 'off') toggle = 'on'; else toggle = 'off';
74 ev.target.setAttribute('toggleAll',toggle);
75 obj._toggle_checkbox_column(ev.target,toggle);
82 treecol.addEventListener(
86 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
87 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
88 ev.target.setAttribute('sortDir',sortDir);
89 obj._sort_tree(ev.target,sortDir);
92 if (obj.row_count.total != obj.row_count.fleshed && (obj.row_count.total - obj.row_count.fleshed) > 50) {
93 var r = window.confirm(document.getElementById('offlineStrings').getFormattedString('list.row_fetch_warning',[obj.row_count.fleshed,obj.row_count.total]));
96 setTimeout( do_it, 0 );
99 setTimeout( do_it, 0 );
105 var splitter = document.createElement('splitter');
106 splitter.setAttribute('class','tree-splitter');
107 treecols.appendChild(splitter);
110 var treechildren = document.createElement('treechildren');
111 this.node.appendChild(treechildren);
112 this.treechildren = treechildren;
114 if (typeof params.on_sort == 'function') {
115 this.on_sort = params.on_sort;
117 if (typeof params.on_checkbox_toggle == 'function') {
118 this.on_checkbox_toggle = params.on_checkbox_toggle;
120 this.node.addEventListener(
123 if (typeof params.on_select == 'function') {
124 params.on_select(ev);
126 var x = document.getElementById(obj.node.id + '_clipfield');
128 var sel = obj.retrieve_selection();
129 x.setAttribute('disabled', sel.length == 0);
134 if (typeof params.on_click == 'function') {
135 this.node.addEventListener(
142 this.node.addEventListener(
144 function(ev) { obj.detect_visible(); },
148 this.node.addEventListener(
150 function(ev) { obj.auto_retrieve(); },
153 this.node.addEventListener(
155 function(ev) { obj.auto_retrieve(); },
158 window.addEventListener(
160 function(ev) { obj.auto_retrieve(); },
163 /* FIXME -- find events on scrollbar to trigger this */
164 obj.detect_visible_polling();
166 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
167 var slider = document.getAnonymousNodes( scrollbar )[2];
168 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
169 scrollbar.addEventListener('click',function(){alert('sb click');},false);
170 scrollbar.addEventListener('command',function(){alert('sb command');},false);
171 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
172 slider.addEventListener('click',function(){alert('slider click');},false);
173 slider.addEventListener('command',function(){alert('slider command');},false);
174 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
176 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
178 this.restores_columns(params);
181 '_init_listbox' : function (params) {
184 var listhead = document.createElement('listhead');
185 this.node.appendChild(listhead);
187 var listcols = document.createElement('listcols');
188 this.node.appendChild(listcols);
190 for (var i = 0; i < this.columns.length; i++) {
191 var listheader = document.createElement('listheader');
192 listhead.appendChild(listheader);
193 var listcol = document.createElement('listcol');
194 listcols.appendChild(listcol);
195 for (var j in this.columns[i]) {
196 listheader.setAttribute(j,this.columns[i][j]);
197 listcol.setAttribute(j,this.columns[i][j]);
203 'save_columns' : function (params) {
205 switch (this.node.nodeName) {
206 case 'tree' : this._save_columns_tree(params); break;
207 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
211 '_save_columns_tree' : function (params) {
214 var id = obj.node.getAttribute('id'); if (!id) {
215 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
219 var nl = obj.node.getElementsByTagName('treecol');
220 for (var i = 0; i < nl.length; i++) {
222 var col_id = col.getAttribute('id');
224 alert('FIXME: A column in this list does not have an id and cannot be saved');
227 var col_hidden = col.getAttribute('hidden');
228 var col_width = col.getAttribute('width');
229 var col_ordinal = col.getAttribute('ordinal');
230 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
232 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
233 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
234 file.set_object(my_cols);
236 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
238 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
242 'restores_columns' : function (params) {
244 switch (this.node.nodeName) {
245 case 'tree' : this._restores_columns_tree(params); break;
246 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
250 '_restores_columns_tree' : function (params) {
253 var id = obj.node.getAttribute('id'); if (!id) {
254 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
258 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
259 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
260 if (file._file.exists()) {
261 var my_cols = file.get_object(); file.close();
262 var nl = obj.node.getElementsByTagName('treecol');
263 for (var i = 0; i < nl.length; i++) {
265 var col_id = col.getAttribute('id');
267 alert('FIXME: A column in this list does not have an id and cannot be saved');
270 if (typeof my_cols[col_id] != 'undefined') {
271 col.setAttribute('hidden',my_cols[col_id].hidden);
272 col.setAttribute('width',my_cols[col_id].width);
273 col.setAttribute('ordinal',my_cols[col_id].ordinal);
275 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
280 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
284 'clear' : function (params) {
286 switch (this.node.nodeName) {
287 case 'tree' : this._clear_tree(params); break;
288 case 'listbox' : this._clear_listbox(params); break;
289 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
291 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
292 this.row_count.total = 0;
293 this.row_count.fleshed = 0;
294 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
297 '_clear_tree' : function(params) {
299 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
300 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
302 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
303 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
305 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
308 '_clear_listbox' : function(params) {
311 var nl = this.node.getElementsByTagName('listitem');
312 for (var i = 0; i < nl.length; i++) {
315 for (var i = 0; i < items.length; i++) {
316 this.node.removeChild(items[i]);
320 'append' : function (params) {
323 switch (this.node.nodeName) {
324 case 'tree' : rparams = this._append_to_tree(params); break;
325 case 'listbox' : rparams = this._append_to_listbox(params); break;
326 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
328 if (rparams && params.attributes) {
329 for (var i in params.attributes) {
330 rparams.my_node.setAttribute(i,params.attributes[i]);
333 this.row_count.total++;
334 if (this.row_count.fleshed == this.row_count.total) {
335 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
340 'refresh_row' : function (params) {
343 switch (this.node.nodeName) {
344 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
345 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
347 if (rparams && params.attributes) {
348 for (var i in params.attributes) {
349 rparams.my_node.setAttribute(i,params.attributes[i]);
352 this.row_count.fleshed--;
357 '_append_to_tree' : function (params) {
361 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
363 var s = ('util.list.append: params = ' + (params) + '\n');
365 var treechildren_node = this.treechildren;
367 if (params.node && params.node.nodeName == 'treeitem') {
368 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
369 if (params.node.lastChild.nodeName == 'treechildren') {
370 treechildren_node = params.node.lastChild;
372 treechildren_node = document.createElement('treechildren');
373 params.node.appendChild(treechildren_node);
377 var treeitem = document.createElement('treeitem');
378 treeitem.setAttribute('retrieve_id',params.retrieve_id);
379 if (typeof params.to_bottom != 'undefined') {
380 treechildren_node.appendChild( treeitem );
381 if (typeof params.no_auto_select == 'undefined') {
382 if (!obj.auto_select_pending) {
383 obj.auto_select_pending = true;
384 setTimeout(function() {
385 dump('auto-selecting\n');
386 var idx = Number(obj.node.view.rowCount)-1;
387 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
388 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
389 obj.auto_select_pending = false;
390 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
395 if (treechildren_node.firstChild) {
396 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
398 treechildren_node.appendChild( treeitem );
400 if (typeof params.no_auto_select == 'undefined') {
401 if (!obj.auto_select_pending) {
402 obj.auto_select_pending = true;
403 setTimeout(function() {
404 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
405 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
406 obj.auto_select_pending = false;
407 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
412 var treerow = document.createElement('treerow');
413 treeitem.appendChild( treerow );
414 treerow.setAttribute('retrieve_id',params.retrieve_id);
415 if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
417 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
418 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
420 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
422 obj.put_retrieving_label(treerow);
423 treerow.addEventListener(
427 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
429 treerow.setAttribute('retrieved','true');
431 //dump('fleshing = ' + params.retrieve_id + '\n');
433 function inc_fleshed() {
434 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
435 treerow.setAttribute('fleshed','true');
436 obj.row_count.fleshed++;
437 if (obj.row_count.fleshed >= obj.row_count.total) {
438 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
442 params.row_node = treeitem;
443 params.on_retrieve = function(p) {
446 obj._map_row_to_treecell(p,treerow);
448 var idx = obj.node.contentView.getIndexOfItem( params.row_node );
449 dump('idx = ' + idx + '\n');
450 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
451 if ( obj.node.view.selection.isSelected( idx ) ) {
452 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
453 util.widgets.dispatch('select',obj.node);
456 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
457 dump('fixme2: ' + E + '\n');
461 if (typeof params.retrieve_row == 'function') {
463 params.retrieve_row( params );
465 } else if (typeof obj.retrieve_row == 'function') {
467 obj.retrieve_row( params );
479 util.widgets.dispatch('flesh',treerow);
484 obj.put_retrieving_label(treerow);
485 treerow.addEventListener(
488 //dump('fleshing anon\n');
489 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
490 obj._map_row_to_treecell(params,treerow);
491 treerow.setAttribute('retrieved','true');
492 treerow.setAttribute('fleshed','true');
493 obj.row_count.fleshed++;
494 if (obj.row_count.fleshed >= obj.row_count.total) {
495 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
503 util.widgets.dispatch('flesh',treerow);
508 this.error.sdump('D_LIST',s);
512 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
514 //if (typeof params.to_bottom != 'undefined')
515 if (typeof params.to_top == 'undefined') {
516 treechildren_node.removeChild( treechildren_node.firstChild );
518 treechildren_node.removeChild( treechildren_node.lastChild );
524 setTimeout( function() { obj.auto_retrieve(); }, 0 );
526 params.my_node = treeitem;
530 '_refresh_row_in_tree' : function (params) {
534 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
535 if (typeof params.my_node == 'undefined') throw('util.list.refresh_row: Object must contain a my_node');
536 if (params.my_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: my_node must be a treeitem');
538 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
540 var treeitem = params.my_node;
541 treeitem.setAttribute('retrieve_id',params.retrieve_id);
542 if (typeof params.to_bottom != 'undefined') {
543 if (typeof params.no_auto_select == 'undefined') {
544 if (!obj.auto_select_pending) {
545 obj.auto_select_pending = true;
546 setTimeout(function() {
547 dump('auto-selecting\n');
548 var idx = Number(obj.node.view.rowCount)-1;
549 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
550 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
551 obj.auto_select_pending = false;
552 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
558 for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
559 for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
560 var treerow = document.createElement('treerow');
561 treeitem.appendChild( treerow );
562 treerow.setAttribute('retrieve_id',params.retrieve_id);
564 s += ('tree = ' + this.node.nodeName + '\n');
565 s += ('treeitem = ' + treeitem.nodeName + ' treerow = ' + treerow.nodeName + '\n');
567 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
569 s += 'found a retrieve_row function\n';
571 obj.put_retrieving_label(treerow);
572 treerow.addEventListener(
576 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
578 treerow.setAttribute('retrieved','true');
580 //dump('fleshing = ' + params.retrieve_id + '\n');
582 function inc_fleshed() {
583 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
584 treerow.setAttribute('fleshed','true');
585 obj.row_count.fleshed++;
586 if (obj.row_count.fleshed >= obj.row_count.total) {
587 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
591 params.row_node = treeitem;
592 params.on_retrieve = function(p) {
595 obj._map_row_to_treecell(p,treerow);
597 var idx = obj.node.contentView.getIndexOfItem( params.row_node );
598 dump('idx = ' + idx + '\n');
599 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
600 if ( obj.node.view.selection.isSelected( idx ) ) {
601 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
602 util.widgets.dispatch('select',obj.node);
605 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
606 dump('fixme2: ' + E + '\n');
610 if (typeof params.retrieve_row == 'function') {
612 params.retrieve_row( params );
614 } else if (typeof obj.retrieve_row == 'function') {
616 obj.retrieve_row( params );
628 util.widgets.dispatch('flesh',treerow);
634 s += 'did not find a retrieve_row function\n';
636 obj.put_retrieving_label(treerow);
637 treerow.addEventListener(
640 //dump('fleshing anon\n');
641 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
642 obj._map_row_to_treecell(params,treerow);
643 treerow.setAttribute('retrieved','true');
644 treerow.setAttribute('fleshed','true');
645 obj.row_count.fleshed++;
646 if (obj.row_count.fleshed >= obj.row_count.total) {
647 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
655 util.widgets.dispatch('flesh',treerow);
663 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
665 //if (typeof params.to_bottom != 'undefined')
666 if (typeof params.to_top == 'undefined') {
667 treechildren_node.removeChild( treechildren_node.firstChild );
669 treechildren_node.removeChild( treechildren_node.lastChild );
675 setTimeout( function() { obj.auto_retrieve(); }, 0 );
677 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
679 this.error.sdump('D_LIST',s);
684 'put_retrieving_label' : function(treerow) {
689 dump('put_retrieving_label. columns = ' + js2JSON(obj.columns) + '\n');
690 while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
691 dump('\t' + cols_idx);
692 var treecell = document.createElement('treecell');
693 treerow.appendChild(treecell);
697 for (var i = 0; i < obj.columns.length; i++) {
698 var treecell = document.createElement('treecell'); treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
699 treerow.appendChild(treecell);
702 dump('\t' + cols_idx + '\n');
709 'detect_visible' : function() {
712 //dump('detect_visible obj.node = ' + obj.node + '\n');
713 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
715 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
718 s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n';
719 if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
720 var C = B.childNodes[j];
721 s += '\t\t' + C.nodeName + '\n';
724 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
725 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
726 var curpos = scrollbar.getAttribute('curpos');
727 var maxpos = scrollbar.getAttribute('maxpos');
728 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
729 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
730 if ( obj.auto_retrieve() > 0 ) {
731 obj.curpos = curpos; obj.maxpos = maxpos;
735 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
737 } catch(E) { obj.error.sdump('D_ERROR',E); }
740 'detect_visible_polling' : function() {
742 //alert('detect_visible_polling');
744 obj.detect_visible();
745 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
752 'auto_retrieve' : function(params) {
754 switch (this.node.nodeName) {
755 case 'tree' : obj._auto_retrieve_tree(params); break;
756 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
760 '_auto_retrieve_tree' : function (params) {
762 if (!obj.auto_retrieve_in_progress) {
763 obj.auto_retrieve_in_progress = true;
767 //alert('auto_retrieve\n');
769 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
770 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
771 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
772 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
773 for (var i = startpos; i < endpos + 4; i++) {
775 //dump('trying index ' + i + '\n');
776 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
777 if (item && item.getAttribute('retrieved') != 'true' ) {
778 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
779 util.widgets.dispatch('flesh',item); count++;
782 //dump(i + ' : ' + E + '\n');
785 obj.auto_retrieve_in_progress = false;
787 } catch(E) { alert(E); }
793 'exec_on_all_fleshed' : function() {
796 if (obj.on_all_fleshed) {
797 if (typeof obj.on_all_fleshed == 'function') {
798 dump('exec_on_all_fleshed == function\n');
801 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
804 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
805 dump('exec_on_all_fleshed == array\n');
809 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
810 var f = obj.on_all_fleshed.pop();
811 if (typeof f == 'function') {
812 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); }
814 if (obj.on_all_fleshed.length > 0) arguments.callee();
816 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
821 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
825 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
829 'full_retrieve' : function(params) {
831 switch (this.node.nodeName) {
832 case 'tree' : obj._full_retrieve_tree(params); break;
833 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
837 '_full_retrieve_tree' : function(params) {
840 if (obj.row_count.fleshed >= obj.row_count.total) {
841 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
842 obj.exec_on_all_fleshed();
844 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
845 JSAN.use('util.widgets');
846 var nodes = obj.treechildren.childNodes;
847 for (var i = 0; i < nodes.length; i++) {
848 util.widgets.dispatch('flesh',nodes[i].firstChild);
852 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
856 '_append_to_listbox' : function (params) {
860 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
862 var s = ('util.list.append: params = ' + (params) + '\n');
864 var listitem = document.createElement('listitem');
866 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
868 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
872 listitem.setAttribute('retrieve_id',params.retrieve_id);
873 //FIXME//Make async and fire when row is visible in list
876 params.row_node = listitem;
877 params.on_retrieve = function(row) {
879 obj._map_row_to_listcell(params,listitem);
880 obj.node.appendChild( listitem );
881 util.widgets.dispatch('select',obj.node);
884 if (typeof params.retrieve_row == 'function') {
886 row = params.retrieve_row( params );
890 if (typeof obj.retrieve_row == 'function') {
892 row = obj.retrieve_row( params );
899 this._map_row_to_listcell(params,listitem);
900 this.node.appendChild( listitem );
903 this.error.sdump('D_LIST',s);
904 params.my_node = listitem;
909 '_map_row_to_treecell' : function(params,treerow) {
912 util.widgets.remove_children(treerow);
914 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
916 for (var i = 0; i < this.columns.length; i++) {
917 var treecell = document.createElement('treecell');
918 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
920 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
921 treecell.setAttribute('label',label);
922 treerow.appendChild( treecell );
923 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
926 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
927 treecell.setAttribute('label',label);
928 treerow.appendChild( treecell );
929 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
933 if (typeof params.map_row_to_column == 'function') {
935 label = params.map_row_to_column(params.row,this.columns[i]);
937 } else if (typeof this.map_row_to_column == 'function') {
939 label = this.map_row_to_column(params.row,this.columns[i]);
942 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
943 treerow.appendChild( treecell );
944 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
946 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
950 if (typeof params.map_row_to_columns == 'function') {
952 labels = params.map_row_to_columns(params.row,this.columns);
954 } else if (typeof this.map_row_to_columns == 'function') {
956 labels = this.map_row_to_columns(params.row,this.columns);
959 for (var i = 0; i < labels.length; i++) {
960 var treecell = document.createElement('treecell');
961 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
962 if ( this.columns[i].type == 'checkbox') {
963 treecell.setAttribute('value', labels[i]);
965 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
967 treerow.appendChild( treecell );
968 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
973 throw('No row to column mapping function.');
975 this.error.sdump('D_LIST',s);
978 '_map_row_to_listcell' : function(params,listitem) {
981 for (var i = 0; i < this.columns.length; i++) {
983 if (typeof params.map_row_to_column == 'function') {
985 value = params.map_row_to_column(params.row,this.columns[i]);
989 if (typeof this.map_row_to_column == 'function') {
991 value = this.map_row_to_column(params.row,this.columns[i]);
994 if (typeof value == 'string' || typeof value == 'number') {
995 var listcell = document.createElement('listcell');
996 listcell.setAttribute('label',value);
997 listitem.appendChild(listcell);
998 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1000 listitem.appendChild(value);
1001 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1004 this.error.sdump('D_LIST',s);
1007 'select_all' : function(params) {
1009 switch(this.node.nodeName) {
1010 case 'tree' : return this._select_all_from_tree(params); break;
1011 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1015 '_select_all_from_tree' : function(params) {
1017 this.node.view.selection.selectAll();
1020 'retrieve_selection' : function(params) {
1022 switch(this.node.nodeName) {
1023 case 'tree' : return this._retrieve_selection_from_tree(params); break;
1024 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1028 '_retrieve_selection_from_tree' : function(params) {
1031 var start = new Object();
1032 var end = new Object();
1033 var numRanges = this.node.view.selection.getRangeCount();
1034 for (var t=0; t<numRanges; t++){
1035 this.node.view.selection.getRangeAt(t,start,end);
1036 for (var v=start.value; v<=end.value; v++){
1037 var i = this.node.contentView.getItemAtIndex(v);
1044 'dump' : function(params) {
1046 switch(this.node.nodeName) {
1047 case 'tree' : return this._dump_tree(params); break;
1048 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1052 '_dump_tree' : function(params) {
1055 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1057 var treeitem = this.treechildren.childNodes[i];
1058 var treerow = treeitem.firstChild;
1059 for (var j = 0; j < treerow.childNodes.length; j++) {
1060 row.push( treerow.childNodes[j].getAttribute('label') );
1067 'dump_with_keys' : function(params) {
1069 switch(this.node.nodeName) {
1070 case 'tree' : return this._dump_tree_with_keys(params); break;
1071 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1076 '_dump_tree_with_keys' : function(params) {
1079 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1081 var treeitem = this.treechildren.childNodes[i];
1082 var treerow = treeitem.firstChild;
1083 for (var j = 0; j < treerow.childNodes.length; j++) {
1084 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1091 'dump_csv' : function(params) {
1093 switch(this.node.nodeName) {
1094 case 'tree' : return this._dump_tree_csv(params); break;
1095 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1100 '_dump_tree_csv' : function(params) {
1103 for (var j = 0; j < obj.columns.length; j++) {
1104 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1107 if (dump) dump += ',';
1108 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1112 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1114 var treeitem = this.treechildren.childNodes[i];
1115 var treerow = treeitem.firstChild;
1116 for (var j = 0; j < treerow.childNodes.length; j++) {
1117 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1120 if (row) row += ',';
1121 row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1124 dump += row + '\r\n';
1129 'dump_csv_to_clipboard' : function(params) {
1131 if (typeof params == 'undefined') params = {};
1132 if (params.no_full_retrieve) {
1133 copy_to_clipboard( obj.dump_csv( params ) );
1135 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1139 'dump_csv_to_printer' : function(params) {
1141 JSAN.use('util.print'); var print = new util.print();
1142 if (typeof params == 'undefined') params = {};
1143 if (params.no_full_retrieve) {
1144 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1146 obj.wrap_in_full_retrieve(
1148 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1154 'dump_csv_to_file' : function(params) {
1156 JSAN.use('util.file'); var f = new util.file();
1157 if (typeof params == 'undefined') params = {};
1158 if (params.no_full_retrieve) {
1159 params.data = obj.dump_csv( params );
1160 params.not_json = true;
1161 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1162 f.export_file( params );
1164 obj.wrap_in_full_retrieve(
1166 params.data = obj.dump_csv( params );
1167 params.not_json = true;
1168 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1169 f.export_file( params );
1175 'print' : function(params) {
1176 if (!params) params = {};
1177 switch(this.node.nodeName) {
1178 case 'tree' : return this._print_tree(params); break;
1179 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1183 '_print_tree' : function(params) {
1186 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1187 if (!params.staff && data.list.au && data.list.au[0]) {
1188 params.staff = data.list.au[0];
1190 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() ]) {
1191 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1192 params.lib.children(null);
1194 if (params.template && data.print_list_templates[ params.template ]) {
1195 var template = data.print_list_templates[ params.template ];
1196 for (var i in template) params[i] = template[i];
1198 obj.wrap_in_full_retrieve(
1201 if (!params.list) params.list = obj.dump_with_keys();
1202 JSAN.use('util.print'); var print = new util.print();
1203 print.tree_list( params );
1204 if (typeof params.callback == 'function') params.callback();
1206 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1212 obj.error.standard_unexpected_error_alert('_print_tree',E);
1216 'dump_selected_with_keys' : function(params) {
1218 switch(this.node.nodeName) {
1219 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1220 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1225 '_dump_tree_selection_with_keys' : function(params) {
1228 var list = obj._retrieve_selection_from_tree();
1229 for (var i = 0; i < list.length; i++) {
1231 var treeitem = list[i];
1232 var treerow = treeitem.firstChild;
1233 for (var j = 0; j < treerow.childNodes.length; j++) {
1234 var value = treerow.childNodes[j].getAttribute('label');
1235 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1236 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1244 'clipboard' : function(params) {
1247 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1248 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1249 data.list_clipboard = dump; data.stash('list_clipboard');
1250 JSAN.use('util.window'); var win = new util.window();
1251 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1252 window.focus(); // sometimes the main window will lower after a clipboard action
1254 this.error.standard_unexpected_error_alert('clipboard',E);
1258 'dump_retrieve_ids' : function(params) {
1260 switch(this.node.nodeName) {
1261 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1262 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1266 '_dump_retrieve_ids_tree' : function(params) {
1269 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1270 var treeitem = this.treechildren.childNodes[i];
1271 dump.push( treeitem.getAttribute('retrieve_id') );
1276 'wrap_in_full_retrieve' : function(f) {
1278 if (typeof obj.on_all_fleshed == 'function') { // legacy
1279 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1281 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1282 obj.on_all_fleshed.push(f);
1283 obj.full_retrieve();
1286 '_sort_tree' : function(col,sortDir) {
1289 if (obj.node.getAttribute('no_sort')) {
1293 for (var i = 0; i < obj.columns.length; i++) {
1294 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1296 obj.wrap_in_full_retrieve(
1299 JSAN.use('util.money');
1301 var treeitems = obj.treechildren.childNodes;
1302 for (var i = 0; i < treeitems.length; i++) {
1303 var treeitem = treeitems[i];
1304 var treerow = treeitem.firstChild;
1305 var treecell = treerow.childNodes[ col_pos ];
1306 value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1309 rows = rows.sort( function(a,b) {
1310 a = a.value; b = b.value;
1311 if (col.getAttribute('sort_type')) {
1312 switch(col.getAttribute('sort_type')) {
1314 a = Number(a); b = Number(b);
1317 a = util.money.dollars_float_to_cents_integer(a);
1318 b = util.money.dollars_float_to_cents_integer(b);
1320 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1321 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1322 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1325 a = String( a ).toUpperCase();
1326 b = String( b ).toUpperCase();
1330 if (typeof a == 'string' || typeof b == 'string') {
1331 a = String( a ).toUpperCase();
1332 b = String( b ).toUpperCase();
1335 if (a < b) return -1;
1336 if (a > b) return 1;
1339 if (sortDir == 'asc') rows = rows.reverse();
1340 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1341 for (var i = 0; i < rows.length; i++) {
1342 obj.treechildren.appendChild( rows[i].node );
1344 if (typeof obj.on_sort == 'function') obj.on_sort();
1346 obj.error.standard_unexpected_error_alert('sorting',E);
1351 obj.error.standard_unexpected_error_alert('pre sorting', E);
1355 '_toggle_checkbox_column' : function(col,toggle) {
1358 if (obj.node.getAttribute('no_toggle')) {
1362 for (var i = 0; i < obj.columns.length; i++) {
1363 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1365 var treeitems = obj.treechildren.childNodes;
1366 for (var i = 0; i < treeitems.length; i++) {
1367 var treeitem = treeitems[i];
1368 var treerow = treeitem.firstChild;
1369 var treecell = treerow.childNodes[ col_pos ];
1370 treecell.setAttribute('value',(toggle == 'on'));
1372 if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1374 obj.error.standard_unexpected_error_alert('pre toggle', E);
1378 'render_list_actions' : function(params) {
1380 switch(this.node.nodeName) {
1381 case 'tree' : return this._render_list_actions_for_tree(params); break;
1382 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1386 '_render_list_actions_for_tree' : function(params) {
1389 var btn = document.createElement('button');
1390 btn.setAttribute('id',obj.node.id + '_list_actions');
1391 btn.setAttribute('type','menu');
1392 btn.setAttribute('allowevents','true');
1393 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1394 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1395 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1396 var mp = document.createElement('menupopup');
1397 btn.appendChild(mp);
1398 var mi = document.createElement('menuitem');
1399 mi.setAttribute('id',obj.node.id + '_clipfield');
1400 mi.setAttribute('disabled','true');
1401 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1402 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1404 mi = document.createElement('menuitem');
1405 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1406 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1407 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1409 mi = document.createElement('menuitem');
1410 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1411 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1412 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1414 mi = document.createElement('menuitem');
1415 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1416 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1417 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1419 mi = document.createElement('menuitem');
1420 mi.setAttribute('id',obj.node.id + '_save_columns');
1421 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1422 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1426 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1430 'set_list_actions' : function(params) {
1432 switch(this.node.nodeName) {
1433 case 'tree' : return this._set_list_actions_for_tree(params); break;
1434 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1438 '_set_list_actions_for_tree' : function(params) {
1439 // This should be called after the button element from render_list_actions has been appended to the DOM
1442 var x = document.getElementById(obj.node.id + '_clipfield');
1447 obj.clipboard(params);
1448 if (params && typeof params.on_complete == 'function') {
1449 params.on_complete(params);
1455 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1460 obj.dump_csv_to_clipboard(params);
1461 if (params && typeof params.on_complete == 'function') {
1462 params.on_complete(params);
1468 x = document.getElementById(obj.node.id + '_csv_to_printer');
1473 obj.dump_csv_to_printer(params);
1474 if (params && typeof params.on_complete == 'function') {
1475 params.on_complete(params);
1481 x = document.getElementById(obj.node.id + '_csv_to_file');
1486 obj.dump_csv_to_file(params);
1487 if (params && typeof params.on_complete == 'function') {
1488 params.on_complete(params);
1494 x = document.getElementById(obj.node.id + '_save_columns');
1499 obj.save_columns(params);
1500 if (params && typeof params.on_complete == 'function') {
1501 params.on_complete(params);
1509 obj.error.standard_unexpected_error_alert('setting list actions',E);
1514 dump('exiting util.list.js\n');