diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2015-06-11 01:07:59 +0200 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2015-06-11 01:07:59 +0200 |
commit | 843c0f5b0f7140c4d46d92293c2c18767d070f69 (patch) | |
tree | e123f0649d4ebe9bc6b37cd18da8aca4ab38142c /src | |
parent | d5f5dd18a6c9d986dd0582256e2120675e1ca561 (diff) | |
download | svg.js-843c0f5b0f7140c4d46d92293c2c18767d070f69.tar.gz svg.js-843c0f5b0f7140c4d46d92293c2c18767d070f69.zip |
Merged all changes and fixes from SVG 1.1 into this branch
Diffstat (limited to 'src')
-rwxr-xr-x | src/doc.js | 9 | ||||
-rwxr-xr-x | src/event.js | 84 | ||||
-rwxr-xr-x | src/fx.js | 5 | ||||
-rw-r--r-- | src/helpers.js | 11 | ||||
-rw-r--r-- | src/polyfill.js | 32 | ||||
-rwxr-xr-x | src/rect.js | 18 | ||||
-rwxr-xr-x | src/svg.js | 2 | ||||
-rwxr-xr-x | src/text.js | 8 | ||||
-rwxr-xr-x | src/use.js | 12 |
9 files changed, 109 insertions, 72 deletions
@@ -65,6 +65,15 @@ SVG.Doc = SVG.invent({ return this } + + // Removes the doc from the DOM + , remove: function() { + if(this.parent()) { + this.parent().removeChild(this.node); + } + + return this; + } } }) diff --git a/src/event.js b/src/event.js index 2855afa..22a6117 100755 --- a/src/event.js +++ b/src/event.js @@ -6,8 +6,8 @@ , 'mouseover' , 'mouseout' , 'mousemove' - , 'mouseenter' - , 'mouseleave' + // , 'mouseenter' -> not supported by IE + // , 'mouseleave' -> not supported by IE , 'touchstart' , 'touchmove' , 'touchleave' @@ -27,60 +27,73 @@ }) -// Initialize events and listeners stack -SVG.events = {} -SVG.listeners = {} - -// Event constructor -SVG.registerEvent = function(event) { - if (!SVG.events[event]) - SVG.events[event] = new CustomEvent(event) -} +// Initialize listeners stack +SVG.listeners = [] +SVG.handlerMap = [] // Add event binder in the SVG namespace SVG.on = function(node, event, listener) { - // create listener - var l = listener.bind(node.instance || node) - - // ensure reference objects - SVG.listeners[node] = SVG.listeners[node] || {} - SVG.listeners[node][event] = SVG.listeners[node][event] || {} + // create listener, get object-index + var l = listener.bind(node.instance || node) + , index = (SVG.handlerMap.indexOf(node) + 1 || SVG.handlerMap.push(node)) - 1 + , ev = event.split('.')[0] + , ns = event.split('.')[1] || '*' + + + // ensure valid object + SVG.listeners[index] = SVG.listeners[index] || {} + SVG.listeners[index][ev] = SVG.listeners[index][ev] || {} + SVG.listeners[index][ev][ns] = SVG.listeners[index][ev][ns] || {} // reference listener - SVG.listeners[node][event][listener] = l + SVG.listeners[index][ev][ns][listener] = l // add listener - node.addEventListener(event, l, false) + node.addEventListener(ev, l, false) } // Add event unbinder in the SVG namespace SVG.off = function(node, event, listener) { + var index = SVG.handlerMap.indexOf(node) + , ev = event && event.split('.')[0] + , ns = event && event.split('.')[1] + + if(index == -1) return + if (listener) { // remove listener reference - if (SVG.listeners[node] && SVG.listeners[node][event]) { + if (SVG.listeners[index][ev] && SVG.listeners[index][ev][ns || '*']) { // remove listener - node.removeEventListener(event, SVG.listeners[node][event][listener], false) + node.removeEventListener(ev, SVG.listeners[index][ev][ns || '*'][listener], false) - delete SVG.listeners[node][event][listener] + delete SVG.listeners[index][ev][ns || '*'][listener] } - } else if (event) { + } else if (ns) { + // remove all listeners for the namespaced event + if (SVG.listeners[index][ev] && SVG.listeners[index][ev][ns]) { + for (listener in SVG.listeners[index][ev][ns]) + SVG.off(node, [ev, ns].join('.'), listener) + + delete SVG.listeners[index][ev][ns] + } + + } else if (ev) { // remove all listeners for the event - if (SVG.listeners[node][event]) { - for (listener in SVG.listeners[node][event]) - SVG.off(node, event, listener) + if (SVG.listeners[index][ev]) { + for (namespace in SVG.listeners[index][ev]) + SVG.off(node, [ev, namespace].join('.')) - delete SVG.listeners[node][event] + delete SVG.listeners[index][ev] } } else { // remove all listeners on a given node - if (SVG.listeners[node]) { - for (event in SVG.listeners[node]) - SVG.off(node, event) + for (event in SVG.listeners[index]) + SVG.off(node, event) + + delete SVG.listeners[index] - delete SVG.listeners[node] - } } } @@ -100,14 +113,9 @@ SVG.extend(SVG.Element, { } // Fire given event , fire: function(event, data) { - // Add detail data to event - SVG.events[event].detail = data // Dispatch event - this.node.dispatchEvent(SVG.events[event]) - - // Remove detail - delete SVG.events[event].detail + this.node.dispatchEvent(new CustomEvent(event, {detail:data})) return this } @@ -190,10 +190,10 @@ SVG.FX = SVG.invent({ } } else { - requestAnimFrame(fx.render) + fx.animationFrame = requestAnimationFrame(fx.render) } } else { - requestAnimFrame(fx.render) + fx.animationFrame = requestAnimationFrame(fx.render) } } @@ -390,6 +390,7 @@ SVG.FX = SVG.invent({ } else { // stop current animation clearTimeout(this.timeout) + cancelAnimationFrame(this.animationFrame); // reset storage for properties this.attrs = {} diff --git a/src/helpers.js b/src/helpers.js index 3facb5d..7fe9f7a 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -169,13 +169,4 @@ function idFromReference(url) { } // Create matrix array for looping -var abcdef = 'abcdef'.split('') - -// Shim layer with setTimeout fallback by Paul Irish -window.requestAnimFrame = (function(){ - return window.requestAnimationFrame || - window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.msRequestAnimationFrame || - function (c) { window.setTimeout(c, 1000 / 60) } -})()
\ No newline at end of file +var abcdef = 'abcdef'.split('')
\ No newline at end of file diff --git a/src/polyfill.js b/src/polyfill.js index 3a3c25c..06ae0fd 100644 --- a/src/polyfill.js +++ b/src/polyfill.js @@ -1,7 +1,7 @@ // Add CustomEvent to IE9 and IE10 if (typeof CustomEvent !== 'function') { // Code from: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent - function CustomEvent (event, options) { + var CustomEvent = function(event, options) { options = options || { bubbles: false, cancelable: false, detail: undefined } var e = document.createEvent('CustomEvent') e.initCustomEvent(event, options.bubbles, options.cancelable, options.detail) @@ -11,4 +11,32 @@ if (typeof CustomEvent !== 'function') { CustomEvent.prototype = window.Event.prototype window.CustomEvent = CustomEvent -}
\ No newline at end of file +} + +// requestAnimationFrame / cancelAnimationFrame Polyfill with fallback based on Paul Irish +(function(w) { + var lastTime = 0 + var vendors = ['moz', 'webkit'] + + for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { + w.requestAnimationFrame = w[vendors[x] + 'RequestAnimationFrame'] + w.cancelAnimationFrame = w[vendors[x] + 'CancelAnimationFrame'] || + w[vendors[x] + 'CancelRequestAnimationFrame'] + } + + w.requestAnimationFrame = w.requestAnimationFrame || + function(callback) { + var currTime = new Date().getTime() + var timeToCall = Math.max(0, 16 - (currTime - lastTime)) + + var id = w.setTimeout(function() { + callback(currTime + timeToCall) + }, timeToCall) + + lastTime = currTime + timeToCall + return id + } + + w.cancelAnimationFrame = w.cancelAnimationFrame || w.clearTimeout; + +}(window))
\ No newline at end of file diff --git a/src/rect.js b/src/rect.js index 825dd32..8f90e90 100755 --- a/src/rect.js +++ b/src/rect.js @@ -1,15 +1,15 @@ SVG.Rect = SVG.invent({ - // Initialize node + // Initialize node create: 'rect' - // Inherit from + // Inherit from , inherit: SVG.Shape - - // Add parent method + + // Add parent method , construct: { - // Create a rect element - rect: function(width, height) { - return this.put(new SVG.Rect().size(width, height)) - } - } + // Create a rect element + rect: function(width, height) { + return this.put(new SVG.Rect().size(width, height)) + } + } })
\ No newline at end of file @@ -98,7 +98,7 @@ SVG.adopt = function(node) { // adopt with element-specific settings if (node.nodeName == 'svg') element = node.parentNode instanceof SVGElement ? new SVG.Nested : new SVG.Doc - else if (node.nodeName == 'lineairGradient') + else if (node.nodeName == 'lineairGradient') // lineair? element = new SVG.Gradient('lineair') else if (node.nodeName == 'radialGradient') element = new SVG.Gradient('radial') diff --git a/src/text.js b/src/text.js index d697681..389a670 100755 --- a/src/text.js +++ b/src/text.js @@ -200,10 +200,13 @@ SVG.extend(SVG.Text, SVG.Tspan, { if (this._build === false) this.clear() - // add new tspan and reference + // add new tspan node.appendChild(tspan.node) // only first level tspans are considered to be "lines" + // that doenst make sence. A line is added to a SVG.Set which is never used or returned. + // So why bother adding it? + // Also: lines() reads all children so it already has this tspan in it because we added it before if (this instanceof SVG.Text) this.lines().add(tspan) @@ -228,6 +231,3 @@ SVG.extend(SVG.Text, SVG.Tspan, { return this.node.getComputedTextLength() } }) - -// Register rebuild event -SVG.registerEvent('rebuild') @@ -8,20 +8,20 @@ SVG.Use = SVG.invent({ // Add class methods , extend: { // Use element as a reference - element: function(element) { - // Store target element + element: function(element, file) { + /* Store target element */ this.target = element - // Set lined element - return this.attr('href', '#' + element, SVG.xlink) + /* Set lined element */ + return this.attr('href', (file || '') + '#' + element, SVG.xlink) } } // Add parent method , construct: { // Create a use element - use: function(element) { - return this.put(new SVG.Use).element(element) + use: function(element, file) { + return this.put(new SVG.Use).element(element, file) } } })
\ No newline at end of file |