/
abstract-shape-element.js
72 lines (65 loc) · 2.23 KB
/
abstract-shape-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Abstract shape element. Shape element represents some visual vector or raster object.
* @constructor
* @param {String} name Tag name of the element.
* @param {Object} config Set of parameters to initialize element with.
* @param {Object} style Object with styles to set on element initialization.
*/
jvm.AbstractShapeElement = function(name, config, style){
this.style = style || {};
this.style.current = {};
this.isHovered = false;
this.isSelected = false;
this.updateStyle();
};
/**
* Set hovered state to the element. Hovered state means mouse cursor is over element. Styles will be updates respectively.
* @param {Boolean} isHovered <code>true</code> to make element hovered, <code>false</code> otherwise.
*/
jvm.AbstractShapeElement.prototype.setHovered = function(isHovered){
if (this.isHovered !== isHovered) {
this.isHovered = isHovered;
this.updateStyle();
}
};
/**
* Set selected state to the element. Styles will be updates respectively.
* @param {Boolean} isSelected <code>true</code> to make element selected, <code>false</code> otherwise.
*/
jvm.AbstractShapeElement.prototype.setSelected = function(isSelected){
if (this.isSelected !== isSelected) {
this.isSelected = isSelected;
this.updateStyle();
jvm.$(this.node).trigger('selected', [isSelected]);
}
};
/**
* Set element's style.
* @param {Object|String} property Could be string to set only one property or object to set several style properties at once.
* @param {String} value Value to set in case only one property should be set.
*/
jvm.AbstractShapeElement.prototype.setStyle = function(property, value){
var styles = {};
if (typeof property === 'object') {
styles = property;
} else {
styles[property] = value;
}
jvm.$.extend(this.style.current, styles);
this.updateStyle();
};
jvm.AbstractShapeElement.prototype.updateStyle = function(){
var attrs = {};
jvm.$.extend(attrs, this.style.initial || {});
jvm.$.extend(attrs, this.style.current || {});
if (this.isHovered) {
jvm.$.extend(attrs, this.style.hover || {});
}
if (this.isSelected) {
jvm.$.extend(attrs, this.style.selected || {});
if (this.isHovered) {
jvm.$.extend(attrs, this.style.selectedHover || {});
}
}
this.set(attrs);
};