/
menupalette.js
62 lines (48 loc) · 1.91 KB
/
menupalette.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
define(["sugar-web/graphics/palette",
"mustache"], function (palette, mustache) {
'use strict';
var menupalette = {};
menupalette.MenuPalette = function (invoker, primaryText, menuData) {
palette.Palette.call(this, invoker, primaryText);
this.selectItemEvent = document.createEvent("CustomEvent");
this.selectItemEvent.initCustomEvent('selectItem', true, true, {
'item': undefined
});
var template = '{{#.}}' +
'<li>' +
' <button {{ #icon }}class="icon"{{ /icon }} {{ #id }}id="{{ id }}"{{ /id }}>' +
' {{ #icon }}<span></span>{{ /icon }}' +
' {{ label }}' +
' </button>' +
'</li>' +
'{{/.}}';
var menuElem = document.createElement('ul');
menuElem.className = "menu";
menuElem.innerHTML = mustache.render(template, menuData);
this.setContent([menuElem]);
// Pop-down the palette when a item in the menu is clicked.
this.buttons = menuElem.querySelectorAll('button');
var that = this;
function popDownOnButtonClick(event) {
that.selectItemEvent.detail.target = event.target;
that.getPalette().dispatchEvent(that.selectItemEvent);
that.popDown();
}
for (var i = 0; i < this.buttons.length; i++) {
this.buttons[i].addEventListener('click', popDownOnButtonClick);
}
};
var addEventListener = function (type, listener, useCapture) {
return this.getPalette().addEventListener(type, listener, useCapture);
};
menupalette.MenuPalette.prototype =
Object.create(palette.Palette.prototype, {
addEventListener: {
value: addEventListener,
enumerable: true,
configurable: true,
writable: true
}
});
return menupalette;
});