en.javascript.info/2-ui/5-widgets/5-custom-events/menu-callback.view/menu.js
Ilya Kantor 87bf53d076 update
2014-11-16 01:40:20 +03:00

59 lines
No EOL
1 KiB
JavaScript
Executable file

function Menu(options) {
var elem;
function getElem() {
if (!elem) render();
return elem;
}
function render() {
var elemHtml = options.template({title: options.title});
elem = $(elemHtml);
elem.on('mousedown selectstart', false);
elem.on('click', '.title', onTitleClick);
elem.on('click', 'a', onItemClick)
}
function renderItems() {
if (elem.find('ul').length) return;
var listHtml = options.listTemplate({items: options.items});
elem.append(listHtml);
}
function onItemClick(e) {
e.preventDefault();
var onselect = options.onselect;
if (onselect) {
onselect(e.currentTarget.getAttribute('href').slice(1));
}
}
function onTitleClick(e) {
toggle();
}
function open() {
renderItems();
elem.addClass('open');
};
function close() {
elem.removeClass('open');
};
function toggle() {
if (elem.hasClass('open')) close();
else open();
};
this.getElem = getElem;
this.toggle = toggle;
this.close = close;
this.open = open;
}