222 lines
6.3 KiB
JavaScript
Raw Normal View History

2012-01-09 23:50:52 -03:00
/*
2012-10-04 20:48:01 -03:00
* Copyright [2012] [wisemapping]
2012-01-09 23:50:52 -03:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mindplot.layout.Node = new Class(/** @lends Node */{
/**
* @constructs
* @param id
* @param size
* @param position
* @param sorter
* @throws will throw an error if id is not a finite number or is null or undefined
* @throws will throw an error if size is null or undefined
* @throws will throw an error if position is null or undefined
* @throws will throw an error if sorter is null or undefined
*/
2012-07-07 13:18:27 -03:00
initialize:function (id, size, position, sorter) {
2011-12-11 23:47:01 -03:00
$assert(typeof id === 'number' && isFinite(id), "id can not be null");
$assert(size, "size can not be null");
$assert(position, "position can not be null");
$assert(sorter, "sorter can not be null");
this._id = id;
this._sorter = sorter;
this._properties = {};
this.setSize(size);
this.setPosition(position);
this.setShrunken(false);
},
/** */
2012-07-07 13:18:27 -03:00
getId:function () {
return this._id;
},
/** */
2012-07-07 13:18:27 -03:00
setFree:function (value) {
this._setProperty('free', value);
},
/** */
2012-07-07 13:18:27 -03:00
isFree:function () {
return this._getProperty('free');
},
/** */
2012-07-07 13:18:27 -03:00
hasFreeChanged:function () {
return this._isPropertyChanged('free');
},
/** */
2012-07-07 13:18:27 -03:00
hasFreeDisplacementChanged:function () {
2012-01-17 17:45:51 -03:00
return this._isPropertyChanged('freeDisplacement');
},
/** */
2012-07-07 13:18:27 -03:00
setShrunken:function (value) {
this._setProperty('shrink', value);
},
/** */
2012-07-07 13:18:27 -03:00
areChildrenShrunken:function () {
return this._getProperty('shrink');
},
/** */
2012-07-07 13:18:27 -03:00
setOrder:function (order) {
2011-12-11 23:47:01 -03:00
$assert(typeof order === 'number' && isFinite(order), "Order can not be null. Value:" + order);
this._setProperty('order', order);
},
/** */
2012-07-07 13:18:27 -03:00
resetPositionState:function () {
var prop = this._properties['position'];
if (prop) {
2012-01-12 00:49:18 -03:00
prop.hasChanged = false;
}
},
/** */
2012-07-07 13:18:27 -03:00
resetOrderState:function () {
var prop = this._properties['order'];
if (prop) {
2012-01-17 17:45:51 -03:00
prop.hasChanged = false;
}
},
/** */
2012-07-07 13:18:27 -03:00
resetFreeState:function () {
2012-01-17 17:45:51 -03:00
var prop = this._properties['freeDisplacement'];
if (prop) {
2012-01-12 00:49:18 -03:00
prop.hasChanged = false;
}
},
/** */
2012-07-07 13:18:27 -03:00
getOrder:function () {
return this._getProperty('order');
},
/** */
2012-07-07 13:18:27 -03:00
hasOrderChanged:function () {
return this._isPropertyChanged('order');
},
/** */
2012-07-07 13:18:27 -03:00
hasPositionChanged:function () {
2012-01-12 00:49:18 -03:00
return this._isPropertyChanged('position');
},
/** */
2012-07-07 13:18:27 -03:00
hasSizeChanged:function () {
2012-01-18 20:02:15 -03:00
return this._isPropertyChanged('size');
},
/** */
2012-07-07 13:18:27 -03:00
getPosition:function () {
return this._getProperty('position');
},
/** */
2012-07-07 13:18:27 -03:00
setSize:function (size) {
$assert($defined(size), "Size can not be null");
this._setProperty('size', Object.clone(size));
},
/** */
2012-07-07 13:18:27 -03:00
getSize:function () {
return this._getProperty('size');
},
/** */
2012-07-07 13:18:27 -03:00
setFreeDisplacement:function (displacement) {
$assert($defined(displacement), "Position can not be null");
$assert($defined(displacement.x), "x can not be null");
$assert($defined(displacement.y), "y can not be null");
var oldDisplacement = this.getFreeDisplacement();
var newDisplacement = {x:oldDisplacement.x + displacement.x, y:oldDisplacement.y + displacement.y};
this._setProperty('freeDisplacement', Object.clone(newDisplacement));
},
/** */
2012-07-07 13:18:27 -03:00
resetFreeDisplacement:function () {
2012-01-26 13:07:45 -03:00
this._setProperty('freeDisplacement', {x:0, y:0});
},
/** */
2012-07-07 13:18:27 -03:00
getFreeDisplacement:function () {
var freeDisplacement = this._getProperty('freeDisplacement');
return (freeDisplacement || {x:0, y:0});
},
/** */
2012-07-07 13:18:27 -03:00
setPosition:function (position) {
$assert($defined(position), "Position can not be null");
2011-12-11 23:47:01 -03:00
$assert($defined(position.x), "x can not be null");
$assert($defined(position.y), "y can not be null");
2012-07-07 13:18:27 -03:00
// This is a performance improvement to avoid movements that really could be avoided.
var currentPos = this.getPosition();
if (currentPos == null || Math.abs(currentPos.x - position.x) > 2 || Math.abs(currentPos.y - position.y) > 2)
this._setProperty('position', position);
},
2012-07-07 13:18:27 -03:00
_setProperty:function (key, value) {
var prop = this._properties[key];
if (!prop) {
prop = {
2012-01-12 00:49:18 -03:00
hasChanged:false,
2012-07-07 13:18:27 -03:00
value:null,
oldValue:null
};
}
2012-01-12 00:49:18 -03:00
// Only update if the property has changed ...
2014-03-05 00:14:28 -03:00
if (JSON.stringify(prop.value) != JSON.stringify(value)) {
2012-01-12 00:49:18 -03:00
prop.oldValue = prop.value;
prop.value = value;
prop.hasChanged = true;
}
2012-01-17 00:26:29 -03:00
this._properties[key] = prop;
},
2012-07-07 13:18:27 -03:00
_getProperty:function (key) {
var prop = this._properties[key];
return $defined(prop) ? prop.value : null;
},
2012-07-07 13:18:27 -03:00
_isPropertyChanged:function (key) {
var prop = this._properties[key];
2012-01-12 00:49:18 -03:00
return prop ? prop.hasChanged : false;
},
/** */
2012-07-07 13:18:27 -03:00
getSorter:function () {
return this._sorter;
},
/** @return {String} returns id, order, position, size and shrink information*/
2012-07-07 13:18:27 -03:00
toString:function () {
return "[id:" + this.getId() + ", order:" + this.getOrder() + ", position: {" + this.getPosition().x + "," + this.getPosition().y + "}, size: {" + this.getSize().width + "," + this.getSize().height + "}, shrink:" + this.areChildrenShrunken() + "]";
}
});