340 lines
8.8 KiB
JavaScript
Raw Normal View History

2009-06-07 18:59:43 +00:00
/*
2015-04-12 00:15:12 -03:00
* Copyright [2015] [wisemapping]
2011-07-27 08:25:10 -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.IconGroup = new Class(/**@lends IconGroup */{
/**
* @constructs
* @param topicId
* @param iconSize
* @throws will throw an error if topicId is null or undefined
* @throws will throw an error if iconSize is null or undefined
*/
2015-04-12 00:23:24 -03:00
initialize: function (topicId, iconSize) {
2011-09-09 21:53:41 -03:00
$assert($defined(topicId), "topicId can not be null");
2011-10-18 09:29:29 -03:00
$assert($defined(iconSize), "iconSize can not be null");
2011-09-02 02:31:03 -03:00
this._icons = [];
2015-04-12 00:23:24 -03:00
this._group = new web2d.Group({
width: 0,
height: iconSize,
x: 0,
y: 0,
coordSizeWidth: 0,
coordSizeHeight: 100
});
2011-09-02 02:31:03 -03:00
this._removeTip = new mindplot.IconGroup.RemoveTip(this._group, topicId);
this.seIconSize(iconSize, iconSize);
2011-08-24 00:03:52 -03:00
this._registerListeners();
2011-08-24 00:03:52 -03:00
2011-07-27 08:25:10 -03:00
},
/** */
2015-04-12 00:23:24 -03:00
setPosition: function (x, y) {
2011-09-02 02:31:03 -03:00
this._group.setPosition(x, y);
2011-07-27 08:25:10 -03:00
},
/** */
2015-04-12 00:23:24 -03:00
getPosition: function () {
2011-09-02 02:31:03 -03:00
return this._group.getPosition();
},
/** */
2015-04-12 00:23:24 -03:00
getNativeElement: function () {
2011-09-02 02:31:03 -03:00
return this._group;
2011-07-27 08:25:10 -03:00
},
/** */
2015-04-12 00:23:24 -03:00
getSize: function () {
2011-09-02 02:31:03 -03:00
return this._group.getSize();
},
/** */
2015-04-12 00:23:24 -03:00
seIconSize: function (width, height) {
this._iconSize = {width: width, height: height};
2011-09-02 20:05:51 -03:00
this._resize(this._icons.length);
2011-07-27 08:25:10 -03:00
},
/**
* @param icon the icon to be added to the icon group
* @param {Boolean} remove
* @throws will throw an error if icon is not defined
*/
2015-04-12 00:23:24 -03:00
addIcon: function (icon, remove) {
2011-08-21 12:42:00 -03:00
$defined(icon, "icon is not defined");
2011-08-05 01:06:56 -03:00
icon.setGroup(this);
this._icons.push(icon);
2011-07-27 08:25:10 -03:00
// Adjust group and position ...
this._resize(this._icons.length);
this._positionIcon(icon, this._icons.length - 1);
2011-07-27 08:25:10 -03:00
var imageShape = icon.getImage();
2014-03-05 00:14:28 -03:00
this._group.append(imageShape);
2011-08-24 00:03:52 -03:00
// Register event for the group ..
2011-10-07 00:21:49 -03:00
if (remove) {
this._removeTip.decorate(this._topicId, icon);
}
2011-07-27 08:25:10 -03:00
},
2015-04-12 00:23:24 -03:00
_findIconFromModel: function (iconModel) {
2011-07-27 08:25:10 -03:00
var result = null;
_.each(this._icons, function (icon) {
var elModel = icon.getModel();
if (elModel.getId() == iconModel.getId()) {
result = icon;
2011-07-27 08:25:10 -03:00
}
}, this);
if (result == null) {
throw new Error("Icon can no be found:" + iconModel.getId() + ", Icons:" + this._icons);
2009-06-07 18:59:43 +00:00
}
2011-07-27 08:25:10 -03:00
return result;
},
/** */
2015-04-12 00:23:24 -03:00
removeIconByModel: function (featureModel) {
$assert(featureModel, "featureModel can not be null");
var icon = this._findIconFromModel(featureModel);
this._removeIcon(icon);
},
2015-04-12 00:23:24 -03:00
_removeIcon: function (icon) {
$assert(icon, "icon can not be null");
this._removeTip.close(0);
this._group.removeChild(icon.getImage());
2011-09-02 02:31:03 -03:00
this._icons.erase(icon);
this._resize(this._icons.length);
var me = this;
2011-09-02 02:31:03 -03:00
// Add all again ...
_.each(this._icons, function (elem, i) {
me._positionIcon(elem, i);
});
2011-07-27 08:25:10 -03:00
},
/** */
2015-04-12 00:23:24 -03:00
moveToFront: function () {
2011-09-02 02:31:03 -03:00
this._group.moveToFront();
2011-07-27 08:25:10 -03:00
},
2015-04-12 00:23:24 -03:00
_registerListeners: function () {
this._group.addEvent('click', function (event) {
2011-07-27 08:25:10 -03:00
// Avoid node creation ...
2011-08-21 12:42:00 -03:00
event.stopPropagation();
2011-07-27 08:25:10 -03:00
});
2011-08-24 00:03:52 -03:00
this._group.addEvent('dblclick', function (event) {
2011-08-21 12:42:00 -03:00
event.stopPropagation();
2011-07-27 08:25:10 -03:00
});
},
2015-04-12 00:23:24 -03:00
_resize: function (iconsLength) {
this._group.setSize(iconsLength * this._iconSize.width, this._iconSize.height);
2011-09-02 18:25:34 -03:00
var iconSize = mindplot.Icon.SIZE + (mindplot.IconGroup.ICON_PADDING * 2);
this._group.setCoordSize(iconsLength * iconSize, iconSize);
},
2015-04-12 00:23:24 -03:00
_positionIcon: function (icon, order) {
2011-09-02 18:25:34 -03:00
var iconSize = mindplot.Icon.SIZE + (mindplot.IconGroup.ICON_PADDING * 2);
icon.getImage().setPosition(iconSize * order + mindplot.IconGroup.ICON_PADDING, mindplot.IconGroup.ICON_PADDING);
2011-07-27 08:25:10 -03:00
}
2011-08-24 00:03:52 -03:00
});
/**
* @constant
* @type {Number}
* @default
*/
2011-09-02 18:25:34 -03:00
mindplot.IconGroup.ICON_PADDING = 5;
2011-08-24 00:03:52 -03:00
mindplot.IconGroup.RemoveTip = new Class(/** @lends IconGroup.RemoveTip */{
/**
* @classdesc inner class of IconGroup
* @constructs
* @param container
*/
2015-04-12 00:23:24 -03:00
initialize: function (container) {
2011-08-24 00:03:52 -03:00
$assert(container, "group can not be null");
2011-10-04 01:16:29 -03:00
this._fadeElem = container;
2011-08-24 00:03:52 -03:00
},
/**
* @param topicId
* @param icon
* @throws will throw an error if icon is null or undefined
*/
2015-04-12 00:23:24 -03:00
show: function (topicId, icon) {
2011-08-24 00:03:52 -03:00
$assert(icon, 'icon can not be null');
// Nothing to do ...
if (this._activeIcon != icon) {
// If there is an active icon, close it first ...
if (this._activeIcon) {
2011-08-24 01:06:32 -03:00
this.close(0);
2011-08-24 00:03:52 -03:00
}
// Now, let move the position the icon...
var pos = icon.getPosition();
2011-08-24 01:06:32 -03:00
// Register events ...
2011-08-24 00:03:52 -03:00
var widget = this._buildWeb2d();
widget.addEvent('click', function () {
2011-08-25 22:08:39 -03:00
icon.remove();
2011-08-24 00:03:52 -03:00
});
2014-08-03 19:09:25 -03:00
var me = this;
widget.addEvent('mouseover', function () {
2014-08-03 19:09:25 -03:00
me.show(topicId, icon);
});
2011-08-24 01:06:32 -03:00
widget.addEvent('mouseout', function () {
2014-08-03 19:09:25 -03:00
me.hide();
});
2011-08-24 01:06:32 -03:00
2011-09-02 02:31:03 -03:00
widget.setPosition(pos.x + 80, pos.y - 50);
2014-03-05 00:14:28 -03:00
this._fadeElem.append(widget);
2011-08-24 00:03:52 -03:00
// Setup current element ...
this._activeIcon = icon;
this._widget = widget;
2011-08-24 01:06:32 -03:00
} else {
clearTimeout(this._closeTimeoutId);
2011-08-24 00:03:52 -03:00
}
},
/** */
2015-04-12 00:23:24 -03:00
hide: function () {
2011-08-25 00:17:13 -03:00
this.close(200);
2011-08-24 00:03:52 -03:00
},
/**
* @param delay
*/
2015-04-12 00:23:24 -03:00
close: function (delay) {
2011-08-24 00:03:52 -03:00
2011-08-24 01:06:32 -03:00
// This is not ok, trying to close the same dialog twice ?
if (this._closeTimeoutId) {
clearTimeout(this._closeTimeoutId)
}
2011-08-24 00:03:52 -03:00
2014-08-03 19:09:25 -03:00
var me = this;
2011-08-24 00:03:52 -03:00
if (this._activeIcon) {
2011-08-24 01:06:32 -03:00
var widget = this._widget;
var close = function () {
2014-08-03 19:09:25 -03:00
me._activeIcon = null;
me._fadeElem.removeChild(widget);
me._widget = null;
me._closeTimeoutId = null;
};
2011-08-24 00:03:52 -03:00
2011-08-24 01:06:32 -03:00
if (!$defined(delay) || delay == 0) {
close();
}
else {
this._closeTimeoutId = close.delay(delay);
}
2011-08-24 00:03:52 -03:00
}
},
2015-04-12 00:23:24 -03:00
_buildWeb2d: function () {
2011-08-24 01:06:32 -03:00
var result = new web2d.Group({
2015-04-12 00:23:24 -03:00
width: 10,
height: 10,
x: 0,
y: 0,
coordSizeWidth: 10,
coordSizeHeight: 10
2011-08-24 01:06:32 -03:00
});
2011-08-24 00:03:52 -03:00
2011-08-24 01:06:32 -03:00
var outerRect = new web2d.Rect(0, {
2015-04-12 00:23:24 -03:00
x: 0,
y: 0,
width: 10,
height: 10,
stroke: '0',
fillColor: 'black'
2011-08-24 01:06:32 -03:00
});
2014-03-05 00:14:28 -03:00
result.append(outerRect);
2011-08-24 01:06:32 -03:00
outerRect.setCursor('pointer');
var innerRect = new web2d.Rect(0, {
2015-04-12 00:23:24 -03:00
x: 1,
y: 1,
width: 8,
height: 8,
stroke: '1 solid white',
fillColor: 'gray'
2011-08-24 01:06:32 -03:00
});
2014-03-05 00:14:28 -03:00
result.append(innerRect);
2011-08-24 00:03:52 -03:00
2015-04-12 00:23:24 -03:00
var line = new web2d.Line({stroke: '1 solid white'});
2011-08-24 00:03:52 -03:00
line.setFrom(1, 1);
line.setTo(9, 9);
2014-03-05 00:14:28 -03:00
result.append(line);
2011-08-24 00:03:52 -03:00
2015-04-12 00:23:24 -03:00
var line2 = new web2d.Line({stroke: '1 solid white'});
2011-08-24 00:03:52 -03:00
line2.setFrom(1, 9);
line2.setTo(9, 1);
2014-03-05 00:14:28 -03:00
result.append(line2);
2011-08-24 00:03:52 -03:00
2011-09-02 02:31:03 -03:00
// Some events ...
result.addEvent('mouseover', function () {
2011-08-24 01:06:32 -03:00
innerRect.setFill('#CC0033');
2011-08-24 00:03:52 -03:00
});
result.addEvent('mouseout', function () {
2011-08-24 01:06:32 -03:00
innerRect.setFill('gray');
2011-08-24 00:03:52 -03:00
});
2011-09-02 02:31:03 -03:00
result.setSize(50, 50);
2011-08-24 00:03:52 -03:00
return result;
2011-08-24 01:06:32 -03:00
},
/**
* @param topicId
* @param icon
*/
2015-04-12 00:23:24 -03:00
decorate: function (topicId, icon) {
2011-08-24 01:06:32 -03:00
2014-08-03 19:09:25 -03:00
var me = this;
2011-09-02 02:31:03 -03:00
if (!icon.__remove) {
icon.addEvent('mouseover', function () {
2014-08-03 19:09:25 -03:00
me.show(topicId, icon);
});
2011-09-02 02:31:03 -03:00
icon.addEvent('mouseout', function () {
2014-08-03 19:09:25 -03:00
me.hide();
});
2011-09-02 02:31:03 -03:00
icon.__remove = true;
}
2011-08-24 00:03:52 -03:00
}
2011-07-27 08:25:10 -03:00
});