[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / directives / multiple-select / multiple-select2.directive.js
diff --git a/ecomp-portal-FE-common/client/app/directives/multiple-select/multiple-select2.directive.js b/ecomp-portal-FE-common/client/app/directives/multiple-select/multiple-select2.directive.js
new file mode 100644 (file)
index 0000000..e047163
--- /dev/null
@@ -0,0 +1,113 @@
+/*-\r
+ * ================================================================================\r
+ * ECOMP Portal\r
+ * ================================================================================\r
+ * Copyright (C) 2017 AT&T Intellectual Property\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ================================================================================\r
+ */\r
+/**\r
+ * Created by nnaffar on 12/21/15.+\r
+ */\r
+angular.module('ecompApp')\r
+    .directive('multipleSelect2', function ($window) {\r
+        return {\r
+            restrict: 'E',\r
+            templateUrl: 'app/directives/multiple-select/multiple-select2.tpl.html',\r
+            scope: {\r
+                onChange: '&',\r
+                nameAttr: '@',\r
+                valueAttr: '@',\r
+                ngModel: '=',\r
+                placeholder: '@',\r
+                uniqueData: '@?',\r
+                onDropdownClose: '&?'\r
+            },\r
+            link: function(scope, elm, attrs){\r
+                scope.isExpanded = false;\r
+\r
+                scope.isDisabled = !scope.ngModel || !scope.ngModel.length;\r
+                scope.$watch('ngModel', function(newVal){\r
+                    scope.isDisabled = !newVal || !newVal.length;\r
+                });\r
+\r
+\r
+                let startListening = () => {\r
+                    console.log('listening on $window!');\r
+                    angular.element($window).on('click', function () {\r
+                        stopListening();\r
+                    });\r
+\r
+                    angular.element('multiple-select2').on('click', function(e) {\r
+                        if($(e.target).closest('multiple-select2')[0].attributes['unique-data'].value === attrs.uniqueData){\r
+                            console.log('ignored that..:', attrs.uniqueData);\r
+                            e.stopPropagation();\r
+                        }else{\r
+                            console.log('shouldnt ignore, close expanded!:', attrs.uniqueData);\r
+                            scope.isExpanded = false;\r
+                            scope.$applyAsync();\r
+                        }\r
+                    });\r
+                };\r
+\r
+                let stopListening = function() {\r
+                    if(scope.onDropdownClose){\r
+                        scope.onDropdownClose();\r
+                    }\r
+                    scope.isExpanded = false;\r
+                    scope.$applyAsync();\r
+                    console.log('stop listening on $window and multiple-element!');\r
+                    angular.element($window).off('click');\r
+                    angular.element('multiple-select2').off('click');\r
+                };\r
+\r
+                scope.showCheckboxes = function(){\r
+                    scope.isExpanded = !scope.isExpanded;\r
+                    if(scope.isExpanded){\r
+                        startListening();\r
+                    }else{\r
+                        stopListening();\r
+                        if(scope.onDropdownClose){\r
+                            scope.onDropdownClose();\r
+                        }\r
+                    }\r
+                };\r
+\r
+                scope.onCheckboxClicked = function() {\r
+                    console.log('checkbox clicked; unique data: ',attrs.uniqueData);\r
+                    if(scope.onChange) {\r
+                        scope.onChange();\r
+                    }\r
+                }\r
+\r
+                scope.getTitle = function(){\r
+                    var disp = '';\r
+                    if(!scope.ngModel || !scope.ngModel.length) {\r
+                        return disp;\r
+                    }\r
+                    scope.ngModel.forEach(function(item){\r
+                        if(item[scope.valueAttr]){\r
+                            disp+=item[scope.nameAttr] + ',';\r
+                        }\r
+                    });\r
+                    if(disp!==''){\r
+                        disp = disp.slice(0,disp.length-1);\r
+                    }else{\r
+                        disp = scope.placeholder;\r
+                    }\r
+                    return disp;\r
+                };\r
+            }\r
+        };\r
+    });\r