-
Notifications
You must be signed in to change notification settings - Fork 0
/
moonmenu.js
310 lines (264 loc) · 8.2 KB
/
moonmenu.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* ----------------------------- Moon Menu -------------------------------------
* Circular (Moon) JavaScript based menu that can be attached to elements or
* as a context menu. Supported by all modern browsers: IE9+, Firefox, Safari,
* Chrome and Opera
* Prerequisite: JQuery
*
* Copyright (c) 2013 - 2014 Liron Aichel, [email protected]
*
* Licensed under MIT-style license:
*
* 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 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() {
'use strict';
function MoonMenu() {
this.version = "1.0.2";
this.options = {
openTrigger: 'click touchend',
closeTrigger: undefined,
closeOnLostFocus: true,
radius: 40,
position: 'top right',
reverseOrder: false,
densityFactor: 2,
allowMultiple: false
};
this.sel = {
moons: ".moonmenu"
};
this.events = {
beforeOpen: "beforeOpen",
afterOpen: "opened",
beforeClose: "beforeClose",
afterClose: "afterClose",
itemClick: 'itemClick'
};
}
var convertFromDegreeToRad = function (degree) {
return degree * (Math.PI / 180);
}
var arrangeInCircle = function(elements,moonmenu) {
var self = this;
var radius = moonmenu.options.radius;
var box = moonmenu.options.radius * 2;
var width = box;
var height = box;
var step = (2 * Math.PI) / ((elements.length * moonmenu.options.densityFactor - 2));
var position = {
top: moonmenu.options.position.indexOf("top") != -1,
left: moonmenu.options.position.indexOf("left") != -1,
right: moonmenu.options.position.indexOf("right") != -1,
bottom: moonmenu.options.position.indexOf("bottom") != -1,
center: moonmenu.options.position.indexOf("center") != -1,
middle: moonmenu.options.position.indexOf("middle") != -1,
};
// calc angle
var angle;
if (position.top && position.left) {
angle = convertFromDegreeToRad(125);
} else if (position.bottom && position.left) {
angle = convertFromDegreeToRad(45);
} else if (position.bottom && position.right) {
angle = convertFromDegreeToRad(-45);
} else if (position.middle && position.left) {
angle = convertFromDegreeToRad(90);
} else if (position.middle && position.right) {
angle = convertFromDegreeToRad(-90);
} else if (position.center && position.top) {
angle = convertFromDegreeToRad(180);
} else if (position.center && position.bottom) {
angle = convertFromDegreeToRad(0);
} else {
angle = convertFromDegreeToRad(-125); // default: top right
}
// calc y offset
var yoffset = 0;
if (position.top) {
yoffset = 0;
} else if (position.middle) {
yoffset = moonmenu.attachedTo? moonmenu.attachedTo.height() / 2: 0;
} else {
yoffset = moonmenu.attachedTo? moonmenu.attachedTo.height(): 0;
}
// calc x offset
var xoffset = 0;
if (position.left) {
xoffset = 0;
} else if (position.center) {
xoffset = moonmenu.attachedTo? moonmenu.attachedTo.width() / 2: 0;
} else {
xoffset = moonmenu.attachedTo? moonmenu.attachedTo.width(): 0;
}
// fix offsets
yoffset -= (height/1.5);
xoffset -= (width/2);
// arrange elements
if (moonmenu.options.reverseOrder) {
elements = $(elements.get().reverse());
}
elements.each(function () {
var x = Math.round(width / 2 + radius * Math.cos(angle) - $(this).width() / 2);
var y = Math.round(height / 2 + radius * Math.sin(angle) - $(this).height() / 2);
$(this).css('top',(y+yoffset)+'px');
$(this).css('left',(x+xoffset)+'px');
angle += step;
});
}
MoonMenu.prototype.init = function (menuElement,attachToElement) {
var self = this;
// save pointers to elements
this.element = menuElement;
this.attachedTo = attachToElement;
// set necessery styling
this.element.css('position','absolute');
this.element.children().css('position','absolute');
this.element.css('list-style','none');
this.element.addClass('moonmenu');
var items = this.element.find('li');
items.addClass('moonMenuItem');
items.hide();
items.on('click touchend',function(e) {
$(self).trigger({type: self.events.itemClick, clickedItem: this});
});
// check if attach it to element
if (this.attachedTo) {
// register for triggers
if (!this.options.closeTrigger) {
// toggle on open trigger
this.attachedTo.on(this.options.openTrigger,function() {self.toggle()});
} else {
// open on open trigger
this.attachedTo.on(this.options.openTrigger,function() {self.open()});
// close on close trigger
this.attachedTo.on(this.options.closeTrigger,function() {self.close()});
}
}
// close on lost focus
if (this.options.closeOnLostFocus) {
$(document).on('mousedown',function(e) {
if (self.isActive()) {
if (self.attachedTo && $(e.target)[0] !== self.attachedTo[0]) {
self.close();
}
if (!self.attachedTo && e.button == 0) {
self.close();
}
}
});
}
}
MoonMenu.prototype.isActive = function (e) {
return this.element.is('.active');
}
MoonMenu.prototype.toggle = function (e) {
if (this.isActive()){
this.close(e);
} else {
this.open(e);
}
}
MoonMenu.prototype.open = function (e) {
// triggers event
$(this).trigger(this.events.beforeOpen,this);
// check if already open (for attached menus)
if (this.attachedTo && this.isActive()) {
return;
}
// close all opened menus
if (!this.options.allowMultiple) {
$(".moonmenu").each(function() {
var menuInstance = $(this).data('moonMenu');
if (menuInstance && menuInstance.isActive())
menuInstance.close(e);
});
}
var position = {x:0,y:0};
// calculate position for element
if (this.attachedTo) {
position = {
x: this.attachedTo.offset().left,
y: this.attachedTo.offset().top,
};
} else if (e.pageX && e.pageY) { // or according to mouse cursor
position = {
x: e.pageX,
y: e.pageY,
};
} else {
return;
}
// set the menu position
this.element.css('top', position.y + 'px');
this.element.css('left', position.x + 'px');
// animate items opening
var items = this.element.children();
items.hide();
arrangeInCircle(items,this);
$(items).each(function(index) {
// stop all previous animation
$(this).stop(true);
$(this).delay(40*index).fadeIn('fast');
});
this.element.addClass('active');
// triggers event
$(this).trigger(this.events.afterOpen,this);
}
MoonMenu.prototype.close = function () {
// triggers event
$(this).trigger(this.events.beforeClose,this);
var items = this.element.children();
$(items).each(function(index) {
$(this).delay(40*index).fadeOut('fast');
});
this.element.removeClass('active');
// triggers event
$(this).trigger(this.events.afterClose,this);
}
$.fn.moonMenu = function(options) {
if (this.length > 1) {
var returnValues = [];
this.each(function() {
returnValues.push($(this).moonMenu(options));
});
return returnValues;
}
// if menu already exists
if (this.data('moonMenu')) {
return this.data('moonMenu');
}
// verify element is valid
if (this &&
this.is('ul')) {
var instance = new MoonMenu();
$.extend(instance.options,options);
if (this.attr('for') &&
$(this.attr('for')).length>0) {
instance.init(this,$(this.attr('for')));
} else {
instance.init(this);
}
this.data('moonMenu',instance);
return instance;
}
}
// auto detect menus
$(document).ready(function() {
$("body [data-role='moonMenu']").moonMenu();
});
})();