-
Notifications
You must be signed in to change notification settings - Fork 0
/
popupMenu.js
278 lines (267 loc) · 11.7 KB
/
popupMenu.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
// main
$(function () {
// define popup menues
var newsItems = (function () {
return [{
Title: 'Yediot',
Url: 'http://www.ynet.co.il',
childItems: [{
Title: 'Entertainment',
Url: '',
childItems: [{
Title: 'Blazer',
Url: 'http://www.ynet.co.il/home/0,7340,L-3456,00.html',
childItems: []
},
{
Title: 'פנאי פלוס',
Url: 'http://pplus.ynet.co.il/home/0,7340,L-11211,00.html',
childItems: []
},
{
Title: 'לאשה',
Url: 'http://xnet.ynet.co.il/laisha/home/0,14961,L-4335,00.html',
childItems: []
},
{
Title: 'Tours',
Url: 'http://www.ynettours.co.il/',
childItems: []
}
]
},
{
Title: 'Health',
Url: '',
childItems: [{
Title: 'מנטה',
Url: 'http://www.ynet.co.il/home/0,7340,L-1209,00.html',
childItems: []
}
]
},
{
Title: 'English',
Url: 'http://www.ynetnews.com/home/0,7340,L-3083,00.html',
childItems: []
}
]
},
{
Title: 'קבוצת שוקן',
Url: '',
childItems: [{
Title: 'הארץ',
Url: 'http://www.haaretz.co.il/',
childItems: []
},
{
Title: 'וואלה',
Url: 'http://www.walla.co.il/',
childItems: [{
Title: 'ספורט',
Url: 'http://sports.walla.co.il/',
childItems: []
},
{
Title: 'עסקים',
Url: 'http://finance.walla.co.il/',
childItems: []
},
{
Title: 'Timeout',
Url: 'http://timeout.co.il/',
childItems: []
}
]
}
]
}
];
})();
var productsItems = (function () {
return [{
Title: 'מכוניות',
Url: '',
childItems: [{
Title: 'יונדאי',
Url: '',
childItems: [{
Title: 'יונדאי סנטה פה',
Url: 'http://www.hyundaimotors.co.il/models/santafe/',
childItems: []
},
{
Title: 'יונדאי i10',
Url: 'http://www.hyundaimotors.co.il/models/i10/',
childItems: []
}
]
},
{
Title: 'למבורגיני',
Url: 'http://www.lamborghini.com/en/home/',
childItems: []
},
{
Title: 'פולקסוואגן',
Url: 'http://www.vw.co.il/',
childItems: [{
Title: 'פולו',
Url: 'http://www.vw.co.il/models/%D7%94%D7%A4%D7%95%D7%9C%D7%95-%D7%94%D7%97%D7%93%D7%A9%D7%94/%D7%91%D7%99%D7%A6%D7%95%D7%A2%D7%99%D7%9D/',
childItems: []
},
{
Title: 'גולף',
Url: 'http://www.vw.co.il/models/sv/%D7%9E%D7%90%D7%A4%D7%99%D7%99%D7%A0%D7%99%D7%9D-%D7%95%D7%91%D7%99%D7%A6%D7%95%D7%A2%D7%99%D7%9D/',
childItems: []
},
]
}
]
},
{
Title: 'מוצרי חשמל',
Url: '',
childItems: [{
Title: 'אבי סופר',
Url: 'http://www.soferavi.co.il/',
childItems: []
},
{
Title: 'טרקלין חשמל',
Url: 'http://www.traklin.co.il/',
childItems: []
}
]
}
];
})();
var newsPopupMenu = new popupMenu(newsItems, 'news_popup_menu', 'newsMenuItem', 150, true);
var productsPopupMenu = new popupMenu(productsItems, 'products_popup_menu', 'productsMenuItem', 100);
})
// popupMenu class
var popupMenu = function (popupItems, popupMenuIdInitial, topMenuItemId, itemWidth, openInSameWindow) {
this.popupItems = popupItems;
this.popupMenuIdInitial = popupMenuIdInitial;
this.topMenuItemId = topMenuItemId;
this.itemWidth = itemWidth;
this.openInSameWindow = (openInSameWindow != null ? openInSameWindow : false);
this.menuitem_hover();
this.topmenuitem_hover();
this.invoke_popup_hover();
this.menulink_click();
this.document_click();
};
popupMenu.prototype = (function () {
return {
hidePopupMenu: function (itemHierarchy) {
var self = this;
var items = self.findItems(self.popupItems, itemHierarchy);
$(items).each(function (indx, item) {
self.hidePopupMenu(itemHierarchy + '_' + indx);
});
$('#' + self.popupMenuIdInitial + itemHierarchy).hide();
},
findItems: function (items, relativeHierarchy) {
var self = this;
if (relativeHierarchy.indexOf('_') != -1) {
var indx = Number(relativeHierarchy.match(/^\d+(?=\_)/)[0]);
return self.findItems(items[indx].childItems, relativeHierarchy.replace(/^\d+\_/, ''));
}
else
return items[Number(relativeHierarchy)].childItems;
},
getMenuItemWidth: function () {
return (this.itemWidth != null ? this.itemWidth : $('#' + this.topMenuItemId).width());
},
menuitem_hover: function () {
var self = this;
$('.menuitem').live('hover', function (event) {
if ($(event.target).closest('#' + self.topMenuItemId).length == 0 && $(event.target).closest('[id^="' + self.popupMenuIdInitial + '"]').length == 0) {
$('[id^="' + self.popupMenuIdInitial + '"]').hide();
return;
}
if ($(event.target).closest('#' + self.topMenuItemId).length > 0)
return;
if ($(event.target).attr('item-hierarchy').indexOf('_') != -1) {
var parentHierarchy = $(event.target).attr('item-hierarchy').replace(/\_\d+$/, '');
var menuItemIndx = Number($(event.target).attr('item-hierarchy').match(/(?:\_)\d+$/, '')[0].replace('_',''));
var items = self.findItems(self.popupItems, parentHierarchy);
$(items).each(function (indx, item) {
if (indx == menuItemIndx) return true;
self.hidePopupMenu(parentHierarchy + '_' + indx);
});
}
else {
var menuItemIndx = Number($(event.target).attr('item-hierarchy'));
$(self.popupItems).each(function (indx, item) {
if (indx == menuItemIndx) return true;
self.hidePopupMenu(String(indx));
});
}
});
},
topmenuitem_hover: function () {
var self = this;
var $topMenuItem = $('#' + self.topMenuItemId);
$topMenuItem.hover(function () {
var $el = $('#' + self.popupMenuIdInitial);
if ($el.length == 0) {
$el = $(handlebarsCompile($('#popup-menu-template'), { items: self.popupItems, parentInitial: '', popupMenuIdInitial: self.popupMenuIdInitial }));
$el.appendTo('body')
$el.css({ top: $topMenuItem.position().top + $topMenuItem.height(), left: $topMenuItem.position().left, width: self.getMenuItemWidth() + 5 });
}
$el.show();
})
},
invoke_popup_hover: function () {
var self = this;
var $topMenuItem = $('#' + self.topMenuItemId);
$('[id^="' + self.popupMenuIdInitial + '"] .menuitem.invoke_popup').live('hover', function () {
var $menuitem = $(this);
var itemHierarchy = $menuitem.attr('item-hierarchy');
var $el = $('#' + self.popupMenuIdInitial + itemHierarchy);
if ($el.length == 0) {
var items = self.findItems(self.popupItems, itemHierarchy);
$el = $(handlebarsCompile($('#popup-menu-template'), { items: items, parentInitial: itemHierarchy, popupMenuIdInitial: self.popupMenuIdInitial }));
$el.appendTo('body')
$el.css({ top: $menuitem.offset().top, left: $menuitem.offset().left - self.getMenuItemWidth(), width: self.getMenuItemWidth() });
}
$el.show();
})
},
menulink_click: function () {
var self = this;
$('[id^="' + self.popupMenuIdInitial + '"] .menulink').live('click', function () {
var href = $(this).attr('href-data');
if (!/^(https?\:\/\/|www\.)/.test(href)) href = (!/^[\/\\]/.test(href) ? '/' : '') + href;
window.open(href, self.openInSameWindow ? '_self' : '_blank');
});
},
document_click: function () {
var self = this;
$(document).click(function (event) {
if ($(event.target).closest('#' + self.topMenuItemId).length == 0 && $(event.target).closest('[id^="' + self.popupMenuIdInitial + '"]').length == 0)
$('[id^="' + self.popupMenuIdInitial + '"]').hide();
})
}
}
})();
// handlebars
var handlebarsCompile = function ($mv_chat_template, context) {
var source = $mv_chat_template.html();
var template = Handlebars.compile(source);
return template(context);
};
function IsURL(url) {
var strRegex = "^((https|http)?://)"
+ "([0-9a-z_!~*'()-]+\.)*" // www.
+ "([0-9a-z][0-9a-z-]{0,61})?([0-9a-z]\."
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re = new RegExp(strRegex);
return re.test(url);
}