nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / functionalMenu / jqTreeContextMenu.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 (function ($) {
21         if (!$.fn.tree) {
22                 throw "Error jqTree is not loaded.";
23         }
24
25         $.fn.jqTreeContextMenu = function (menuElement, callbacks) {
26
27                 var self = this;
28                 var $el = this;
29
30                 var $menuEl = menuElement;
31                 
32                 var nodeToDisabledMenuItems = {};
33                 
34                 $menuEl.hide();
35
36                 $el.bind("contextmenu", function (e) {
37                         e.preventDefault();
38                         return false; 
39                 });
40
41                 $el.bind('tree.contextmenu', function (event) {
42                         var x = event.click_event.pageX;
43                         var y = event.click_event.pageY;
44                         var yPadding = 5;
45                         var xPadding = 5;
46                         var menuHeight = $menuEl.height();
47                         var menuWidth = $menuEl.width();
48                         var windowHeight = $(window).height();
49                         var windowWidth = $(window).width();
50                         
51                         if (menuHeight + y + yPadding > windowHeight) {
52                                 y = y - menuHeight;
53                         }
54                         if (menuWidth + x + xPadding > windowWidth) {
55                                 x = x - menuWidth;
56                         }
57
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');
65                                         } else {
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');
73                                                         }
74                                                 });     
75                                         }
76                                 } else {
77                                         $menuEl.find('li.disabled').removeClass('disabled');
78                                 }
79                         }
80
81                         $menuEl.show();
82
83                         $menuEl.offset({ left: x, top: y });
84
85                         var dismissContextMenu = function () {
86                                 $(document).unbind('click.jqtreecontextmenu');
87                                 $el.unbind('tree.click.jqtreecontextmenu');
88                                 $menuEl.hide();
89                         }
90
91                         $(document).bind('click.jqtreecontextmenu', function () {
92                                 dismissContextMenu();
93                         });
94
95                         $el.bind('tree.click.jqtreecontextmenu', function (e) {
96                                 dismissContextMenu();
97                         });
98
99                         var selectedNode = $el.tree('getSelectedNode');
100                         if (selectedNode !== event.node) {
101                                 $el.tree('selectNode', event.node);
102                         }
103
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];
113                                         if (callbackFn) {
114                                                 callbackFn(event.node);
115                                         }
116                                         return false;
117                                 });
118                         }
119                 });
120                 
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') {
129                                         return;
130                                 }
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');
137                                         }
138                                 });
139                                 nodeToDisabledMenuItems = {};
140                         } else if (arguments.length === 2) {
141                                 var nodeName = arguments[0];
142                                 var items = arguments[1];
143                                 nodeToDisabledMenuItems[nodeName] = items;
144                         }
145                 };
146
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') {
154                                         return;
155                                 }
156                                 
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');
162                                         }
163                                 });
164
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];
171                                 } else {
172                                         var disabledItems = nodeToDisabledMenuItems[nodeName];
173                                         for (var i = 0; i < items.length; i++) {
174                                                 var idx = disabledItems.indexOf(items[i]);
175                                                 if (idx > -1) {
176                                                         disabledItems.splice(idx, 1);
177                                                 }
178                                         }
179                                         if (disabledItems.length === 0) {
180                                                 delete nodeToDisabledMenuItems[nodeName];
181                                         } else {
182                                                 nodeToDisabledMenuItems[nodeName] = disabledItems;      
183                                         }
184                                 }
185                                 if (Object.keys(nodeToDisabledMenuItems).length === 0) {
186                                         $menuEl.find('li.disabled').removeClass('disabled');
187                                 }
188                         }
189                 };
190                 return this;
191         };
192 } (jQuery));