aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/resources/META-INF/resources/designer/lib/angularjs-dropdown-multiselect-old.js
blob: 7a67bae001ea6d54bd82f47aa8e8725fcef4b16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
'use strict';

var directiveModule = angular.module('angularjs-dropdown-multiselect-old', ['ngRoute', 
                                                                        'ngResource',                                                                        
                                                                        'hljs',
                                                                        'ui.bootstrap',
                                                                        'angular-loading-bar', 
                                                                        'ngAnimate',
                                                                        'dialogs.main',
                                                                        'ui.grid', 
                                                                        'ui.grid.resizeColumns',
                                                                        'ui.grid.paging',
                                                                        'ui.grid.selection',
                                                                        'ui.grid.cellNav',
                                                                        'ui.grid.pinning',
                                                                        'ngSanitize','vs-repeat']);

directiveModule.directive('ngDropdownMultiselectOld', ['$filter', '$document', '$compile', '$parse','$rootScope', '$resource', '$http','$location',
    function ($filter, $document, $compile, $parse, $rootScope, $resource, $http,$location) 
    {
	
		//console.log($http);

        return {
            restrict: 'AE',
            scope: {
                selectedModel: '=',
                options: '=',
                extraSettings: '=',
                events: '=',
                searchFilter: '=?',
                translationTexts: '=',
                groupBy: '@'
            },
            template: function (element, attrs) 
            {
                var checkboxes = attrs.checkboxes ? true : false;
                var groups = attrs.groupBy ? true : false;

                var template = '<div class="multiselect-parent btn-group dropdown-multiselect" style="width:100%;">';
                template += '<div style="width:98%;" class="dropdown-toggle" ng-class="settings.buttonClasses" ng-click="toggleDropdown()">{{getButtonText()}}&nbsp;<span class="caret"></span></div>';
                template += '<ul class="dropdown-menu dropdown-menu-form" ng-style="{display: open ? \'block\' : \'none\', height : settings.scrollable ? settings.scrollableHeight : \'auto\' }" style="overflow: scroll; width:280px;" >';
                template += '<li ng-show="settings.enableSearch"><div class="dropdown-header"><input type="text"  class="form-control" style="width: 100%;" ng-model="searchFilter"  ng-focus="this"  placeholder="{{texts.searchPlaceholder}}" /></li>';
                template += '<li ng-show="settings.enableSearch" class="divider"></li>';
                	
                template += '<div>';
                
                if (groups) {
                	
                    template += '<li ng-repeat-start="option in orderedItems | filter: searchFilter" ng-show="getPropertyForObject(option, settings.groupBy) !== getPropertyForObject(orderedItems[$index - 1], settings.groupBy)" role="presentation" class="dropdown-header1">{{ getGroupTitle(getPropertyForObject(option, settings.groupBy)) }}</li>';
                    template += '<li ng-repeat-end role="presentation">';
                } else {
                    template += '<li role="presentation" ng-repeat="option in options| filter: searchFilter">';
                }

                template += '<a role="menuitem" tabindex="-1" ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))">';

                if (checkboxes) {
                    template += '<div class="checkbox"><label><input class="checkboxInput" type="checkbox" ng-click="checkboxClick($event, getPropertyForObject(option,settings.idProp))" ng-checked="isChecked(getPropertyForObject(option,settings.idProp))" /> {{getPropertyForObject(option, settings.displayProp)}}</label></div></a>';
                } else {
                    template += '<span data-ng-class="{\'glyphicon glyphicon-ok\': isChecked(getPropertyForObject(option,settings.idProp))}"></span> {{getPropertyForObject(option, settings.displayProp)}}</a>';
                }

                template += '</li>';
                template += '</div>';
                
                template += '<li ng-hide="(!settings.showCheckAll) && !settings.showUncheckAll" class="divider"></li>';
                template += '<li ng-hide="!settings.showCheckAll"><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> {{texts.checkAll}}</a>';
                template += '<li ng-show="settings.showUncheckAll"><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> {{texts.uncheckAll}}</a></li>';

                template += '<li class="divider" ng-show="settings.selectionLimit > 1"></li>';
                template += '<li role="presentation" ng-show="settings.selectionLimit > 1"><a role="menuitem">{{selectedModel.length}} {{texts.selectionOf}} {{settings.selectionLimit}} {{texts.selectionCount}}</a></li>';

                template += '</ul>';
                template += '</div>';

                element.html(template);
            },
            link: function ($scope, $element, $attrs) 
            {
                var $dropdownTrigger = $element.children()[0];
            
               
                $scope.toggleDropdown = function () {
                    $scope.open = !$scope.open;
                   
                   
                    
                    
                };

                $scope.checkboxClick = function ($event, id) {
                    $scope.setSelectedItem(id);
                    $event.stopImmediatePropagation();
                };

                $scope.externalEvents = 
                {
                    onItemSelect: angular.noop,
                    onItemDeselect: angular.noop,
                    onSelectAll: angular.noop,
                    onDeselectAll: angular.noop,
                    onInitDone: angular.noop,
                    onMaxSelectionReached: angular.noop
                };

                $scope.settings = {
                    dynamicTitle: true,
                    scrollable: false,
                    scrollableHeight: '300px',
                    closeOnBlur: true,
                    displayProp: 'id',
                    idProp: 'id',
                    externalIdProp: 'id',
                    enableSearch: false,
                    selectionLimit: 0,
                    showCheckAll: true,
                    showUncheckAll: true,
                    closeOnSelect: false,
                    buttonClasses: 'btn btn-default',
                    closeOnDeselect: false,
                    groupBy: $attrs.groupBy || undefined,
                    groupByTextProvider: null,
                    smartButtonMaxItems: 0,
                    smartButtonTextConverter: angular.noop
                };

                $scope.texts = {
                    checkAll: 'Check All',
                    uncheckAll: 'Uncheck All',
                    selectionCount: 'checked',
                    selectionOf: '/',
                    searchPlaceholder: 'Search...',
                    buttonDefaultText: 'Select',
                    dynamicButtonTextSuffix: 'checked'
                };

                $scope.searchFilter = $scope.searchFilter || '';

                if (angular.isDefined($scope.settings.groupBy)) 
                {
                    $scope.$watch('options', function (newValue)
                    {
                        if (angular.isDefined(newValue)) 
                        {
                            $scope.orderedItems = $filter('orderBy')(newValue, $scope.settings.groupBy);
                        }
                    });
                }

                angular.extend($scope.settings, $scope.extraSettings || []);
                angular.extend($scope.externalEvents, $scope.events || []);
                angular.extend($scope.texts, $scope.translationTexts);

                $scope.singleSelection = $scope.settings.selectionLimit === 1;

                function getFindObj(id) 
                {
                    var findObj = {};

                    if ($scope.settings.externalIdProp === '') 
                    {
                        findObj[$scope.settings.idProp] = id;
                    } 
                    else 
                    {
                        findObj[$scope.settings.externalIdProp] = id;
                    }

                    return findObj;
                }

                function clearObject(object) 
                {
                    for (var prop in object) 
                    {
                        delete object[prop];
                    }
                }

                if ($scope.singleSelection) 
                {
                    if (angular.isArray($scope.selectedModel) && $scope.selectedModel.length === 0) 
                    {
                        clearObject($scope.selectedModel);
                    }
                }

                if ($scope.settings.closeOnBlur) 
                {
                    $document.on('click', function (e) 
                    {
                    	var target = e.target.parentElement;
                        var parentFound = false;

                        while (angular.isDefined(target) && target !== null && !parentFound) 
                        {
                        	try
                        	{
                        		if (_.contains(target.className.split(' '), 'multiselect-parent') && !parentFound)
                                    {
                                        if(target === $dropdownTrigger) 
                                        {
                                            parentFound = true;
                                        }
                                    }
                                    
                                    target = target.parentElement;                            	  
                        		
                        	}catch(e){break;}
                        	
                        }

                        if (!parentFound) {
                            $scope.$apply(function () {
                                $scope.open = false;
                            });
                        }
                    });
                }

                $scope.getGroupTitle = function (groupValue) {
                    if ($scope.settings.groupByTextProvider !== null) {
                        return $scope.settings.groupByTextProvider(groupValue);
                    }

                    return groupValue;
                };

                $scope.getButtonText = function () {
                    if ($scope.settings.dynamicTitle && ($scope.selectedModel.length > 0 || (angular.isObject($scope.selectedModel) && _.keys($scope.selectedModel).length > 0))) {
                        if ($scope.settings.smartButtonMaxItems > 0) {
                            var itemsText = [];

                            angular.forEach($scope.options, function (optionItem) {
                                if ($scope.isChecked($scope.getPropertyForObject(optionItem, $scope.settings.idProp))) {
                                    var displayText = $scope.getPropertyForObject(optionItem, $scope.settings.displayProp);
                                    var converterResponse = $scope.settings.smartButtonTextConverter(displayText, optionItem);

                                    itemsText.push(converterResponse ? converterResponse : displayText);
                                }
                            });

                            if ($scope.selectedModel.length > $scope.settings.smartButtonMaxItems) {
                                itemsText = itemsText.slice(0, $scope.settings.smartButtonMaxItems);
                                itemsText.push('...');
                            }

                            return itemsText.join(', ');
                        } else {
                            var totalSelected;

                            if ($scope.singleSelection) {
                                totalSelected = ($scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp])) ? 1 : 0;
                            } else {
                                totalSelected = angular.isDefined($scope.selectedModel) ? $scope.selectedModel.length : 0;
                            }

                            if (totalSelected === 0) {
                                return $scope.texts.buttonDefaultText;
                            } else {
                                return totalSelected + ' ' + $scope.texts.dynamicButtonTextSuffix;
                            }
                        }
                    } else {
                        return $scope.texts.buttonDefaultText;
                    }
                };

                $scope.getPropertyForObject = function (object, property)
                {
                    if (angular.isDefined(object) && object.hasOwnProperty(property)) {
                        return object[property];
                    }

                    return '';
                };

                $scope.selectAll = function () 
                {
                	$scope.deselectAll(false,true);
                    $scope.externalEvents.onSelectAll();
                    
                    var len = $scope.selectedModel.length;
                    
                    angular.forEach($scope.options, function (value) 
                    {
                    	if(value[$scope.settings.idProp]=="All")
                		{
                    		if(len > 1)
                            {
                    			$scope.setSelectedItem(value[$scope.settings.idProp], true, true);
                            }
                            else
                        	{
                            	$scope.setSelectedItem(value[$scope.settings.idProp], true, true);
                            }
                		}
                    });
                };

                $scope.deselectAll = function (sendEvent,ignore) 
                {
                	var len = $scope.selectedModel.length;
                	
                	sendEvent = sendEvent || true;

                    if (sendEvent) 
                    {
                        $scope.externalEvents.onDeselectAll();
                    }

                    if ($scope.singleSelection) 
                    {
                        clearObject($scope.selectedModel);
                    } 
                    else 
                    {
                        $scope.selectedModel.splice(0, $scope.selectedModel.length);
                    }                    
                    
                    if(ignore!=true || ignore==undefined)
                    {
	                    if(len > 1)
	                    {
	                    	$scope.setSelectedItem("All", true, true);
	                    }
	                    else
	                	{
	                    	$scope.setSelectedItem("All", true, true);
	                    } 
                    }
                    
                    
                };

                $scope.setSelectedItem = function (id, dontRemove, refresh) 
                {
                	var findObj = getFindObj(id);
                    var finalObj = null;

                    if ($scope.settings.externalIdProp === '') 
                    {
                        finalObj = _.find($scope.options, findObj);
                    } 
                    else 
                    {
                        finalObj = findObj;
                    }
                    
                    
                    if ($scope.singleSelection) 
                    {
                        clearObject($scope.selectedModel);
                        angular.extend($scope.selectedModel, finalObj);
                        $scope.externalEvents.onItemSelect(finalObj);
                        
                        if ($scope.settings.closeOnSelect) $scope.open = false;
                        
                        return;
                    }

                    dontRemove = dontRemove || false;
                    

                    var exists = _.findIndex($scope.selectedModel, findObj) !== -1;

                    if (!dontRemove && exists) {
                        $scope.selectedModel.splice(_.findIndex($scope.selectedModel, findObj), 1);
                        $scope.externalEvents.onItemDeselect(findObj);
                    } else if (!exists && ($scope.settings.selectionLimit === 0 || $scope.selectedModel.length < $scope.settings.selectionLimit)) {
                        $scope.selectedModel.push(finalObj);
                        $scope.externalEvents.onItemSelect(finalObj);
                    }
                    
                    if ($scope.settings.closeOnSelect) $scope.open = false;
                    
                    if(refresh || refresh==undefined)
                	{
                    	 if("/dashboard"==$location.path())
	               		 {
                    		$rootScope.ReLoadDashboardComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	 
	               		  }
	               		  else if("/api_portfolio"==$location.path())
	               		  {
	               			$rootScope.ReLoadAPIPortfolioComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);
	               		  }
	               		  else if("/project_portfolio"==$location.path())
	               		  {
	               			$rootScope.ReLoadProjectPortFolioComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);
	               		  }
	               		  else if("/installed_bundle_versions"==$location.path())
	               		  {
	               			$rootScope.ReLoadInstalledBundleComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	
	               		  }else if("/adapter_inventory"==$location.path())
	               		  {
		               			$rootScope.ReLoadAdapterInventoryComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	
		               	  }
	               		  else if("/api_taxonomy"==$location.path())
	               		  {
		               			$rootScope.ReLoadAPITaxonomyComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	
		               	  }else if("/defects"==$location.path())
	               		  {
		               			$rootScope.ReLoadDefectReportComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	
		               	  } else if("/environment_configurations"==$location.path())
	               		  {
		               			$rootScope.ReLoadEnvConfigComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	
		               	  }
			               	else if("/federated_qc_coverage"==$location.path()) 	  {
		               			$rootScope.ReLoadQcCodeCoverageComboBox($scope.translationTexts.buttonDefaultText,finalObj.id);	
		               	  }
                  	 
                    	 
                    	 
                    	 
                    	 
                    	
                	}
                    
                };

                $scope.isChecked = function (id) {
                    if ($scope.singleSelection) {
                        return $scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp]) && $scope.selectedModel[$scope.settings.idProp] === getFindObj(id)[$scope.settings.idProp];
                    }

                    return _.findIndex($scope.selectedModel, getFindObj(id)) !== -1;
                };

                $scope.externalEvents.onInitDone();
            }
        };
}]);