1 /* Top level widget class */
3 /* Generic HTML container box. */
7 /* default to no sorting and allowing dups */
8 Box.prototype.init = function(title, hidden, noDups, maxItems) {
10 this.node = createAppElement("div");
11 this.contentNode = createAppElement("div");
14 add_css_class(this.node, "box");
15 add_css_class(this.contentNode, "box_content");
16 this.node.appendChild(this.contentNode);
18 this.items = new Array();
19 this.itemCount = new Array();
20 this.sortCounts = false;
21 this.sortKeys = false;
25 this.setMax(maxItems);
26 this.setHidden(hidden);
29 /* top level box title */
30 Box.prototype.title = function(title) {
31 if(title == null) return;
32 this.titleNode = createAppElement("div");
33 add_css_class(this.titleNode, "box_title");
34 this.titleNode.appendChild(createAppTextNode(title));
35 this.node.insertBefore(this.titleNode, this.node.firstChild);
39 Box.prototype.hideOnEmpty = function() {
40 this.hideOnEmpty = true;
43 Box.prototype.addFooter = function(domItem) {
44 this.footerNode = createAppElement("div")
45 add_css_class(this.footerNode, "box_footer");
46 this.footerNode.appendChild(domItem);
47 this.contentNode.appendChild(this.footerNode);
50 /* add a caption below the title */
51 Box.prototype.addCaption = function(caption) {
52 alert(this.node.name);
53 if(caption == null) return;
54 var captionNode = createAppElement("div");
55 add_css_class(captionNode, "box_caption");
56 captionNode.appendChild(createAppTextNode(caption));
57 this.node.insertBefore(captionNode, this.contentNode);
61 Box.prototype.setHidden = function(bool) {
64 add_css_class( this.node, "hide_me" );
65 remove_css_class( this.node, "show_me" );
67 add_css_class( this.node, "show_me" );
68 remove_css_class( this.node, "hide_me" );
73 Box.prototype.addItem = function(domItem, key) {
74 var boxItem = new BoxItem();
75 boxItem.init(domItem, key);
76 this.addRestrictDups(boxItem,key);
79 /* only restricts dups if necessary. */
80 Box.prototype.addRestrictDups = function(boxItem, key) {
82 if(key) { /* check for dups */
84 if(this.itemCount[key] == null)
85 this.itemCount[key] = 0;
86 this.itemCount[key] += 1;
92 for( var index in this.items ) {
93 if(this.items[index].key == key) {
99 /* append the new item */
101 this.append(boxItem.getNode());
102 this.items.push(boxItem);
106 this.append(boxItem.getNode());
111 Box.prototype.append = function(domItem) {
112 this.contentNode.appendChild(domItem);
115 Box.prototype.remove = function(domItem) {
116 this.contentNode.removeChild(domItem);
120 Box.prototype.sortByCount = function() {
121 this.sortCounts = true;
122 this.sortKeys = false;
125 Box.prototype.sortByKey = function() {
126 this.sortCounts = false;
127 this.sortKeys = true;
130 Box.prototype.noDup = function() {
134 Box.prototype.getNode = function() {
138 Box.prototype.setMax = function(max) {
139 if(max != null && max > -1)
145 Box.prototype.sortMyKeys = function() {
147 var keys = new Array();
149 for( var i in this.items)
150 keys.push(this.items[i].getKey());
153 debug("Keys: " + keys);
155 /* drop all of the children */
156 while(this.contentNode.childNodes.length > 0 )
157 this.contentNode.removeChild(
158 this.contentNode.lastChild);
161 var node = this.findByKey(keys[k]);
163 this.contentNode.appendChild(node.getNode());
169 Box.prototype.sortMyCounts = function() {
171 var counts = new Array();
172 for( var x in this.itemCount) {
173 counts.push(this.itemCount[x]);
177 /* remove all of the divs */
178 while(this.contentNode.childNodes.length > 0 )
179 this.contentNode.removeChild(
180 this.contentNode.lastChild);
182 /* then re-insert in order of the sorted counts array*/
183 counts = counts.sort().reverse();
184 for(var i in counts) {
185 for( var c in this.itemCount ) {
186 if(this.itemCount[c] == counts[i]) {
187 var item = this.findByKey(c);
188 this.contentNode.appendChild(item.getNode());
196 Box.prototype.sortMyBoth = function() {
199 Box.prototype.findByKey = function(key) {
200 for( var i in this.items) {
201 if( this.items[i] && this.items[i].getKey() == key)
202 return this.items[i];
207 /* removes the given item from this box and returns the item */
208 Box.prototype.removeItem = function(key) {
209 for( var i in this.items) {
210 if( this.items[i] && this.items[i].getKey() == key) {
211 var obj = this.items[i];
212 this.remove(obj.getNode());
214 /* scooch all the other items down by one */
215 for( var j = i; j!= this.items.length; j++ ) {
216 this.items[j] = this.items[j+1];
224 /* checks for sorting order/max items and does the final
225 div drawing. Sets hidden to false */
226 Box.prototype.finalize = function() {
228 /* first sort if necessary */
230 if(this.sortCounts && this.sortKeys)
233 else if(this.sortKeys)
236 else if(this.sortCounts)
241 while(this.contentNode.childNodes.length > this.max )
242 this.contentNode.removeChild(this.contentNode.lastChild);
245 /* only display the box if there is data inside */
246 //if(this.contentNode.childNodes.length > 0)
247 if( this.items.length > 0 )
248 this.setHidden(false);
252 /* ---------------------------------------------------- */
253 function BoxItem() {}
255 /* if listItem, we put everything into an 'li' block */
256 BoxItem.prototype.init = function(domItem, key) {
261 this.node = createAppElement("div");
262 this.node.appendChild(domItem);
264 add_css_class( this.node, "box_item" );
267 BoxItem.prototype.getNode = function() {
271 BoxItem.prototype.getKey = function() {