-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeFormat.coffee
More file actions
74 lines (41 loc) · 1.24 KB
/
TimeFormat.coffee
File metadata and controls
74 lines (41 loc) · 1.24 KB
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
$ = jQuery
$.fn.extend
timeFormat: (options) ->
defaults =
separator: ':'
options = $.extend defaults, options
return @each () ->
# set the object
obj = $(this)
opt = options
format00 = (value) ->
zero = if value.length is 1 then '0' else ''
"#{zero}#{value}"
# return the value formated like 00:00
time_it = () ->
str = obj.val().replace opt.separator, ''
hours = minutes = '0'
if str.length is 1
hours = str
else if str.length is 2
hours = str
else if str.length is 3
hours = str.substr 0, 1
minutes = str.substr 1
else if str.length > 3
hours = str.substr 0, 2
minutes = str.substr 2, 2
obj.val "#{format00(hours)}#{opt.separator}#{format00(minutes)}"
# check to not input alphabetical chars
key_check = (e) ->
code = if e.keyCode then e.keyCode else e.which
functional = off
# allow key numbers, 0 to 9
functional = on if (code >= 48 and code <= 57) or (code >= 96 and code <= 105)
functional = on if code in [8,9,13,46,37,39]
if not functional
e.preventDefault()
e.stopPropagation()
# bind the action
$(@).bind 'keydown.time_format', key_check
$(@).bind 'focusout.time_format', time_it