]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/opac/LocationTree.js
098e2a3173a13c4f5d64fb4e8077d283ad03d72e
[working/Evergreen.git] / Open-ILS / src / javascript / opac / LocationTree.js
1 /* */
2
3 function LocationTree( tree, box_id, container_id ) {
4         this.orgTree = tree;
5
6         this.treeContainerBoxId = container_id;
7         this.treeBoxId = box_id;
8         this.setObjects();
9         this.treeBuilder = buildOrgTreeWidget;
10 }
11
12
13 LocationTree.prototype.setObjects = function() {
14         if(this.treeContainerBoxId)
15                 this.treeContainerBox = getById(this.treeContainerBoxId);
16         else
17                 this.treeContainerBox = getById("ot_nav_widget");
18
19         if(this.treeBoxId)
20                 this.treeBox = getById(this.treeBoxId);
21         else
22                 this.treeBox = getById("ot_nav_widget_box");
23
24 }
25
26 LocationTree.prototype.buildOrgTreeWidget = function() {
27
28         debug("Somebody called buildOrgTreeWidget on me...");
29         this.setObjects();
30         //this.widget = buildOrgTreeWidget(globalOrgTree, true);
31         this.widget = this.treeBuilder(globalOrgTree, true);
32 }
33
34
35 function buildOrgTreeWidget(org_node, root) {
36
37         var item;
38
39         if(root) {
40                 item = new WebFXTree(org_node.name());
41                 item.setBehavior('classic');
42         } else {
43                 item = new WebFXTreeItem(org_node.name());
44         }
45
46         /* make org tree re-submit search on click */
47         item.action = 
48                 "javascript:globalPage.updateSelectedLocation('" + org_node.id() + "');" +
49                 "globalPage.locationTree.hide();"; 
50
51         for( var index in org_node.children()) {
52                 var childorg = org_node.children()[index];
53                 if( childorg != null ) {
54                         var tree_node = buildOrgTreeWidget(childorg);
55                         if(tree_node != null)
56                                 item.add(tree_node);
57                 }
58         }
59
60         return item;
61 }
62
63
64 LocationTree.prototype.hide = function() {
65         this.setObjects();
66         this.widget = this.treeBuilder(globalOrgTree, true);
67         if(this.treeContainerBox &&  
68                         this.treeContainerBox.className.indexOf("show_me") != -1 ) {
69                 swapClass( this.treeContainerBox, "hide_me", "show_me" );
70         }
71 }
72
73
74
75 LocationTree.prototype.toggle = function(button_div, offsetx, offsety, relative) {
76
77         this.setObjects();
78         debug("Tree container " + this.treeContainerBox );
79         debug("Tree box " + this.treeBox );
80
81         swapClass( this.treeContainerBox, "hide_me", "show_me" );
82
83         var obj = this;
84         if( (this.treeBox && this.treeBox.firstChild && 
85                         this.treeBox.firstChild.nodeType == 3) ||
86                         (this.treeBox && !this.treeBox.firstChild)) {
87
88                 debug("location tree has not been rendered... rendering..");
89                 //setTimeout(function() { renderTree(obj); }, 5 );
90                 renderTree(obj);
91         }
92
93         //alert(this.treeBox.firstChild.nodeType);
94
95         if( button_div && 
96                         ((offsetx == null && offsety == null) || relative) ) {
97
98                 var x = findPosX(button_div);
99                 var y = findPosY(button_div);
100                 var height = getObjectHeight(button_div);
101                 var xpos = x - getObjectWidth(this.treeBox) + getObjectWidth(button_div);
102
103                 if(offsety == null) offsety = 0;
104                 if(offsetx == null) offsetx = 0;
105
106                 offsety = y + height + offsety;
107                 offsetx = xpos + offsetx;       
108         }
109
110         if(IE) { /*HACK XXX*/
111                 offsety = parseInt(offsety) + 15;
112                 offsetx = parseInt(offsetx) + 8;
113         }
114
115
116         debug("TREE OFFSET y: " + offsety + " x: " + offsetx );
117
118         this.treeContainerBox.style.position = "absolute"; 
119         this.treeContainerBox.style.top = offsety; 
120         this.treeContainerBox.style.left = offsetx;
121 }
122
123
124 function renderTree(tree) {
125         tree.setObjects();
126         if(!tree.widget) tree.buildOrgTreeWidget(); 
127         /*
128         debug("Spitting tree out to the treeBox:\n" +
129                         tree.widget.toString() ); */
130         tree.treeBox.innerHTML = tree.widget.toString();
131 }
132
133
134
135 /* generates a new chunk within with the tree is inserted */
136 LocationTree.prototype.newSpot = function(box_id, container_id) {
137
138         var cont                        = elem("div", { id : this.treeContainerBoxId } );
139         var box                         = elem("div", { id : this.treeBoxId } );
140         var expando_line        = elem("div");
141         var expando             = elem("div");
142         var expand_all          = elem("a", null, null, "Expand All");
143         var collapse_all        = elem("a", null, null, "Collapse All");
144
145         add_css_class(cont, "nav_widget");
146         add_css_class(cont, "hide_me");
147         add_css_class(box, "ot_nav_widget_box");
148         add_css_class(expando_line, "expando_links");
149         add_css_class(expando, "expando_links");
150
151
152         cont.appendChild(expando_line);
153         cont.appendChild(expando);
154         cont.appendChild(elem("br"));
155         cont.appendChild(box);
156
157         expando_line.appendChild(elem("br"));
158         var obj = this;
159         expand_all.onclick = function() { obj.widget.expandAll(); };
160         collapse_all.onclick = function() {
161         obj.widget.collapseAll();
162                 obj.widget.expand();};
163
164         expando.appendChild(expand_all);
165         expando.appendChild(createAppTextNode(" "));
166         expando.appendChild(collapse_all);
167         expando.appendChild(createAppTextNode(" "));
168
169         return cont;
170
171 }