Ox.$: add missing methods (replace, replaceWith, siblings, text), + cosmetic changes

This commit is contained in:
rolux 2013-12-06 10:26:31 +01:00
parent de660af394
commit 7516cf8c00

View file

@ -115,7 +115,7 @@ Ox.$ = Ox.element = function(value) {
}, },
/*@ /*@
children <f> Returns the children of this element children <f> Returns the children of this element
() -> <[o]> Children () -> <[h]> Children
@*/ @*/
children: function() { children: function() {
return Ox.slice(this[0].childNodes); return Ox.slice(this[0].childNodes);
@ -149,7 +149,7 @@ Ox.$ = Ox.element = function(value) {
}, },
/*@ /*@
find <f> Find descendant elements find <f> Find descendant elements
([selector]) -> <a> Elements ([selector]) -> <[h]> Elements
selector <s|'*'> CSS selector selector <s|'*'> CSS selector
@*/ @*/
find: function(string) { find: function(string) {
@ -213,18 +213,17 @@ Ox.$ = Ox.element = function(value) {
}, },
/*@ /*@
next <f> Returns the sibling after this element next <f> Returns the sibling after this element
() -> <o> Next element () -> <h> Next element
@*/ @*/
next: function() { next: function() {
return this[0].nextSibling; return this[0].nextSibling;
}, },
/*@ /*@
nextAll <f> Returns all siblings after this element nextAll <f> Returns all siblings after this element
() -> <[o]> Next elements () -> <[h]> Next elements
@*/ @*/
nextAll: function() { nextAll: function() {
var sibling = this[0], var sibling = this[0], siblings = [];
siblings = [];
while (true) { while (true) {
var sibling = sibling.nextSibling; var sibling = sibling.nextSibling;
if (!sibling) { if (!sibling) {
@ -277,8 +276,7 @@ Ox.$ = Ox.element = function(value) {
e <o> Event properties e <o> Event properties
@*/ @*/
one: function(events) { one: function(events) {
var args = Ox.slice(arguments), var args = Ox.slice(arguments), that = this;
that = this;
Ox.forEach(normalizeEvents(arguments), function(callback, event) { Ox.forEach(normalizeEvents(arguments), function(callback, event) {
that.on(event, function fn() { that.on(event, function fn() {
that.off(event, fn); that.off(event, fn);
@ -289,18 +287,17 @@ Ox.$ = Ox.element = function(value) {
}, },
/*@ /*@
parent <f> Returns the parent of this element parent <f> Returns the parent of this element
() -> <o> Parent element () -> <h> Parent element
@*/ @*/
parent: function() { parent: function() {
return this[0].parentNode; return this[0].parentNode;
}, },
/*@ /*@
parents <f> Returns all ancestors of this element parents <f> Returns all ancestors of this element
() -> <[o]> Ancestor elements () -> <[h]> Ancestor elements
@*/ @*/
parents: function() { parents: function() {
var parent = this[0], var parent = this[0], parents = [];
parents = [];
while (true) { while (true) {
var parent = parent.parentNode; var parent = parent.parentNode;
if (!parent) { if (!parent) {
@ -334,18 +331,17 @@ Ox.$ = Ox.element = function(value) {
}, },
/*@ /*@
prev <f> Returns the sibling before this element prev <f> Returns the sibling before this element
() -> <o> Next element () -> <h> Next element
@*/ @*/
prev: function() { prev: function() {
return this[0].previousSibling; return this[0].previousSibling;
}, },
/*@ /*@
prevAll <f> Returns all siblings before this element prevAll <f> Returns all siblings before this element
() -> <[o]> Next elements () -> <[h]> Next elements
@*/ @*/
prevAll: function() { prevAll: function() {
var sibling = this[0], var sibling = this[0], siblings = [];
siblings = [];
while (true) { while (true) {
var sibling = sibling.previousSibling; var sibling = sibling.previousSibling;
if (!sibling) { if (!sibling) {
@ -391,13 +387,61 @@ Ox.$ = Ox.element = function(value) {
return this; return this;
}, },
/*@ /*@
show <f> Show this element replace <f> Replaces another element with this element
(element) -> <o> This element
element <o> Another element
@*/
replace: function($element) {
var next = $element[0].nextSibling, parent = $element[0].parentNode;
$element.remove();
parent.insertBefore(this[0], next);
return this;
},
/*@
replaceWith <f> Replaces this element with another element
(element) -> <o> This element
element <o> Another element
@*/
replaceWith: function($element) {
var next = this[0].nextSibling, parent = this[0].parentNode;
this.remove();
parent.insertBefore($element[0], next);
return this;
},
/*@
show <f> Shows this element
() -> This element () -> This element
@*/ @*/
show: function() { show: function() {
return this.css({display: 'block'}); return this.css({display: 'block'});
}, },
/*@ /*@
siblings <f> Returns all siblings of this element
() -> <[oh]> Sibling elements
@*/
siblings: function() {
var that = this;
return Ox.filter(this[0].parentNode.childNodes, function(element) {
return element !== that[0];
});
},
/*@
text <f> Gets or sets the text contents
() -> <s> The text contents
(text) -> <o> This element
text <s> The text contents
@*/
text: function() {
var ret;
if (arguments.length == 0) {
ret = Ox.isString(this.textContent)
? this.textContent : this.innerText;
} else {
ret = this.empty().append(this[0].createTextNode(string));
}
return ret;
},
/*@
toggleClass <f> Toggles a class name toggleClass <f> Toggles a class name
(className) -> <o> This element (className) -> <o> This element
className <s> Class name className <s> Class name