Ox.$: fix data and parent, add toggle

This commit is contained in:
rolux 2014-09-22 15:55:29 +02:00
parent 1899f470ee
commit c106fa845e

View file

@ -33,8 +33,7 @@ Ox.$ <f> Generic HTML element, mimics jQuery
@*/
Ox.$ = Ox.element = function $(value) {
var elementData = {},
elements = Ox.isArray(value) ? value // array of elements
var elements = Ox.isArray(value) ? value // array of elements
: Ox.isNodeList(value) ? Ox.slice(value) // nodelist
: !Ox.isString(value) ? [value] // window, document or element
: value[0] == '<' ? [document.createElement(value.slice(1, -1))]
@ -217,13 +216,15 @@ Ox.$ = Ox.element = function $(value) {
value <*> Value
@*/
data: function data() {
if (arguments.length == 0) {
return elementData;
} else if (arguments.length == 1 && Ox.isString(arguments[0])) {
return elementData[arguments[0]];
var args;
if (arguments.length == 1 && Ox.isString(arguments[0])) {
return element.getAttribute('data-' + arguments[0]);
} else {
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
elementData[key] = value;
args = Ox.makeObject(arguments);
elements.forEach(function(element) {
Ox.forEach(args, function(value, key) {
element.setAttribute('data-' + key, value);
});
});
return this;
}
@ -509,7 +510,7 @@ Ox.$ = Ox.element = function $(value) {
var parent = element;
while (true) {
parent = parent.parentNode;
if (!parent) {
if (!parent || parent == document) {
break;
}
parents.unshift(parent);
@ -736,6 +737,16 @@ Ox.$ = Ox.element = function $(value) {
}
},
/*@
toggle <f> Toggle visibility of all elements
() -> This DOM object
@*/
toggle: function toggle() {
return this[
Ox.$(element).css('display') == 'none' ? 'show' : 'hide'
]();
},
/*@
toggleClass <f> Toggles a class name for all elements
(className) -> <o> This DOM object