2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
22 throw "Error jqTree is not loaded.";
25 $.fn.jqTreeContextMenu = function (menuElement, callbacks) {
30 var $menuEl = menuElement;
32 var nodeToDisabledMenuItems = {};
36 $el.bind("contextmenu", function (e) {
41 $el.bind('tree.contextmenu', function (event) {
42 var x = event.click_event.pageX;
43 var y = event.click_event.pageY;
46 var menuHeight = $menuEl.height();
47 var menuWidth = $menuEl.width();
48 var windowHeight = $(window).height();
49 var windowWidth = $(window).width();
51 if (menuHeight + y + yPadding > windowHeight) {
54 if (menuWidth + x + xPadding > windowWidth) {
58 if (Object.keys(nodeToDisabledMenuItems).length > 0) {
59 if (event.node.name in nodeToDisabledMenuItems) {
60 var nodeName = event.node.name;
61 var items = nodeToDisabledMenuItems[nodeName];
62 if (items.length === 0) {
63 $menuEl.find('li').addClass('disabled');
64 $menuEl.find('li > a').unbind('click');
66 $menuEl.find('li > a').each(function () {
67 $(this).closest('li').removeClass('disabled');
68 var hrefValue = $(this).attr('href');
69 var value = hrefValue.slice(hrefValue.indexOf("#") + 1, hrefValue.length)
70 if ($.inArray(value, items) > -1) {
71 $(this).closest('li').addClass('disabled');
72 $(this).unbind('click');
77 $menuEl.find('li.disabled').removeClass('disabled');
83 $menuEl.offset({ left: x, top: y });
85 var dismissContextMenu = function () {
86 $(document).unbind('click.jqtreecontextmenu');
87 $el.unbind('tree.click.jqtreecontextmenu');
91 $(document).bind('click.jqtreecontextmenu', function () {
95 $el.bind('tree.click.jqtreecontextmenu', function (e) {
99 var selectedNode = $el.tree('getSelectedNode');
100 if (selectedNode !== event.node) {
101 $el.tree('selectNode', event.node);
104 var menuItems = $menuEl.find('li:not(.disabled) a');
105 if (menuItems.length !== 0) {
106 menuItems.unbind('click');
107 menuItems.click(function (e) {
108 e.stopImmediatePropagation();
109 dismissContextMenu();
110 var hrefAnchor = e.currentTarget.attributes.href.nodeValue;
111 var funcKey = hrefAnchor.slice(hrefAnchor.indexOf("#") + 1, hrefAnchor.length)
112 var callbackFn = callbacks[funcKey];
114 callbackFn(event.node);
121 this.disable = function () {
122 if (arguments.length === 0) {
123 $menuEl.find('li:not(.disabled)').addClass('disabled');
124 $menuEl.find('li a').unbind('click');
125 nodeToDisabledMenuItems = {};
126 } else if (arguments.length === 1) {
127 var items = arguments[0];
128 if (typeof items !== 'object') {
131 $menuEl.find('li > a').each(function () {
132 var hrefValue = $(this).attr('href');
133 var value = hrefValue.slice(hrefValue.indexOf("#") + 1, hrefValue.length)
134 if ($.inArray(value, items) > -1) {
135 $(this).closest('li').addClass('disabled');
136 $(this).unbind('click');
139 nodeToDisabledMenuItems = {};
140 } else if (arguments.length === 2) {
141 var nodeName = arguments[0];
142 var items = arguments[1];
143 nodeToDisabledMenuItems[nodeName] = items;
147 this.enable = function () {
148 if (arguments.length === 0) {
149 $menuEl.find('li.disabled').removeClass('disabled');
150 nodeToDisabledMenuItems = {};
151 } else if (arguments.length === 1) {
152 var items = arguments[0];
153 if (typeof items !== 'object') {
157 $menuEl.find('li > a').each(function () {
158 var hrefValue = $(this).attr('href');
159 var value = hrefValue.slice(hrefValue.indexOf("#") + 1, hrefValue.length)
160 if ($.inArray(value, items) > -1) {
161 $(this).closest('li').removeClass('disabled');
165 nodeToDisabledMenuItems = {};
166 } else if (arguments.length === 2) {
167 var nodeName = arguments[0];
168 var items = arguments[1];
169 if (items.length === 0) {
170 delete nodeToDisabledMenuItems[nodeName];
172 var disabledItems = nodeToDisabledMenuItems[nodeName];
173 for (var i = 0; i < items.length; i++) {
174 var idx = disabledItems.indexOf(items[i]);
176 disabledItems.splice(idx, 1);
179 if (disabledItems.length === 0) {
180 delete nodeToDisabledMenuItems[nodeName];
182 nodeToDisabledMenuItems[nodeName] = disabledItems;
185 if (Object.keys(nodeToDisabledMenuItems).length === 0) {
186 $menuEl.find('li.disabled').removeClass('disabled');