forked from foamdino/raphael-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.js
executable file
·167 lines (148 loc) · 4.15 KB
/
menu.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
$(document).ready(function() {
var paper = new Raphael($('#canvas_container'), 50, 500);
var circle = paper.circle(200, 200, 80);
//circle.attr({stroke-width:'0'});
var main_menu = getMenu();
draw_options(paper, main_menu['children'], main_menu);
//circle.attr({stroke:'none', fill:'blue'});
});
function draw_options(paper, menu, parent) {
var layer = [];
var siblings = get_siblings(getMenu()['children'], parent);
for(var i=0; i < menu.length; i++) {
var o = eval( menu[i] );
var menu_option = o['option'];
var angle_inc = 360/menu.length;
var angle = angle_inc * (i+1);
coords = calculate_position(80, 200, 200, angle)
var c;
var text;
if(angle == 360) {
c = paper.circle(coords[0],coords[1]-15,10);
text = paper.text(coords[0]+30, coords[1]-15, menu_option);
} else if(angle < 180) {
text = paper.text(coords[0]+40, coords[1]+10, menu_option);
c = paper.circle(coords[0]+10,coords[1]+10,10);
} else {
c = paper.circle(coords[0]-10,coords[1]+10,10);
text = paper.text(coords[0]-40, coords[1]+10, menu_option);
}
c.attr({stroke:'none', fill:'blue'});
text.attr({fill:'blue'});
if(typeof o['link'] !== 'undefined' && undefined != o['link']) {
c.attr({href:o['link']});
}
// add glow effect
c.hover(
function() {
this.g = this.glow({color: "#06F", width: 4});
},
function() {
if(this.g !== null && undefined !== this.g) {
this.g.remove();
}
}
);
layer.push(c);
layer.push(text);
if(typeof o['children'] !== 'undefined' && undefined !== o['children'] && o['children'].length > 0) {
c.click(
function() {
this.g.remove();
}
);
c.node.onclick = menu_activation(paper, o['children'], layer, o);
}
}
if(typeof parent !== 'undefined' && undefined !== parent) {
if(parent['option'] !== 'menu' && !parent_is_at_same_level(menu, parent)) {
var c = paper.circle(200, 200, 10);
c.attr({stroke:'none', fill:'red'});
var text = paper.text(220, 200, parent['option']);
text.attr({fill:'blue'});
c.hover(
function() {
this.g = this.glow({color: "#06F", width: 4});
},
function() {
if(null !== this.g && undefined !== this.g && typeof this.g !== 'undefined') {
this.g.remove();
}
}
);
layer.push(c);
layer.push(text);
c.click(
function() {
if(null !== this.g && undefined !== this.g && typeof this.g !== 'undefined') {
this.g.remove();
}
}
);
c.node.onclick = menu_activation(paper, siblings, layer, parent);
}
}
}
function parent_is_at_same_level(menu, parent) {
for(var i=0; i<menu.length; i++) {
if(menu[i]['option'] === parent['option']) {
return true;
}
}
return false;
}
function get_siblings(menu, parent) {
var correct_level = false;
var siblings = [];
if(parent_is_at_same_level(menu, parent)) {
for(var i=0; i< menu.length; i++) {
siblings[i] = menu[i];
}
return siblings;
} else {
for(var i=0; i<menu.length; i++) {
get_siblings(menu[i], parent);
}
}
}
function clean_up(paper, layer) {
var paper_dom = paper.canvas;
for(var i=0; i<layer.length; i++) {
var e = layer[i];
if(null !== e && undefined !== e && typeof e !== 'undefined') {
e.remove();
}
}
}
function menu_activation(paper, submenu, layer, parent) {
var f = function() {
clean_up(paper, layer);
draw_options(paper, submenu, parent);
}
return f;
}
function calculate_position(radius, origin_x, origin_y, angle) {
var x = origin_x+radius*Math.sin(angle*Math.PI/180);
var y = origin_y+radius*-Math.cos(angle*Math.PI/180);
return [x,y];
}
// used for testing, the contents can be replaced with a call to a service to get the correct menu structure
function getMenu() {
return {'option':'menu', 'children':[
{'option':'a', 'children': [
{'option':'a.1','link':'http://www.google.com','children':[]},
{'option':'a.2', 'children': []},
{'option':'a.3', 'children': []},
{'option':'a.4', 'children': []}
]},
{'option':'b', 'children': [
{'option':'b.1', 'children':[]},
{'option':'b.2', 'children':[]},
{'option':'b.3', 'children':[]}
]},
{'option':'c', 'children': [
{'option':'c.1', 'children':[]},
{'option':'c.2', 'children':[]}
]}
]};
}