]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/widgets/Box.js
removing old opac images and css
[working/Evergreen.git] / Open-ILS / src / javascript / widgets / Box.js
1 /* Top level widget class */
2
3 /* Generic HTML container box. */
4
5 function Box() {}
6
7 /* default to no sorting and allowing dups  */
8 Box.prototype.init = function(title, hidden, noDups, maxItems) {
9
10         this.node = createAppElement("div");
11         this.contentNode = createAppElement("div");
12         this.title(title);
13
14         add_css_class(this.node, "box");
15         add_css_class(this.contentNode, "box_content");
16         this.node.appendChild(this.contentNode);
17
18         this.items                      = new Array();
19         this.itemCount          = new Array();
20         this.sortCounts = false;
21         this.sortKeys           = false;
22         this.dup                                = true;
23
24         this.noDup(noDups);
25         this.setMax(maxItems);
26         this.setHidden(hidden);
27 }
28
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);
36         return this;
37 }
38
39 Box.prototype.hideOnEmpty = function() {
40         this.hideOnEmpty = true;
41 }
42
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);
48 }
49
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);
58 }
59
60
61 Box.prototype.setHidden = function(bool) {
62         this.hidden = bool;
63         if( bool ) {
64                 add_css_class( this.node, "hide_me" );
65                 remove_css_class( this.node, "show_me" );
66         } else {
67                 add_css_class( this.node, "show_me" );
68                 remove_css_class( this.node, "hide_me" );
69         }
70 }
71
72
73 Box.prototype.addItem = function(domItem, key) {
74         var boxItem = new BoxItem();
75         boxItem.init(domItem, key);
76         this.addRestrictDups(boxItem,key);
77 }
78
79 /* only restricts dups if necessary. */
80 Box.prototype.addRestrictDups = function(boxItem, key) {
81
82         if(key) { /* check for dups */
83
84                 if(this.itemCount[key] == null)
85                         this.itemCount[key] = 0;
86                 this.itemCount[key] += 1;
87
88                 var found = false;
89         
90                 /* uniquify */
91                 if(!this.dup) {
92                         for( var index in this.items ) {
93                                 if(this.items[index].key == key) {
94                                         found = true; break;
95                                 }
96                         }
97                 }
98         
99                 /* append the new item */
100                 if(!found) 
101                         this.append(boxItem.getNode());
102                 this.items.push(boxItem);
103
104         } else { 
105                 /* just append it */
106                 this.append(boxItem.getNode());
107         }
108
109 }
110
111 Box.prototype.append = function(domItem) {
112         this.contentNode.appendChild(domItem);
113 }
114
115 Box.prototype.remove = function(domItem) {
116         this.contentNode.removeChild(domItem);
117 }
118
119
120 Box.prototype.sortByCount = function() {
121         this.sortCounts = true;
122         this.sortKeys   = false;
123 }
124
125 Box.prototype.sortByKey = function() {
126         this.sortCounts = false;
127         this.sortKeys   = true;
128 }
129
130 Box.prototype.noDup     = function() {
131         this.dup = false;
132 }
133
134 Box.prototype.getNode = function() {
135         return this.node;
136 }
137
138 Box.prototype.setMax = function(max) {
139         if(max != null && max > -1)
140                 this.max = max;
141         else
142                 this.max = null;
143 }
144
145 Box.prototype.sortMyKeys = function() {
146
147         var keys = new Array();
148
149         for( var i in this.items) 
150                 keys.push(this.items[i].getKey());
151
152         keys = keys.sort();
153         debug("Keys: " + keys);
154
155         /* drop all of the children */
156         while(this.contentNode.childNodes.length > 0 ) 
157                 this.contentNode.removeChild(
158                                 this.contentNode.lastChild);
159
160         for(var k in keys) {
161                 var node = this.findByKey(keys[k]);
162                 if(node)
163                         this.contentNode.appendChild(node.getNode());
164         }
165
166 }
167
168
169 Box.prototype.sortMyCounts = function() {
170
171         var counts = new Array();
172         for( var x in this.itemCount) {
173                 counts.push(this.itemCount[x]);
174         }
175
176
177         /* remove all of the divs */
178         while(this.contentNode.childNodes.length > 0 ) 
179                 this.contentNode.removeChild(
180                                 this.contentNode.lastChild);
181
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());
189                         }
190                 }
191         }
192
193 }
194
195
196 Box.prototype.sortMyBoth = function() {
197 }
198
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];
203         }
204         return null;
205 }
206
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());
213
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];
217                         }
218                         return obj;
219                 }
220         }
221         return null;
222 }
223
224 /* checks for sorting order/max items and does the final 
225         div drawing. Sets hidden to false */
226 Box.prototype.finalize = function() {
227
228         /* first sort if necessary */
229
230         if(this.sortCounts && this.sortKeys)
231                 this.sortMyBoth(); 
232
233         else if(this.sortKeys)
234                 this.sortMyKeys();
235
236         else if(this.sortCounts) 
237                 this.sortMyCounts();
238
239         /* then trim */
240         if( this.max ) {
241                 while(this.contentNode.childNodes.length > this.max ) 
242                         this.contentNode.removeChild(this.contentNode.lastChild);
243         }
244
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);
249 }
250
251
252 /* ---------------------------------------------------- */
253 function BoxItem() {}
254
255 /* if listItem, we put everything into an 'li' block */
256 BoxItem.prototype.init = function(domItem, key) {
257
258         this.item = domItem;
259         this.key  = key;
260
261         this.node = createAppElement("div");
262         this.node.appendChild(domItem);
263
264         add_css_class( this.node, "box_item" );
265 }
266
267 BoxItem.prototype.getNode = function() {
268         return this.node;
269 }
270
271 BoxItem.prototype.getKey = function() {
272         return this.key;
273 }
274
275
276