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
() -> <[o]> Children
() -> <[h]> Children
@*/
children: function() {
return Ox.slice(this[0].childNodes);
@ -149,7 +149,7 @@ Ox.$ = Ox.element = function(value) {
},
/*@
find <f> Find descendant elements
([selector]) -> <a> Elements
([selector]) -> <[h]> Elements
selector <s|'*'> CSS selector
@*/
find: function(string) {
@ -213,18 +213,17 @@ Ox.$ = Ox.element = function(value) {
},
/*@
next <f> Returns the sibling after this element
() -> <o> Next element
() -> <h> Next element
@*/
next: function() {
return this[0].nextSibling;
},
/*@
nextAll <f> Returns all siblings after this element
() -> <[o]> Next elements
() -> <[h]> Next elements
@*/
nextAll: function() {
var sibling = this[0],
siblings = [];
var sibling = this[0], siblings = [];
while (true) {
var sibling = sibling.nextSibling;
if (!sibling) {
@ -277,8 +276,7 @@ Ox.$ = Ox.element = function(value) {
e <o> Event properties
@*/
one: function(events) {
var args = Ox.slice(arguments),
that = this;
var args = Ox.slice(arguments), that = this;
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
that.on(event, function fn() {
that.off(event, fn);
@ -289,18 +287,17 @@ Ox.$ = Ox.element = function(value) {
},
/*@
parent <f> Returns the parent of this element
() -> <o> Parent element
() -> <h> Parent element
@*/
parent: function() {
return this[0].parentNode;
},
/*@
parents <f> Returns all ancestors of this element
() -> <[o]> Ancestor elements
() -> <[h]> Ancestor elements
@*/
parents: function() {
var parent = this[0],
parents = [];
var parent = this[0], parents = [];
while (true) {
var parent = parent.parentNode;
if (!parent) {
@ -334,18 +331,17 @@ Ox.$ = Ox.element = function(value) {
},
/*@
prev <f> Returns the sibling before this element
() -> <o> Next element
() -> <h> Next element
@*/
prev: function() {
return this[0].previousSibling;
},
/*@
prevAll <f> Returns all siblings before this element
() -> <[o]> Next elements
() -> <[h]> Next elements
@*/
prevAll: function() {
var sibling = this[0],
siblings = [];
var sibling = this[0], siblings = [];
while (true) {
var sibling = sibling.previousSibling;
if (!sibling) {
@ -391,13 +387,61 @@ Ox.$ = Ox.element = function(value) {
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
@*/
show: function() {
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
(className) -> <o> This element
className <s> Class name