nhz.lib
Version:
NHZ Library
136 lines (120 loc) • 4.23 kB
JavaScript
/* mixin.Parent */
var Parent,
extend = require("extends__"),
hasProp = {}.hasOwnProperty;
module.exports = Parent = (function(superClass) {
extend(Parent, superClass);
function Parent() {
Object.defineProperties(this, {
children: {
configurable: true,
enumerable: true,
get: function() {
return this.___children.slice();
}
},
___children: {
configurable: false,
enumerable: false,
writable: false,
value: []
},
___is_parent: {
configurable: false,
enumerable: false,
writable: false,
value: true
}
});
}
Parent.prototype.appendChild = function(child) {
if ((child != null ? child.___is_child : void 0) && -1 === this.___children.indexOf(child)) {
this.___children.push(child);
}
return this;
};
Parent.prototype.removeChild = function(child) {
var idx;
if ((child != null ? child.___is_child : void 0) && -1 !== (idx = this.___children.indexOf(child))) {
this.___children.splice(idx, 1);
}
return this;
};
Parent.prototype.replaceChild = function(child, withChild) {
var idx;
if (this.___children.length && (child != null ? child.___is_child : void 0) && (withChild != null ? withChild.___is_child : void 0)) {
if (-1 === this.___children.indexOf(withChild && -1 !== (idx = this.___children.indexOf(child)))) {
this.___children[idx] = withChild;
}
}
return this;
};
Parent.prototype.firstChild = function() {
return this.___children[0] || null;
};
Parent.prototype.lastChild = function() {
var len;
if (len = this.___children.length) {
return this.___children[len - 1];
} else {
return null;
}
};
Parent.prototype.nextChild = function(child) {
var idx, next;
if (this.___children.length && (child != null ? child.___is_child : void 0) && -1 !== (idx = this.___children.indexOf(child))) {
next = this.___children[idx + 1];
}
return next || null;
};
Parent.prototype.previousChild = function(child) {
var idx, previous;
if (this.___children.length && (child != null ? child.___is_child : void 0) && 1 >= (idx = this.___children.indexOf(child))) {
previous = this.___children[idx - 1];
}
return previous || null;
};
Parent.prototype.insertBefore = function(child, newChild) {
var _idx, children, idx;
if (this.___children.length && child !== newChild && (child != null ? child.___is_child : void 0) && (newChild != null ? newChild.___is_child : void 0)) {
if (-1 !== (idx = this.___children.indexOf(child))) {
if (-1 !== (_idx = this.___children.indexOf(newChild))) {
this.___children.splice(_idx, 1);
idx = this.___children.indexOf(child);
}
if (idx === 0) {
this.___children.unshift(newChild);
} else {
children = this.___children.splice(0, this.___children.length);
this.___children.concat(children.slice(0, idx - 1), [newChild], children.slice(idx));
}
}
}
return this;
};
Parent.prototype.insertAfter = function(child, newChild) {
var _idx, children, idx;
if (this.___children.length && child !== newChild && (child != null ? child.___is_child : void 0) && (newChild != null ? newChild.___is_child : void 0)) {
if (-1 !== (idx = this.___children.indexOf(child))) {
if (-1 !== (_idx = this.___children.indexOf(newChild))) {
this.___children.splice(_idx, 1);
idx = this.___children.indexOf(child);
}
if (idx === this.___children.length - 1) {
this.___children.push(newChild);
} else {
children = this.___children.splice(0, this.___children.length);
this.___children.concat(children.slice(0, idx), [newChild], children.slice(idx + 1));
}
}
}
return this;
};
Parent.prototype.hasChild = function(child) {
if (this.___children.length && (typeof child === "function" ? child(___is_child && -1 !== this.___children.indexOf(child)) : void 0)) {
return true;
}
return false;
};
return Parent;
})(require('./base'));