mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
More work on micro DOM library.
This commit is contained in:
parent
c4a66cff0a
commit
9a180b5377
1 changed files with 31 additions and 21 deletions
|
@ -31,11 +31,14 @@ var DomElement = new function() {
|
||||||
function create(nodes, parent) {
|
function create(nodes, parent) {
|
||||||
var res = [];
|
var res = [];
|
||||||
for (var i = 0, l = nodes && nodes.length; i < l;) {
|
for (var i = 0, l = nodes && nodes.length; i < l;) {
|
||||||
var tag = nodes[i++];
|
var el = nodes[i++];
|
||||||
if (typeof tag === 'string') {
|
if (typeof el === 'string') {
|
||||||
var el = document.createElement(tag);
|
el = document.createElement(el);
|
||||||
|
} else if (!el || !el.nodeType) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
// Do we have attributes?
|
// Do we have attributes?
|
||||||
if (typeof nodes[i] === 'object' && !Array.isArray(nodes[i]))
|
if (Base.isObject(nodes[i]))
|
||||||
DomElement.set(el, nodes[i++]);
|
DomElement.set(el, nodes[i++]);
|
||||||
// Do we have children?
|
// Do we have children?
|
||||||
if (Array.isArray(nodes[i]))
|
if (Array.isArray(nodes[i]))
|
||||||
|
@ -45,30 +48,30 @@ var DomElement = new function() {
|
||||||
parent.appendChild(el);
|
parent.appendChild(el);
|
||||||
res.push(el);
|
res.push(el);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
create: function(tag, attributes, children) {
|
create: function(arg0, arg1) {
|
||||||
var res = create(arguments);
|
var isArray = Array.isArray(arg0),
|
||||||
|
res = create(isArray ? arg0 : arguments, isArray ? arg1 : null);
|
||||||
return res.length == 1 ? res[0] : res;
|
return res.length == 1 ? res[0] : res;
|
||||||
},
|
},
|
||||||
|
|
||||||
find: function(selector) {
|
find: function(selector, root) {
|
||||||
return document.querySelector(selector);
|
return (root || document).querySelector(selector);
|
||||||
},
|
},
|
||||||
|
|
||||||
findAll: function(selector) {
|
findAll: function(selector, root) {
|
||||||
return document.querySelectorAll(selector);
|
return (root || document).querySelectorAll(selector);
|
||||||
},
|
},
|
||||||
|
|
||||||
get: function(el, key) {
|
get: function(el, key) {
|
||||||
return el
|
return el
|
||||||
? special.test(key)
|
? special.test(key)
|
||||||
? key === 'value'
|
? key === 'value' || typeof el[key] !== 'string'
|
||||||
? el[key]
|
? el[key]
|
||||||
: typeof el[key] === 'string'
|
: true
|
||||||
: key in translated
|
: key in translated
|
||||||
? el[translated[key]]
|
? el[translated[key]]
|
||||||
: el.getAttribute(key)
|
: el.getAttribute(key)
|
||||||
|
@ -76,6 +79,8 @@ var DomElement = new function() {
|
||||||
},
|
},
|
||||||
|
|
||||||
set: function(el, key, value) {
|
set: function(el, key, value) {
|
||||||
|
if (!el)
|
||||||
|
return el;
|
||||||
if (typeof key !== 'string') {
|
if (typeof key !== 'string') {
|
||||||
for (var name in key)
|
for (var name in key)
|
||||||
if (key.hasOwnProperty(name))
|
if (key.hasOwnProperty(name))
|
||||||
|
@ -125,6 +130,11 @@ var DomElement = new function() {
|
||||||
new RegExp('\\s*' + cls + '\\s*'), ' ').trim();
|
new RegExp('\\s*' + cls + '\\s*'), ' ').trim();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
removeChildren: function(el) {
|
||||||
|
while (el.firstChild)
|
||||||
|
el.removeChild(el.firstChild);
|
||||||
|
},
|
||||||
|
|
||||||
getBounds: function(el, viewport) {
|
getBounds: function(el, viewport) {
|
||||||
var rect = el.getBoundingClientRect(),
|
var rect = el.getBoundingClientRect(),
|
||||||
doc = el.ownerDocument,
|
doc = el.ownerDocument,
|
||||||
|
|
Loading…
Reference in a new issue