This repository has been archived by the owner on Nov 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathknockout-pickadate.coffee
231 lines (186 loc) · 6.43 KB
/
knockout-pickadate.coffee
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
ko.bindingHandlers.pickadate =
init: (element, valueAccessor, allBindings) ->
value = valueAccessor()
options =
# Strings
monthsFull: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
weekdaysFull: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
showMonthsShort: undefined
showWeekdaysFull: undefined
# Buttons
today: 'Today'
clear: 'Clear'
# Accessibility Labels
labelMonthNext: 'Next month'
labelMonthPrev: 'Previous month'
labelMonthSelect: 'Select a month'
labelYearSelect: 'Select a year'
# Formats
format: 'd mmmm, yyyy'
formatSubmit: undefined
hiddenPrefix: undefined
hiddenSuffix: '_submit'
hiddenName: undefined
# Editable input
editable: undefined
# Dropdown selectors
selectYears: undefined
selectMonths: undefined
# First day of the week
firstDay: undefined
# Date limits
min: undefined
max: undefined
# Disable dates
disable: undefined
# Root container
container: undefined
# Events
onStart: undefined
onRender: undefined
onOpen: undefined
onClose: undefined
onSet: undefined
onStop: undefined
# Classes
klass: {
# The element states
input: 'picker__input'
active: 'picker__input--active'
# The root picker and states *
picker: 'picker'
opened: 'picker--opened'
focused: 'picker--focused'
# The picker holder
holder: 'picker__holder'
# The picker frame, wrapper, and box
frame: 'picker__frame'
wrap: 'picker__wrap'
box: 'picker__box'
# The picker header
header: 'picker__header'
# Month navigation
navPrev: 'picker__nav--prev'
navNext: 'picker__nav--next'
navDisabled: 'picker__nav--disabled'
# Month & year labels
month: 'picker__month'
year: 'picker__year'
# Month & year dropdowns
selectMonth: 'picker__select--month'
selectYear: 'picker__select--year'
# Table of dates
table: 'picker__table'
# Weekday labels
weekdays: 'picker__weekday'
# Day states
day: 'picker__day'
disabled: 'picker__day--disabled'
selected: 'picker__day--selected'
highlighted: 'picker__day--highlighted'
now: 'picker__day--today'
infocus: 'picker__day--infocus'
outfocus: 'picker__day--outfocus'
# The picker footer
footer: 'picker__footer'
# Today & clear buttons
buttonClear: 'picker__button--clear'
buttonToday: 'picker__button--today'
}
pickadate_options = allBindings.get('pickadate_options')
_init_picker = ($elem) ->
return $elem
.attr 'autocomplete', 'off'
.pickadate options
.pickadate 'picker'
if 'function' is typeof pickadate_options
options_from_binding = pickadate_options()
else
options_from_binding = pickadate_options or {}
for key, val of options_from_binding
options[key] = val
if options.clear_button_addon or options.calendar_addon
wrapper_id = new Date().getTime()
options.container = "##{wrapper_id}"
calendar_addon_position = if options.calendar_addon is 'before' then 'before' else 'after'
clear_button_addon_position = if options.clear_button_addon is 'before' then 'before' else 'after'
$calendar_addon =
if options.calendar_addon
$(
"<span class='input-group-addon'>" +
"<i style='color: navy; cursor: pointer'" +
"title='A calendar appears when interacting with this field'" +
"class='fa fa-calendar'>" +
"</i>" +
"</span>"
)
else
undefined
$clear_button_addon =
if options.clear_button_addon
$(
"<span class='input-group-addon'>" +
"<i style='color: navy; cursor: pointer'" +
"title='Click to clear date'" +
"class='fa fa-times'>" +
"</i>" +
"</span>"
)
else
undefined
picker = _init_picker(
$(element)
.wrap $("<div id=#{wrapper_id}></div>")
.wrap $("<div class='input-group'></div>")
)
$(element)[clear_button_addon_position] $clear_button_addon
$(element)[calendar_addon_position] $calendar_addon
if options.calendar_addon
$calendar_addon.on "click", (event) ->
picker.open()
event.stopPropagation()
event.preventDefault()
if options.clear_button_addon
$clear_button_addon.on "click", (event) ->
picker.set('clear')
event.stopPropagation()
event.preventDefault()
else
picker = _init_picker $(element)
picker.on 'set', (context) ->
item = picker.get('select')
if item
if options.update_as_date
# if item has been updated
if item.obj.toString() isnt value()?.toString()
value item.obj
else
if item isnt value()
value picker.get()
else
value item
ko.utils.domNodeDisposal.addDisposeCallback element, ->
if options.calendar_addon
$calendar_addon.off 'click'
if options.clear_button_addon
$clear_button_addon.off 'click'
picker.stop() if picker.get('start')
return
update: (element, valueAccessor, allBindings) ->
value = valueAccessor()
new_val = ko.unwrap value
picker = $(element).pickadate('picker')
# observable updated with a blank date means we empty the input
if not new_val? or new_val is ''
picker.set('clear')
return
date = Date.parse(new_val)
return if date is picker.get('select')?.pick
# Try setting picker to whatever was passed in
#
# Pickadate seems to convert anything it can't handle (e.g. errors, NaN) to today's date
# TODO: Maybe there's something more sensible than that?
picker.set 'select', date
return