2011-03-14 17:14:20 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-03-14 17:14:20 -04:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-03-14 17:14:20 -04:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-14 17:14:20 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-07-31 10:23:26 -04:00
|
|
|
/**
|
|
|
|
* @name DomElement
|
|
|
|
* @namespace
|
|
|
|
* @private
|
|
|
|
*/
|
2012-11-14 01:58:03 -05:00
|
|
|
var DomElement = new function() {
|
|
|
|
// We use a mix of Bootstrap.js legacy and Bonzo.js magic, ported over and
|
|
|
|
// furhter simplified to a subset actually required by Paper.js
|
|
|
|
|
|
|
|
var special = /^(checked|value|selected|disabled)$/i,
|
|
|
|
translated = { text: 'textContent', html: 'innerHTML' },
|
|
|
|
unitless = { lineHeight: 1, zoom: 1, zIndex: 1, opacity: 1 };
|
|
|
|
|
|
|
|
function create(nodes, parent) {
|
|
|
|
var res = [];
|
|
|
|
for (var i = 0, l = nodes && nodes.length; i < l;) {
|
2012-11-14 04:31:49 -05:00
|
|
|
var el = nodes[i++];
|
|
|
|
if (typeof el === 'string') {
|
|
|
|
el = document.createElement(el);
|
|
|
|
} else if (!el || !el.nodeType) {
|
|
|
|
continue;
|
2012-11-14 01:58:03 -05:00
|
|
|
}
|
2012-11-14 04:31:49 -05:00
|
|
|
// Do we have attributes?
|
2012-12-30 11:30:13 -05:00
|
|
|
if (Base.isPlainObject(nodes[i]))
|
2012-11-14 04:31:49 -05:00
|
|
|
DomElement.set(el, nodes[i++]);
|
|
|
|
// Do we have children?
|
|
|
|
if (Array.isArray(nodes[i]))
|
|
|
|
create(nodes[i++], el);
|
|
|
|
// Are we adding to a parent?
|
|
|
|
if (parent)
|
|
|
|
parent.appendChild(el);
|
|
|
|
res.push(el);
|
2011-03-14 17:14:20 -04:00
|
|
|
}
|
2012-11-14 01:58:03 -05:00
|
|
|
return res;
|
2011-07-31 09:13:29 -04:00
|
|
|
}
|
2012-11-14 01:58:03 -05:00
|
|
|
|
|
|
|
return {
|
2012-11-14 13:02:07 -05:00
|
|
|
create: function(nodes, parent) {
|
|
|
|
var isArray = Array.isArray(nodes),
|
|
|
|
res = create(isArray ? nodes : arguments, isArray ? parent : null);
|
2012-11-14 01:58:03 -05:00
|
|
|
return res.length == 1 ? res[0] : res;
|
|
|
|
},
|
|
|
|
|
2012-11-14 04:31:49 -05:00
|
|
|
find: function(selector, root) {
|
|
|
|
return (root || document).querySelector(selector);
|
2012-11-14 01:58:03 -05:00
|
|
|
},
|
|
|
|
|
2012-11-14 04:31:49 -05:00
|
|
|
findAll: function(selector, root) {
|
|
|
|
return (root || document).querySelectorAll(selector);
|
2012-11-14 01:58:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
get: function(el, key) {
|
|
|
|
return el
|
|
|
|
? special.test(key)
|
2012-11-14 04:31:49 -05:00
|
|
|
? key === 'value' || typeof el[key] !== 'string'
|
2012-11-14 01:58:03 -05:00
|
|
|
? el[key]
|
2012-11-14 04:31:49 -05:00
|
|
|
: true
|
2012-11-14 01:58:03 -05:00
|
|
|
: key in translated
|
|
|
|
? el[translated[key]]
|
|
|
|
: el.getAttribute(key)
|
|
|
|
: null;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(el, key, value) {
|
|
|
|
if (typeof key !== 'string') {
|
|
|
|
for (var name in key)
|
|
|
|
if (key.hasOwnProperty(name))
|
|
|
|
this.set(el, name, key[name]);
|
2012-11-15 00:16:33 -05:00
|
|
|
} else if (!el || value === undefined) {
|
|
|
|
return el;
|
2012-11-14 01:58:03 -05:00
|
|
|
} else if (special.test(key)) {
|
|
|
|
el[key] = value;
|
|
|
|
} else if (key in translated) {
|
|
|
|
el[translated[key]] = value;
|
|
|
|
} else if (key === 'style') {
|
|
|
|
this.setStyle(el, value);
|
|
|
|
} else if (key === 'events') {
|
|
|
|
DomEvent.add(el, value);
|
|
|
|
} else {
|
|
|
|
el.setAttribute(key, value);
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2013-02-28 20:15:03 -05:00
|
|
|
getStyles: function(el) {
|
2013-03-01 17:17:31 -05:00
|
|
|
var view = el && el.ownerDocument.defaultView;
|
|
|
|
return view && view.getComputedStyle(el, '');
|
2013-02-28 20:15:03 -05:00
|
|
|
},
|
|
|
|
|
2012-11-14 01:58:03 -05:00
|
|
|
getStyle: function(el, key) {
|
2013-03-01 17:17:31 -05:00
|
|
|
return el && el.style[key] || this.getStyles(el)[key] || null;
|
2012-11-14 01:58:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
setStyle: function(el, key, value) {
|
|
|
|
if (typeof key !== 'string') {
|
|
|
|
for (var name in key)
|
|
|
|
if (key.hasOwnProperty(name))
|
|
|
|
this.setStyle(el, name, key[name]);
|
|
|
|
} else {
|
|
|
|
if (/^-?[\d\.]+$/.test(value) && !(key in unitless))
|
|
|
|
value += 'px';
|
|
|
|
el.style[key] = value;
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
hasClass: function(el, cls) {
|
|
|
|
return new RegExp('\\s*' + cls + '\\s*').test(el.className);
|
|
|
|
},
|
|
|
|
|
|
|
|
addClass: function(el, cls) {
|
|
|
|
el.className = (el.className + ' ' + cls).trim();
|
|
|
|
},
|
|
|
|
|
|
|
|
removeClass: function(el, cls) {
|
|
|
|
el.className = el.className.replace(
|
|
|
|
new RegExp('\\s*' + cls + '\\s*'), ' ').trim();
|
|
|
|
},
|
|
|
|
|
2012-11-14 15:06:53 -05:00
|
|
|
remove: function(el) {
|
|
|
|
if (el.parentNode)
|
|
|
|
el.parentNode.removeChild(el);
|
|
|
|
},
|
|
|
|
|
2012-11-14 04:31:49 -05:00
|
|
|
removeChildren: function(el) {
|
|
|
|
while (el.firstChild)
|
|
|
|
el.removeChild(el.firstChild);
|
|
|
|
},
|
|
|
|
|
2012-11-14 01:58:03 -05:00
|
|
|
getBounds: function(el, viewport) {
|
|
|
|
var rect = el.getBoundingClientRect(),
|
|
|
|
doc = el.ownerDocument,
|
|
|
|
body = doc.body,
|
|
|
|
html = doc.documentElement,
|
|
|
|
x = rect.left - (html.clientLeft || body.clientLeft || 0),
|
|
|
|
y = rect.top - (html.clientTop || body.clientTop || 0);
|
|
|
|
if (!viewport) {
|
|
|
|
var view = doc.defaultView;
|
|
|
|
x += view.pageXOffset || html.scrollLeft || body.scrollLeft;
|
|
|
|
y += view.pageYOffset || html.scrollTop || body.scrollTop;
|
|
|
|
}
|
|
|
|
return new Rectangle(x, y, rect.width, rect.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
getViewportBounds: function(el) {
|
|
|
|
var doc = el.ownerDocument,
|
|
|
|
view = doc.defaultView,
|
|
|
|
html = doc.documentElement;
|
2013-05-28 02:57:31 -04:00
|
|
|
return new Rectangle(0, 0,
|
2012-11-14 01:58:03 -05:00
|
|
|
view.innerWidth || html.clientWidth,
|
|
|
|
view.innerHeight || html.clientHeight
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
getOffset: function(el, viewport) {
|
|
|
|
return this.getBounds(el, viewport).getPoint();
|
|
|
|
},
|
|
|
|
|
|
|
|
getSize: function(el) {
|
|
|
|
return this.getBounds(el, true).getSize();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if element is invisibile (display: none, ...)
|
|
|
|
*/
|
|
|
|
isInvisible: function(el) {
|
|
|
|
return this.getSize(el).equals([0, 0]);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if element is visibile in current viewport
|
|
|
|
*/
|
|
|
|
isInView: function(el) {
|
|
|
|
// See if the viewport bounds intersect with the windows rectangle
|
|
|
|
// which always starts at 0, 0
|
|
|
|
return !this.isInvisible(el) && this.getViewportBounds(el).intersects(
|
|
|
|
this.getBounds(el, true));
|
|
|
|
}
|
|
|
|
};
|
2011-03-14 17:14:20 -04:00
|
|
|
};
|