Mercurial > hg > digilib
comparison client/digitallibrary/jquery/svg/jquery.svgdom.js @ 749:4b492b7900fb jquery
added jQuery ui and svg javascripts
author | hertzhaft |
---|---|
date | Sun, 06 Feb 2011 22:17:41 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
748:fb4ffac2950d | 749:4b492b7900fb |
---|---|
1 /* http://keith-wood.name/svg.html | |
2 SVG/jQuery DOM compatibility for jQuery v1.4.3. | |
3 Written by Keith Wood (kbwood{at}iinet.com.au) April 2009. | |
4 Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and | |
5 MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. | |
6 Please attribute the author if you use it. */ | |
7 | |
8 (function($) { // Hide scope, no $ conflict | |
9 | |
10 /* Support adding class names to SVG nodes. */ | |
11 $.fn.addClass = function(origAddClass) { | |
12 return function(classNames) { | |
13 classNames = classNames || ''; | |
14 return this.each(function() { | |
15 if (isSVGElem(this)) { | |
16 var node = this; | |
17 $.each(classNames.split(/\s+/), function(i, className) { | |
18 var classes = (node.className ? node.className.baseVal : node.getAttribute('class')); | |
19 if ($.inArray(className, classes.split(/\s+/)) == -1) { | |
20 classes += (classes ? ' ' : '') + className; | |
21 (node.className ? node.className.baseVal = classes : | |
22 node.setAttribute('class', classes)); | |
23 } | |
24 }); | |
25 } | |
26 else { | |
27 origAddClass.apply($(this), [classNames]); | |
28 } | |
29 }); | |
30 }; | |
31 }($.fn.addClass); | |
32 | |
33 /* Support removing class names from SVG nodes. */ | |
34 $.fn.removeClass = function(origRemoveClass) { | |
35 return function(classNames) { | |
36 classNames = classNames || ''; | |
37 return this.each(function() { | |
38 if (isSVGElem(this)) { | |
39 var node = this; | |
40 $.each(classNames.split(/\s+/), function(i, className) { | |
41 var classes = (node.className ? node.className.baseVal : node.getAttribute('class')); | |
42 classes = $.grep(classes.split(/\s+/), function(n, i) { return n != className; }). | |
43 join(' '); | |
44 (node.className ? node.className.baseVal = classes : | |
45 node.setAttribute('class', classes)); | |
46 }); | |
47 } | |
48 else { | |
49 origRemoveClass.apply($(this), [classNames]); | |
50 } | |
51 }); | |
52 }; | |
53 }($.fn.removeClass); | |
54 | |
55 /* Support toggling class names on SVG nodes. */ | |
56 $.fn.toggleClass = function(origToggleClass) { | |
57 return function(className, state) { | |
58 return this.each(function() { | |
59 if (isSVGElem(this)) { | |
60 if (typeof state !== 'boolean') { | |
61 state = !$(this).hasClass(className); | |
62 } | |
63 $(this)[(state ? 'add' : 'remove') + 'Class'](className); | |
64 } | |
65 else { | |
66 origToggleClass.apply($(this), [className, state]); | |
67 } | |
68 }); | |
69 }; | |
70 }($.fn.toggleClass); | |
71 | |
72 /* Support checking class names on SVG nodes. */ | |
73 $.fn.hasClass = function(origHasClass) { | |
74 return function(className) { | |
75 className = className || ''; | |
76 var found = false; | |
77 this.each(function() { | |
78 if (isSVGElem(this)) { | |
79 var classes = (this.className ? this.className.baseVal : | |
80 this.getAttribute('class')).split(/\s+/); | |
81 found = ($.inArray(className, classes) > -1); | |
82 } | |
83 else { | |
84 found = (origHasClass.apply($(this), [className])); | |
85 } | |
86 return !found; | |
87 }); | |
88 return found; | |
89 }; | |
90 }($.fn.hasClass); | |
91 | |
92 /* Support attributes on SVG nodes. */ | |
93 $.fn.attr = function(origAttr) { | |
94 return function(name, value, type) { | |
95 if (typeof name === 'string' && value === undefined) { | |
96 var val = origAttr.apply(this, [name, value, type]); | |
97 if (val && val.baseVal && val.baseVal.numberOfItems != null) { // Transform | |
98 value = ''; | |
99 val = val.baseVal; | |
100 for (var i = 0; i < val.numberOfItems; i++) { | |
101 var item = val.getItem(i); | |
102 switch (item.type) { | |
103 case 1: value += ' matrix(' + item.matrix.a + ',' + item.matrix.b + ',' + | |
104 item.matrix.c + ',' + item.matrix.d + ',' + | |
105 item.matrix.e + ',' + item.matrix.f + ')'; | |
106 break; | |
107 case 2: value += ' translate(' + item.matrix.e + ',' + item.matrix.f + ')'; break; | |
108 case 3: value += ' scale(' + item.matrix.a + ',' + item.matrix.d + ')'; break; | |
109 case 4: value += ' rotate(' + item.angle + ')'; break; // Doesn't handle new origin | |
110 case 5: value += ' skewX(' + item.angle + ')'; break; | |
111 case 6: value += ' skewY(' + item.angle + ')'; break; | |
112 } | |
113 } | |
114 val = value.substring(1); | |
115 } | |
116 return (val && val.baseVal ? val.baseVal.valueAsString : val); | |
117 } | |
118 var options = name; | |
119 if (typeof name === 'string') { | |
120 options = {}; | |
121 options[name] = value; | |
122 } | |
123 return this.each(function() { | |
124 if (isSVGElem(this)) { | |
125 for (var n in options) { | |
126 var val = ($.isFunction(options[n]) ? options[n]() : options[n]); | |
127 (type ? this.style[n] = val : this.setAttribute(n, val)); | |
128 } | |
129 } | |
130 else { | |
131 origAttr.apply($(this), [name, value, type]); | |
132 } | |
133 }); | |
134 }; | |
135 }($.fn.attr); | |
136 | |
137 /* Support removing attributes on SVG nodes. */ | |
138 $.fn.removeAttr = function(origRemoveAttr) { | |
139 return function(name) { | |
140 return this.each(function() { | |
141 if (isSVGElem(this)) { | |
142 (this[name] && this[name].baseVal ? this[name].baseVal.value = '' : | |
143 this.setAttribute(name, '')); | |
144 } | |
145 else { | |
146 origRemoveAttr.apply($(this), [name]); | |
147 } | |
148 }); | |
149 }; | |
150 }($.fn.removeAttr); | |
151 | |
152 /* Determine if any nodes are SVG nodes. */ | |
153 function anySVG(checkSet) { | |
154 for (var i = 0; i < checkSet.length; i++) { | |
155 if (checkSet[i].nodeType == 1 && checkSet[i].namespaceURI == $.svg.svgNS) { | |
156 return true; | |
157 } | |
158 } | |
159 return false; | |
160 } | |
161 | |
162 /* Update Sizzle selectors. */ | |
163 | |
164 $.expr.relative['+'] = function(origRelativeNext) { | |
165 return function(checkSet, part, isXML) { | |
166 origRelativeNext(checkSet, part, isXML || anySVG(checkSet)); | |
167 }; | |
168 }($.expr.relative['+']); | |
169 | |
170 $.expr.relative['>'] = function(origRelativeChild) { | |
171 return function(checkSet, part, isXML) { | |
172 origRelativeChild(checkSet, part, isXML || anySVG(checkSet)); | |
173 }; | |
174 }($.expr.relative['>']); | |
175 | |
176 $.expr.relative[''] = function(origRelativeDescendant) { | |
177 return function(checkSet, part, isXML) { | |
178 origRelativeDescendant(checkSet, part, isXML || anySVG(checkSet)); | |
179 }; | |
180 }($.expr.relative['']); | |
181 | |
182 $.expr.relative['~'] = function(origRelativeSiblings) { | |
183 return function(checkSet, part, isXML) { | |
184 origRelativeSiblings(checkSet, part, isXML || anySVG(checkSet)); | |
185 }; | |
186 }($.expr.relative['~']); | |
187 | |
188 $.expr.find.ID = function(origFindId) { | |
189 return function(match, context, isXML) { | |
190 return (isSVGElem(context) ? | |
191 [context.ownerDocument.getElementById(match[1])] : | |
192 origFindId(match, context, isXML)); | |
193 }; | |
194 }($.expr.find.ID); | |
195 | |
196 var div = document.createElement('div'); | |
197 div.appendChild(document.createComment('')); | |
198 if (div.getElementsByTagName('*').length > 0) { // Make sure no comments are found | |
199 $.expr.find.TAG = function(match, context) { | |
200 var results = context.getElementsByTagName(match[1]); | |
201 if (match[1] === '*') { // Filter out possible comments | |
202 var tmp = []; | |
203 for (var i = 0; results[i] || results.item(i); i++) { | |
204 if ((results[i] || results.item(i)).nodeType === 1) { | |
205 tmp.push(results[i] || results.item(i)); | |
206 } | |
207 } | |
208 results = tmp; | |
209 } | |
210 return results; | |
211 }; | |
212 } | |
213 | |
214 $.expr.preFilter.CLASS = function(match, curLoop, inplace, result, not, isXML) { | |
215 match = ' ' + match[1].replace(/\\/g, '') + ' '; | |
216 if (isXML) { | |
217 return match; | |
218 } | |
219 for (var i = 0, elem = {}; elem != null; i++) { | |
220 elem = curLoop[i]; | |
221 if (!elem) { | |
222 try { | |
223 elem = curLoop.item(i); | |
224 } | |
225 catch (e) { | |
226 // Ignore | |
227 } | |
228 } | |
229 if (elem) { | |
230 var className = (!isSVGElem(elem) ? elem.className : | |
231 (elem.className ? elem.className.baseVal : '') || elem.getAttribute('class')); | |
232 if (not ^ (className && (' ' + className + ' ').indexOf(match) > -1)) { | |
233 if (!inplace) | |
234 result.push(elem); | |
235 } | |
236 else if (inplace) { | |
237 curLoop[i] = false; | |
238 } | |
239 } | |
240 } | |
241 return false; | |
242 }; | |
243 | |
244 $.expr.filter.CLASS = function(elem, match) { | |
245 var className = (!isSVGElem(elem) ? elem.className : | |
246 (elem.className ? elem.className.baseVal : elem.getAttribute('class'))); | |
247 return (' ' + className + ' ').indexOf(match) > -1; | |
248 }; | |
249 | |
250 $.expr.filter.ATTR = function(origFilterAttr) { | |
251 return function(elem, match) { | |
252 var handler = null; | |
253 if (isSVGElem(elem)) { | |
254 handler = match[1]; | |
255 $.expr.attrHandle[handler] = function(elem){ | |
256 var attr = elem.getAttribute(handler); | |
257 return attr && attr.baseVal || attr; | |
258 }; | |
259 } | |
260 var filter = origFilterAttr(elem, match); | |
261 if (handler) { | |
262 $.expr.attrHandle[handler] = null; | |
263 } | |
264 return filter; | |
265 }; | |
266 }($.expr.filter.ATTR); | |
267 | |
268 /* | |
269 Change Sizzle initialisation (line 1425) in jQuery v1.3.2 base code... | |
270 | |
271 if ( toString.call(checkSet) === "[object Array]" ) { | |
272 if ( !prune ) { | |
273 results.push.apply( results, checkSet ); | |
274 } else if ( context.nodeType === 1 ) { | |
275 for ( var i = 0; checkSet[i] != null; i++ ) { | |
276 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) { | |
277 results.push( set[i] || set.item(i) ); // Here | |
278 } | |
279 } | |
280 } else { | |
281 for ( var i = 0; checkSet[i] != null; i++ ) { | |
282 if ( checkSet[i] && checkSet[i].nodeType === 1 ) { | |
283 results.push( set[i] || set.item(i) ); // Here | |
284 } | |
285 } | |
286 } | |
287 } | |
288 | |
289 Change fallback makeArray (line 2076) implementation in jQuery Sizzle... | |
290 | |
291 if ( typeof array.length === "number" ) { | |
292 for ( var i = 0, l = array.length; i < l; i++ ) { | |
293 ret.push( array[i] || array.item(i) ); // Here | |
294 } | |
295 } | |
296 */ | |
297 | |
298 /* | |
299 Events management requires changes to jQuery v1.3.2 base code... | |
300 | |
301 In $.event.add (line 2437)... | |
302 | |
303 if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem, data, namespaces) === false ) { | |
304 // Bind the global event handler to the element | |
305 try { // Here | |
306 elem.addEventListener(type, handle, false); | |
307 } | |
308 catch(e) { | |
309 if (elem.attachEvent) | |
310 elem.attachEvent("on" + type, handle); | |
311 } | |
312 } | |
313 | |
314 In $.event.remove (line 2521)... | |
315 | |
316 if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem, namespaces) === false ) { | |
317 try { // Here | |
318 elem.removeEventListener(type, jQuery.data(elem, "handle"), false); | |
319 } | |
320 catch (e) { | |
321 if (elem.detachEvent) | |
322 elem.detachEvent("on" + type, jQuery.data(elem, "handle")); | |
323 } | |
324 } | |
325 */ | |
326 | |
327 /* Does this node belong to SVG? */ | |
328 function isSVGElem(node) { | |
329 return (node.nodeType == 1 && node.namespaceURI == $.svg.svgNS); | |
330 } | |
331 | |
332 })(jQuery); |