6fc13e8d26de85902ddf93571998df92c70c7ec2
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 (function( app ) {
17
18         var ui = app.ns("ui");
19
20         ui.MenuPanel = ui.AbstractPanel.extend({
21                 defaults: {
22                         items: [],              // (required) an array of menu items
23                         modal: false
24                 },
25                 _baseCls: "uiMenuPanel",
26                 init: function() {
27                         this._super();
28                         this.el = $(this._main_template());
29                 },
30                 open: function(jEv) {
31                         this._super(jEv);
32                         var cx = this; setTimeout(function() { $(document).bind("click", cx._close_handler); }, 50);
33                 },
34                 _getItems: function() {
35                         return this.config.items;
36                 },
37                 _close_handler: function(jEv) {
38                         this._super(jEv);
39                         $(document).unbind("click", this._close_handler);
40                 },
41                 _main_template: function() {
42                         return { tag: "DIV", cls: this._baseCls, children: this._getItems().map(this._menuItem_template, this) };
43                 },
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 ) ] };
47                 },
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 )
54                                 .asOffset();
55                 }
56         });
57
58 })( this.app );