2 * Copyright 2010-2013 Ben Birch
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this software except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 var ui = app.ns("ui");
20 ui.MenuPanel = ui.AbstractPanel.extend({
22 items: [], // (required) an array of menu items
25 _baseCls: "uiMenuPanel",
28 this.el = $(this._main_template());
32 var cx = this; setTimeout(function() { $(document).bind("click", cx._close_handler); }, 50);
34 _getItems: function() {
35 return this.config.items;
37 _close_handler: function(jEv) {
39 $(document).unbind("click", this._close_handler);
41 _main_template: function() {
42 return { tag: "DIV", cls: this._baseCls, children: this._getItems().map(this._menuItem_template, this) };
44 _menuItem_template: function(item) {
45 var dx = item.disabled ? { onclick: function() {} } : {};
46 return { tag: "LI", cls: "uiMenuPanel-item" + (item.disabled ? " disabled" : "") + (item.selected ? " selected" : ""), children: [ $.extend({ tag: "DIV", cls: "uiMenuPanel-label" }, item, dx ) ] };
48 _getPosition: function(jEv) {
49 var right = !! $(jEv.target).parents(".pull-right").length;
50 var parent = $(jEv.target).closest("BUTTON");
51 return parent.vOffset()
52 .addY(parent.vSize().y)
53 .addX( right ? parent.vSize().x - this.el.vOuterSize().x : 0 )