Skip to content
Snippets Groups Projects
ActionDialogCtrl.js 2.16 KiB
Newer Older
  • Learn to ignore specific revisions
  •   angular.module('form_processor').controller('ActionDialogCtrl', function InputDialogCtrl($scope, dialogService, crmApi) {
    
        $scope.action = angular.copy($scope.model.action);
    
        $scope.actionType = $scope.model.actionType;
    
        $scope.actionTitles = CRM.form_processor.actionTitles;
    
        if (Object.prototype.toString.call($scope.action.configuration) == '[object Array]') {
          $scope.action.configuration = {};
        }
        if (Object.prototype.toString.call($scope.action.mapping) == '[object Array]') {
          $scope.action.mapping = {};
        }
    
        $scope.locks = {name: true};
        $scope.isNameValid = false;
        
        $scope.$watch('action.name', function(newName, oldName) {
        	// Watch for changes in the name field
        	$scope.isNameValid = false;
        	if (newName) {
    
    	      crmApi($scope.model.entity, 'validatename', {'name': newName, 'form_processor_instance_id': $scope.action.form_processor_instance_id,'id': $scope.action.id})
    
    	      .then(function(data) {
    	      	if (data.is_valid) {
    	      		$scope.isNameValid = true;
    	      	}
    	      });
         }
        }, true);
        
        $scope.$watch('action.title', function(newTitle, oldTitle) {
        	// Watch for changes in the name field
        	if ($scope.locks.name && $scope.action.id <= 0) {
    
        		$scope.action.name = newTitle; 
            $scope.action.name = $scope.action.name.toLowerCase();
            $scope.action.name = $scope.action.name.replace(/[^a-zA-Z\d]/g, '_');
    
    noah's avatar
    noah committed
            $scope.action.name = $scope.action.name.replace(/_+/g, '_');
    
        	}
        }, true);
        
        $scope.isValidName = function(name) {
    
          for (var i = 0; i < $scope.model.actions.length; i++) {
            if ($scope.model.actions[i] != $scope.model.action) {
              if ($scope.model.actions[i].name == name) {
                return false;
              }
            }
          }
    
        	return (!name) || name.match(/^[a-z0-9_]+$/) ? true : false;
        };
        
    
        $scope.saveClick = function() {
    
        	$scope.model.action = $scope.action;
    
        	dialogService.close('ActionDialog', $scope.model);
        };
    		
    		$scope.cancelClick = function() {
        	dialogService.cancel('ActionDialog');
        };
        
      });
    
    })(angular, CRM.$, CRM._);