summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/element.js2
-rw-r--r--src/event.js222
-rw-r--r--src/set.js17
-rw-r--r--src/svg.js2
4 files changed, 121 insertions, 122 deletions
diff --git a/src/element.js b/src/element.js
index e2f9c47..d7dcbc1 100644
--- a/src/element.js
+++ b/src/element.js
@@ -5,6 +5,7 @@ SVG.Element = SVG.invent({
// make stroke value accessible dynamically
this._stroke = SVG.defaults.attrs.stroke
this._event = null
+ this._events = {}
// initialize data object
this.dom = {}
@@ -13,6 +14,7 @@ SVG.Element = SVG.invent({
if (this.node = node) {
this.type = node.nodeName
this.node.instance = this
+ this._events = node._events || {}
// store current attribute value
this._stroke = node.getAttribute('stroke') || this._stroke
diff --git a/src/event.js b/src/event.js
index 2563699..fae7314 100644
--- a/src/event.js
+++ b/src/event.js
@@ -1,144 +1,140 @@
// Add events to elements
-;[ 'click'
- , 'dblclick'
- , 'mousedown'
- , 'mouseup'
- , 'mouseover'
- , 'mouseout'
- , 'mousemove'
- // , 'mouseenter' -> not supported by IE
- // , 'mouseleave' -> not supported by IE
- , 'touchstart'
- , 'touchmove'
- , 'touchleave'
- , 'touchend'
- , 'touchcancel' ].forEach(function(event) {
-
- // add event to SVG.Element
- SVG.Element.prototype[event] = function(f) {
- // bind event to element rather than element node
- SVG.on(this.node, event, f)
- return this
- }
-})
-// Initialize listeners stack
-SVG.listeners = []
-SVG.handlerMap = []
+;[ 'click',
+ 'dblclick',
+ 'mousedown',
+ 'mouseup',
+ 'mouseover',
+ 'mouseout',
+ 'mousemove',
+ 'mouseenter',
+ 'mouseleave',
+ 'touchstart',
+ 'touchmove',
+ 'touchleave',
+ 'touchend',
+ 'touchcancel' ].forEach(function (event) {
+ // add event to SVG.Element
+ SVG.Element.prototype[event] = function (f) {
+ // bind event to element rather than element node
+ if (f == null) {
+ SVG.off(this, event)
+ } else {
+ SVG.on(this, event, f)
+ }
+ return this
+ }
+ })
+
SVG.listenerId = 0
// Add event binder in the SVG namespace
-SVG.on = function(node, event, listener, binding, options) {
- // create listener, get object-index
- var l = listener.bind(binding || node.instance || node)
- , index = (SVG.handlerMap.indexOf(node) + 1 || SVG.handlerMap.push(node)) - 1
- , ev = event.split('.')[0]
- , ns = event.split('.')[1] || '*'
-
+SVG.on = function (node, events, listener, binding, options) {
+ var l = listener.bind(binding || node)
+ var n = node instanceof SVG.Element ? node.node : node
- // 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] || {}
+ // ensure instance object for nodes which are not adopted
+ n.instance = n.instance || {_events: {}}
- if(!listener._svgjsListenerId)
- listener._svgjsListenerId = ++SVG.listenerId
+ var bag = n.instance._events
- // reference listener
- SVG.listeners[index][ev][ns][listener._svgjsListenerId] = l
+ // add id to listener
+ if (!listener._svgjsListenerId) { listener._svgjsListenerId = ++SVG.listenerId }
- // add listener
- node.addEventListener(ev, l, options || false)
-}
+ events.split(SVG.regex.delimiter).forEach(function (event) {
+ var ev = event.split('.')[0]
+ var ns = event.split('.')[1] || '*'
-// 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]
- , namespace = ''
+ // ensure valid object
+ bag[ev] = bag[ev] || {}
+ bag[ev][ns] = bag[ev][ns] || {}
- if(index == -1) return
+ // reference listener
+ bag[ev][ns][listener._svgjsListenerId] = l
- if (listener) {
- if(typeof listener == 'function') listener = listener._svgjsListenerId
- if(!listener) return
-
- // remove listener reference
- if (SVG.listeners[index][ev] && SVG.listeners[index][ev][ns || '*']) {
- // remove listener
- node.removeEventListener(ev, SVG.listeners[index][ev][ns || '*'][listener], false)
-
- delete SVG.listeners[index][ev][ns || '*'][listener]
- }
-
- } else if (ns && ev) {
- // remove all listeners for a 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)
+ // add listener
+ n.addEventListener(ev, l, options || false)
+ })
+}
- delete SVG.listeners[index][ev][ns]
- }
+// Add event unbinder in the SVG namespace
+SVG.off = function (node, events, listener, options) {
+ var n = node instanceof SVG.Element ? node.node : node
+ if (!n.instance) return
+
+ // listener can be a function or a number
+ if (typeof listener === 'function') {
+ listener = listener._svgjsListenerId
+ if (!listener) return
+ }
- } else if (ns){
- // remove all listeners for a specific namespace
- for(event in SVG.listeners[index]){
- for(namespace in SVG.listeners[index][event]){
- if(ns === namespace){
- SVG.off(node, [event, ns].join('.'))
- }
+ var bag = n.instance._events
+
+ ;(events || '').split(SVG.regex.delimiter).forEach(function (event) {
+ var ev = event && event.split('.')[0]
+ var ns = event && event.split('.')[1]
+ var namespace, l
+
+ if (listener) {
+ // remove listener reference
+ if (bag[ev] && bag[ev][ns || '*']) {
+ // removeListener
+ n.removeEventListener(ev, bag[ev][ns || '*'][listener], options || false)
+
+ delete bag[ev][ns || '*'][listener]
+ }
+ } else if (ev && ns) {
+ // remove all listeners for a namespaced event
+ if (bag[ev] && bag[ev][ns]) {
+ for (l in bag[ev][ns]) { SVG.off(n, [ev, ns].join('.'), l) }
+
+ delete bag[ev][ns]
+ }
+ } else if (ns) {
+ // remove all listeners for a specific namespace
+ for (event in bag) {
+ for (namespace in bag[event]) {
+ if (ns === namespace) { SVG.off(n, [event, ns].join('.')) }
}
+ }
+ } else if (ev) {
+ // remove all listeners for the event
+ if (bag[ev]) {
+ for (namespace in bag[ev]) { SVG.off(n, [ev, namespace].join('.')) }
+
+ delete bag[ev]
+ }
+ } else {
+ // remove all listeners on a given node
+ for (event in bag) { SVG.off(n, event) }
+
+ n.instance._events = {}
}
-
- } else if (ev) {
- // remove all listeners for the event
- if (SVG.listeners[index][ev]) {
- for (namespace in SVG.listeners[index][ev])
- SVG.off(node, [ev, namespace].join('.'))
-
- delete SVG.listeners[index][ev]
- }
-
- } else {
- // remove all listeners on a given node
- for (event in SVG.listeners[index])
- SVG.off(node, event)
-
- delete SVG.listeners[index]
- delete SVG.handlerMap[index]
-
- }
+ })
}
-//
SVG.extend(SVG.Element, {
// Bind given event to listener
- on: function(event, listener, binding, options) {
- SVG.on(this.node, event, listener, binding, options)
-
+ on: function (event, listener, binding, options) {
+ SVG.on(this, event, listener, binding, options)
return this
- }
+ },
// Unbind event from listener
-, off: function(event, listener) {
+ off: function (event, listener) {
SVG.off(this.node, event, listener)
-
return this
- }
- // Fire given event
-, fire: function(event, data) {
-
+ },
+ fire: function (event, data) {
// Dispatch event
- if(event instanceof window.Event){
- this.node.dispatchEvent(event)
- }else{
- this.node.dispatchEvent(event = new SVG.CustomEvent(event, {detail:data, cancelable: true}))
+ if (event instanceof window.Event) {
+ this.node.dispatchEvent(event)
+ } else {
+ this.node.dispatchEvent(event = new window.CustomEvent(event, {detail: data, cancelable: true}))
}
-
this._event = event
return this
- }
-, event: function() {
+ },
+ event: function() {
return this._event
}
})
diff --git a/src/set.js b/src/set.js
index 9da52c7..a602d1d 100644
--- a/src/set.js
+++ b/src/set.js
@@ -1,8 +1,11 @@
SVG.Set = SVG.invent({
// Initialize
create: function(members) {
- // Set initial state
- Array.isArray(members) ? this.members = members : this.clear()
+ if (members instanceof SVG.Set) {
+ this.members = members.members.slice()
+ } else {
+ Array.isArray(members) ? this.members = members : this.clear()
+ }
}
// Add class methods
@@ -13,13 +16,13 @@ SVG.Set = SVG.invent({
for (i = 0, il = elements.length; i < il; i++)
this.members.push(elements[i])
-
+
return this
}
// Remove element from set
, remove: function(element) {
var i = this.index(element)
-
+
// remove given child
if (i > -1)
this.members.splice(i, 1)
@@ -85,7 +88,7 @@ SVG.Set = SVG.invent({
return rbox
}
}
-
+
// Add parent method
, construct: {
// Create a new set
@@ -108,7 +111,7 @@ SVG.FX.Set = SVG.invent({
SVG.Set.inherit = function() {
var m
, methods = []
-
+
// gather shape methods
for(var m in SVG.Shape.prototype)
if (typeof SVG.Shape.prototype[m] == 'function' && typeof SVG.Set.prototype[m] != 'function')
@@ -143,5 +146,3 @@ SVG.Set.inherit = function() {
}
})
}
-
-
diff --git a/src/svg.js b/src/svg.js
index a0bd5fc..6312736 100644
--- a/src/svg.js
+++ b/src/svg.js
@@ -139,7 +139,7 @@ SVG.prepare = function() {
// Create parser object
SVG.parser = {
body: body || document.documentElement
- , draw: draw.style('opacity:0;position:absolute;left:-100%;top:-100%;overflow:hidden').node
+ , draw: draw.style('opacity:0;position:absolute;left:-100%;top:-100%;overflow:hidden').attr('focusable', 'false').node
, poly: draw.polyline().node
, path: draw.path().node
, native: SVG.create('svg')