1 var Polygon, ToolWithStroke, createShape,
2 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
3 hasProp = {}.hasOwnProperty;
5 ToolWithStroke = require('./base').ToolWithStroke;
7 createShape = require('../core/shapes').createShape;
9 module.exports = Polygon = (function(superClass) {
10 extend(Polygon, superClass);
13 return Polygon.__super__.constructor.apply(this, arguments);
16 Polygon.prototype.name = 'Polygon';
18 Polygon.prototype.iconName = 'polygon';
20 Polygon.prototype.usesSimpleAPI = false;
22 Polygon.prototype.didBecomeActive = function(lc) {
23 var onDown, onMove, onUp, polygonCancel, polygonFinishClosed, polygonFinishOpen, polygonUnsubscribeFuncs;
24 Polygon.__super__.didBecomeActive.call(this, lc);
25 polygonUnsubscribeFuncs = [];
26 this.polygonUnsubscribe = (function(_this) {
28 var func, i, len, results;
30 for (i = 0, len = polygonUnsubscribeFuncs.length; i < len; i++) {
31 func = polygonUnsubscribeFuncs[i];
38 this.maybePoint = null;
39 onUp = (function(_this) {
41 if (_this._getWillFinish()) {
42 return _this._close(lc);
44 lc.trigger('lc-polygon-started');
46 _this.points.push(_this.maybePoint);
48 _this.points = [_this.maybePoint];
51 x: _this.maybePoint.x,
54 lc.setShapesInProgress(_this._getShapes(lc));
55 return lc.repaintLayer('main');
58 onMove = (function(_this) {
59 return function(arg) {
62 if (_this.maybePoint) {
63 _this.maybePoint.x = x;
64 _this.maybePoint.y = y;
65 lc.setShapesInProgress(_this._getShapes(lc));
66 return lc.repaintLayer('main');
70 onDown = (function(_this) {
71 return function(arg) {
78 lc.setShapesInProgress(_this._getShapes(lc));
79 return lc.repaintLayer('main');
82 polygonFinishOpen = (function(_this) {
88 return _this._close(lc);
91 polygonFinishClosed = (function(_this) {
93 _this.maybePoint = _this.points[0];
94 return _this._close(lc);
97 polygonCancel = (function(_this) {
99 return _this._cancel(lc);
102 polygonUnsubscribeFuncs.push(lc.on('drawingChange', (function(_this) {
104 return _this._cancel(lc);
107 polygonUnsubscribeFuncs.push(lc.on('lc-pointerdown', onDown));
108 polygonUnsubscribeFuncs.push(lc.on('lc-pointerdrag', onMove));
109 polygonUnsubscribeFuncs.push(lc.on('lc-pointermove', onMove));
110 polygonUnsubscribeFuncs.push(lc.on('lc-pointerup', onUp));
111 polygonUnsubscribeFuncs.push(lc.on('lc-polygon-finishopen', polygonFinishOpen));
112 polygonUnsubscribeFuncs.push(lc.on('lc-polygon-finishclosed', polygonFinishClosed));
113 return polygonUnsubscribeFuncs.push(lc.on('lc-polygon-cancel', polygonCancel));
116 Polygon.prototype.willBecomeInactive = function(lc) {
117 Polygon.__super__.willBecomeInactive.call(this, lc);
118 if (this.points || this.maybePoint) {
121 return this.polygonUnsubscribe();
124 Polygon.prototype._getArePointsClose = function(a, b) {
125 return (Math.abs(a.x - b.x) + Math.abs(a.y - b.y)) < 10;
128 Polygon.prototype._getWillClose = function() {
129 if (!(this.points && this.points.length > 1)) {
132 if (!this.maybePoint) {
135 return this._getArePointsClose(this.points[0], this.maybePoint);
138 Polygon.prototype._getWillFinish = function() {
139 if (!(this.points && this.points.length > 1)) {
142 if (!this.maybePoint) {
145 return this._getArePointsClose(this.points[0], this.maybePoint) || this._getArePointsClose(this.points[this.points.length - 1], this.maybePoint);
148 Polygon.prototype._cancel = function(lc) {
149 lc.trigger('lc-polygon-stopped');
150 this.maybePoint = null;
152 lc.setShapesInProgress([]);
153 return lc.repaintLayer('main');
156 Polygon.prototype._close = function(lc) {
157 lc.trigger('lc-polygon-stopped');
158 lc.setShapesInProgress([]);
159 if (this.points.length > 2) {
160 lc.saveShape(this._getShape(lc, false));
162 this.maybePoint = null;
163 return this.points = null;
166 Polygon.prototype._getShapes = function(lc, isInProgress) {
168 if (isInProgress == null) {
171 shape = this._getShape(lc, isInProgress);
179 Polygon.prototype._getShape = function(lc, isInProgress) {
181 if (isInProgress == null) {
186 points = points.concat(this.points);
188 if ((!isInProgress) && points.length < 3) {
191 if (isInProgress && this.maybePoint) {
192 points.push(this.maybePoint);
194 if (points.length > 1) {
195 return createShape('Polygon', {
196 isClosed: this._getWillClose(),
197 strokeColor: lc.getColor('primary'),
198 fillColor: lc.getColor('secondary'),
199 strokeWidth: this.strokeWidth,
200 points: points.map(function(xy) {
201 return createShape('Point', xy);
209 Polygon.prototype.optionsStyle = 'polygon-and-stroke-width';