movable.js
Summary
No overview generated for 'movable.js'
Zapatec.Movable = {};
Zapatec.Movable.setPosition = function(x, y) {
if (!this.isMovableSafely()) {
Zapatec.Log({description : "The object with ID '" + this.id + "' was not prepared for moving! Use obj.makeMovable() to do so!", type : "warning"});
return false;
}
var msgValue = null, moveConfig = this.getMoveConfig();
if (x || x === 0) {
msgValue = x;
x = this._parseCoordinate(x, "x", moveConfig.moveLayer);
if (!x && x !== 0) {
Zapatec.Log({description : "The X coordinate " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
}
if (y || y === 0) {
msgValue = y;
y = this._parseCoordinate(y, "y", moveConfig.moveLayer);
if (!y && y !== 0) {
Zapatec.Log({description : "The Y coordinate " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
}
var elements = Zapatec.Array(this.getMovableElements());
if (this.fireEvent("beforePositionChange", x, y) === false) {
return false;
}
if (Zapatec.GlobalEvents.fireEvent("beforePositionChange", x, y, this) === false) {
return false;
}
this._proceedElementsCoords(x, y, elements);
if (this.isMoving()) {
this.fireEvent("onMove", x || this.getPosition().x, y || this.getPosition().y);
Zapatec.GlobalEvents.fireEvent("onMove", x || this.getPosition().x, y || this.getPosition().y, this);
}
this.fireEvent("onPositionChange", x || this.getPosition().x, y || this.getPosition().y);
Zapatec.GlobalEvents.fireEvent("onPositionChange", x || this.getPosition().x, y || this.getPosition().y, this);
return true;
};
Zapatec.Movable.setOrientedPosition = function(x, y) {
switch (this.getMoveConfig().orientation) {
case "vertical" : {
return this.setPosition(y, x);
}
case "horizontal" : {
return this.setPosition(x, y);
}
}
return false;
};
Zapatec.Movable.setPagePosition = function(x, y) {
var moveConfig = this.getMoveConfig();
if (moveConfig.moveLayer == document.body) {
return this.setPosition(x, y);
}
var msgValue = null;
if (x || x === 0) {
msgValue = x;
x = this._parseCoordinate(x, "x", document.body);
if (!x && x !== 0) {
Zapatec.Log({description : "The X page coordinate " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
}
if (y || y === 0) {
msgValue = y;
y = this._parseCoordinate(y, "y", document.body);
if (!y && y !== 0) {
Zapatec.Log({description : "The Y page coordinate " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
}
var pos = Zapatec.Utils.getElementOffset(moveConfig.moveLayer);
return this.setPosition((x || x === 0) ? (x - pos.x) : x, (y || y === 0) ? (y - pos.y) : y);
};
Zapatec.Movable.setScreenPosition = function(x, y) {
var moveConfig = this.getMoveConfig();
var msgValue = null;
if (x || x === 0) {
msgValue = x;
x = this._parseCoordinate(x, "x", window);
if (!x && x !== 0) {
Zapatec.Log({description : "The X screen coordinate " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
}
if (y || y === 0) {
msgValue = y;
y = this._parseCoordinate(y, "y", window);
if (!y && y !== 0) {
Zapatec.Log({description : "The Y screen coordinate " + msgValue + " can not be set for object with ID '" + this.id + "'!", type : "warning"});
return false;
}
}
if (moveConfig.moveLayer != document.body) {
var pos = Zapatec.Utils.getElementOffset(moveConfig.moveLayer);
} else {
var pos = {x : 0, y : 0};
}
return this.setPosition((x || x === 0) ? (x - pos.x) : x, (y || y === 0) ? (y - pos.y) : y);
};
Zapatec.Movable.moveFor = function(offsetLeft, offsetTop) {
var pos = this.getPosition();
return this.setPosition(
offsetLeft == null ? null : pos.x + offsetLeft,
offsetTop == null ? null : pos.y + offsetTop
);
};
Zapatec.Movable.getPosition = function() {
var el = this.getMovableMeasurement();
if (Zapatec.isHtmlElement(el) ||
(el && typeof el == "object" && typeof el.x == "number" && typeof el.y == "number")) {
return Zapatec.Utils.getPos(el) || el;
}
Zapatec.Log({description : "Can't calculate position for object with ID '" + this.id + "'!", type : "warning"});
return false;
};
Zapatec.Movable.getPagePosition = function() {
var el = this.getMovableMeasurement();
if (Zapatec.isHtmlElement(el) ||
(el && typeof el == "object" && typeof el.x == "number" && typeof el.y == "number")) {
return Zapatec.Utils.getElementOffset(el) || el;
}
Zapatec.Log({description : "Can't calculate screen position for object with ID '" + this.id + "'!", type : "warning"});
return false;
};
Zapatec.Movable.getScreenPosition = function() {
var pos = this.getPagePosition();
pos.x -= Zapatec.Utils.getPageScrollX();
pos.y -= Zapatec.Utils.getPageScrollY();
return pos;
};
Zapatec.Movable.startMove = function() {
if (!this.isMovableSafely()) {
Zapatec.Log({description : "The object with ID '" + this.id + "' was not prepared for moving! Use obj.makeMovable() to do so!", type : "warning"});
return false;
}
this.fireEvent("moveStart");
Zapatec.GlobalEvents.fireEvent("moveStart", this);
this._setMovingState(true);
return true;
};
Zapatec.Movable.endMove = function() {
if (!this.isMoving()) {
Zapatec.Log({description : "The moving for object with ID '" + this.id + "' was not started!", type : "warning"});
return false;
}
this.fireEvent("moveEnd");
Zapatec.GlobalEvents.fireEvent("moveEnd", this);
this._setMovingState(false);
return true;
};
Zapatec.Movable.isMoving = function() {
return this.movingState;
};
Zapatec.Movable._setMovingState = function(on) {
this.movingState = on;
};
Zapatec.Movable.isMovableSafely = function() {
return this.safelyMovable;
};
Zapatec.Movable._setMovableSafely = function(on) {
this.safelyMovable = on;
};
Zapatec.Movable.makeMovable = function() {
if (!this.requireInterface("Zapatec.CommandEvent")) {
return false;
}
if (this.isMovableSafely()) {
return true;
}
var elements = Zapatec.Array(this.getMovableElements()), self = this,
moveConfig = this.getMoveConfig();
success = elements.each(function(index, movable) {
if (Zapatec.isHtmlElement(movable)) {
if (moveConfig.preserveSizes &&
!Zapatec.Utils.makeSafelySizable(movable)) {
return "break";
}
if (!Zapatec.Utils.makeSafelyMovable(movable, moveConfig.moveLayer)) {
return "break";
}
self.createProperty(movable, "moveObj", self);
} else if (Zapatec.isMovableObj(movable)) {
if (!movable.makeMovable()) {
return "break";
}
}
});
if (!success) {
this.restoreOfMove();
Zapatec.Log({description : "Can not make the object with ID '" + this.id + "' movable!"});
return false;
}
this._setMovableSafely(true);
return true;
};
Zapatec.Movable.restoreOfMove = function() {
if (!this.isMovableSafely()) {
return true;
}
var elements = Zapatec.Array(this.getMovableElements());
var self = this;
elements.each(function(index, movable) {
if (Zapatec.isHtmlElement(movable)) {
Zapatec.Utils.restoreOfMove(movable);
if (self.getMoveConfig().preserveSizes) {
Zapatec.Utils.restoreOfSizing(movable);
}
movable.moveObj = null;
} else if (Zapatec.isMovableObj(movable)) {
movable.restoreOfMove();
}
}, true);
this._setMovableSafely(false);
return true;
};
Zapatec.Movable.getMovableMeasurement = function() {
return this.getContainer();
};
Zapatec.Movable.getMoveConfig = function() {
return this.getConfiguration();
};
Zapatec.Movable.setMoveConfig = function(config) {
this.reconfigure(config);
};
Zapatec.Movable.getMovableElements = function() {
return this.getContainer();
};
Zapatec.Movable.getContainer = function() {
return this.getMoveConfig().container;
};
Zapatec.Movable._parseCoordinate = function(coord, dimension, within) {
switch (true) {
case (typeof coord == "number") : {
if (within == window) {
coord += Zapatec.Utils["getPageScroll" + dimension.toUpperCase()]();
}
break;
}
case ((/^\d+px$/).test(String(coord))) : {
coord = parseInt(coord, 10);
if (within == window) {
coord += Zapatec.Utils["getPageScroll" + dimension.toUpperCase()]();
}
break;
}
case ((/^(left|top|bottom|right|center)$/i).test(String(coord))) : {
coord = this._parseWordCoordinate(coord, dimension, within);
break;
}
}
return this._canSetCoordinate(coord, dimension, within);
};
Zapatec.Movable._parseWordCoordinate = function(coord, dimension, within) {
if ((/(left|right)/i).test(String(coord)) && dimension.toUpperCase() != "X") {
return false;
}
if ((/(top|bottom)/i).test(String(coord)) && dimension.toUpperCase() != "Y") {
return false;
}
var dim = (dimension.toUpperCase() == "X") ? "Left" : "Top";
var sizeDim = (dimension.toUpperCase() == "X") ? "Width" : "Height";
var parsedCoord = 0;
var wSize = null;
if (Zapatec.isHtmlElement(within)) {
parsedCoord = within["scroll" + dim];
wSize = Zapatec.Utils["get" + sizeDim](within);
} else if (within == window) {
parsedCoord = Zapatec.Utils["getPageScroll" + dimension.toUpperCase()]();
wSize = Zapatec.Utils.getWindowSize()[sizeDim.toLowerCase()];
} else if (within && typeof within == "object") {
parsedCoord += within["scroll" + dim] || 0;
wSize = within[sizeDim.toLowerCase()];
} else {
return false;
}
var measurement = this.getMovableMeasurement();
var mSize = null;
if (Zapatec.isHtmlElement(measurement)) {
mSize = Zapatec.Utils["get" + sizeDim](measurement);
} else if (measurement && typeof measurement == "object") {
mSize = measurement[sizeDim.toLowerCase()];
}
switch (coord) {
case "left" : case "top" : {
break;
}
case "LEFT" : case "TOP" : {
parsedCoord -= mSize;
break;
}
case "right" : case "bottom" : {
parsedCoord += wSize - mSize;
break;
}
case "RIGHT" : case "BOTTOM" : {
parsedCoord += wSize;
break;
}
case "center" : {
parsedCoord += Math.round((wSize - mSize) / 2);
break;
}
case "CENTER" : {
parsedCoord += Math.round(wSize / 2);
break;
}
default : {
parsedCoord = null;
break;
}
}
if (!parsedCoord && parsedCoord !== 0) {
return false;
} else {
return parsedCoord;
}
};
Zapatec.Movable._canSetCoordinate = function(coord, dimension, within) {
if (typeof coord != "number") {
return false;
}
var moveConfig = this.getMoveConfig();
if (within != moveConfig.moveLayer) {
return coord;
}
var limitObj = moveConfig.limit;
var dim = dimension.toUpperCase();
var direction = moveConfig.direction.toLowerCase();
var correction = this._getMovableShape();
if (dim == "X" && direction == "vertical") {
return this._handleCoordOverflow(this.getPosition().x);
} else if (dim == "Y" && direction == "horizontal") {
return this._handleCoordOverflow(this.getPosition().y);
}
if ((limitObj["min" + dim] || limitObj["min" + dim] === 0) && coord < limitObj["min" + dim] + correction["min" + dim]) {
return this._handleCoordOverflow(limitObj["min" + dim] + correction["min" + dim]);
}
if ((limitObj["max" + dim] || limitObj["max" + dim] === 0) && coord > limitObj["max" + dim] + correction["max" + dim]) {
return this._handleCoordOverflow(limitObj["max" + dim] + correction["max" + dim]);
}
return coord;
};
Zapatec.Movable._handleCoordOverflow = function(limit, dimension) {
return false;
};
Zapatec.Movable._getMovableShape = function() {
var measurement = this.getMovableMeasurement();
var obj = {minX : 0, maxX : 0, minY : 0, maxY : 0};
switch (this.getMoveConfig().followShape) {
case (true) : {
if (!Zapatec.isHtmlElement(measurement)) {
return obj;
}
obj.maxX = -Zapatec.Utils.getWidth(measurement);
obj.maxY = -Zapatec.Utils.getHeight(measurement);
return obj
}
default : {
return obj;
}
}
};
Zapatec.Movable.getMovingPoint = function() {
return this.getScreenPosition();
};
Zapatec.Movable._proceedElementsCoords = function(x, y, elements) {
elements = Zapatec.Array(elements);
var measurement = this.getMovableMeasurement();
var pos = null;
if (Zapatec.isHtmlElement(measurement)) {
pos = Zapatec.Utils.getPos(measurement);
} else if (measurement && typeof measurement == "object") {
pos = {
x : (typeof measurement.x == "number") ? measurement.x : null,
y : (typeof measurement.y == "number") ? measurement.y : null
};
if (!measurement.x || !measurement.y) {
pos = null;
}
}
elements.each(function(index, movable) {
var mX = x, mY = y;
if (pos) {
var mPos = null;
if (Zapatec.isHtmlElement(movable)) {
mPos = Zapatec.Utils.getPos(movable);
} else if (Zapatec.isMovableObj(movable)) {
mPos = movable.getPosition();
} else {
return;
}
if (x || x === 0) {
mX = mPos.x + (x - pos.x);
}
if (y || y === 0) {
mY = mPos.y + (y - pos.y);
}
}
if (Zapatec.isHtmlElement(movable)) {
Zapatec.Utils.moveTo(movable, mX, mY);
} else if (Zapatec.isMovableObj(movable)) {
movable.setPosition(mX, mY);
}
});
};
Zapatec.Movable.sortElementsByPath = function(elements) {
elements = Zapatec.Array(elements);
elements.each(function(index, element) {
var path = Zapatec.Utils.getElementPath(element, document.body);
if (!path) {
return;
}
path = Zapatec.Array(path.split("-"));
for(var i = index; i < elements.length; ++i) {
var compPath = Zapatec.Utils.getElementPath(elements[i], document.body);
if (!compPath) {
continue;
}
compPath = Zapatec.Array(compPath.split("-"));
path.each(function(k, pathPiece, whole) {
if (parseInt(pathPiece, 10) > (parseInt(compPath[k], 10) || 0)) {
return "break";
} else if (parseInt(pathPiece, 10) < parseInt(compPath[k], 10)) {
var el = elements[index]
elements[index] = elements[i];
elements[i] = el;
path = compPath;
return "break";
}
});
}
});
return true;
};
Zapatec.Utils.Movable = function(config) {
Zapatec.Utils.Movable.SUPERconstructor.call(this, config);
};
Zapatec.Utils.Movable.id = "Zapatec.Utils.Movable";
Zapatec.inherit(Zapatec.Utils.Movable, Zapatec.Widget);
Zapatec.implement(Zapatec.Utils.Movable, "Zapatec.CommandEvent");
Zapatec.implement(Zapatec.Utils.Movable, "Zapatec.Movable");
Zapatec.Utils.Movable.prototype.init = function(config) {
Zapatec.Utils.Movable.SUPERclass.init.call(this, config);
var self = this;
var elements = null;
if (this.getConfiguration().makeMovable) {
this.makeMovable();
} else {
elements = Zapatec.Array(this.getMovableElements());
elements.each(function(index, movable) {
if (Zapatec.isHtmlElement(movable)) {
self.createProperty(movable, "moveObj", self);
}
});
}
};
Zapatec.Utils.Movable.prototype.configure = function(config) {
this.defineConfigOption("synchronize", []);
this.defineConfigOption("container", null);
this.defineConfigOption("limit", {
minX : null,
maxX : null,
minY : null,
maxY : null
});
this.defineConfigOption("direction", "both");
this.defineConfigOption("moveLayer", document.body);
this.defineConfigOption("followShape", "LT");
this.defineConfigOption("preserveSizes", true);
this.defineConfigOption("makeMovable", true);
this.defineConfigOption("theme", null);
Zapatec.Utils.Movable.SUPERclass.configure.call(this, config);
config = this.getConfiguration();
if (!config.limit || typeof config.limit != "object") {
config.limit = {
minX : null,
maxX : null,
minY : null,
maxY : null
};
}
var self = this;
config.synchronize = Zapatec.Array(config.synchronize);
config.synchronize.each(function(index, element) {
if (element === null) {
return;
}
element = Zapatec.Widget.getElementById(element);
element = Zapatec.Utils.img2div(element);
if (!element) {
Zapatec.Log({description : "Wrong element in synchronize array for the movable object with ID '" + self.id + "'!"});
} else {
config.synchronize[index] = element;
}
});
config.container = Zapatec.Widget.getElementById(config.container);
config.container = Zapatec.Utils.img2div(config.container);
if (!Zapatec.isHtmlElement(config.container)) {
Zapatec.Log({description : "Wrong element passed as container for the movable object with ID '" + self.id + "'!"});
}
this.movableElements = Zapatec.Array(config.synchronize.concat(config.container));
if (this.movableElements.length > 1) {
this.sortElementsByPath(this.movableElements);
}
if (!Zapatec.isHtmlElement(config.moveLayer = Zapatec.Widget.getElementById(config.moveLayer))) {
config.moveLayer = document.body;
}
};
Zapatec.Utils.Movable.prototype.reconfigure = function(config) {
Zapatec.Utils.Movable.SUPERclass.reconfigure.call(this, config);
};
Zapatec.Utils.Movable.prototype.getMovableElements = function() {
return this.movableElements;
};
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007