aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/coffee/common/inputs.coffee
blob: 4352040b021dae6e697a8d0cc8c91ea3873c7be1 (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
$ = jQuery

transformPattern = (pattern) ->
  return pattern.replace /\{0\}/g, '(\\d+)'


convertWorkDuration = (value) ->
  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 = if days then +days[1] else 0
  hours = if hours then +hours[1] else 0
  minutes = if minutes then +minutes[1] else 0

  if !value || (value.length > 0 && days == 0 && hours == 0 && minutes == 0)
    value
  else
    (days * 8 + hours) * 60 + minutes


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


convertRating = (value) ->
  if /^[ABCDE]$/.test(value)
    value.charCodeAt(0) - 'A'.charCodeAt(0) + 1
  else
    value


convertValue = (value, input) ->
  type = input.data 'type'

  # No convertation if input doesn't have data-type
  return value unless type?

  # Do necessary convertion depeneds on input data-type
  switch type
    when 'WORK_DUR' then convertWorkDuration value
    when 'RATING' then convertRating value
    else value


restoreRating = (value) ->
  return value unless /^[12345]+$/.test value
  String.fromCharCode(value - 1 + 'A'.charCodeAt(0))


restoreValue = (value, input) ->
  type = input.data 'type'

  # No convertation if input doesn't have data-type
  return value unless type?

  # Do necessary convertion depeneds on input data-type
  switch type
    when 'WORK_DUR' then restoreWorkDuration value
    when 'RATING' then restoreRating value
    else value


originalVal = $.fn.val
$.fn.val = (value) ->
  if arguments.length
    originalVal.call @, (restoreValue value, @)
  else
    convertValue originalVal.call(@), @

$.fn.originalVal = originalVal