2 * Copyright 2014 IBM Corp.
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
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.
19 RED.menu = (function() {
23 function createMenuItem(opt) {
26 item = $('<li class="divider"></li>');
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> ':'')+
33 '</a>').appendTo(item);
35 menuItems[opt.id] = opt;
38 link.click(function() {
39 if ($(this).parent().hasClass("disabled")) {
43 setSelected(opt.id,!isSelected(opt.id));
45 opt.onselect.call(opt);
48 } else if (opt.href) {
49 link.attr("target","_blank").attr("href",opt.href);
52 item.addClass("dropdown-submenu pull-left");
53 var submenu = $('<ul id="'+opt.id+'-submenu" class="dropdown-menu"></ul>').appendTo(item);
55 for (var i=0;i<opt.options.length;i++) {
56 createMenuItem(opt.options[i]).appendTo(submenu);
60 item.addClass("disabled");
68 function createMenu(options) {
70 var button = $("#"+options.id);
72 var topMenu = $("<ul/>",{class:"dropdown-menu"}).insertAfter(button);
74 for (var i=0;i<options.options.length;i++) {
75 var opt = options.options[i];
76 createMenuItem(opt).appendTo(topMenu);
80 function isSelected(id) {
81 return $("#"+id).hasClass("active");
83 function setSelected(id,state) {
84 if (isSelected(id) == state) {
87 var opt = menuItems[id];
89 $("#"+id).addClass("active");
91 $("#"+id).removeClass("active");
94 opt.onselect.call(opt,state);
98 function setDisabled(id,state) {
100 $("#"+id).parent().addClass("disabled");
102 $("#"+id).parent().removeClass("disabled");
106 function addItem(id,opt) {
107 createMenuItem(opt).appendTo("#"+id+"-submenu");
109 function removeItem(id) {
110 $("#"+id).parent().remove();
115 setSelected: setSelected,
116 isSelected: isSelected,
117 setDisabled: setDisabled,
119 removeItem: removeItem
120 //TODO: add an api for replacing a submenu - see library.js:loadFlowLibrary