summaryrefslogtreecommitdiff
path: root/frontend/beta/js/Clipperz/YUI/DomHelper.js
Unidiff
Diffstat (limited to 'frontend/beta/js/Clipperz/YUI/DomHelper.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/beta/js/Clipperz/YUI/DomHelper.js22
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/YUI/DomHelper.js b/frontend/beta/js/Clipperz/YUI/DomHelper.js
index 05edc49..2c0ba34 100644
--- a/frontend/beta/js/Clipperz/YUI/DomHelper.js
+++ b/frontend/beta/js/Clipperz/YUI/DomHelper.js
@@ -1,214 +1,212 @@
1/* 1/*
2 2
3Copyright 2008-2011 Clipperz Srl 3Copyright 2008-2013 Clipperz Srl
4 4
5This file is part of Clipperz Community Edition. 5This file is part of Clipperz, the online password manager.
6Clipperz Community Edition is an online password manager.
7For further information about its features and functionalities please 6For further information about its features and functionalities please
8refer to http://www.clipperz.com. 7refer to http://www.clipperz.com.
9 8
10* Clipperz Community Edition is free software: you can redistribute 9* Clipperz is free software: you can redistribute it and/or modify it
11 it and/or modify it under the terms of the GNU Affero General Public 10 under the terms of the GNU Affero General Public License as published
12 License as published by the Free Software Foundation, either version 11 by the Free Software Foundation, either version 3 of the License, or
13 3 of the License, or (at your option) any later version. 12 (at your option) any later version.
14 13
15* Clipperz Community Edition is distributed in the hope that it will 14* Clipperz is distributed in the hope that it will be useful, but
16 be useful, but WITHOUT ANY WARRANTY; without even the implied 15 WITHOUT ANY WARRANTY; without even the implied warranty of
17 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU Affero General Public License for more details. 17 See the GNU Affero General Public License for more details.
19 18
20* You should have received a copy of the GNU Affero General Public 19* You should have received a copy of the GNU Affero General Public
21 License along with Clipperz Community Edition. If not, see 20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
22 <http://www.gnu.org/licenses/>.
23 21
24*/ 22*/
25 23
26if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } 24if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
27if (typeof(Clipperz.ext) == 'undefined') { Clipperz.ext = {}; } 25if (typeof(Clipperz.ext) == 'undefined') { Clipperz.ext = {}; }
28 26
29/** 27/**
30 * @class Clipperz.YUI.DomHelper 28 * @class Clipperz.YUI.DomHelper
31 * Utility class for working with DOM and/or Templates. It transparently supports using HTML fragments or DOM. 29 * Utility class for working with DOM and/or Templates. It transparently supports using HTML fragments or DOM.
32 * For more information see <a href="http://www.jackslocum.com/yui/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">this blog post with examples</a>. 30 * For more information see <a href="http://www.jackslocum.com/yui/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">this blog post with examples</a>.
33 * @singleton 31 * @singleton
34 */ 32 */
35Clipperz.YUI.DomHelper = new function(){ 33Clipperz.YUI.DomHelper = new function(){
36 /**@private*/ 34 /**@private*/
37 var d = document; 35 var d = document;
38 var tempTableEl = null; 36 var tempTableEl = null;
39 /** True to force the use of DOM instead of html fragments @type Boolean */ 37 /** True to force the use of DOM instead of html fragments @type Boolean */
40 this.useDom = false; 38 this.useDom = false;
41 var emptyTags = /^(?:base|basefont|br|frame|hr|img|input|isindex|link|meta|nextid|range|spacer|wbr|audioscope|area|param|keygen|col|limittext|spot|tab|over|right|left|choose|atop|of)$/i; 39 var emptyTags = /^(?:base|basefont|br|frame|hr|img|input|isindex|link|meta|nextid|range|spacer|wbr|audioscope|area|param|keygen|col|limittext|spot|tab|over|right|left|choose|atop|of)$/i;
42 /** 40 /**
43 * Applies a style specification to an element 41 * Applies a style specification to an element
44 * @param {String/HTMLElement} el The element to apply styles to 42 * @param {String/HTMLElement} el The element to apply styles to
45 * @param {String/Object/Function} styles A style specification string eg "width:100px", or object in the form {width:"100px"}, or 43 * @param {String/Object/Function} styles A style specification string eg "width:100px", or object in the form {width:"100px"}, or
46 * a function which returns such a specification. 44 * a function which returns such a specification.
47 */ 45 */
48 this.applyStyles = function(el, styles){ 46 this.applyStyles = function(el, styles){
49 if(styles){ 47 if(styles){
50 var D = YAHOO.util.Dom; 48 var D = YAHOO.util.Dom;
51 if (typeof styles == "string"){ 49 if (typeof styles == "string"){
52 var re = /\s?([a-z\-]*)\:([^;]*);?/gi; 50 var re = /\s?([a-z\-]*)\:([^;]*);?/gi;
53 var matches; 51 var matches;
54 while ((matches = re.exec(styles)) != null){ 52 while ((matches = re.exec(styles)) != null){
55 D.setStyle(el, matches[1], matches[2]); 53 D.setStyle(el, matches[1], matches[2]);
56 } 54 }
57 }else if (typeof styles == "object"){ 55 }else if (typeof styles == "object"){
58 for (var style in styles){ 56 for (var style in styles){
59 D.setStyle(el, style, styles[style]); 57 D.setStyle(el, style, styles[style]);
60 } 58 }
61 }else if (typeof styles == "function"){ 59 }else if (typeof styles == "function"){
62 Clipperz.YUI.DomHelper.applyStyles(el, styles.call()); 60 Clipperz.YUI.DomHelper.applyStyles(el, styles.call());
63 } 61 }
64 } 62 }
65 }; 63 };
66 64
67 // build as innerHTML where available 65 // build as innerHTML where available
68 /** @ignore */ 66 /** @ignore */
69 var createHtml = function(o){ 67 var createHtml = function(o){
70 var b = ''; 68 var b = '';
71 69
72 if(typeof(o['html']) != 'undefined') { 70 if(typeof(o['html']) != 'undefined') {
73 o['html'] = Clipperz.Base.sanitizeString(o['html']); 71 o['html'] = Clipperz.Base.sanitizeString(o['html']);
74 } else if (typeof(o['htmlString']) != 'undefined') { 72 } else if (typeof(o['htmlString']) != 'undefined') {
75 o['html'] = o['htmlString']; 73 o['html'] = o['htmlString'];
76 delete o.htmlString; 74 delete o.htmlString;
77 } 75 }
78 76
79 b += '<' + o.tag; 77 b += '<' + o.tag;
80 for(var attr in o){ 78 for(var attr in o){
81 if(attr == 'tag' || attr == 'children' || attr == 'html' || typeof o[attr] == 'function') continue; 79 if(attr == 'tag' || attr == 'children' || attr == 'html' || typeof o[attr] == 'function') continue;
82 if(attr == 'style'){ 80 if(attr == 'style'){
83 var s = o['style']; 81 var s = o['style'];
84 if(typeof s == 'function'){ 82 if(typeof s == 'function'){
85 s = s.call(); 83 s = s.call();
86 } 84 }
87 if(typeof s == 'string'){ 85 if(typeof s == 'string'){
88 b += ' style="' + s + '"'; 86 b += ' style="' + s + '"';
89 }else if(typeof s == 'object'){ 87 }else if(typeof s == 'object'){
90 b += ' style="'; 88 b += ' style="';
91 for(var key in s){ 89 for(var key in s){
92 if(typeof s[key] != 'function'){ 90 if(typeof s[key] != 'function'){
93 b += key + ':' + s[key] + ';'; 91 b += key + ':' + s[key] + ';';
94 } 92 }
95 } 93 }
96 b += '"'; 94 b += '"';
97 } 95 }
98 }else{ 96 }else{
99 if(attr == 'cls'){ 97 if(attr == 'cls'){
100 b += ' class="' + o['cls'] + '"'; 98 b += ' class="' + o['cls'] + '"';
101 }else if(attr == 'htmlFor'){ 99 }else if(attr == 'htmlFor'){
102 b += ' for="' + o['htmlFor'] + '"'; 100 b += ' for="' + o['htmlFor'] + '"';
103 }else{ 101 }else{
104 b += ' ' + attr + '="' + o[attr] + '"'; 102 b += ' ' + attr + '="' + o[attr] + '"';
105 } 103 }
106 } 104 }
107 } 105 }
108 if(emptyTags.test(o.tag)){ 106 if(emptyTags.test(o.tag)){
109 b += ' />'; 107 b += ' />';
110 }else{ 108 }else{
111 b += '>'; 109 b += '>';
112 if(o.children){ 110 if(o.children){
113 for(var i = 0, len = o.children.length; i < len; i++) { 111 for(var i = 0, len = o.children.length; i < len; i++) {
114 b += createHtml(o.children[i], b); 112 b += createHtml(o.children[i], b);
115 } 113 }
116 } 114 }
117 if(o.html){ 115 if(o.html){
118 b += o.html; 116 b += o.html;
119 } 117 }
120 b += '</' + o.tag + '>'; 118 b += '</' + o.tag + '>';
121 } 119 }
122 return b; 120 return b;
123 } 121 }
124 122
125 // build as dom 123 // build as dom
126 /** @ignore */ 124 /** @ignore */
127 var createDom = function(o, parentNode){ 125 var createDom = function(o, parentNode){
128 var el = d.createElement(o.tag); 126 var el = d.createElement(o.tag);
129 var useSet = el.setAttribute ? true : false; // In IE some elements don't have setAttribute 127 var useSet = el.setAttribute ? true : false; // In IE some elements don't have setAttribute
130 for(var attr in o){ 128 for(var attr in o){
131 if(attr == 'tag' || attr == 'children' || attr == 'html' || attr == 'style' || typeof o[attr] == 'function') continue; 129 if(attr == 'tag' || attr == 'children' || attr == 'html' || attr == 'style' || typeof o[attr] == 'function') continue;
132 if(attr=='cls'){ 130 if(attr=='cls'){
133 el.className = o['cls']; 131 el.className = o['cls'];
134 }else{ 132 }else{
135 if(useSet) el.setAttribute(attr, o[attr]); 133 if(useSet) el.setAttribute(attr, o[attr]);
136 else el[attr] = o[attr]; 134 else el[attr] = o[attr];
137 } 135 }
138 } 136 }
139 Clipperz.YUI.DomHelper.applyStyles(el, o.style); 137 Clipperz.YUI.DomHelper.applyStyles(el, o.style);
140 if(o.children){ 138 if(o.children){
141 for(var i = 0, len = o.children.length; i < len; i++) { 139 for(var i = 0, len = o.children.length; i < len; i++) {
142 createDom(o.children[i], el); 140 createDom(o.children[i], el);
143 } 141 }
144 } 142 }
145 if(o.html){ 143 if(o.html){
146 el.innerHTML = o.html; 144 el.innerHTML = o.html;
147 } 145 }
148 if(parentNode){ 146 if(parentNode){
149 parentNode.appendChild(el); 147 parentNode.appendChild(el);
150 } 148 }
151 return el; 149 return el;
152 }; 150 };
153 151
154 /** 152 /**
155 * @ignore 153 * @ignore
156 * Nasty code for IE's broken table implementation 154 * Nasty code for IE's broken table implementation
157 */ 155 */
158 var insertIntoTable = function(tag, where, el, html){ 156 var insertIntoTable = function(tag, where, el, html){
159 if(!tempTableEl){ 157 if(!tempTableEl){
160 tempTableEl = document.createElement('div'); 158 tempTableEl = document.createElement('div');
161 } 159 }
162 var node; 160 var node;
163 if(tag == 'table' || tag == 'tbody'){ 161 if(tag == 'table' || tag == 'tbody'){
164 tempTableEl.innerHTML = '<table><tbody>'+html+'</tbody></table>'; 162 tempTableEl.innerHTML = '<table><tbody>'+html+'</tbody></table>';
165 node = tempTableEl.firstChild.firstChild.firstChild; 163 node = tempTableEl.firstChild.firstChild.firstChild;
166 }else{ 164 }else{
167 tempTableEl.innerHTML = '<table><tbody><tr>'+html+'</tr></tbody></table>'; 165 tempTableEl.innerHTML = '<table><tbody><tr>'+html+'</tr></tbody></table>';
168 node = tempTableEl.firstChild.firstChild.firstChild.firstChild; 166 node = tempTableEl.firstChild.firstChild.firstChild.firstChild;
169 } 167 }
170 if(where == 'beforebegin'){ 168 if(where == 'beforebegin'){
171 el.parentNode.insertBefore(node, el); 169 el.parentNode.insertBefore(node, el);
172 return node; 170 return node;
173 }else if(where == 'afterbegin'){ 171 }else if(where == 'afterbegin'){
174 el.insertBefore(node, el.firstChild); 172 el.insertBefore(node, el.firstChild);
175 return node; 173 return node;
176 }else if(where == 'beforeend'){ 174 }else if(where == 'beforeend'){
177 el.appendChild(node); 175 el.appendChild(node);
178 return node; 176 return node;
179 }else if(where == 'afterend'){ 177 }else if(where == 'afterend'){
180 el.parentNode.insertBefore(node, el.nextSibling); 178 el.parentNode.insertBefore(node, el.nextSibling);
181 return node; 179 return node;
182 } 180 }
183 } 181 }
184 182
185 /** 183 /**
186 * Inserts an HTML fragment into the Dom 184 * Inserts an HTML fragment into the Dom
187 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd. 185 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
188 * @param {HTMLElement} el The context element 186 * @param {HTMLElement} el The context element
189 * @param {String} html The HTML fragmenet 187 * @param {String} html The HTML fragmenet
190 * @return {HTMLElement} The new node 188 * @return {HTMLElement} The new node
191 */ 189 */
192 this.insertHtml = function(where, el, html){ 190 this.insertHtml = function(where, el, html){
193 where = where.toLowerCase(); 191 where = where.toLowerCase();
194 if(el.insertAdjacentHTML){ 192 if(el.insertAdjacentHTML){
195 var tag = el.tagName.toLowerCase(); 193 var tag = el.tagName.toLowerCase();
196 if(tag == 'table' || tag == 'tbody' || tag == 'tr'){ 194 if(tag == 'table' || tag == 'tbody' || tag == 'tr'){
197 return insertIntoTable(tag, where, el, html); 195 return insertIntoTable(tag, where, el, html);
198 } 196 }
199 switch(where){ 197 switch(where){
200 case 'beforebegin': 198 case 'beforebegin':
201 el.insertAdjacentHTML(where, html); 199 el.insertAdjacentHTML(where, html);
202 return el.previousSibling; 200 return el.previousSibling;
203 case 'afterbegin': 201 case 'afterbegin':
204 el.insertAdjacentHTML(where, html); 202 el.insertAdjacentHTML(where, html);
205 return el.firstChild; 203 return el.firstChild;
206 case 'beforeend': 204 case 'beforeend':
207 el.insertAdjacentHTML(where, html); 205 el.insertAdjacentHTML(where, html);
208 return el.lastChild; 206 return el.lastChild;
209 case 'afterend': 207 case 'afterend':
210 el.insertAdjacentHTML(where, html); 208 el.insertAdjacentHTML(where, html);
211 return el.nextSibling; 209 return el.nextSibling;
212 } 210 }
213 throw 'Illegal insertion point -> "' + where + '"'; 211 throw 'Illegal insertion point -> "' + where + '"';
214 } 212 }