CRM-14480 - crmCaseType - Use select2. Fix autocomplete in Chrome.
authorTim Otten <totten@civicrm.org>
Mon, 2 Jun 2014 22:41:23 +0000 (15:41 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 2 Jun 2014 23:12:08 +0000 (16:12 -0700)
js/angular-crmCaseType.js
partials/crmCaseType/addName.html [deleted file]

index e83e488e0729fd7ba4c76a69436aadc40fafa700..617f6726c7d2aaa74d83199a28f92d41a800dfaa 100644 (file)
   crmCaseType.directive('crmAddName', function() {
     return {
       restrict: 'AE',
-      scope: {
-        crmOptions: '=',
-        crmVar: '=',
-        crmOnAdd: '&'
-      },
-      templateUrl: partialUrl('addName.html')
+      template: '<input class="add-activity" type="hidden" />',
+      link: function(scope, element, attrs) {
+        /// Format list of options for select2's "data"
+        var getFormattedOptions = function() {
+          return {
+            results: _.map(scope[attrs.crmOptions], function(option){
+              return {id: option, text: option};
+            })
+          };
+        };
+
+        var input = $('input', element);
+
+        scope._resetSelection = function() {
+          $(input).select2('close');
+          $(input).select2('val', '');
+          scope[attrs.crmVar] = '';
+        };
+
+        $(input).select2({
+          data: getFormattedOptions,
+          createSearchChoice: function(term) {
+            return {id: term, text: term};
+          }
+        });
+        $(input).on('select2-selecting', function(e) {
+          scope[attrs.crmVar] = e.val;
+          scope.$evalAsync(attrs.crmOnAdd);
+          scope.$evalAsync('_resetSelection()');
+          e.preventDefault();
+        });
+      }
     };
   });
 
 
     /// Add a new role
     $scope.addRole = function(roles, roleName) {
-      roles.push({
-        name: roleName
-      });
+      var names = _.pluck($scope.caseType.definition.caseRoles, 'name');
+      if (!_.contains(names, roleName)) {
+        roles.push({
+          name: roleName
+        });
+      }
     };
 
     $scope.onManagerChange = function(managerRole) {
diff --git a/partials/crmCaseType/addName.html b/partials/crmCaseType/addName.html
deleted file mode 100644 (file)
index 5850ef6..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<!--
-Directive: crmAddName
--->
-Add:
-<input
-        ui-jq="autocomplete"
-        ui-options="{source:crmOptions}"
-        ng-model="crmVar"
-        class="add-activity"
-        />
-
-<button
-        ng-class="{disabled: (crmVar == '' || crmVar == null)}"
-        ng-disabled="crmVar == '' || crmVar == null"
-        ng-click="crmOnAdd(); crmVar = ''"
-        >Add
-</button>