aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/libs/inputs.js
blob: 5e92a3bc0bb6b9039519dfc3ab40335d777b3df8 (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
(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(window.t('work_duration.x_days'));
    hoursPattern = transformPattern(window.t('work_duration.x_hours'));
    minutesPattern = transformPattern(window.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.sonar.properties['sonar.technicalDebt.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.sonar.properties['sonar.technicalDebt.hoursInDay'] * 60));
    hours = Math.floor((value - days * window.sonar.properties['sonar.technicalDebt.hoursInDay'] * 60) / 60);
    minutes = value % 60;
    result = [];
    if (days > 0) {
      result.push(window.t('work_duration.x_days').replace('{0}', days));
    }
    if (hours > 0) {
      result.push(window.t('work_duration.x_hours').replace('{0}', hours));
    }
    if (minutes > 0) {
      result.push(window.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);