]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/HTMLElement.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1037 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / HTMLElement.js
1 /* custom extensions to the DOM api */
2 HTMLElement.prototype.addInterface = function(objectInterface) {
3   Object.extend(this, objectInterface);
4   if (this.bind)
5     this.bind();
6 }
7
8 HTMLElement.prototype.childNodesWithTag = function(tagName) {
9   var matchingNodes = new Array();
10   var tagName = tagName.toUpperCase();
11
12   for (var i = 0; i < this.childNodes.length; i++) {
13 //     log("(" + tagName + ") childNodes " + i + " = " + this.childNodes[i]);
14     if (typeof(this.childNodes[i]) == "object"
15         && this.childNodes[i].tagName
16         && this.childNodes[i].tagName.toUpperCase() == tagName)
17       matchingNodes.push(this.childNodes[i]);
18   }
19
20 //   log ("matching: " + matchingNodes.length);
21
22   return matchingNodes;
23 }
24
25 HTMLElement.prototype.addClassName = function(className) {
26   var classStr = '' + this.getAttribute("class");
27
28   position = classStr.indexOf(className, 0);
29   if (position < 0) {
30     classStr = classStr + ' ' + className;
31     this.setAttribute('class', classStr);
32   }
33 }
34
35 HTMLElement.prototype.removeClassName = function(className) {
36   var classStr = '' + this.getAttribute('class');
37
38   position = classStr.indexOf(className, 0);
39   while (position > -1) {
40     classStr1 = classStr.substring(0, position); 
41     classStr2 = classStr.substring(position + 10, classStr.length);
42     classStr = classStr1 + classStr2;
43     position = classStr.indexOf(className, 0);
44   }
45
46   this.setAttribute('class', classStr);
47 }
48
49 HTMLElement.prototype.hasClassName = function(className) {
50   var classStr = '' + this.getAttribute('class');
51   position = classStr.indexOf(className, 0);
52   return (position > -1);
53 }
54
55 HTMLElement.prototype.getParentWithTagName = function(tagName) {
56   var currentElement = this;
57   tagName = tagName.toUpperCase();
58
59   currentElement = currentElement.parentNode;
60   while (currentElement
61          && currentElement.tagName != tagName) {
62     currentElement = currentElement.parentNode;
63   }
64
65   return currentElement;
66 }
67
68 HTMLElement.prototype.cascadeLeftOffset = function() {
69   var currentElement = this;
70
71   var offset = 0;
72   while (currentElement) {
73     offset += currentElement.offsetLeft;
74     currentElement = currentElement.getParentWithTagName("div");
75   }
76
77   return offset;
78 }
79
80 HTMLElement.prototype.cascadeTopOffset = function() {
81   var currentElement = this;
82   var offset = 0;
83
84   var i = 0;
85
86   while (currentElement
87          && currentElement instanceof HTMLElement) {
88     offset += currentElement.offsetTop;
89     currentElement = currentElement.parentNode;
90     i++;
91   }
92
93   return offset;
94 }
95
96 HTMLElement.prototype.dump = function(additionalInfo, additionalKeys) {
97   var id = this.getAttribute("id");
98   var nclass = this.getAttribute("class");
99   
100   var str = this.tagName;
101   if (id)
102     str += "; id = " + id;
103   if (nclass)
104     str += "; class = " + nclass;
105
106   if (additionalInfo)
107     str += "; " + additionalInfo;
108
109   if (additionalKeys)
110     for (var i = 0; i < additionalKeys.length; i++) {
111       var value = this.getAttribute(additionalKeys[i]);
112       if (value)
113         str += "; " + additionalKeys[i] + " = " + value;
114     }
115
116   log (str);
117 }
118
119 HTMLElement.prototype.getSelectedNodes = function() {
120   var selArray = new Array();
121
122   for (var i = 0; i < this.childNodes.length; i++) {
123     node = this.childNodes.item(i);
124     if (node.nodeType == 1
125         && isNodeSelected(node))
126       selArray.push(node);
127   }
128
129   return selArray;
130 }
131
132 HTMLElement.prototype.getSelectedNodesId = function() {
133   var selArray = new Array();
134
135   for (var i = 0; i < this.childNodes.length; i++) {
136     node = this.childNodes.item(i);
137     if (node.nodeType == 1
138         && isNodeSelected(node))
139       selArray.push(node.getAttribute("id"));
140   }
141
142   return selArray;
143 }
144
145 HTMLElement.prototype.onContextMenu = function(event) {
146   var popup = this.sogoContextMenu;
147
148   if (document.currentPopupMenu)
149     hideMenu(event, document.currentPopupMenu);
150
151   var menuTop = event.pageY;
152   var menuLeft = event.pageX;
153   var heightDiff = (window.innerHeight
154                     - (menuTop + popup.offsetHeight));
155   if (heightDiff < 0)
156     menuTop += heightDiff;
157
158   var leftDiff = (window.innerWidth
159                   - (menuLeft + popup.offsetWidth));
160   if (leftDiff < 0)
161     menuLeft -= popup.offsetWidth;
162
163   popup.style.top = menuTop + "px;";
164   popup.style.left = menuLeft + "px;";
165   popup.style.visibility = "visible;";
166 //   setupMenuTarget(popup, event.target);
167
168   bodyOnClick = "" + document.body.getAttribute("onclick");
169   document.body.setAttribute("onclick", "onBodyClick(event);");
170   document.currentPopupMenu = popup;
171
172 //   event.cancelBubble = true;
173 //   event.returnValue = false;
174 }
175
176 HTMLElement.prototype.attachMenu = function(menuName) {
177   this.sogoContextMenu = $(menuName);
178   this.addEventListener("contextmenu", this.onContextMenu, true);
179 }
180
181 HTMLElement.prototype.select = function() {
182   this.addClassName('_selected');
183 }
184
185 HTMLElement.prototype.deselect = function() {
186   this.removeClassName('_selected');
187 }
188
189 HTMLElement.prototype.deselectAll = function () {
190   for (var i = 0; i < this.childNodes.length; i++) {
191     var node = this.childNodes.item(i);
192     if (node.nodeType == 1)
193       node.deselect();
194   }
195 }