[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / public / red / ui / menu.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file 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  
17
18
19 RED.menu = (function() {
20     
21     var menuItems = {};
22     
23     function createMenuItem(opt) {
24         var item;
25         if (opt === null) {
26             item = $('<li class="divider"></li>');
27         } else {
28             item = $('<li></li>');
29             var link = $('<a '+(opt.id?'id="'+opt.id+'" ':'')+'tabindex="-1" href="#">'+
30                 (opt.toggle?'<i class="fa fa-check pull-right"></i>':'')+
31                 (opt.icon?'<i class="'+opt.icon+'"></i> ':'')+
32                 opt.label+
33                 '</a>').appendTo(item);
34                 
35             menuItems[opt.id] = opt;
36             
37             if (opt.onselect) {
38                 link.click(function() {
39                         if ($(this).parent().hasClass("disabled")) {
40                             return;
41                         }
42                         if (opt.toggle) {
43                             setSelected(opt.id,!isSelected(opt.id));
44                         } else {
45                             opt.onselect.call(opt);
46                         }
47                 })
48             } else if (opt.href) {
49                 link.attr("target","_blank").attr("href",opt.href);
50             }
51             if (opt.options) {
52                 item.addClass("dropdown-submenu pull-left");
53                 var submenu = $('<ul id="'+opt.id+'-submenu" class="dropdown-menu"></ul>').appendTo(item);
54                 
55                 for (var i=0;i<opt.options.length;i++) {
56                     createMenuItem(opt.options[i]).appendTo(submenu);
57                 }
58             }
59             if (opt.disabled) {
60                 item.addClass("disabled");
61             }
62         }
63         
64         
65         return item;
66         
67     }
68     function createMenu(options) {
69         
70         var button = $("#"+options.id);
71         
72         var topMenu = $("<ul/>",{class:"dropdown-menu"}).insertAfter(button);
73         
74         for (var i=0;i<options.options.length;i++) {
75             var opt = options.options[i];
76             createMenuItem(opt).appendTo(topMenu);
77         }
78     }
79     
80     function isSelected(id) {
81         return $("#"+id).hasClass("active");
82     }
83     function setSelected(id,state) {
84         if (isSelected(id) == state) {
85             return;
86         }
87         var opt = menuItems[id];
88         if (state) {
89             $("#"+id).addClass("active");
90         } else {
91             $("#"+id).removeClass("active");
92         }
93         if (opt.onselect) {
94             opt.onselect.call(opt,state);
95         }
96     }
97     
98     function setDisabled(id,state) {
99         if (state) {
100             $("#"+id).parent().addClass("disabled");
101         } else {
102             $("#"+id).parent().removeClass("disabled");
103         }
104     }
105     
106     function addItem(id,opt) {
107         createMenuItem(opt).appendTo("#"+id+"-submenu");
108     }
109     function removeItem(id) {
110         $("#"+id).parent().remove();
111     }
112     
113     return {
114         init: createMenu,
115         setSelected: setSelected,
116         isSelected: isSelected,
117         setDisabled: setDisabled,
118         addItem: addItem,
119         removeItem: removeItem
120         //TODO: add an api for replacing a submenu - see library.js:loadFlowLibrary
121     }
122 })();