zpobjects.js
Summary
No overview generated for 'zpobjects.js'
Zapatec.Array = function(arr) {
if (!Zapatec.isArray(arr)) {
var array = [];
for(var i = 0; i < arguments.length; ++i) {
array.push(arguments[i]);
}
arr = array;
}
arr.clear = function() {
Zapatec.Array.clear(this);
};
arr.compact = function() {
var compact = Zapatec.Array.compact(this);
return Zapatec.Array(compact);
};
arr.indexOf = function(value) {
return Zapatec.Array.indexOf(this, value);
};
arr.without = function() {
var args = [].slice.call(arguments, 0);
args.unshift(this);
var without = Zapatec.Array.without.apply(Zapatec.Array, args);
return Zapatec.Array(without);
};
arr.remove = function() {
var args = [].slice.call(arguments, 0);
args.unshift(this);
var cut = Zapatec.Array.remove.apply(Zapatec.Array, args);
return Zapatec.Array(cut);
};
arr.each = function(func, reverse) {
var result;
for(var index = reverse ? this.length - 1 : 0;
reverse ? (index >= 0) : (index < this.length);
reverse ? --index : ++index) {
if (typeof func == "function") {
result = func(index, this[index], this);
if (result == "break") {
break;
}
}
}
if (result == "break") {
return false;
}
return true;
};
arr.isZpArray = true;
return arr;
};
Zapatec.Array.compact = function(arr) {
var newArr = [];
for(var item = 0; item < arr.length; ++item) {
if (arr[item] !== null && typeof arr[item] != "undefined") {
newArr.push(arr[item]);
}
}
return newArr;
};
Zapatec.Array.clear = function(arr) {
arr.length = 0;
};
Zapatec.Array.indexOf = function(arr, value) {
for(var item = 0; item < arr.length; ++item) {
if (arr[item] === value) {
return item;
}
}
return -1;
};
Zapatec.Array.without = function(arr) {
var newArr = [], without;
for(var item = 0; item < arr.length; ++item) {
without = false;
for(var value = 1; value < arguments.length; ++value) {
if (arr[item] === arguments[value]) {
without = true;
break;
}
}
if (!without) {
newArr.push(arr[item]);
}
}
return newArr;
};
Zapatec.Array.remove = function(arr) {
var newArr = [], without, value, start = 1;
if (arguments[1] && arguments[1].length && typeof arguments[1] == "object") {
args = arguments[1];
start = 0;
} else {
args = arguments;
}
for(var item = 0; item < arr.length; ++item) {
without = false;
for(value = start; value < args.length; ++value) {
if (item === args[value]) {
without = true;
break;
}
}
if (!without) {
newArr.push(arr[item]);
}
}
return newArr;
};
Zapatec.Hash = function(hash) {
if (!hash || typeof hash != "object") {
hash = {};
}
hash.hashRemove = function() {
var args = [].slice.call(arguments, 0);
args.unshift(this);
var without = Zapatec.Hash.remove.apply(Zapatec.Hash, args);
return Zapatec.Hash(without);
};
hash.hashEach = function(func) {
var result = null;
for(var prop in this) {
if (prop == "hashRemove" || prop == "hashEach"
|| prop == "hashIsEmpty" || prop == "isZpHash") {
continue;
}
if (typeof Object.prototype[prop] != "undefined") {
continue;
}
result = func(prop, this[prop], this);
if (result == "break") {
break;
}
}
if (result == "break") {
return false;
}
return true;
};
hash.hashIsEmpty = function() {
var empty = true;
this.hashEach(function() {
empty = false;
return "break";
});
return empty;
};
hash.isZpHash = true;
return hash;
};
Zapatec.Hash.remove = function(hash) {
var newHash = {}, without, value, start = 1;
if (arguments[1] && arguments[1].length && typeof arguments[1] == "object") {
args = arguments[1];
start = 0;
} else {
args = arguments;
}
for(var item in hash) {
without = false;
for(value = start; value < args.length; ++value) {
if (item === args[value]) {
without = true;
break;
}
}
if (!without) {
newHash[item] = hash[item];
}
}
return newHash;
};
Zapatec.Hash.getByPath = function(hash, path) {
if (!path || typeof path != "string") {
Zapatec.Log({description : "Not a path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
var paths = path.split(".");
if (!paths.length) {
Zapatec.Log({description : "Wrong path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
var item = 0;
var value = hash;
var name = "";
var scope = null;
while(paths[item]) {
if (value === null || typeof value == "undefined") {
Zapatec.Log({description : "Incorrect path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
name = paths[item].replace(/(\(\)|\[[^\[\]]+\])+/, "");
try {
scope = value;
value = value[name];
} catch (e) {
Zapatec.Log({description : "Incorrect path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
paths[item] = paths[item].replace(name, "");
while (paths[item] != "") {
name = paths[item].match(/(\(\)|\[[^\[\]]+\])/)[1];
if (name && /\(\)$/.test(name)) {
try {
value = value.call(scope || value);
scope = null;
} catch (e) {
Zapatec.Log({description : "Incorrect path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
} else if (name && /\[["']?[^\[\]"']+["']?\]$/.test(name)) {
try {
value = value[name.match(/\[["']?([^\[\]"']+)["']?\]/)[1]];
scope = null;
} catch (e) {
Zapatec.Log({description : "Incorrect path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
}
paths[item] = paths[item].replace(name, "");
}
++item;
}
if (typeof value == "undefined") {
Zapatec.Log({description : "Incorrect path passed to Zapatec.Hash.getByPath function!", type : "warning"});
return {
result : false
};
}
return {
result : true,
value : value
};
};
Zapatec.Hash.setByPath = function(hash, path, val) {
if (!path || typeof path != "string") {
Zapatec.Log({description : "Not a path passed to Zapatec.Hash.setByPath function!", type : "warning"});
return false;
}
var paths = path.split(".");
if (!paths.length) {
Zapatec.Log({description : "Wrong path passed to Zapatec.Hash.setByPath function!", type : "warning"});
return false;
}
var lastItem = paths[paths.length - 1];
var obj = hash;
var getPath = paths.slice(0, -1).join(".");
var arrIndexReg = /\[[^\[\]]+\]$/;
if (arrIndexReg.test(lastItem)) {
getPath += (getPath == "" ? "" : ".") + lastItem.replace(arrIndexReg, "");
lastItem = lastItem.match(/\[["']?([^\[\]"']+)["']?\]$/)[1];
}
if (getPath != "") {
var obj = Zapatec.Hash.getByPath(hash, getPath).value;
}
try {
obj[lastItem] = val;
} catch (e) {
Zapatec.Log({description : "Incorrect path passed to Zapatec.Hash.setByPath function!", type : "warning"});
return false;
}
return true;
};
Zapatec.isHtmlElement = function(el) {
if (!el || el.nodeType != 1) {
return false;
}
return true;
};
Zapatec.isSizableObj = function(obj) {
if (obj && obj.hasInterface && obj.hasInterface("Zapatec.Sizable")) {
return true;
}
return false;
};
Zapatec.isMovableObj = function(obj) {
if (obj && obj.hasInterface && obj.hasInterface("Zapatec.Movable")) {
return true;
}
return false;
};
Zapatec.isArray = function(arr) {
if (arr && typeof arr == "object" && arr.constructor == Array) {
return true;
}
return false;
};
Zapatec.isDate = function(date) {
if (date && typeof date == "object" && date.constructor == Date) {
return true;
}
return false;
};
Zapatec.SRProp = function(obj) {
this.obj = obj;
this.savedProps = new Zapatec.Hash();
Zapatec.Utils.createProperty(obj, "restorer", this);
}
Zapatec.SRProp.prototype.getSavedProps = function() {
return this.savedProps;
};
Zapatec.SRProp.prototype.getObject = function() {
return this.obj;
};
Zapatec.SRProp.prototype.saveProp = function (propName) {
if (typeof propName != "string") {
return false;
}
var value = Zapatec.Hash.getByPath(this.getObject(), propName);
if (value.result) {
if (typeof this.getProp(propName) != "undefined") {
var prop = this.getSavedProps()[propName] = Zapatec.Array(this.getSavedProps()[propName]);
prop.push(value.value);
prop.combination = true;
Zapatec.Log({description : "The property '" + propName + "' now contains more than one value!", type : "warning"});
} else {
this.getSavedProps()[propName] = value.value;
}
return true;
} else {
return false;
}
}
Zapatec.SRProp.prototype.saveProps = function () {
var result = [];
for(var i = 0; i < arguments.length; ++i) {
if (this.saveProp(arguments[i])) {
result.push(arguments[i]);
}
}
return result;
}
Zapatec.SRProp.prototype.restoreProp = function (propName) {
if (typeof propName != "string" || typeof this.getSavedProps()[propName] == "undefined") {
return false;
}
var prop = this.getSavedProps()[propName];
var combination = false, nextSibling = null;
if (Zapatec.isArray(prop) && prop.combination) {
prop = prop[prop.length - 1];
combination = true;
}
if (propName.match(/parentNode$/) !== null && prop && typeof prop == "object" && prop.appendChild) {
nextSibling = this.getSavedProps()[propName.replace(/parentNode/, "nextSibling")] || null;
if (nextSibling && nextSibling.parentNode == prop) {
prop.insertBefore(this.getObject(), nextSibling);
} else {
prop.appendChild(this.getObject());
}
this.savedProps = this.getSavedProps().hashRemove(propName.replace(/parentNode/, "nextSibling"));
} else {
if (!Zapatec.Hash.setByPath(this.getObject(), propName, prop)) {
return false;
}
}
if (!combination) {
this.savedProps = this.getSavedProps().hashRemove(propName);
} else {
prop = this.getSavedProps()[propName];
this.getSavedProps()[propName] = Zapatec.Array.without(prop, prop.length - 1);
}
return true;
}
Zapatec.SRProp.prototype.restoreProps = function (propName) {
var result = [];
for(var i = 0; i < arguments.length; ++i) {
if (this.restoreProp(arguments[i])) {
result.push(arguments[i]);
}
}
return result;
}
Zapatec.SRProp.prototype.restoreAll = function() {
var self = this;
this.getSavedProps().hashEach(function(i) {
self.restoreProp(i);
});
}
Zapatec.SRProp.prototype.getProp = function(propName) {
return this.getSavedProps()[propName];
}
Zapatec.SRProp.prototype.isEmpty = function() {
return this.getSavedProps().hashIsEmpty();
};
Zapatec.SRProp.prototype.destroy = function() {
this.getObject().restorer = null;
for(var iProp in this) {
this[iProp] = null;
}
return null;
};
Zapatec.CommandEvent = {};
Zapatec.CommandEvent.fireEvent = function(strEvent) {
if (!this.events[strEvent]) {
return;
}
var arrListeners = this.events[strEvent].listeners.slice();
this._setReturnedValue(null);
this._setEventPropagation(true);
for (var iListener = 0; iListener < arrListeners.length; iListener++) {
var arrArgs = [].slice.call(arguments, 1);
arrListeners[iListener].apply(this, arrArgs);
var result = this._getReturnedValue();
if (!this._getEventPropagation()) {
return result;
}
if (result == "re-execute") {
this.fireEvent(strEvent);
break;
} else if (result == "parent-re-execute") {
return result;
}
}
return this._getReturnedValue();
};
Zapatec.CommandEvent.returnValue = function(val) {
this._setReturnedValue(val);
};
Zapatec.CommandEvent._setReturnedValue = function(val) {
this.returnedValue = val;
};
Zapatec.CommandEvent._getReturnedValue = function() {
return this.returnedValue;
};
Zapatec.CommandEvent.stopPropagation = function() {
this._setEventPropagation(false);
};
Zapatec.CommandEvent._setEventPropagation = function(on) {
this.eventPropagation = on;
};
Zapatec.CommandEvent._getEventPropagation = function() {
return this.eventPropagation;
};
Zapatec.GlobalEvents = new Zapatec.EventDriven();
Zapatec.implement(Zapatec.GlobalEvents, "Zapatec.CommandEvent");
Zapatec.GlobalEvents.init();
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007