abstract-shape-element.js 2.6 KB

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