Skip to content

Commit

Permalink
Initial split-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian committed Sep 25, 2011
1 parent 998785a commit 0862df5
Show file tree
Hide file tree
Showing 11 changed files with 495 additions and 464 deletions.
33 changes: 33 additions & 0 deletions Source/Document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Document Wrapper
*/

define(['./Node', './select'], function(Node){

var Document = Class({

Extends: Node,

Matches: function(node){
return node.nodeType == 9;
},

createElement: function(tag){
return select(this.node.createElement(tag));
},

createTextNode: function(text){
return this.node.createTextNode(text);
},

build: function(){}

});

Document.prototype.toString = function(){
return '<document>';
};

return Document;

});
53 changes: 53 additions & 0 deletions Source/Element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

define(['./Node', './select', 'Base/Core/Class', 'Slick/Finder'], function(Node, select, Class, Slick){

var Element = new Class({
Extends: Node,
Matches: '*'
});

Element.prototype.toString = function(){
var node = this.node,
tag = node.tagName.toLowerCase(),
id = node.id,
className = node.className;
var str = '<' + tag;
if (id) str += '#' + id;
if (className) str += '.' + className.replace(/\s+/g, '.');
return str + '>';
};

Element.implement({

appendChild: function(child){
if ((child = select(child))) this.node.appendChild(child.valueOf());
return this;
},

setAttribute: function(name, value){
this.node.setAttribute(name, value);
return this;
},

getAttribute: function(name){
return this.node.getAttribute(name);
},

removeAttribute: function(name){
this.node.removeAttribute(name);
return this;
},

contains: function(node){
return ((node = select(node))) ? Slick.contains(this.node, node.valueOf()) : false;
},

match: function(expression){
return Slick.match(this.node, expression);
}

});

return Element;

});
121 changes: 121 additions & 0 deletions Source/Element/Attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

define(['./Element', 'Base/Host/Array', 'Base/Utility/Object'], function(Element, Array, Object){

Element.addAccessor('Getter').addAccessor('Setter');

var properties = {};

Array.forEach([
'checked', 'defaultChecked', 'type', 'value', 'accessKey', 'cellPadding', 'cellSpacing', 'colSpan',
'frameBorder', 'maxLength', 'readOnly', 'rowSpan', 'tabIndex', 'useMap',
// Attributes
'id', 'attributes', 'childNodes', 'className', 'clientHeight', 'clientLeft', 'clientTop', 'clientWidth', 'dir', 'firstChild',
'lang', 'lastChild', 'name', 'nextSibling', 'nodeName', 'nodeType', 'nodeValue',
'offsetHeight', 'offsetLeft', 'offsetParent', 'offsetTop', 'offsetWidth',
'ownerDocument', 'parentNode', 'prefix', 'previousSibling', 'scrollHeight', 'scrollWidth', 'tabIndex', 'tagName',
'textContent', 'innerHTML', 'title'
], function(property){
properties[property] = property;
});

Object.append(properties, {
'html': 'innerHTML',
'class': 'className',
'for': 'htmlFor',
'text': (function(){
var temp = document.createElement('div');
return (temp.innerText == null) ? 'textContent' : 'innerText';
})()
});

Object.forEach(properties, function(real, key){
Element.defineSetter(key, function(value){
return this.node[real] = value;
}).defineGetter(key, function(){
return this.node[real];
});
});

var booleans = ['compact', 'nowrap', 'ismap', 'declare', 'noshade', 'checked', 'disabled', 'multiple', 'readonly', 'selected', 'noresize', 'defer'];

Array.forEach(booleans, function(bool){
Element.defineSetter(bool, function(value){
return this.node[bool] = !!value;
}).defineGetter(bool, function(){
return !!this.node[bool];
});
});

Element.defineGetters({

'class': function(){
var node = this.node;
return ('className' in node) ? node.className : node.getAttribute('class');
},

'for': function(){
var node = this.node;
return ('htmlFor' in node) ? node.htmlFor : node.getAttribute('for');
},

'href': function(){
var node = this.node;
return ('href' in node) ? node.getAttribute('href', 2) : node.getAttribute('href');
},

'style': function(){
var node = this.node;
return (node.style) ? node.style.cssText : node.getAttribute('style');
}

}).defineSetters({

'class': function(value){
var node = this.node;
return ('className' in node) ? node.className = value : node.setAttribute('class', value);
},

'for': function(value){
var node = this.node;
return ('htmlFor' in node) ? node.htmlFor = value : node.setAttribute('for', value);
},

'style': function(value){
var node = this.node;
return (node.style) ? node.style.cssText = value : node.setAttribute('style', value);
}

});

/* get, set */

Element.implement({

set: function(name, value){
if (typeof name != 'string') for (var k in name) this.set(k, name[k]); else {
var setter = Element.lookupSetter(name = String.camelCase(name));
if (setter) setter.call(this, value);
else if (value == null) this.node.removeAttribute(name);
else this.node.setAttribute(name, value);
}
return this;
},

get: function(name){
if (arguments.length > 1) return Array.map(arguments, function(v, i){
return this.get(v);
}, this);
var getter = Element.lookupGetter(name = String.camelCase(name));
if (getter) return getter.call(this);
return this.node.getAttribute(name);
}

});

Element.defineGetter('tag', function(){
return this.node.tagName.toLowerCase();
});

return Element;

});
29 changes: 29 additions & 0 deletions Source/Element/Class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

define(['./Element', 'Base/Utility/RegExp', 'Base/Utility/String'], function(Element, RegExp_, String){

var classRegExps = {};
var classRegExpOf = function(string){
return classRegExps[string] || (classRegExps[string] = new RegExp('(^|\\s)' + RegExp_.escape(string) + '(?:\\s|$)'));
};

return Element.implement({

hasClass: function(className){
return classRegExpOf(className).test(this.node.className);
},

addClass: function(className){
var node = this.node;
if (!this.hasClass(className)) node.className = String.clean(node.className + ' ' + className);
return this;
},

removeClass: function(className){
var node = this.node;
node.className = String.clean(node.className.replace(classRegExpOf(className), '$1'));
return this;
}

});

});
73 changes: 73 additions & 0 deletions Source/Element/Injectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Add inject/eject and other methods to move elements.
*/

define(['./select', './Element', 'Base/Host/Array'], function(select, Element, Array){

var inserters = {

before: function(context, element){
var parent = element.parentNode;
if (parent) parent.insertBefore(context, element);
},

after: function(context, element){
var parent = element.parentNode;
if (parent) parent.insertBefore(context, element.nextSibling);
},

bottom: function(context, element){
element.appendChild(context);
},

top: function(context, element){
element.insertBefore(context, element.firstChild);
}

};

return Element.implement({

inject: function(element, where){
if ((element = select(element))) inserters[where || 'bottom'](this.node, element.valueOf());
return this;
},

eject: function(){
var parent = this.node.parentNode;
if (parent) parent.removeChild(this.node);
return this;
},

adopt: function(){
Array.forEach(arguments, function(element){
if ((element = select(element))) this.node.appendChild(element.valueOf());
}, this);
return this;
},

appendText: function(text, where){
inserters[where || 'bottom'](document.createTextNode(text), this.node);
return this;
},

grab: function(element, where){
if ((element = select(element))) inserters[where || 'bottom'](element.valueOf(), this.node);
return this;
},

replace: function(element){
if ((element = select(element))){
element = element.valueOf();
element.parentNode.replaceChild(this.node, element);
}
return this;
},

wrap: function(element, where){
return this.replace(element).grab(element, where);
}

});

});
50 changes: 50 additions & 0 deletions Source/Element/Traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

define([
'./Document', './Window', './Element', 'Base/Utility/Array', 'Base/Utility/Object'
], function(Window, Document, Element, Array, Object){


Array.invoke([Element, Document, Window], 'implement', {

find: function(expression){
return select(Slick.find(this.node, expression));
},

search: function(expression){
var elements = new Elements, nodes = Slick.search(this.node, expression);
for (var i = 0; i < nodes.length; i++) elements[elements.length++] = select(nodes[i]);
return elements;
}

});

/* Tree Walking */

methods = {
find: {
getNext: '~',
getPrevious: '!~',
getFirst: '^',
getLast: '!^',
getParent: '!'
},
search: {
getAllNext: '~',
getAllPrevious: '!~',
getSiblings: '~~',
getChildren: '>',
getParents: '!'
}
};

Object.forEach(methods, function(getters, method){
Element.implement(Object.map(getters, function(combinator){
return function(expression){
return this[method](combinator + (expression || '*'));
};
}));
});

return Element;

});
Loading

0 comments on commit 0862df5

Please sign in to comment.