<commit_before>'use strict';

(function() {
  angular.module('ncsaas')
    .directive('serviceSettingsEditor', [serviceSettingsEditor]);

  function serviceSettingsEditor() {
    return {
      restrict: 'A',
      scope: {
        service: '='
      },
      replace: true,
      controller: 'ServiceSettingsEditorController',
      templateUrl: 'views/directives/service-settings-editor.html'
    };
  }
})();


(function() {
  angular.module('ncsaas')
    .controller('ServiceSettingsEditorController', [
      '$scope',
      'usersService',
      'servicesService',
      'ncUtils',
      'ncUtilsFlash',
      'joinService',
      ServiceSettingsEditorController
    ]);

  function ServiceSettingsEditorController(
    $scope,
    usersService,
    servicesService,
    ncUtils,
    ncUtilsFlash,
    joinService) {
    angular.extend($scope, {
      init: function() {
        this.loadService($scope.service);
      },
      loadService: function(service) {
        var vm = this;
        usersService.getCurrentUser().then(function(user) {
          service.editable = user.is_staff || !service.shared;
          if (!service.editable || service.values) {
            return;
          }

          return joinService.getOptions(service.service_type).then(function(options) {
            service.options = options;
            service.fields = vm.getFields(options);

            return servicesService.$get(null, service.settings).then(function(settings) {
              service.values = settings;
            });
          });
        });
      },
      updateSettings: function(service) {
        var url = service.settings;
        var data = this.getData(service);
        return joinService.update(url, data).then(
          this.onSaveSuccess.bind(this, service),
          this.onSaveError.bind(this, service)
        );
      },
      getFields: function(options) {
        var fields = [];
        var blacklist = ['name', 'customer', 'settings'];
        for (var name in options) {
          var option = options[name];
          if (!option.read_only && blacklist.indexOf(name) == -1) {
            option.name = name;
            fields.push(option);
          }
        }
        return fields;
      },
      getFilename: ncUtils.getFilename,
      isDisabled: function(service) {
        for (var name in service.values) {
          var option = service.options[name];
          var value = service.values[name];
          if (option && option.required && !value) {
            return true;
          }
        }
        return false;
      },
      getData: function(service) {
        var values = {};
        for (var name in service.values) {
          var option = service.options[name];
          if (!option || option.read_only) {
            continue;
          }
          var value = service.values[name];
          if (!value) {
            continue;
          }
          if (ncUtils.isFileOption(option)) {
            if (value.length != 1 || !ncUtils.isFileValue(value[0])) {
              continue;
            }
            value = value[0];
          }
          values[name] = value;
        }
        return values;
      },
      update: function(service) {
        var saveService = joinService.$update(null, service.url, service);
        return saveService.then(this.onSaveSuccess.bind(this), this.onSaveError.bind(this, service));
      },
      onSaveSuccess: function(service) {
        if (service) {
          this.saveRevision(service);
        }
      },
      onSaveError: function(service, response) {
        var message = '';
        for (var name in response.data) {
          message += (service.options[name] ? service.options[name].label : name) + ': ' + response.data[name];
        }
        if (message) {
          ncUtilsFlash.error('Unable to save provider. ' + message);
        }
      },
      hasChanged: function(model) {
        if (!model.values) {
          return false;
        }

        if (!model.revision) {
          this.saveRevision(model);
          return false;
        }

        return this.revisionsDiffer(model.revision, model.values.toJSON());
      },
      saveRevision: function(model) {
        if (model.values) {
          model.revision = model.values.toJSON();
        }
      },
      revisionsDiffer: function(revision1, revision2) {
        for (var name in revision1) {
          var val1 = revision1[name] ? revision1[name] : '';
          var val2 = revision2[name] ? revision2[name] : '';
          if (val1 != val2) {
            return true;
          }
        }
      }
    });
    $scope.init();
  }
})();<commit_msg>Fix comparison of empty values in service settings editor (SAAS-981)<commit_after>'use strict';

(function() {
  angular.module('ncsaas')
    .directive('serviceSettingsEditor', [serviceSettingsEditor]);

  function serviceSettingsEditor() {
    return {
      restrict: 'A',
      scope: {
        service: '='
      },
      replace: true,
      controller: 'ServiceSettingsEditorController',
      templateUrl: 'views/directives/service-settings-editor.html'
    };
  }
})();


(function() {
  angular.module('ncsaas')
    .controller('ServiceSettingsEditorController', [
      '$scope',
      'usersService',
      'servicesService',
      'ncUtils',
      'ncUtilsFlash',
      'joinService',
      ServiceSettingsEditorController
    ]);

  function ServiceSettingsEditorController(
    $scope,
    usersService,
    servicesService,
    ncUtils,
    ncUtilsFlash,
    joinService) {
    angular.extend($scope, {
      init: function() {
        this.loadService($scope.service);
      },
      loadService: function(service) {
        var vm = this;
        usersService.getCurrentUser().then(function(user) {
          service.editable = user.is_staff || !service.shared;
          if (!service.editable || service.values) {
            return;
          }

          return joinService.getOptions(service.service_type).then(function(options) {
            service.options = options;
            service.fields = vm.getFields(options);

            return servicesService.$get(null, service.settings).then(function(settings) {
              service.values = settings.toJSON();
              angular.forEach(service.fields, function(field) {
                if (angular.isUndefined(service.values[field.name])) {
                  service.values[field.name] = '';
                }
              })
            });
          });
        });
      },
      updateSettings: function(service) {
        var url = service.settings;
        var data = this.getData(service);
        return joinService.update(url, data).then(
          this.onSaveSuccess.bind(this, service),
          this.onSaveError.bind(this, service)
        );
      },
      getFields: function(options) {
        var fields = [];
        var blacklist = ['name', 'customer', 'settings'];
        for (var name in options) {
          var option = options[name];
          if (!option.read_only && blacklist.indexOf(name) == -1) {
            option.name = name;
            fields.push(option);
          }
        }
        return fields;
      },
      getFilename: ncUtils.getFilename,
      isDisabled: function(service) {
        for (var name in service.values) {
          var option = service.options[name];
          var value = service.values[name];
          if (option && option.required && !value) {
            return true;
          }
        }
        return false;
      },
      getData: function(service) {
        var values = {};
        for (var name in service.values) {
          var option = service.options[name];
          if (!option || option.read_only) {
            continue;
          }
          var value = service.values[name];
          if (!value) {
            continue;
          }
          if (ncUtils.isFileOption(option)) {
            if (value.length != 1 || !ncUtils.isFileValue(value[0])) {
              continue;
            }
            value = value[0];
          }
          values[name] = value;
        }
        return values;
      },
      update: function(service) {
        var saveService = joinService.$update(null, service.url, service);
        return saveService.then(this.onSaveSuccess.bind(this), this.onSaveError.bind(this, service));
      },
      onSaveSuccess: function(service) {
        if (service) {
          this.saveRevision(service);
        }
      },
      onSaveError: function(service, response) {
        var message = '';
        for (var name in response.data) {
          message += (service.options[name] ? service.options[name].label : name) + ': ' + response.data[name];
        }
        if (message) {
          ncUtilsFlash.error('Unable to save provider. ' + message);
        }
      },
      hasChanged: function(model) {
        if (!model.values) {
          return false;
        }

        if (!model.revision) {
          this.saveRevision(model);
          return false;
        }

        return !angular.equals(model.revision, model.values);
      },
      saveRevision: function(model) {
        if (model.values) {
          model.revision = angular.copy(model.values);
        }
      }
    });
    $scope.init();
  }
})();
{'fraction_non_alphanumeric': 0.12681237827310107, 'fraction_numerical': 0.0023804371348193033, 'mean_word_length': 1.8749611197511664, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1555646', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2504, 'n_words': 481}