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