aboutsummaryrefslogtreecommitdiffstats
path: root/ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2017-02-14 19:41:00 -0500
committerPamela Dragosh <pdragosh@research.att.com>2017-02-14 19:41:32 -0500
commit91d04c64771832a0b8815ffbe1f0f9920320d94d (patch)
treefb02d5e1c84a3d91def9a7ee95bc87f9c046cc96 /ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js
parentb9d4caa40ef8e3566ac475968bce17b9b64b6939 (diff)
Initial OpenECOMP policy/engine commit
Change-Id: I7dbff37733b661643dd4d1caefa3d7dccc361b6e Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js')
-rw-r--r--ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js b/ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js
new file mode 100644
index 000000000..09d8cefb4
--- /dev/null
+++ b/ecomp-sdk-app/src/main/webapp/app/fusion/external/ebz/angular_js/checklist-model.js
@@ -0,0 +1,99 @@
+/**
+ * Checklist-model
+ * AngularJS directive for list of checkboxes
+ */
+
+angular.module('checklist-model', [])
+.directive('checklistModel', ['$parse', '$compile', function($parse, $compile) {
+ // contains
+ function contains(arr, item) {
+ if (angular.isArray(arr)) {
+ for (var i = 0; i < arr.length; i++) {
+ if (angular.equals(arr[i], item)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ // add
+ function add(arr, item) {
+ arr = angular.isArray(arr) ? arr : [];
+ for (var i = 0; i < arr.length; i++) {
+ if (angular.equals(arr[i], item)) {
+ return arr;
+ }
+ }
+ arr.push(item);
+ return arr;
+ }
+
+ // remove
+ function remove(arr, item) {
+ if (angular.isArray(arr)) {
+ for (var i = 0; i < arr.length; i++) {
+ if (angular.equals(arr[i], item)) {
+ arr.splice(i, 1);
+ break;
+ }
+ }
+ }
+ return arr;
+ }
+
+ // http://stackoverflow.com/a/19228302/1458162
+ function postLinkFn(scope, elem, attrs) {
+ // compile with `ng-model` pointing to `checked`
+ $compile(elem)(scope);
+
+ // getter / setter for original model
+ var getter = $parse(attrs.checklistModel);
+ var setter = getter.assign;
+
+ // value added to list
+ var value = $parse(attrs.checklistValue)(scope.$parent);
+
+ // watch UI checked change
+ scope.$watch('checked', function(newValue, oldValue) {
+ if (newValue === oldValue) {
+ return;
+ }
+ var current = getter(scope.$parent);
+ if (newValue === true) {
+ setter(scope.$parent, add(current, value));
+ } else {
+ setter(scope.$parent, remove(current, value));
+ }
+ });
+
+ // watch original model change
+ scope.$parent.$watch(attrs.checklistModel, function(newArr, oldArr) {
+ scope.checked = contains(newArr, value);
+ }, true);
+ }
+
+ return {
+ restrict: 'A',
+ priority: 1000,
+ terminal: true,
+ scope: true,
+ compile: function(tElement, tAttrs) {
+ if (tElement[0].tagName !== 'INPUT' || tAttrs.type !== 'checkbox') {
+ throw 'checklist-model should be applied to `input[type="checkbox"]`.';
+ }
+
+ if (!tAttrs.checklistValue) {
+ throw 'You should provide `checklist-value`.';
+ }
+
+ // exclude recursion
+ tElement.removeAttr('checklist-model');
+
+ // local scope var storing individual checkbox model
+ tElement.attr('ng-model', 'checked');
+
+ return postLinkFn;
+ }
+ };
+}]);