ajsfw
Version:
Ajs Framework
149 lines (148 loc) • 4.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var exceptions = require("./exceptions");
var NodeListOf = (function () {
function NodeListOf(parentNode) {
this.__parentNode = parentNode;
this.__firstNode = null;
this.__lastNode = null;
this.__length = 0;
}
Object.defineProperty(NodeListOf.prototype, "firstNode", {
get: function () { return this.__firstNode; },
enumerable: true,
configurable: true
});
Object.defineProperty(NodeListOf.prototype, "lastNode", {
get: function () { return this.__lastNode; },
enumerable: true,
configurable: true
});
Object.defineProperty(NodeListOf.prototype, "length", {
get: function () { return this.__length; },
enumerable: true,
configurable: true
});
NodeListOf.prototype.item = function (index) {
if (this.__firstNode === null) {
return null;
}
var i = 0;
var node = this.__firstNode;
while (i !== index && node.nextSibling !== null) {
node = node.nextSibling;
i++;
}
if (node !== null && i === index) {
return node;
}
else {
return null;
}
};
NodeListOf.prototype.itemIndex = function (node) {
var i = 0;
var n = this.__firstNode;
while (n !== node && n.nextSibling !== null) {
n = n.nextSibling;
i++;
}
if (n !== null && n === node) {
return i;
}
else {
return -1;
}
};
NodeListOf.prototype.append = function (node) {
if (this.__firstNode === null) {
this.__firstNode = node;
}
if (this.__lastNode !== null) {
this.__lastNode.nextSibling = node;
node.previousSibling = this.__lastNode;
}
this.__lastNode = node;
this.__length++;
};
NodeListOf.prototype.insertBefore = function (newNode, beforeNode) {
if (beforeNode.parentNode !== this.__parentNode) {
throw new exceptions.BeforeNodeIsNotNodeChildrenException();
}
if (beforeNode.previousSibling !== null) {
beforeNode.previousSibling.nextSibling = newNode;
newNode.previousSibling = beforeNode.previousSibling;
}
else {
newNode.previousSibling = null;
}
newNode.nextSibling = beforeNode;
beforeNode.previousSibling = newNode;
if (this.__firstNode === beforeNode) {
this.__firstNode = newNode;
}
this.__length++;
};
NodeListOf.prototype.remove = function (index) {
var item = this.item(index);
if (item !== null) {
this.removeNode(item);
}
};
NodeListOf.prototype.removeNode = function (node) {
if (node.parentNode !== this.__parentNode) {
throw new exceptions.NodeIsNotChildrenNodeException();
}
if (this.__lastNode === node) {
if (node.previousSibling) {
this.__lastNode = node.previousSibling;
}
else {
this.__lastNode = null;
}
}
if (this.__firstNode === node) {
if (node.nextSibling) {
this.__firstNode = node.nextSibling;
}
else {
this.__firstNode = null;
}
}
if (node.previousSibling && node.nextSibling) {
node.previousSibling.nextSibling = node.nextSibling;
node.nextSibling.previousSibling = node.previousSibling;
this.__length--;
return;
}
if (node.previousSibling) {
node.previousSibling.nextSibling = null;
}
if (node.nextSibling) {
node.nextSibling.previousSibling = null;
}
this.__length--;
};
NodeListOf.prototype.replaceNode = function (newNode, oldNode) {
if (oldNode.parentNode !== this.__parentNode) {
throw new exceptions.BeforeNodeIsNotNodeChildrenException();
}
newNode.__parentNode = this.__parentNode;
if (oldNode.previousSibling) {
newNode.previousSibling = oldNode.previousSibling;
newNode.previousSibling.nextSibling = newNode;
}
if (oldNode.nextSibling) {
newNode.nextSibling = oldNode.nextSibling;
newNode.nextSibling.previousSibling = newNode;
}
if (this.__firstNode === oldNode) {
this.__firstNode = newNode;
}
if (this.__lastNode === oldNode) {
this.__lastNode = newNode;
}
};
return NodeListOf;
}());
exports.default = NodeListOf;