sizable.js
Summary
No overview generated for 'sizable.js'
Zapatec.Sizable = {};
Zapatec.Sizable.setWidth = function(width) {
return this._setDimension(width, "width");
};
Zapatec.Sizable.setHeight = function(height) {
return this._setDimension(height, "height");
};
Zapatec.Sizable.setOrientedWidth = function(width) {
if (!this.getOrientation) {
return false;
}
switch (this.getOrientation()) {
case "vertical" :
return this._setDimension(width, "height");
case "horizontal" :
return this._setDimension(width, "width");
}
};
Zapatec.Sizable.setOrientedHeight = function(height) {
if (!this.getOrientation) {
return false;
}
switch (this.getOrientation()) {
case "vertical" :
return this._setDimension(height, "width");
case "horizontal" :
return this._setDimension(height, "height");
}
};
Zapatec.Sizable._setDimension = function(val, dimension) {
var evDim = dimension.charAt(0).toUpperCase() + dimension.slice(1);
if (!this.isSizableSafely(dimension)) {
Zapatec.Log({description : "The object " + dimension + " ID '" + this.id + "' was not prepared for sizing! Use obj.makeSizable() to do so!", type : "warning"});
return false;
}
var msgValue = val + "";
val = this._parseSize(val, dimension);
if (!val) {
Zapatec.Log({description : "The " + dimension + " " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
var elements = Zapatec.Array(this.getSizableElements(dimension));
var toRestore = [], self = this;
if (this.fireEvent("before" + evDim + "Change", val) === false) {
return false;
}
if (Zapatec.GlobalEvents.fireEvent("before" + evDim + "Change", val, this) === false) {
return false;
}
if (!this._proceedElementsSizes(val, dimension, elements, toRestore)) {
this._rollBackSizing(toRestore, dimension);
this.fireEvent(dimension + "ChangeFailure", val)
Zapatec.GlobalEvents.fireEvent(dimension + "ChangeFailure", val, this)
Zapatec.Log({description : "Impossible to set the " + dimension + " " + msgValue + " for the object with ID '" + this.id + "'!", type : "warning"});
return false;
} else {
if (this.isSizing()) {
if (dimension == "width") {
this.fireEvent("onSizing", val, this.getHeight());
Zapatec.GlobalEvents.fireEvent("onSizing", val, this.getHeight(), this);
} else {
this.fireEvent("onSizing", this.getWidth(), val);
Zapatec.GlobalEvents.fireEvent("onSizing", this.getWidth(), val, this);
}
}
this.fireEvent("on" + evDim + "Change", val);
Zapatec.GlobalEvents.fireEvent("on" + evDim + "Change", val, this);
}
return true;
};
Zapatec.Sizable.startSizing = function() {
if (!this.isSizableSafely()) {
Zapatec.Log({description : "The object with ID '" + this.id + "' was not prepared for sizing! Use obj.makeSizable() to do so!", type : "warning"});
return false;
}
this.fireEvent("sizingStart");
Zapatec.GlobalEvents.fireEvent("sizingStart", this);
this._setSizingState(true);
return true;
};
Zapatec.Sizable.endSizing = function() {
if (!this.isSizing()) {
Zapatec.Log({description : "The sizing for object with ID '" + this.id + "' was not started!", type : "warning"});
return false;
}
this.fireEvent("sizingEnd");
Zapatec.GlobalEvents.fireEvent("sizingEnd", this);
this._setSizingState(false);
return true;
};
Zapatec.Sizable.isSizing = function() {
return this.sizingState;
};
Zapatec.Sizable._setSizingState = function(on) {
this.sizingState = on;
};
Zapatec.Sizable.getWidth = function() {
var el = this.getSizableMeasurement("width");
if (Zapatec.isHtmlElement(el) || typeof el == "number") {
return Zapatec.Utils.getWidth(el) || el;
}
Zapatec.Log({description : "Can't calculate width for object with ID '" + this.id + "'!", type : "warning"});
return false;
};
Zapatec.Sizable.getHeight = function() {
var el = this.getSizableMeasurement("height");
if (Zapatec.isHtmlElement(el) || typeof el == "number") {
return Zapatec.Utils.getHeight(el) || el;
}
Zapatec.Log({description : "Can't calculate height for object with ID '" + this.id + "'!", type : "warning"});
return false;
};
Zapatec.Sizable.isSizableSafely = function(dimension) {
return this.safelySizable;
};
Zapatec.Sizable.makeSizable = function() {
if (!this.hasInterface("Zapatec.CommandEvent")) {
Zapatec.Log({description : "The object with ID '" + this.id + "' has no Zapatec.CommandEvent interface!"});
return false;
}
if (this.isSizableSafely()) {
return true;
}
var elements = Zapatec.Array(this.getSizableElements()),
success = true, self = this;
elements.each(function(index, sizable) {
sizable = Zapatec.Array(sizable);
sizable.each(function(index, sizable) {
if (Zapatec.isHtmlElement(sizable)) {
if (!Zapatec.Utils.makeSafelySizable(sizable)) {
success = false;
return "break";
}
self.createProperty(sizable, "sizingObj", self);
} else if (Zapatec.isSizableObj(sizable)) {
if (!sizable.makeSizable()) {
success = false;
return "break";
}
}
});
});
if (!success) {
this.restoreOfSizing();
Zapatec.Log({description : "Can not make the object with ID '" + this.id + "' sizable!"});
return false;
}
this._setSizableSafely(true);
return true;
};
Zapatec.Sizable.restoreOfSizing = function() {
if (!this.isSizableSafely()) {
return true;
}
var elements = Zapatec.Array(this.getSizableElements());
elements.each(function(index, sizable) {
sizable = Zapatec.Array(sizable);
sizable.each(function(index, sizable) {
if (Zapatec.isHtmlElement(sizable)) {
Zapatec.Utils.restoreOfSizing(sizable);
sizable.sizingObj = null;
} else if (Zapatec.isSizableObj(sizable)) {
sizable.restoreOfSizing();
}
});
});
this._setSizableSafely(false);
return true;
};
Zapatec.Sizable.replaceWithSizable = function(element, withEl) {
if (!Zapatec.isHtmlElement(element) || !Zapatec.isHtmlElement(withEl)) {
return false;
}
var width = Zapatec.Utils.getWidth(element);
var height = Zapatec.Utils.getHeight(element);
element.parentNode.insertBefore(withEl, element.nextSibling);
element.parentNode.removeChild(element);
if (!Zapatec.Utils.makeSafelySizable(withEl)) {
return false;
}
if (Zapatec.Utils.setWidth(withEl, width) && Zapatec.Utils.setHeight(withEl, height)) {
return true;
}
return false;
};
Zapatec.Sizable._setSizableSafely = function(on) {
this.safelySizable = on;
};
Zapatec.Sizable._parseSize = function(size, dimension) {
switch (true) {
case (size == "auto") : {
size = this._parseAutoSize(dimension);
break;
}
case ((/^\d+px$/).test(String(size))) : {
size = parseInt(size, 10);
break;
}
case ((/^\d+%$/).test(String(size))) : {
size = this._parsePercentSize(size, dimension);
break;
}
case (typeof size == "number") : {
break;
}
}
return this._canSetSize(size, dimension);
};
Zapatec.Sizable._parseAutoSize = function(dimension) {
var measurement = this.getSizableMeasurement(dimension);
var autoSizable = this._getAutoSizableElement(dimension);
if (!Zapatec.isHtmlElement(autoSizable)) {
return null;
}
var dim = dimension.charAt(0).toUpperCase() + dimension.slice(1).toLowerCase();
var diff = 0;
if (Zapatec.isHtmlElement(measurement) || typeof measurement == "number") {
diff = (Zapatec.Utils["get" + dim](measurement) || measurement) - Zapatec.Utils["get" + dim](autoSizable);
}
var el = autoSizable;
if (el.tagName.toLowerCase() == "iframe") {
try {
if (el.contentDocument != null) {
el = el.contentDocument.body;
} else if (el.contentWindow.document != null) {
el = el.contentWindow.document.body;
}
if (!Zapatec.isHtmlElement(el)) {
throw "No element to calculate auto size!";
}
} catch(e) {
Zapatec.Log({description : "Can't calculate auto size for the IFRAME in the object with ID '" + this.id + "'!", type : "warning"});
return null;
}
}
return el["scroll" + dim] + diff;
};
Zapatec.Sizable._parsePercentSize = function(value, dimension) {
var sizableParent = this._getSizableParent(dimension);
if (!Zapatec.isHtmlElement(sizableParent)) {
return null;
}
var dim = dimension.charAt(0).toUpperCase() + dimension.slice(1).toLowerCase();
value = parseInt(value, 10);
return Math.round((value / 100) * Zapatec.Utils["get" + dim](sizableParent));
};
Zapatec.Sizable._canSetSize = function(value, dimension) {
if (typeof value != "number") {
return false;
}
var dim = dimension.charAt(0).toUpperCase() + dimension.slice(1).toLowerCase();
var sizingConfig = this.getSizingConfig();
var limit = sizingConfig.limit;
var direction = sizingConfig.direction;
if (dim == "Width" && direction == "vertical") {
return this._handleSizeOverflow(this.getWidth());
}
if (dim == "Height" && direction == "horizontal") {
return this._handleSizeOverflow(this.getHeight());
}
if (typeof limit["min" + dim] == "number" && value < limit["min" + dim]) {
return this._handleSizeOverflow(limit["min" + dim]);
}
if (typeof limit["max" + dim] == "number" && value > limit["max" + dim]) {
return this._handleSizeOverflow(limit["max" + dim]);
}
return value;
};
Zapatec.Sizable._handleSizeOverflow = function(limit, dimension) {
return false;
};
Zapatec.Sizable.getSizingConfig = function() {
return this.getConfiguration();
};
Zapatec.Sizable.setSizingConfig = function(config) {
this.reconfigure(config);
};
Zapatec.Sizable._getAutoSizableElement = function(dimension) {
return this.getContainer();
};
Zapatec.Sizable._getSizableParent = function(dimension) {
return this.getContainer().parentNode;
};
Zapatec.Sizable.getSizableMeasurement = function(dimension) {
return this.getContainer();
};
Zapatec.Sizable.getSizableElements = function(dimension) {
return this.getContainer();
};
Zapatec.Sizable.getContainer = function() {
return this.getSizingConfig().container;
};
Zapatec.Sizable._setElementSize = function(size, sizable, dimension) {
var dim = dimension.charAt(0).toUpperCase() + dimension.slice(1).toLowerCase();
if (Zapatec.isHtmlElement(sizable)) {
if (Zapatec.Utils["set" + dim](sizable, size)) {
return true;
} else {
return false;
}
} else if (Zapatec.isSizableObj(sizable)) {
if (sizable["set" + dim](size)) {
return true;
} else {
return false;
}
}
return true;
};
Zapatec.Sizable._proceedElementsSizes = function(value, dimension, elArr, restArr) {
var diff = null, self = this, sizes = Zapatec.Array();
var measurement = this.getSizableMeasurement(dimension);
var dim = dimension.charAt(0).toUpperCase() + dimension.slice(1).toLowerCase();
if (typeof measurement == "number" || Zapatec.isHtmlElement(measurement)) {
diff = value - (Zapatec.Utils["get" + dim](measurement) || measurement);
}
elArr = Zapatec.Array(elArr);
elArr.each(function(row, val) {
var oneDiff = diff, size = value;
val = Zapatec.Array(val);
if (diff) {
oneDiff = Math.round(oneDiff / val.length);
} else {
size = Math.round(size / val.length);
}
sizes[row] = [];
val.each(function(index, sizable) {
var setSize = null;
var elSize = null;
if (Zapatec.isHtmlElement(sizable)) {
elSize = Zapatec.Utils["get" + dim](sizable)
} else if (Zapatec.isSizableObj(sizable)) {
elSize = sizable["get" + dim]();
} else {
return;
}
if (oneDiff || oneDiff === 0) {
if (index != val.length - 1) {
setSize = elSize + oneDiff;
} else {
setSize = elSize + (diff - oneDiff * index);
}
} else {
if (index != val.length - 1) {
setSize = size;
} else {
setSize = value - size * index;
}
}
sizes[row][index] = {setSize : setSize, elSize : elSize};
});
});
var res = elArr.each(function(row, val) {
val = Zapatec.Array(val);
var res = val.each(function(index, sizable) {
if (!sizes[row][index]) {
return;
}
var res = self._setElementSize(sizes[row][index].setSize, sizable, dimension);
if (!res && res === false) {
return "break";
} else {
restArr.push({sizable : sizable, oldSize : sizes[row][index].elSize});
}
});
if (!res && res === false) {
return "break";
}
});
return res;
};
Zapatec.Sizable._rollBackSizing = function(rollBackArr, dimension) {
var dim = dimension.charAt(0).toUpperCase() + dimension.slice(1).toLowerCase();
rollBackArr = Zapatec.Array(rollBackArr);
rollBackArr.each(function(index, obj) {
if (Zapatec.isHtmlElement(obj.sizable)) {
Zapatec.Utils["set" + dim](obj.sizable, obj.oldSize);
} else if (Zapatec.isSizableObj(obj.sizable)) {
obj.sizable["set" + dim](obj.oldSize);
}
});
};
Zapatec.Utils.Sizable = function(config) {
Zapatec.Utils.Sizable.SUPERconstructor.call(this, config);
};
Zapatec.Utils.Sizable.id = "Zapatec.Utils.Sizable";
Zapatec.inherit(Zapatec.Utils.Sizable, Zapatec.Widget);
Zapatec.implement(Zapatec.Utils.Sizable, "Zapatec.CommandEvent");
Zapatec.implement(Zapatec.Utils.Sizable, "Zapatec.Sizable");
Zapatec.Utils.Sizable.prototype.init = function(config) {
Zapatec.Utils.Sizable.SUPERclass.init.call(this, config);
this.makeSizable();
};
Zapatec.Utils.Sizable.prototype.configure = function(config) {
this.defineConfigOption("syncVertically", []);
this.defineConfigOption("syncHorizontally", []);
this.defineConfigOption("container", null);
this.defineConfigOption("direction", "both");
this.defineConfigOption("limit", {
minWidth : 10,
maxWidth : null,
minHeight : 10,
maxHeight : null
});
this.defineConfigOption("theme", null);
Zapatec.Utils.Sizable.SUPERclass.configure.call(this, config);
config = this.getConfiguration();
if (!config.limit || typeof config.limit != "object") {
config.limit = {
minWidth : null,
maxWidth : null,
minHeight : null,
maxHeight : null
};
}
var self = this;
config.syncVertically = Zapatec.Array(config.syncVertically);
config.syncVertically.each(function(index, subArr) {
config.syncVertically[index] = Zapatec.Array(subArr);
config.syncVertically[index].each(function(elIndex, element) {
if (element === null) {
return;
}
element = Zapatec.Widget.getElementById(element);
if (!element) {
Zapatec.Log({description : "Wrong element in syncVertically array for the sizable object with ID '" + self.id + "'!"});
} else {
config.syncVertically[index][elIndex] = element;
}
});
});
config.syncHorizontally = Zapatec.Array(config.syncHorizontally);
config.syncHorizontally.each(function(index, subArr) {
config.syncHorizontally[index] = Zapatec.Array(subArr);
config.syncHorizontally[index].each(function(elIndex, element) {
if (element === null) {
return;
}
element = Zapatec.Widget.getElementById(element);
if (!element) {
Zapatec.Log({description : "Wrong element in syncHorizontally array for the sizable object with ID '" + self.id + "'!"});
} else {
config.syncHorizontally[index][elIndex] = element;
}
});
});
if (!Zapatec.isHtmlElement(config.container = Zapatec.Widget.getElementById(config.container))) {
config.container = null;
}
};
Zapatec.Utils.Sizable.prototype.reconfigure = function(config) {
Zapatec.Utils.Sizable.SUPERclass.reconfigure.call(this, config);
};
Zapatec.Utils.Sizable.prototype.getSizableElements = function(dimension) {
var config = this.getConfiguration();
if (dimension && dimension.toLowerCase() == "width") {
return config.syncHorizontally.concat(config.container);
} else if (dimension && dimension.toLowerCase() == "height") {
return config.syncVertically.concat(config.container);
} else {
return config.syncVertically.concat(config.syncHorizontally).concat(config.container);
}
};
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007