comparison src/djol/interaction/FeatureDrag.js @ 10:8705103f074f

Слияние
author cin
date Mon, 21 Aug 2017 18:03:00 +0300
parents f0035923ff3e
children
comparison
equal deleted inserted replaced
9:37e9e6bbe87a 10:8705103f074f
1 define([ "dojo/_base/declare", "ol", "dojo/Evented" ], function(declare, ol,
2 Evented) {
3
4 var peekFirstFeature = function(map, coordinate, pixel) {
5 return map.forEachFeatureAtPixel(function(ft, layer) {
6 return ft;
7 });
8 };
9
10 var cls = declare([ ol.interaction.Pointer ], {
11 "-chains-" : {
12 constructor : "manual"
13 },
14
15 _peek : null,
16
17 _feature : null,
18
19 _coordinate : null,
20
21 cursor : "pointer",
22
23 _oldCursor : undefined,
24
25 /**
26 * Создает новый объект
27 *
28 * @param {Object}
29 * opts опции
30 * @param {Function}
31 * opts.peek Функция выбора фичи для перетаскивания
32 * function(map,coordinate,pixel), возвращает фичу
33 * @param {String}
34 * opts.cursor css курсор который будет использован при
35 * наведении и перетаскивании фичи
36 *
37 */
38 constructor : function(opts) {
39
40 ol.interaction.Pointer.apply(this, [{
41 handleDownEvent : this.handleDownEvent,
42 handleDragEvent : this.handleDragEvent,
43 handleMoveEvent : this.handleMoveEvent,
44 handleUpEvent : this.handleUpEvent
45 }]);
46 Evented.apply(this);
47
48 if (opts) {
49 if (opts.peek) {
50 this._peek = opts.peek;
51 } else {
52 this._peek = peekFirstFeature;
53 }
54 if ("cursor" in opts)
55 this.cursor = opts.cursor;
56 }
57 },
58
59 handleDownEvent : function(evt) {
60 var c = evt.coordinate;
61
62 var ft = this._peek(evt.map, c, evt.pixel);
63
64 if (ft) {
65 this._feature = ft;
66 this._coordinate = c;
67 this._emit("dragbegin", { feature : ft });
68 return true;
69 }
70
71 return false;
72 },
73
74 handleDragEvent : function(evt) {
75 var c1 = this._coordinate, c2 = evt.coordinate;
76
77 var dx = c2[0] - c1[0];
78 var dy = c2[1] - c1[1];
79
80 this._emit("dragging", { feature : this._feature, fromCoord : c1, toCoord : c2 });
81
82 this._feature.getGeometry().translate(dx, dy);
83
84 this._coordinate = c2;
85 },
86
87 handleUpEvent : function(evt) {
88 if (this._feature)
89 this._emit("dragend", { feature : this._feature });
90
91 this._feature = null;
92 this._coordinate = null;
93 return false;
94 },
95
96 handleMoveEvent : function(evt) {
97 if (this.cursor) {
98 var ft = this._feature || this._peek(evt.map, evt.coordinate , evt.pixel);
99
100 var element = evt.map.getTargetElement();
101 if (ft) {
102 if (element.style.cursor != this.cursor) {
103 this._oldCursor = element.style.cursor;
104 element.style.cursor = this.cursor;
105 }
106 } else if (this._oldCursor !== undefined) {
107 element.style.cursor = this._oldCursor;
108 this._oldCursor = undefined;
109 }
110 }
111 },
112
113 _emit : function(name, data) {
114 var evt = new ol.source.VectorEvent(name,this);
115
116 for(var p in data)
117 evt[p] = data[p];
118 this.dispatchEvent(evt);
119 }
120 });
121
122 return cls;
123 });