-
Notifications
You must be signed in to change notification settings - Fork 0
/
compassrose-0.3.1.js
294 lines (274 loc) · 10.9 KB
/
compassrose-0.3.1.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/********** Compass Rose **********
www.compassro.se | www.brianmuenzenmeyer.com
The MIT License (MIT)
Copyright (c) 2011 Brian Muenzenmeyer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**********************************/
(function ($) {
var delayUntilDone = (function() {
var timers = {};
return function (callback, time, id) {
if (!id){
id = "defaultId";
}
if (timers[id]) {
clearTimeout (timers[id]);
}
timers[id] = setTimeout(callback, time);
};
})();
$.compassRose = function (options, element) {
this.element = $(element);
this._init(options);
};
$.compassRose.settings = {
radius: '90%',
arc: 360,
center: true,
point: {x:600, y:600},
radialOffset: 0,
centerElementId: 'compassRoseContent',
centerElementLeftOffset: 0,
centerElementTopOffset: 0,
rotationEasing: undefined,
easingSpeed: 2000,
sliceWidth: 4,
hoverFx: function(){jQuery.noop();},
blurFx: function(){ jQuery.noop();},
clickFx: function(){ jQuery.noop();},
onRotateFx: function(){ jQuery.noop();}
};
$.compassRose.prototype = {
_center: function(){
var element = $('#' + this.settings.centerElementId);
$(element).css('position', 'fixed');
$(element).css('left', this.settings.point.x - ($(element).width() / 2) + this.settings.centerElementLeftOffset);
$(element).css('top', this.settings.point.y - ($(element).height() / 2) + this.settings.centerElementTopOffset);
},
//centers CompassRose on given point or the window
center: function(suppliedPoint){
if (suppliedPoint){
this.settings.center = false;
this.settings.point = suppliedPoint;
} else {
this.settings.center = true;
this.settings.point.y = $(window).height() / 2;
this.settings.point.x = $(window).width() / 2;
}
this._center();
},
_init : function (options) {
this.settings = $.extend( true, {}, $.compassRose.settings, options);
//override point if center chosen
if(this.settings.center){
if(this.settings.centerElementId !== undefined){
this.center();
}
} else {
this._center();
}
this._calibrate();
this._orient();
},
_calibrate: function () {
this.settings.directions$ = this.element.children('div');
//determine position of n directions within arc
this.settings.arcSpan = this.settings.arc / this.settings.directions$.length;
if(typeof(this.settings.radius) === 'string'){
if(this.settings.radius.indexOf('%') !== -1){
var percentString = this.settings.radius.replace('%', ''),
percent = parseInt(percentString, 10),
//determine the minimum window percentage
windowWidth = $(window).width(),
windowHeight = $(window).height();
this.settings.radius = windowWidth > windowHeight ? ((windowHeight * (percent / 100))/2) : ((windowWidth * (percent / 100))/2);
}
}
var rose = this;
//add data to each passed in direction
this.settings.directions$.each(function(n){
var ele = $(this),
content = $(ele).html(),
htmlString = '<span>' + content + '</span>';
$(ele).data('compassRoseDirectionAngle', (n * rose.settings.arcSpan) + 90 + rose.settings.radialOffset); //90 to start at north, offset to allow user to go from there
//create the innerspan
$(ele).empty().append(htmlString);
$(ele).click(function(){
rose.settings.clickFx(this);
});
$(ele).hover(
function(){
rose.settings.hoverFx(this);
},
function(){
rose.settings.blurFx(this);
}
);
});
},
orient: function(performAnimation, performEasing) {
this._orient(performAnimation, performEasing);
},
_orient: function (performAnimation, performEasing) {
if(performAnimation === undefined){
performAnimation = false;
}
if(performEasing === undefined){
performEasing = false;
}
var rose = this;
this.settings.directions$.each(function(n){
//calculate each direction's offset
var ele = $(this),
radianAngle = $(ele).data('compassRoseDirectionAngle') * Math.PI / 180,
xOffset = (Math.cos(radianAngle) * rose.settings.radius) + rose.settings.point.x - ($(ele).find('span').width() / 2),
yOffset = (Math.sin(radianAngle) * rose.settings.radius * -1) + rose.settings.point.y;
$(ele).data('compassRoseX', xOffset);
$(ele).data('compassRoseY', yOffset);
$(ele).css('position', 'fixed');
if(performAnimation){
if(performEasing){
$(ele).animate({left: xOffset,
top: yOffset}, rose.settings.easingSpeed, rose.settings.rotationEasing);
} else {
$(ele).animate({left: xOffset,
top: yOffset}, 50);
}
} else{
$(ele).css('left', xOffset);
$(ele).css('top', yOffset);
}
});
},
//rotate CompassRose by the given amount of degrees
rotate: function (degrees) {
//if a custom Easing function is applied, use it during orientation and do not rotate in slices
if(this.settings.rotationEasing !== undefined && degrees !== 0){
this._rotate(degrees);
this._orient(true, true);
}
else if(degrees !== 0){
this._rotateBySlices(degrees);
this._orient(true, false);
}
this.settings.onRotateFx();
},
_rotate: function (degrees) {
this.settings.directions$.each(function(n){
var ele = $(this),
currentAngle = parseInt($(ele).data('compassRoseDirectionAngle'), 10),
newAngle = currentAngle + parseInt(degrees, 10);
if(newAngle >= 360){
newAngle -= 360;
}
if(newAngle <= 0){
newAngle = 360 + newAngle;
}
$(ele).data('compassRoseDirectionAngle', newAngle);
});
},
_rotateBySlices: function (degrees) {
if(degrees === 0) { return; }
var absDegrees = Math.abs(degrees);
if(absDegrees <= this.settings.sliceWidth){
this._rotateSlice(degrees);
this.rotate(0);
} else if(degrees < 0) {
this._rotateSlice(-this.settings.sliceWidth);
this._rotateBySlices(degrees + this.settings.sliceWidth);
}
else{
this._rotateSlice(this.settings.sliceWidth);
this._rotateBySlices(degrees - this.settings.sliceWidth);
}
},
_rotateSlice: function (degrees) {
this.settings.directions$.each(function(n){
var ele = $(this),
currentAngle = parseInt($(ele).data('compassRoseDirectionAngle'), 10),
newAngle = currentAngle + parseInt(degrees, 10);
if(newAngle >= 360){
newAngle -= 360;
}
if(newAngle <= 0){
newAngle = 360 + newAngle;
}
$(ele).data('compassRoseDirectionAngle', newAngle);
});
this._orient(true, false);
},
//rotate the given CompassRose id to the given angle
rotateTo: function (id, angle) {
//find element within settings.directions$
var index = this._findIndex(id),
ele,
currentAngle,
difference;
//if not found, return
if(index === -1){
return;
}
ele = $(this.settings.directions$).get(index);
currentAngle = $(ele).data('compassRoseDirectionAngle');
//find difference
difference = parseInt(angle, 10) - parseInt(currentAngle, 10);
if (difference < -180){
difference = (360 + difference);
}
//rotate difference
this.rotate(difference);
},
_findIndex: function (id) {
var foundIndex = -1;
$.each(this.settings.directions$, function(i, l){
if($(l).attr("id") === id){
foundIndex = i;
}
});
return foundIndex;
}
};
// Create or return $.CompassRose constructor
// Adapted from jQuery Isotope's architecture
// https://github.com/desandro/isotope/blob/master/jquery.isotope.js
// "A bit from jQuery UI
// https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.widget.js
// A bit from jcarousel
// https://github.com/jsor/jcarousel/blob/master/lib/jquery.jcarousel.js
// "
$.fn.compassRose = function ( options ) {
if ( typeof options === 'string' ) {
// call method
var args = Array.prototype.slice.call( arguments, 1 );
this.each(function(){
var instance = $.data( this, 'compassRose' );
if ( !instance ) {
return $.error( "cannot call methods on compassRose prior to initialization; " +
"attempted to call method '" + options + "'" );
}
if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
return $.error( "no such method '" + options + "' for compassRose instance" );
}
// apply method
instance[ options ].apply( instance, args );
});
} else {
this.each(function() {
var instance = $.data( this, 'compassRose' );
if ( instance ) {
// apply options & init
instance.option( options );
instance._init();
} else {
// initialize new instance
$.data( this, 'compassRose', new $.compassRose( options, this ) );
}
});
}
// return jQuery object
// so plugin methods do not have to
return this;
};
})(jQuery);