Add DCAE MOD design tool project
[dcaegen2/platform.git] / mod / designtool / designtool-web / src / main / webapp / js / nf / canvas / controllers / nf-ng-breadcrumbs-controller.js
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Modifications to the original nifi code for the ONAP project are made
18  * available under the Apache License, Version 2.0
19  */
20
21 /* global define, module, require, exports */
22
23 (function (root, factory) {
24     if (typeof define === 'function' && define.amd) {
25         define(['jquery',
26                 'nf.Common'],
27             function ($, nfCommon) {
28                 return (nf.ng.BreadcrumbsCtrl = factory($, nfCommon));
29             });
30     } else if (typeof exports === 'object' && typeof module === 'object') {
31         module.exports = (nf.ng.BreadcrumbsCtrl =
32             factory(require('jquery'),
33                 require('nf.Common')));
34     } else {
35         nf.ng.BreadcrumbsCtrl = factory(root.$,
36             root.nf.Common);
37     }
38 }(this, function ($, nfCommon) {
39     'use strict';
40
41     return function (serviceProvider) {
42         'use strict';
43
44         function BreadcrumbsCtrl() {
45             this.breadcrumbs = [];
46         }
47
48         BreadcrumbsCtrl.prototype = {
49             constructor: BreadcrumbsCtrl,
50
51             /**
52              *  Register the breadcrumbs controller.
53              */
54             register: function () {
55                 if (serviceProvider.breadcrumbsCtrl === undefined) {
56                     serviceProvider.register('breadcrumbsCtrl', breadcrumbsCtrl);
57                 }
58             },
59
60             /**
61              * Generate the breadcrumbs.
62              *
63              * @param {object} breadcrumbEntity  The breadcrumb
64              */
65             generateBreadcrumbs: function (breadcrumbEntity) {
66                 var label = breadcrumbEntity.id;
67                 if (breadcrumbEntity.permissions.canRead) {
68                     label = breadcrumbEntity.breadcrumb.name;
69                 }
70
71                 this.breadcrumbs.unshift($.extend({
72                     'label': label
73                 }, breadcrumbEntity));
74
75                 if (nfCommon.isDefinedAndNotNull(breadcrumbEntity.parentBreadcrumb)) {
76                     this.generateBreadcrumbs(breadcrumbEntity.parentBreadcrumb);
77                 }
78             },
79
80             /**
81              * Updates the version control information for the specified process group.
82              *
83              * @param processGroupId
84              * @param versionControlInformation
85              */
86             updateVersionControlInformation: function (processGroupId, versionControlInformation) {
87                 $.each(this.breadcrumbs, function (_, breadcrumbEntity) {
88                     if (breadcrumbEntity.id === processGroupId) {
89                         breadcrumbEntity.breadcrumb.versionControlInformation = versionControlInformation;
90                         return false;
91                     }
92                 });
93             },
94
95             /**
96              * Reset the breadcrumbs.
97              */
98             resetBreadcrumbs: function () {
99                 this.breadcrumbs = [];
100             },
101
102             /**
103              * Whether this crumb is tracking.
104              *
105              * @param breadcrumbEntity
106              * @returns {*}
107              */
108             isTracking: function (breadcrumbEntity) {
109                 return nfCommon.isDefinedAndNotNull(breadcrumbEntity.versionedFlowState);
110             },
111
112             /**
113              * Returns the class string to use for the version control of the specified breadcrumb.
114              *
115              * @param breadcrumbEntity
116              * @returns {string}
117              */
118             getVersionControlClass: function (breadcrumbEntity) {
119                 if (nfCommon.isDefinedAndNotNull(breadcrumbEntity.versionedFlowState)) {
120                     var vciState = breadcrumbEntity.versionedFlowState;
121                     if (vciState === 'SYNC_FAILURE') {
122                              console.log("it is been sync failed..000");
123                                  $('#environmentType').prop('disabled', true);
124                                               if($('#environmentType').val() && !$('#environmentType').prop('disabled')){
125                                                      $('#operate-submit-btn').prop('disabled', false);
126                                               }else{$('#operate-submit-btn').prop('disabled', true);}
127
128                         return 'breadcrumb-version-control-gray fa fa-question'
129                     } else if (vciState === 'LOCALLY_MODIFIED_AND_STALE') {
130                        console.log("it is been locally modified and stale...000");
131                                       $('#environmentType').prop('disabled', true);
132                                             if($('#environmentType').val() && !$('#environmentType').prop('disabled')){
133                                                    $('#operate-submit-btn').prop('disabled', false);
134                                             }else{$('#operate-submit-btn').prop('disabled', true);}
135
136                         return 'breadcrumb-version-control-red fa fa-exclamation-circle';
137                     } else if (vciState === 'STALE') {
138                             console.log("it is been stale...000");
139                                         $('#environmentType').prop('disabled', true);
140                                             if($('#environmentType').val() && !$('#environmentType').prop('disabled')){
141                                                    $('#operate-submit-btn').prop('disabled', false);
142                                             }else{$('#operate-submit-btn').prop('disabled', true);}
143
144                         return 'breadcrumb-version-control-red fa fa-arrow-circle-up';
145                     } else if (vciState === 'LOCALLY_MODIFIED') {
146                             console.log("it is been locally modified...000");
147                             $('#environmentType').prop('disabled', true);
148                             if($('#environmentType').val() && !$('#environmentType').prop('disabled')){
149                                     $('#operate-submit-btn').prop('disabled', false);
150                             }else{$('#operate-submit-btn').prop('disabled', true);}
151
152                         return 'breadcrumb-version-control-gray fa fa-asterisk';
153                     } else {
154                              $('#environmentType').prop('disabled', false);
155                              if($('#environmentType').val() &&  !$('#environmentType').prop('disabled')){
156                                    $('#operate-submit-btn').prop('disabled', false);
157                              }else{$('#operate-submit-btn').prop('disabled', true);}
158                         return 'breadcrumb-version-control-green fa fa-check';
159                     }
160                 } else {
161                     console.log("it is NOT been version controlled...000");
162                     $('#environmentType').prop('disabled', true);
163                     return '';
164                 }
165             },
166
167             /**
168              * Gets the content for the version control tooltip for the specified breadcrumb.
169              *
170              * @param breadcrumbEntity
171              */
172             getVersionControlTooltip: function (breadcrumbEntity) {
173                 if (nfCommon.isDefinedAndNotNull(breadcrumbEntity.versionedFlowState) && breadcrumbEntity.permissions.canRead) {
174                     return nfCommon.getVersionControlTooltip(breadcrumbEntity.breadcrumb.versionControlInformation);
175                 } else {
176                     return 'This Process Group is not under version control.'
177                 }
178             },
179
180             /**
181              * Get the breadcrumbs.
182              */
183             getBreadcrumbs: function () {
184                 return this.breadcrumbs;
185             },
186
187             /**
188              * Update the breadcrumbs css.
189              *
190              * @param {object} style  The style to be applied.
191              */
192             updateBreadcrumbsCss: function (style) {
193                 $('#breadcrumbs').css(style);
194             },
195
196             /**
197              * Reset initial scroll position.
198              */
199             resetScrollPosition: function () {
200                 var title = $('#data-flow-title-container');
201                 var titlePosition = title.position();
202                 var titleWidth = title.outerWidth();
203                 var titleRight = titlePosition.left + titleWidth;
204
205                 var padding = $('#breadcrumbs-right-border').width();
206                 var viewport = $('#data-flow-title-viewport');
207                 var viewportWidth = viewport.width();
208                 var viewportRight = viewportWidth - padding;
209
210                 // if the title's right is past the viewport's right, shift accordingly
211                 if (titleRight > viewportRight) {
212                     // adjust the position
213                     title.css('left', (titlePosition.left - (titleRight - viewportRight)) + 'px');
214                 } else {
215                     title.css('left', '10px');
216                 }
217             },
218
219             /**
220              * Registers a scroll event on the `element`
221              *
222              * @param {object} element    The element event listener will be registered upon.
223              */
224             registerMouseWheelEvent: function (element) {
225                 // mousewheel -> IE, Chrome
226                 // DOMMouseScroll -> FF
227                 // wheel -> FF, IE
228
229                 // still having issues with this in IE :/
230                 element.on('DOMMouseScroll mousewheel', function (evt, d) {
231                     if (nfCommon.isUndefinedOrNull(evt.originalEvent)) {
232                         return;
233                     }
234
235                     var title = $('#data-flow-title-container');
236                     var titlePosition = title.position();
237                     var titleWidth = title.outerWidth();
238                     var titleRight = titlePosition.left + titleWidth;
239
240                     var padding = $('#breadcrumbs-right-border').width();
241                     var viewport = $('#data-flow-title-viewport');
242                     var viewportWidth = viewport.width();
243                     var viewportRight = viewportWidth - padding;
244
245                     // if the width of the title is larger than the viewport
246                     if (titleWidth > viewportWidth) {
247                         var adjust = false;
248
249                         var delta = 0;
250
251                         //Chrome and Safari both have evt.originalEvent.detail defined but
252                         //evt.originalEvent.wheelDelta holds the correct value so we must
253                         //check for evt.originalEvent.wheelDelta first!
254                         if (nfCommon.isDefinedAndNotNull(evt.originalEvent.wheelDelta)) {
255                             delta = evt.originalEvent.wheelDelta;
256                         } else if (nfCommon.isDefinedAndNotNull(evt.originalEvent.detail)) {
257                             delta = -evt.originalEvent.detail;
258                         }
259
260                         // determine the increment
261                         if (delta > 0 && titleRight > viewportRight) {
262                             var increment = -25;
263                             adjust = true;
264                         } else if (delta < 0 && (titlePosition.left - padding) < 0) {
265                             increment = 25;
266
267                             // don't shift too far
268                             if (titlePosition.left + increment > padding) {
269                                 increment = padding - titlePosition.left;
270                             }
271
272                             adjust = true;
273                         }
274
275                         if (adjust) {
276                             // adjust the position
277                             title.css('left', (titlePosition.left + increment) + 'px');
278                         }
279                     }
280                 });
281             }
282         }
283
284         var breadcrumbsCtrl = new BreadcrumbsCtrl();
285         breadcrumbsCtrl.register();
286         return breadcrumbsCtrl;
287     }
288 }));