aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/coffee/common/inputs.coffee
diff options
context:
space:
mode:
authorStas Vilchik <vilchiks@gmail.com>2014-03-25 15:18:29 +0600
committerStas Vilchik <vilchiks@gmail.com>2014-03-25 15:18:47 +0600
commit1c3cbb0af1a1294d2d3c672ee35f10008bee2ac7 (patch)
tree64cc0dff86514008bf4f45cdb54b68639b316fa7 /sonar-server/src/main/coffee/common/inputs.coffee
parent6349cd3b899f37567637c78187f73071c1f64e3c (diff)
downloadsonarqube-1c3cbb0af1a1294d2d3c672ee35f10008bee2ac7.tar.gz
sonarqube-1c3cbb0af1a1294d2d3c672ee35f10008bee2ac7.zip
Move source outside from webapp
Diffstat (limited to 'sonar-server/src/main/coffee/common/inputs.coffee')
-rw-r--r--sonar-server/src/main/coffee/common/inputs.coffee84
1 files changed, 84 insertions, 0 deletions
diff --git a/sonar-server/src/main/coffee/common/inputs.coffee b/sonar-server/src/main/coffee/common/inputs.coffee
new file mode 100644
index 00000000000..4352040b021
--- /dev/null
+++ b/sonar-server/src/main/coffee/common/inputs.coffee
@@ -0,0 +1,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
+