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 this.node.addEventListener(
117 if (typeof params.on_select == 'function') {
118 params.on_select(ev);
120 var x = document.getElementById(obj.node.id + '_clipfield');
122 var sel = obj.retrieve_selection();
123 x.setAttribute('disabled', sel.length == 0);
128 if (typeof params.on_click == 'function') {
129 this.node.addEventListener(
136 this.node.addEventListener(
138 function(ev) { obj.detect_visible(); },
142 this.node.addEventListener(
144 function(ev) { obj.auto_retrieve(); },
147 this.node.addEventListener(
149 function(ev) { obj.auto_retrieve(); },
152 window.addEventListener(
154 function(ev) { obj.auto_retrieve(); },
157 /* FIXME -- find events on scrollbar to trigger this */
158 obj.detect_visible_polling();
160 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
161 var slider = document.getAnonymousNodes( scrollbar )[2];
162 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
163 scrollbar.addEventListener('click',function(){alert('sb click');},false);
164 scrollbar.addEventListener('command',function(){alert('sb command');},false);
165 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
166 slider.addEventListener('click',function(){alert('slider click');},false);
167 slider.addEventListener('command',function(){alert('slider command');},false);
168 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
170 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
172 this.restores_columns(params);
175 '_init_listbox' : function (params) {
178 var listhead = document.createElement('listhead');
179 this.node.appendChild(listhead);
181 var listcols = document.createElement('listcols');
182 this.node.appendChild(listcols);
184 for (var i = 0; i < this.columns.length; i++) {
185 var listheader = document.createElement('listheader');
186 listhead.appendChild(listheader);
187 var listcol = document.createElement('listcol');
188 listcols.appendChild(listcol);
189 for (var j in this.columns[i]) {
190 listheader.setAttribute(j,this.columns[i][j]);
191 listcol.setAttribute(j,this.columns[i][j]);
197 'save_columns' : function (params) {
199 switch (this.node.nodeName) {
200 case 'tree' : this._save_columns_tree(params); break;
201 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
205 '_save_columns_tree' : function (params) {
208 var id = obj.node.getAttribute('id'); if (!id) {
209 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
213 var nl = obj.node.getElementsByTagName('treecol');
214 for (var i = 0; i < nl.length; i++) {
216 var col_id = col.getAttribute('id');
218 alert('FIXME: A column in this list does not have an id and cannot be saved');
221 var col_hidden = col.getAttribute('hidden');
222 var col_width = col.getAttribute('width');
223 var col_ordinal = col.getAttribute('ordinal');
224 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
226 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
227 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
228 file.set_object(my_cols);
230 alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
232 obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
236 'restores_columns' : function (params) {
238 switch (this.node.nodeName) {
239 case 'tree' : this._restores_columns_tree(params); break;
240 default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
244 '_restores_columns_tree' : function (params) {
247 var id = obj.node.getAttribute('id'); if (!id) {
248 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
252 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
253 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
254 if (file._file.exists()) {
255 var my_cols = file.get_object(); file.close();
256 var nl = obj.node.getElementsByTagName('treecol');
257 for (var i = 0; i < nl.length; i++) {
259 var col_id = col.getAttribute('id');
261 alert('FIXME: A column in this list does not have an id and cannot be saved');
264 if (typeof my_cols[col_id] != 'undefined') {
265 col.setAttribute('hidden',my_cols[col_id].hidden);
266 col.setAttribute('width',my_cols[col_id].width);
267 col.setAttribute('ordinal',my_cols[col_id].ordinal);
269 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
274 obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
278 'clear' : function (params) {
280 switch (this.node.nodeName) {
281 case 'tree' : this._clear_tree(params); break;
282 case 'listbox' : this._clear_listbox(params); break;
283 default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
285 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
286 this.row_count.total = 0;
287 this.row_count.fleshed = 0;
288 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
291 '_clear_tree' : function(params) {
293 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
294 obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
296 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
297 obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
299 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
302 '_clear_listbox' : function(params) {
305 var nl = this.node.getElementsByTagName('listitem');
306 for (var i = 0; i < nl.length; i++) {
309 for (var i = 0; i < items.length; i++) {
310 this.node.removeChild(items[i]);
314 'append' : function (params) {
317 switch (this.node.nodeName) {
318 case 'tree' : rparams = this._append_to_tree(params); break;
319 case 'listbox' : rparams = this._append_to_listbox(params); break;
320 default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
322 if (rparams && params.attributes) {
323 for (var i in params.attributes) {
324 rparams.my_node.setAttribute(i,params.attributes[i]);
327 this.row_count.total++;
328 if (this.row_count.fleshed == this.row_count.total) {
329 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
334 'refresh_row' : function (params) {
337 switch (this.node.nodeName) {
338 case 'tree' : rparams = this._refresh_row_in_tree(params); break;
339 default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
341 if (rparams && params.attributes) {
342 for (var i in params.attributes) {
343 rparams.my_node.setAttribute(i,params.attributes[i]);
346 this.row_count.fleshed--;
351 '_append_to_tree' : function (params) {
355 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
357 var s = ('util.list.append: params = ' + (params) + '\n');
359 var treechildren_node = this.treechildren;
361 if (params.node && params.node.nodeName == 'treeitem') {
362 params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
363 if (params.node.lastChild.nodeName == 'treechildren') {
364 treechildren_node = params.node.lastChild;
366 treechildren_node = document.createElement('treechildren');
367 params.node.appendChild(treechildren_node);
371 var treeitem = document.createElement('treeitem');
372 treeitem.setAttribute('retrieve_id',params.retrieve_id);
373 if (typeof params.to_bottom != 'undefined') {
374 treechildren_node.appendChild( treeitem );
375 if (typeof params.no_auto_select == 'undefined') {
376 if (!obj.auto_select_pending) {
377 obj.auto_select_pending = true;
378 setTimeout(function() {
379 dump('auto-selecting\n');
380 var idx = Number(obj.node.view.rowCount)-1;
381 try { obj.node.view.selection.select(idx); } 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(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
389 if (treechildren_node.firstChild) {
390 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
392 treechildren_node.appendChild( treeitem );
394 if (typeof params.no_auto_select == 'undefined') {
395 if (!obj.auto_select_pending) {
396 obj.auto_select_pending = true;
397 setTimeout(function() {
398 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
399 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
400 obj.auto_select_pending = false;
401 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
406 var treerow = document.createElement('treerow');
407 treeitem.appendChild( treerow );
408 treerow.setAttribute('retrieve_id',params.retrieve_id);
410 s += ('tree = ' + this.node + ' treechildren = ' + treechildren_node + '\n');
411 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
413 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
415 obj.put_retrieving_label(treerow);
416 treerow.addEventListener(
420 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
422 treerow.setAttribute('retrieved','true');
424 //dump('fleshing = ' + params.retrieve_id + '\n');
426 function inc_fleshed() {
427 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
428 treerow.setAttribute('fleshed','true');
429 obj.row_count.fleshed++;
430 if (obj.row_count.fleshed >= obj.row_count.total) {
431 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
435 params.row_node = treeitem;
436 params.on_retrieve = function(p) {
439 obj._map_row_to_treecell(p,treerow);
441 var idx = obj.node.contentView.getIndexOfItem( params.row_node );
442 dump('idx = ' + idx + '\n');
443 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
444 if ( obj.node.view.selection.isSelected( idx ) ) {
445 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
446 util.widgets.dispatch('select',obj.node);
449 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
450 dump('fixme2: ' + E + '\n');
454 if (typeof params.retrieve_row == 'function') {
456 params.retrieve_row( params );
458 } else if (typeof obj.retrieve_row == 'function') {
460 obj.retrieve_row( params );
472 util.widgets.dispatch('flesh',treerow);
477 obj.put_retrieving_label(treerow);
478 treerow.addEventListener(
481 //dump('fleshing anon\n');
482 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
483 obj._map_row_to_treecell(params,treerow);
484 treerow.setAttribute('retrieved','true');
485 treerow.setAttribute('fleshed','true');
486 obj.row_count.fleshed++;
487 if (obj.row_count.fleshed >= obj.row_count.total) {
488 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
496 util.widgets.dispatch('flesh',treerow);
501 this.error.sdump('D_LIST',s);
505 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
507 //if (typeof params.to_bottom != 'undefined')
508 if (typeof params.to_top == 'undefined') {
509 treechildren_node.removeChild( treechildren_node.firstChild );
511 treechildren_node.removeChild( treechildren_node.lastChild );
517 setTimeout( function() { obj.auto_retrieve(); }, 0 );
519 params.my_node = treeitem;
523 '_refresh_row_in_tree' : function (params) {
527 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
528 if (typeof params.my_node == 'undefined') throw('util.list.refresh_row: Object must contain a my_node');
529 if (params.my_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: my_node must be a treeitem');
531 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
533 var treeitem = params.my_node;
534 treeitem.setAttribute('retrieve_id',params.retrieve_id);
535 if (typeof params.to_bottom != 'undefined') {
536 if (typeof params.no_auto_select == 'undefined') {
537 if (!obj.auto_select_pending) {
538 obj.auto_select_pending = true;
539 setTimeout(function() {
540 dump('auto-selecting\n');
541 var idx = Number(obj.node.view.rowCount)-1;
542 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
543 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
544 obj.auto_select_pending = false;
545 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
551 for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
552 for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
553 var treerow = document.createElement('treerow');
554 treeitem.appendChild( treerow );
555 treerow.setAttribute('retrieve_id',params.retrieve_id);
557 s += ('tree = ' + this.node + '\n');
558 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
560 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
562 obj.put_retrieving_label(treerow);
563 treerow.addEventListener(
567 if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
569 treerow.setAttribute('retrieved','true');
571 //dump('fleshing = ' + params.retrieve_id + '\n');
573 function inc_fleshed() {
574 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
575 treerow.setAttribute('fleshed','true');
576 obj.row_count.fleshed++;
577 if (obj.row_count.fleshed >= obj.row_count.total) {
578 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
582 params.row_node = treeitem;
583 params.on_retrieve = function(p) {
586 obj._map_row_to_treecell(p,treerow);
588 var idx = obj.node.contentView.getIndexOfItem( params.row_node );
589 dump('idx = ' + idx + '\n');
590 // if current row is selected, send another select event to re-sync data that the client code fetches on selects
591 if ( obj.node.view.selection.isSelected( idx ) ) {
592 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
593 util.widgets.dispatch('select',obj.node);
596 // Let's not alert on this for now. Getting contentView has no properties in record buckets under certain conditions
597 dump('fixme2: ' + E + '\n');
601 if (typeof params.retrieve_row == 'function') {
603 params.retrieve_row( params );
605 } else if (typeof obj.retrieve_row == 'function') {
607 obj.retrieve_row( params );
619 util.widgets.dispatch('flesh',treerow);
624 obj.put_retrieving_label(treerow);
625 treerow.addEventListener(
628 //dump('fleshing anon\n');
629 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
630 obj._map_row_to_treecell(params,treerow);
631 treerow.setAttribute('retrieved','true');
632 treerow.setAttribute('fleshed','true');
633 obj.row_count.fleshed++;
634 if (obj.row_count.fleshed >= obj.row_count.total) {
635 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
643 util.widgets.dispatch('flesh',treerow);
648 this.error.sdump('D_LIST',s);
652 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
654 //if (typeof params.to_bottom != 'undefined')
655 if (typeof params.to_top == 'undefined') {
656 treechildren_node.removeChild( treechildren_node.firstChild );
658 treechildren_node.removeChild( treechildren_node.lastChild );
664 setTimeout( function() { obj.auto_retrieve(); }, 0 );
666 JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
671 'put_retrieving_label' : function(treerow) {
676 dump('put_retrieving_label. columns = ' + js2JSON(obj.columns) + '\n');
677 while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
678 dump('\t' + cols_idx);
679 var treecell = document.createElement('treecell');
680 treerow.appendChild(treecell);
684 for (var i = 0; i < obj.columns.length; i++) {
685 var treecell = document.createElement('treecell'); treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
686 treerow.appendChild(treecell);
689 dump('\t' + cols_idx + '\n');
696 'detect_visible' : function() {
699 //dump('detect_visible obj.node = ' + obj.node + '\n');
700 /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
702 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
703 var curpos = scrollbar.getAttribute('curpos');
704 var maxpos = scrollbar.getAttribute('maxpos');
705 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
706 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
707 if ( obj.auto_retrieve() > 0 ) {
708 obj.curpos = curpos; obj.maxpos = maxpos;
712 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
714 } catch(E) { obj.error.sdump('D_ERROR',E); }
717 'detect_visible_polling' : function() {
719 //alert('detect_visible_polling');
721 obj.detect_visible();
722 setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
729 'auto_retrieve' : function(params) {
731 switch (this.node.nodeName) {
732 case 'tree' : obj._auto_retrieve_tree(params); break;
733 default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
737 '_auto_retrieve_tree' : function (params) {
739 if (!obj.auto_retrieve_in_progress) {
740 obj.auto_retrieve_in_progress = true;
744 //alert('auto_retrieve\n');
746 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
747 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
748 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
749 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
750 for (var i = startpos; i < endpos + 4; i++) {
752 //dump('trying index ' + i + '\n');
753 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
754 if (item && item.getAttribute('retrieved') != 'true' ) {
755 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
756 util.widgets.dispatch('flesh',item); count++;
759 //dump(i + ' : ' + E + '\n');
762 obj.auto_retrieve_in_progress = false;
764 } catch(E) { alert(E); }
770 'exec_on_all_fleshed' : function() {
773 if (obj.on_all_fleshed) {
774 if (typeof obj.on_all_fleshed == 'function') {
775 dump('exec_on_all_fleshed == function\n');
778 try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
781 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
782 dump('exec_on_all_fleshed == array\n');
786 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
787 var f = obj.on_all_fleshed.pop();
788 if (typeof f == 'function') {
789 try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); }
791 if (obj.on_all_fleshed.length > 0) arguments.callee();
793 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
798 obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
802 obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
806 'full_retrieve' : function(params) {
808 switch (this.node.nodeName) {
809 case 'tree' : obj._full_retrieve_tree(params); break;
810 default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
814 '_full_retrieve_tree' : function(params) {
817 if (obj.row_count.fleshed >= obj.row_count.total) {
818 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
819 obj.exec_on_all_fleshed();
821 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
822 JSAN.use('util.widgets');
823 var nodes = obj.treechildren.childNodes;
824 for (var i = 0; i < nodes.length; i++) {
825 util.widgets.dispatch('flesh',nodes[i].firstChild);
829 obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
833 '_append_to_listbox' : function (params) {
837 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
839 var s = ('util.list.append: params = ' + (params) + '\n');
841 var listitem = document.createElement('listitem');
843 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
845 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
849 listitem.setAttribute('retrieve_id',params.retrieve_id);
850 //FIXME//Make async and fire when row is visible in list
853 params.row_node = listitem;
854 params.on_retrieve = function(row) {
856 obj._map_row_to_listcell(params,listitem);
857 obj.node.appendChild( listitem );
858 util.widgets.dispatch('select',obj.node);
861 if (typeof params.retrieve_row == 'function') {
863 row = params.retrieve_row( params );
867 if (typeof obj.retrieve_row == 'function') {
869 row = obj.retrieve_row( params );
876 this._map_row_to_listcell(params,listitem);
877 this.node.appendChild( listitem );
880 this.error.sdump('D_LIST',s);
881 params.my_node = listitem;
886 '_map_row_to_treecell' : function(params,treerow) {
889 util.widgets.remove_children(treerow);
891 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
893 for (var i = 0; i < this.columns.length; i++) {
894 var treecell = document.createElement('treecell');
895 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
897 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
898 treecell.setAttribute('label',label);
899 treerow.appendChild( treecell );
900 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
903 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
904 treecell.setAttribute('label',label);
905 treerow.appendChild( treecell );
906 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
910 if (typeof params.map_row_to_column == 'function') {
912 label = params.map_row_to_column(params.row,this.columns[i]);
914 } else if (typeof this.map_row_to_column == 'function') {
916 label = this.map_row_to_column(params.row,this.columns[i]);
919 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
920 treerow.appendChild( treecell );
921 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
923 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
927 if (typeof params.map_row_to_columns == 'function') {
929 labels = params.map_row_to_columns(params.row,this.columns);
931 } else if (typeof this.map_row_to_columns == 'function') {
933 labels = this.map_row_to_columns(params.row,this.columns);
936 for (var i = 0; i < labels.length; i++) {
937 var treecell = document.createElement('treecell');
938 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
939 if ( this.columns[i].type == 'checkbox') {
940 treecell.setAttribute('value', labels[i]);
942 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
944 treerow.appendChild( treecell );
945 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
950 throw('No row to column mapping function.');
952 this.error.sdump('D_LIST',s);
955 '_map_row_to_listcell' : function(params,listitem) {
958 for (var i = 0; i < this.columns.length; i++) {
960 if (typeof params.map_row_to_column == 'function') {
962 value = params.map_row_to_column(params.row,this.columns[i]);
966 if (typeof this.map_row_to_column == 'function') {
968 value = this.map_row_to_column(params.row,this.columns[i]);
971 if (typeof value == 'string' || typeof value == 'number') {
972 var listcell = document.createElement('listcell');
973 listcell.setAttribute('label',value);
974 listitem.appendChild(listcell);
975 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
977 listitem.appendChild(value);
978 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
981 this.error.sdump('D_LIST',s);
984 'select_all' : function(params) {
986 switch(this.node.nodeName) {
987 case 'tree' : return this._select_all_from_tree(params); break;
988 default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
992 '_select_all_from_tree' : function(params) {
994 this.node.view.selection.selectAll();
997 'retrieve_selection' : function(params) {
999 switch(this.node.nodeName) {
1000 case 'tree' : return this._retrieve_selection_from_tree(params); break;
1001 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1005 '_retrieve_selection_from_tree' : function(params) {
1008 var start = new Object();
1009 var end = new Object();
1010 var numRanges = this.node.view.selection.getRangeCount();
1011 for (var t=0; t<numRanges; t++){
1012 this.node.view.selection.getRangeAt(t,start,end);
1013 for (var v=start.value; v<=end.value; v++){
1014 var i = this.node.contentView.getItemAtIndex(v);
1021 'dump' : function(params) {
1023 switch(this.node.nodeName) {
1024 case 'tree' : return this._dump_tree(params); break;
1025 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1029 '_dump_tree' : function(params) {
1032 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1034 var treeitem = this.treechildren.childNodes[i];
1035 var treerow = treeitem.firstChild;
1036 for (var j = 0; j < treerow.childNodes.length; j++) {
1037 row.push( treerow.childNodes[j].getAttribute('label') );
1044 'dump_with_keys' : function(params) {
1046 switch(this.node.nodeName) {
1047 case 'tree' : return this._dump_tree_with_keys(params); break;
1048 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1053 '_dump_tree_with_keys' : function(params) {
1056 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1058 var treeitem = this.treechildren.childNodes[i];
1059 var treerow = treeitem.firstChild;
1060 for (var j = 0; j < treerow.childNodes.length; j++) {
1061 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1068 'dump_csv' : function(params) {
1070 switch(this.node.nodeName) {
1071 case 'tree' : return this._dump_tree_csv(params); break;
1072 default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1077 '_dump_tree_csv' : function(params) {
1080 for (var j = 0; j < obj.columns.length; j++) {
1081 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1084 if (dump) dump += ',';
1085 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1089 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1091 var treeitem = this.treechildren.childNodes[i];
1092 var treerow = treeitem.firstChild;
1093 for (var j = 0; j < treerow.childNodes.length; j++) {
1094 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1097 if (row) row += ',';
1098 row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1101 dump += row + '\r\n';
1106 'dump_csv_to_clipboard' : function(params) {
1108 if (typeof params == 'undefined') params = {};
1109 if (params.no_full_retrieve) {
1110 copy_to_clipboard( obj.dump_csv( params ) );
1112 obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1116 'dump_csv_to_printer' : function(params) {
1118 JSAN.use('util.print'); var print = new util.print();
1119 if (typeof params == 'undefined') params = {};
1120 if (params.no_full_retrieve) {
1121 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1123 obj.wrap_in_full_retrieve(
1125 print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1131 'dump_csv_to_file' : function(params) {
1133 JSAN.use('util.file'); var f = new util.file();
1134 if (typeof params == 'undefined') params = {};
1135 if (params.no_full_retrieve) {
1136 params.data = obj.dump_csv( params );
1137 params.not_json = true;
1138 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1139 f.export_file( params );
1141 obj.wrap_in_full_retrieve(
1143 params.data = obj.dump_csv( params );
1144 params.not_json = true;
1145 if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1146 f.export_file( params );
1152 'print' : function(params) {
1153 if (!params) params = {};
1154 switch(this.node.nodeName) {
1155 case 'tree' : return this._print_tree(params); break;
1156 default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1160 '_print_tree' : function(params) {
1163 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1164 if (!params.staff && data.list.au && data.list.au[0]) {
1165 params.staff = data.list.au[0];
1167 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() ]) {
1168 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1169 params.lib.children(null);
1171 if (params.template && data.print_list_templates[ params.template ]) {
1172 var template = data.print_list_templates[ params.template ];
1173 for (var i in template) params[i] = template[i];
1175 obj.wrap_in_full_retrieve(
1178 if (!params.list) params.list = obj.dump_with_keys();
1179 JSAN.use('util.print'); var print = new util.print();
1180 print.tree_list( params );
1181 if (typeof params.callback == 'function') params.callback();
1183 obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1189 obj.error.standard_unexpected_error_alert('_print_tree',E);
1193 'dump_selected_with_keys' : function(params) {
1195 switch(this.node.nodeName) {
1196 case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1197 default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1202 '_dump_tree_selection_with_keys' : function(params) {
1205 var list = obj._retrieve_selection_from_tree();
1206 for (var i = 0; i < list.length; i++) {
1208 var treeitem = list[i];
1209 var treerow = treeitem.firstChild;
1210 for (var j = 0; j < treerow.childNodes.length; j++) {
1211 var value = treerow.childNodes[j].getAttribute('label');
1212 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1213 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1221 'clipboard' : function(params) {
1224 var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1225 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1226 data.list_clipboard = dump; data.stash('list_clipboard');
1227 JSAN.use('util.window'); var win = new util.window();
1228 win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1229 window.focus(); // sometimes the main window will lower after a clipboard action
1231 this.error.standard_unexpected_error_alert('clipboard',E);
1235 'dump_retrieve_ids' : function(params) {
1237 switch(this.node.nodeName) {
1238 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1239 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1243 '_dump_retrieve_ids_tree' : function(params) {
1246 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1247 var treeitem = this.treechildren.childNodes[i];
1248 dump.push( treeitem.getAttribute('retrieve_id') );
1253 'wrap_in_full_retrieve' : function(f) {
1255 if (typeof obj.on_all_fleshed == 'function') { // legacy
1256 obj.on_all_fleshed = [ obj.on_all_fleshed ];
1258 if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1259 obj.on_all_fleshed.push(f);
1260 obj.full_retrieve();
1263 '_sort_tree' : function(col,sortDir) {
1266 if (obj.node.getAttribute('no_sort')) {
1270 for (var i = 0; i < obj.columns.length; i++) {
1271 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1273 obj.wrap_in_full_retrieve(
1276 JSAN.use('util.money');
1278 var treeitems = obj.treechildren.childNodes;
1279 for (var i = 0; i < treeitems.length; i++) {
1280 var treeitem = treeitems[i];
1281 var treerow = treeitem.firstChild;
1282 var treecell = treerow.childNodes[ col_pos ];
1283 value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1286 rows = rows.sort( function(a,b) {
1287 a = a.value; b = b.value;
1288 if (col.getAttribute('sort_type')) {
1289 switch(col.getAttribute('sort_type')) {
1291 a = Number(a); b = Number(b);
1294 a = util.money.dollars_float_to_cents_integer(a);
1295 b = util.money.dollars_float_to_cents_integer(b);
1297 case 'title' : /* special case for "a" and "the". doesn't use marc 245 indicator */
1298 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1299 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1302 a = String( a ).toUpperCase();
1303 b = String( b ).toUpperCase();
1307 if (typeof a == 'string' || typeof b == 'string') {
1308 a = String( a ).toUpperCase();
1309 b = String( b ).toUpperCase();
1312 if (a < b) return -1;
1313 if (a > b) return 1;
1316 if (sortDir == 'asc') rows = rows.reverse();
1317 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1318 for (var i = 0; i < rows.length; i++) {
1319 obj.treechildren.appendChild( rows[i].node );
1322 obj.error.standard_unexpected_error_alert('sorting',E);
1327 obj.error.standard_unexpected_error_alert('pre sorting', E);
1331 '_toggle_checkbox_column' : function(col,toggle) {
1334 if (obj.node.getAttribute('no_toggle')) {
1338 for (var i = 0; i < obj.columns.length; i++) {
1339 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i);
1341 var treeitems = obj.treechildren.childNodes;
1342 for (var i = 0; i < treeitems.length; i++) {
1343 var treeitem = treeitems[i];
1344 var treerow = treeitem.firstChild;
1345 var treecell = treerow.childNodes[ col_pos ];
1346 treecell.setAttribute('value',(toggle == 'on'));
1349 obj.error.standard_unexpected_error_alert('pre toggle', E);
1353 'render_list_actions' : function(params) {
1355 switch(this.node.nodeName) {
1356 case 'tree' : return this._render_list_actions_for_tree(params); break;
1357 default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1361 '_render_list_actions_for_tree' : function(params) {
1364 var btn = document.createElement('button');
1365 btn.setAttribute('id',obj.node.id + '_list_actions');
1366 btn.setAttribute('type','menu');
1367 btn.setAttribute('allowevents','true');
1368 //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1369 btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1370 btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1371 var mp = document.createElement('menupopup');
1372 btn.appendChild(mp);
1373 var mi = document.createElement('menuitem');
1374 mi.setAttribute('id',obj.node.id + '_clipfield');
1375 mi.setAttribute('disabled','true');
1376 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1377 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1379 mi = document.createElement('menuitem');
1380 mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1381 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1382 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1384 mi = document.createElement('menuitem');
1385 mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1386 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1387 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1389 mi = document.createElement('menuitem');
1390 mi.setAttribute('id',obj.node.id + '_csv_to_file');
1391 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1392 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1394 mi = document.createElement('menuitem');
1395 mi.setAttribute('id',obj.node.id + '_save_columns');
1396 mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1397 mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1401 obj.error.standard_unexpected_error_alert('rendering list actions',E);
1405 'set_list_actions' : function(params) {
1407 switch(this.node.nodeName) {
1408 case 'tree' : return this._set_list_actions_for_tree(params); break;
1409 default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1413 '_set_list_actions_for_tree' : function(params) {
1414 // This should be called after the button element from render_list_actions has been appended to the DOM
1417 var x = document.getElementById(obj.node.id + '_clipfield');
1422 obj.clipboard(params);
1423 if (params && typeof params.on_complete == 'function') {
1424 params.on_complete(params);
1430 x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1435 obj.dump_csv_to_clipboard(params);
1436 if (params && typeof params.on_complete == 'function') {
1437 params.on_complete(params);
1443 x = document.getElementById(obj.node.id + '_csv_to_printer');
1448 obj.dump_csv_to_printer(params);
1449 if (params && typeof params.on_complete == 'function') {
1450 params.on_complete(params);
1456 x = document.getElementById(obj.node.id + '_csv_to_file');
1461 obj.dump_csv_to_file(params);
1462 if (params && typeof params.on_complete == 'function') {
1463 params.on_complete(params);
1469 x = document.getElementById(obj.node.id + '_save_columns');
1474 obj.save_columns(params);
1475 if (params && typeof params.on_complete == 'function') {
1476 params.on_complete(params);
1484 obj.error.standard_unexpected_error_alert('setting list actions',E);
1489 dump('exiting util.list.js\n');