-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathMarkerWithGhost.js
158 lines (118 loc) · 5.29 KB
/
MarkerWithGhost.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* global define,module,require,google */
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'SlidingMarker'], factory.bind(null, root));
} else if (typeof module !== "undefined" && module !== null && module.exports != null) { // jshint ignore:line
// Node module.
module.exports = factory(root, require('jquery'), require('SlidingMarker'));
} else {
// Browser globals
root.MarkerWithGhost = factory(root, root.jQuery, root.SlidingMarker);
}
}(this,
function (root, $, SlidingMarker) {
'use strict';
//Let jQuery be soft dependency
$ = $ || {};
$.extend = $.extend || function extend(obj) {
//Taken (and modified) from here: http://stackoverflow.com/a/14604815/1691132
Array.prototype.slice.call(arguments, 1).forEach(function (source) {
if (source) {
for (var prop in source) {
if (source[prop] && source[prop].constructor === Object) {
if (!obj[prop] || obj[prop].constructor === Object) {
obj[prop] = obj[prop] || {};
extend(obj[prop], source[prop]);
} else {
obj[prop] = source[prop];
}
} else {
obj[prop] = source[prop];
}
}
}
});
return obj;
};
var inherits = function (childCtor, parentCtor) {
/* @constructor */
function TempCtor() { }
TempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new TempCtor();
/* @override */
childCtor.prototype.constructor = childCtor;
};
//constructor
var MarkerWithGhost = function (opt_options) {
this.ghostPosition = null;
this.ghostAnimationPosition = null;
// Call the parent constructor.
SlidingMarker.call(this, opt_options);
this.bindTo("ghostPosition", this, "position");
this.bindTo("ghostAnimationPosition", this._instance, "position");
};
inherits(MarkerWithGhost, SlidingMarker);
//Overrides
$.extend(MarkerWithGhost.prototype, {
_isGhost: false,
set: function (key, value) {
if (key === "position") {
this._turnGhostModeOff();
} else if (key === "ghostPosition") {
this._turnGhostModeOn();
this.originalSet("ghostPosition", value);
SlidingMarker.prototype._setInstancePositionAnimated.call(this, value);
return;
}
SlidingMarker.prototype.set.apply(this, arguments);
},
_rebindEventListener: null,
_turnGhostModeOn: function () {
if (!this._isGhost) {
if (!this._rebindEventListener) {
google.maps.event.removeListener(this._rebindEventListener);
this._rebindEventListener = null;
}
this.unbind("animationPosition");
this.unbind("ghostPosition");
this._isGhost = true;
}
},
_turnGhostModeOff: function () {
var that = this;
if (this._isGhost) {
//rebind only after _instance's position equals position, to prevent raising animationposition_change events for ghost
this._rebindEventListener = google.maps.event.addListener(this._instance, "position_changed", function () {
if (that.getPosition() === that._instance.getPosition()) {
that.bindTo("animationPosition", that._instance, "position");
google.maps.event.removeListener(that._rebindEventListener);
that._rebindEventListener = null;
}
});
this.bindTo("ghostPosition", this, "position");
this._isGhost = false;
}
},
getGhostPosition: function () {
return this.get("ghostPosition");
},
setGhostPosition: function (ghostPosition) {
this.set("ghostPosition", ghostPosition);
},
getGhostAnimationPosition: function () {
return this.get("ghostAnimationPosition");
},
//This will be called by binding created with marker.bindTo() method, instead of call to set("position").
position_changed: function () {
this._turnGhostModeOff();
SlidingMarker.prototype.position_changed.apply(this, arguments);
}
});
MarkerWithGhost.initializeGlobally = function () {
google.maps.Marker = MarkerWithGhost;
};
return MarkerWithGhost;
}));