Skip to content
Snippets Groups Projects
InputDialogCtrl.js 2.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • (function(angular, $, _) {
    
    
    jaapjansma's avatar
    jaapjansma committed
      angular.module('form_processor').controller('InputDialogCtrl', function InputDialogCtrl($scope, dialogService, crmApi) {
    
        $scope.ts = CRM.ts(null);
    
        $scope.input = angular.copy($scope.model.input);
    
    jaapjansma's avatar
    jaapjansma committed
        $scope.locks = {name: true};
        $scope.isNameValid = false;
    
      	$scope.validators = CRM.form_processor.validators;
    
      	
      	if (!$scope.input.deletedValidators) {
      		$scope.input.deletedValidators = [];
      	}
    
    jaapjansma's avatar
    jaapjansma committed
    
    
        $scope.$watch('input.name', function(newName, oldName) {
    
    jaapjansma's avatar
    jaapjansma committed
          // Watch for changes in the name field
          $scope.isNameValid = false;
          if (newName) {
    
            crmApi($scope.model.entity, 'validatename', {'name': newName, 'form_processor_instance_id': $scope.input.form_processor_instance_id, 'id': $scope.input.id})
    
    jaapjansma's avatar
    jaapjansma committed
              .then(function(data) {
                if (data.is_valid) {
                  $scope.isNameValid = true;
                }
              });
          }
    
    jaapjansma's avatar
    jaapjansma committed
    
        $scope.$watch('input.title', function(newTitle, oldTitle) {
          // Watch for changes in the name field
          if ($scope.locks.name && $scope.input.id <= 0) {
            $scope.input.name = newTitle;
            $scope.input.name = $scope.input.name.toLowerCase();
            $scope.input.name = $scope.input.name.replace(/[^a-zA-Z\d]/g, '_');
            $scope.input.name = $scope.input.name.replace(/_+n/g, '_');
          }
        }, true);
    
        $scope.isValidName = function(name) {
    
          for (var i = 0; i < $scope.model.inputs.length; i++) {
            if ($scope.model.inputs[i] != $scope.model.input) {
              if ($scope.model.inputs[i].name == name) {
                return false;
              }
            }
          }
    
    jaapjansma's avatar
    jaapjansma committed
          return (!name) || name.match(/^[a-z0-9_]+$/) ? true : false;
        };
    
        
        $scope.saveClick = function() {
    
        	$scope.model.input = $scope.input;
    
        	dialogService.close('InputDialog', $scope.model);
        };
    		
    
    		$scope.cancelClick = function() {			
    
        	dialogService.cancel('InputDialog');
        };
        
    
        $scope.addValidator = function(newValidator) {
        	if (!newValidator) {
        		return;
        	}
        	var validator = {
        		validator: angular.copy(newValidator),
        		configuration: {}
        	}; 
        	$scope.input.validators.push(validator);
        };
        
        $scope.removeValidator = function(validator) {
        	var index = $scope.input.validators.indexOf(validator);
        	if (index >= 0) {
    
        		$scope.input.deletedValidators.push(validator);
    
        		$scope.input.validators.splice(index, 1);
        	}
        };
        
    
      });
    
    })(angular, CRM.$, CRM._);