-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-menubar.js
213 lines (176 loc) · 8.14 KB
/
jquery-menubar.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
/**
* jQuery horisontal menu bar plugin
*
* @author connect
*/
(function($){
var container;
var active = false;
var methods = {
init: function( options ){
var i;
var item, parent;
container = '#'+this.attr('id');
this.html(''); // clear container
/**
* Add item to root menu
* @param {html} parent
* @param {object} item
* @returns {html}
*/
var addItem = function(parent, item){
return $('<div />')
.attr('id', item.id)
.addClass('item')
.html(item.label)
.bind('click',
(typeof item.action === 'function') ? item.action : function(){}
)
.appendTo( parent );
};
var addSubmenu = function(parent, menu){
var item;
var tparent;
parent = $('<div />')
.attr('id', $(parent).attr('id')+'-submenu')
.addClass('submenu')
.bind('click', function(e){
$(parent).menubar('show',e);
e.stopPropagation();
})
.appendTo(parent);
for (var i in menu) {
item = menu[i];
item.id = i;
if (typeof item.submenu == 'object' ) {
// has submenu
item.action = function(e){
$(parent).menubar('show',e);
e.stopPropagation();
};
tparent = addSubitem(parent, item);
addSubmenu(tparent, item.submenu );
} else {
addSubitem(parent, item);
}
}
};
var addSubitem = function(parent, item) {
var icon = (item.icon === undefined) ? '<i></i>' : '<img src="'+item.icon+'">';
var hotkey = (item.hotkey != undefined) ? '<span class="hotkey">'+item.hotkey+'</span>' : '';
var res;
var arr = [];
var i;
if ( item == 'separator') {
res = $('<div />')
.addClass('separator')
.appendTo(parent);
} else {
res = $('<div />')
.attr('id', item.id)
.addClass('subitem')
.addClass(
(typeof item.submenu === 'object') ? 'submenu-parent' : ''
)
.html(icon+'<span class="separator"> </span>'+ item.label + hotkey)
.appendTo(parent);
if (typeof item.action === 'function') {
res.bind('click',function(e){
item.action(e);
$(container).menubar('hide');
e.stopPropagation();
});
// create hotkey
if (item.hotkey) {
// push event listener
keys.addHotkey(item.hotkey, item.action);
}
} else {
res.addClass('disabled');
}
}
return res;
};
// create capture layer
$('<div />')
.attr('id','captor')
.bind('click',function(e){$(container).menubar('hide',e);})
.insertBefore(container);
// process menu items recursevly
for (i in options.items) {
item = options.items[i];
item.id = i;
if ( typeof item.submenu == 'object' ) {// has submenu
item.action = function(e){
$(container).menubar('show',e);
};
parent = addItem(container, item);
addSubmenu(parent, item.submenu );
} else {
addItem(container, item);
}
}
// add mousemove handler
$(container).bind('mouseover',function(e){
if ( $(e.target).attr('id') == undefined ) return;
// ignore if nothing selected, caller is container, caller is submenu
if (
$(this).find('.selected').length == 0 ||
$(e.target).attr('id') == $(this).attr('id') ||
$(e.target).attr('id').indexOf('-submenu') > 0
) { return; }
// popup submenu if active
if ( $(e.target).find('.submenu').length > 0 ) {
$().menubar( 'show', e );
} else {
// hide other submenu
}
e.stopPropagation();
});
},
show: function(e){
var o = $(e.target).find('.submenu:first');
var id = $(e.target).attr('id');
var parent = e.target.parentElement;
// was hidden => show & hide other
if (o.css('display') == 'none') {
$(container)
.find('.selected')
.removeClass('selected');
$(container)
.find('.submenu')
.hide();
o.show( 'fold', {}, 100 )
.addClass('selected');
$(parent).show(); // keep parent visible
$(container) // reselect top menu item wis submenu opened
.find('.item:has(.selected)')
.addClass('selected');
$('#captor').show();
} else {
//$(container).menubar('hide');
}
},
hide: function(e){
$(container)
.find('.selected')
.removeClass('selected');
$(container)
.find('.submenu')
.hide();
$('#captor').hide();
},
container: function(e){
return container;
}
};
$.fn.menubar = function( method ){
if (methods[method]) {
return methods[method].apply( this, Array.prototype.slice.call(arguments, 1 ));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('There is no such method "'+method+'" in jQuery.menubar');
}
};
}(jQuery));