aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/settings/utils.js
blob: 704ae2c45c1ebe88f818b6f4ad260c69b70abab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { translate, hasMessage } from '../../helpers/l10n';
import {
  TYPE_PROPERTY_SET,
  TYPE_BOOLEAN,
  TYPE_SINGLE_SELECT_LIST,
  TYPE_PASSWORD
} from './constants';

export const DEFAULT_CATEGORY = 'general';

export function getPropertyName(definition) {
  const key = `property.${definition.key}.name`;
  return hasMessage(key) ? translate(key) : definition.name;
}

export function getPropertyDescription(definition) {
  const key = `property.${definition.key}.description`;
  return hasMessage(key) ? translate(key) : definition.description;
}

export function getCategoryName(category) {
  const key = `property.category.${category}`;
  return hasMessage(key) ? translate(key) : category;
}

export function getSubCategoryName(category, subCategory) {
  const key = `property.category.${category}.${subCategory}`;
  return hasMessage(key) ? translate(key) : getCategoryName(subCategory);
}

export function getSubCategoryDescription(category, subCategory) {
  const key = `property.category.${category}.${subCategory}.description`;
  return hasMessage(key) ? translate(key) : null;
}

export function getUniqueName(definition, index = null) {
  const indexSuffix = index != null ? `[${index}]` : '';
  return `settings[${definition.key}]${indexSuffix}`;
}

export function getSettingValue(setting) {
  if (setting.definition.multiValues) {
    return setting.values;
  } else if (setting.definition.type === TYPE_PROPERTY_SET) {
    return setting.fieldValues;
  } else {
    return setting.value;
  }
}

export function isEmptyValue(definition, value) {
  if (value == null) {
    return true;
  } else if (definition.type === TYPE_BOOLEAN) {
    return false;
  } else {
    return value.length === 0;
  }
}

export function getEmptyValue(definition) {
  if (definition.multiValues) {
    return [getEmptyValue({ ...definition, multiValues: false })];
  }

  if (definition.type === TYPE_PROPERTY_SET) {
    const value = {};
    definition.fields.forEach(field => (value[field.key] = getEmptyValue(field)));
    return [value];
  }

  if (definition.type === TYPE_BOOLEAN || definition.type === TYPE_SINGLE_SELECT_LIST) {
    return null;
  }

  return '';
}

export function isDefaultOrInherited(setting) {
  return !!setting.default || !!setting.inherited;
}

function getParentValue(setting) {
  if (setting.definition.multiValues) {
    return setting.parentValues;
  } else if (setting.definition.type === TYPE_PROPERTY_SET) {
    return setting.parentFieldValues;
  } else {
    return setting.parentValue;
  }
}

/**
 * Get and format the default value
 * @param setting
 * @returns {string}
 */
export function getDefaultValue(setting) {
  const parentValue = getParentValue(setting);

  if (parentValue == null) {
    return translate('settings.default.no_value');
  }

  if (setting.definition.multiValues) {
    return parentValue.length > 0 ? parentValue.join(', ') : translate('settings.default.no_value');
  }

  if (setting.definition.type === TYPE_PROPERTY_SET) {
    return parentValue.length > 0
      ? translate('settings.default.complex_value')
      : translate('settings.default.no_value');
  }

  if (setting.definition.type === TYPE_PASSWORD) {
    return translate('settings.default.password');
  }

  if (setting.definition.type === TYPE_BOOLEAN) {
    const isTrue = parentValue === true || parentValue === 'true';
    return isTrue ? translate('settings.boolean.true') : translate('settings.boolean.false');
  }

  return parentValue;
}