]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/HTMLElement.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1067 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / HTMLElement.js
1 if (navigator.vendor == "Apple Computer, Inc." || navigator.vendor == "KDE") { // WebCore/KHTML  
2   /*
3     Crossbrowser HTMLElement Prototyping
4     Copyright (C) 2005  Jason Davis, http://www.browserland.org
5     Additional thanks to Brothercake, http://www.brothercake.com
6     
7     This code is licensed under the LGPL:
8     http://www.gnu.org/licenses/lgpl.html
9   */
10
11   (function(c) {
12     for (var i in c)
13       window["HTML" + i + "Element"] = document.createElement(c[ i ]).constructor;
14   })({
15                 Html: "html", Head: "head", Link: "link", Title: "title", Meta: "meta",
16                         Base: "base", IsIndex: "isindex", Style: "style", Body: "body", Form: "form",
17                         Select: "select", OptGroup: "optgroup", Option: "option", Input: "input",
18                         TextArea: "textarea", Button: "button", Label: "label", FieldSet: "fieldset",
19                         Legend: "legend", UList: "ul", OList: "ol", DList: "dl", Directory: "dir",
20                         Menu: "menu", LI: "li", Div: "div", Paragraph: "p", Heading: "h1", Quote: "q",
21                         Pre: "pre", BR: "br", BaseFont: "basefont", Font: "font", HR: "hr", Mod: "ins",
22                         Anchor: "a", Image: "img", Object: "object", Param: "param", Applet: "applet",
23                         Map: "map", Area: "area", Script: "script", Table: "table", TableCaption: "caption",
24                         TableCol: "col", TableSection: "tbody", TableRow: "tr", TableCell: "td",
25                         FrameSet: "frameset", Frame: "frame", IFrame: "iframe"
26                         });
27   
28   function HTMLElement() {}
29   //HTMLElement.prototype     = HTMLHtmlElement.__proto__.__proto__;    
30   var HTMLDocument          = document.constructor;
31   var HTMLCollection        = document.links.constructor;
32   var HTMLOptionsCollection = document.createElement("select").options.constructor;
33   var Text                  = document.createTextNode("").constructor;
34   //var Node                  = Text;
35   
36   // More efficient for Safari 2
37   function Document() {} 
38   function Event() {} 
39   function HTMLCollection() {} 
40   function HTMLElement() {} 
41   function Node() {} 
42   Document.prototype = window["[[DOMDocument]]"]; 
43   Event.prototype = window["[[DOMEvent]]"]; 
44   HTMLCollection.prototype = window["[[HTMLCollection.prototype]]"]; 
45   HTMLElement.prototype = window["[[DOMElement.prototype]]"]; 
46   Node.prototype = window["[[DOMNode.prototype]]"]; 
47 }
48
49 /* custom extensions to the DOM api */
50 HTMLElement.prototype.addInterface = function(objectInterface) {
51   Object.extend(this, objectInterface);
52   if (this.bind)
53     this.bind();
54 }
55
56 HTMLElement.prototype.childNodesWithTag = function(tagName) {
57   var matchingNodes = new Array();
58   var tagName = tagName.toUpperCase();
59
60   for (var i = 0; i < this.childNodes.length; i++) {
61     if (typeof(this.childNodes[i]) == "object"
62         && this.childNodes[i].tagName
63         && this.childNodes[i].tagName.toUpperCase() == tagName)
64       matchingNodes.push(this.childNodes[i]);
65   }
66
67   return matchingNodes;
68 }
69
70 HTMLElement.prototype.addClassName = function(className) {
71   var classStr = '' + this.getAttribute("class");
72
73   position = classStr.indexOf(className, 0);
74   if (position < 0) {
75     classStr = classStr + ' ' + className;
76     this.setAttribute('class', classStr);
77   }
78 }
79
80 HTMLElement.prototype.removeClassName = function(className) {
81   var classStr = '' + this.getAttribute('class');
82
83   position = classStr.indexOf(className, 0);
84   while (position > -1) {
85     classStr1 = classStr.substring(0, position); 
86     classStr2 = classStr.substring(position + 10, classStr.length);
87     classStr = classStr1 + classStr2;
88     position = classStr.indexOf(className, 0);
89   }
90
91   this.setAttribute('class', classStr);
92 }
93
94 HTMLElement.prototype.hasClassName = function(className) {
95   var classStr = '' + this.getAttribute('class');
96   position = classStr.indexOf(className, 0);
97   return (position > -1);
98 }
99
100 HTMLElement.prototype.getParentWithTagName = function(tagName) {
101   var currentElement = this;
102   tagName = tagName.toUpperCase();
103
104   currentElement = currentElement.parentNode;
105   while (currentElement
106          && currentElement.tagName != tagName) {
107     currentElement = currentElement.parentNode;
108   }
109
110   return currentElement;
111 }
112
113 HTMLElement.prototype.cascadeLeftOffset = function() {
114   var currentElement = this;
115
116   var offset = 0;
117   while (currentElement) {
118     offset += currentElement.offsetLeft;
119     currentElement = currentElement.getParentWithTagName("div");
120   }
121
122   return offset;
123 }
124
125 HTMLElement.prototype.cascadeTopOffset = function() {
126   var currentElement = this;
127   var offset = 0;
128
129   var i = 0;
130
131   while (currentElement
132          && currentElement instanceof HTMLElement) {
133     offset += currentElement.offsetTop;
134     currentElement = currentElement.parentNode;
135     i++;
136   }
137
138   return offset;
139 }
140
141 HTMLElement.prototype.dump = function(additionalInfo, additionalKeys) {
142   var id = this.getAttribute("id");
143   var nclass = this.getAttribute("class");
144   
145   var str = this.tagName;
146   if (id)
147     str += "; id = " + id;
148   if (nclass)
149     str += "; class = " + nclass;
150
151   if (additionalInfo)
152     str += "; " + additionalInfo;
153
154   if (additionalKeys)
155     for (var i = 0; i < additionalKeys.length; i++) {
156       var value = this.getAttribute(additionalKeys[i]);
157       if (value)
158         str += "; " + additionalKeys[i] + " = " + value;
159     }
160
161   log (str);
162 }
163
164 HTMLElement.prototype.getSelectedNodes = function() {
165   var selArray = new Array();
166
167   for (var i = 0; i < this.childNodes.length; i++) {
168     node = this.childNodes.item(i);
169     if (node.nodeType == 1
170         && isNodeSelected(node))
171       selArray.push(node);
172   }
173
174   return selArray;
175 }
176
177 HTMLElement.prototype.getSelectedNodesId = function() {
178   var selArray = new Array();
179
180   for (var i = 0; i < this.childNodes.length; i++) {
181     node = this.childNodes.item(i);
182     if (node.nodeType == 1
183         && isNodeSelected(node))
184       selArray.push(node.getAttribute("id"));
185   }
186
187   return selArray;
188 }
189
190 HTMLElement.prototype.onContextMenu = function(event) {
191   var popup = this.sogoContextMenu;
192
193   if (document.currentPopupMenu)
194     hideMenu(event, document.currentPopupMenu);
195
196   var menuTop = event.pageY;
197   var menuLeft = event.pageX;
198   var heightDiff = (window.innerHeight
199                     - (menuTop + popup.offsetHeight));
200   if (heightDiff < 0)
201     menuTop += heightDiff;
202
203   var leftDiff = (window.innerWidth
204                   - (menuLeft + popup.offsetWidth));
205   if (leftDiff < 0)
206     menuLeft -= popup.offsetWidth;
207
208   popup.style.top = menuTop + "px;";
209   popup.style.left = menuLeft + "px;";
210   popup.style.visibility = "visible;";
211
212   bodyOnClick = "" + document.body.getAttribute("onclick");
213   document.body.setAttribute("onclick", "onBodyClick(event);");
214   document.currentPopupMenu = popup;
215 }
216
217 HTMLElement.prototype.attachMenu = function(menuName) {
218   this.sogoContextMenu = $(menuName);
219   this.addEventListener("contextmenu", this.onContextMenu, true);
220 }
221
222 HTMLElement.prototype.select = function() {
223   this.addClassName('_selected');
224 }
225
226 HTMLElement.prototype.deselect = function() {
227   this.removeClassName('_selected');
228 }
229
230 HTMLElement.prototype.deselectAll = function () {
231   for (var i = 0; i < this.childNodes.length; i++) {
232     var node = this.childNodes.item(i);
233     if (node.nodeType == 1)
234       node.deselect();
235   }
236 }