aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/libs/inputs.js
blob: c8bd09a1a6fa3f1258e64f6390ed7c53572434dc (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
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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.
 */
(function ($) {

  function transformPattern (pattern) {
    return pattern.replace(/\{0\}/g, '(\\d+)');
  }

  function convertWorkDuration (value) {
    var days, daysPattern, hours, hoursPattern, minutes, minutesPattern;
    if (value === '0') {
      return 0;
    }
    daysPattern = transformPattern(t('work_duration.x_days'));
    hoursPattern = transformPattern(t('work_duration.x_hours'));
    minutesPattern = transformPattern(t('work_duration.x_minutes'));
    days = value.match(daysPattern);
    hours = value.match(hoursPattern);
    minutes = value.match(minutesPattern);
    days = days ? +days[1] : 0;
    hours = hours ? +hours[1] : 0;
    minutes = minutes ? +minutes[1] : 0;
    if (!value) {
      return value;
    } else {
      return (days * window.SS.hoursInDay + hours) * 60 + minutes;
    }
  }

  function restoreWorkDuration (value) {
    var days, hours, minutes, result;
    if (value === '0' || value === 0) {
      return '0';
    }
    if (!/^\d+$/.test(value)) {
      return value;
    }
    days = Math.floor(value / (window.SS.hoursInDay * 60));
    hours = Math.floor((value - days * window.SS.hoursInDay * 60) / 60);
    minutes = value % 60;
    result = [];
    if (days > 0) {
      result.push(t('work_duration.x_days').replace('{0}', days));
    }
    if (hours > 0) {
      result.push(t('work_duration.x_hours').replace('{0}', hours));
    }
    if (minutes > 0) {
      result.push(t('work_duration.x_minutes').replace('{0}', minutes));
    }
    return result.join(' ');
  }

  function convertRating (value) {
    if (/^[ABCDE]$/.test(value)) {
      return value.charCodeAt(0) - 'A'.charCodeAt(0) + 1;
    } else {
      return value;
    }
  }

  function convertValue (value, input) {
    var type;
    type = input.data('type');
    if (type == null) {
      return value;
    }
    switch (type) {
      case 'WORK_DUR':
        return convertWorkDuration(value);
      case 'RATING':
        return convertRating(value);
      default:
        return value;
    }
  }

  function restoreRating (value) {
    if (!/^[12345]+$/.test(value)) {
      return value;
    }
    return String.fromCharCode(value - 1 + 'A'.charCodeAt(0));
  }

  function restoreValue (value, input) {
    var type;
    type = input.data('type');
    if (type == null) {
      return value;
    }
    switch (type) {
      case 'WORK_DUR':
        return restoreWorkDuration(value);
      case 'RATING':
        return restoreRating(value);
      default:
        return value;
    }
  }

  var originalVal = $.fn.val;

  $.fn.val = function (value) {
    if (arguments.length) {
      return originalVal.call(this, restoreValue(value, this));
    } else {
      return convertValue(originalVal.call(this), this);
    }
  };

  $.fn.originalVal = originalVal;

})(window.jQuery);