-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslf-datepicker.js
216 lines (200 loc) · 6.85 KB
/
slf-datepicker.js
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
/**
* @author: olivka
* @date: 17.02.15
*
* DatePicker wrapper
*/
var DatePicker = function (element, params) {
this.init(element, params);
};
DatePicker.prototype = {
constructor: DatePicker,
//$dom: DOMElement object
// jQuery object
$dom: null,
//defaults: object
// Default settings
defaults: {
//format: string
// Date format, example: January - 2015
format: "MM-yyyy",
//weekStart: int
// Represents from which date will be start week: 0 - Sunday, 1 - Monday
weekStart: 1,
//startDate: Date object
// The earliest date that may be selected. '-' dates before today will be disabled
startDate: null,
//endDate: Date object
// The latest date that may be selected. '+' dates after today will be disabled
endDate: null,
//startView: int|string
// The view that the datepicker should show when it is opened. 0 - month, 1- year, 2 - 10 years
startView: 1,
//minViewMode: int|string
// Gives the ability to pick only a month or an year. Accepts: “days” or 0, “months” or 1, and “years” or 2
minViewMode: 1,
//language: string
// Default language
language: "uk",
//multidate: boolean
// Enable multidate picking
multidate: false,
//keyboardNavigation: boolena
// Whether or not to allow date navigation by arrow keys
keyboardNavigation: true,
//autoclose: boolean
// Whether or not to close the datepicker immediately when a date is selected
autoclose: true,
//date: Date object
// Show this date by opening datePicker
date: null,
//defaultViewDate: Date object
// Show this date in open calendar by default
defaultViewDate: null,
//disabledPeriods: array of dates
// Periods that must be disabled.
// Format array: [{from: '2014-08-03', to: '2014-08-12'}, {from: '2014-08-21', to: '2014-08-25'}, ...]
disabledPeriods: []
},
/**
* @param jQuery object element
* @param object options
*/
init: function (element, options) {
this.$dom = element;
var settings = $.extend({}, this.defaults, this.$dom.data(), options);
this.$dom.datepicker({
format: settings.format,
weekStart: settings.weekStart,
startDate: settings.startDate,
endDate: settings.endDate,
startView: settings.startView,
minViewMode: settings.minViewMode,
language: settings.language,
multidate: settings.multidate,
autoclose: settings.autoclose,
keyboardNavigation: settings.keyboardNavigation,
beforeShowDay: function (date) {
if (settings.disabledPeriods.length > 0) {
var result = true;
$.each(settings.disabledPeriods, function (i, period) {
var from = new Date(period.from),
to = new Date(period.to);
from.setHours('00'); //set hours to midnight, because by default time is 02:00
if (date >= from && date <= to) {
return result = false;
}
});
return result;
}
}
});
//set default date
if (settings.date !== null) {
this.setDate(settings.date);
} else if (settings.defaultViewDate !== null) {
this.setDefaultViewDate(settings.defaultViewDate);
}
},
/**
* Set date
* @param Date object |string date
*/
setDate: function (date) {
if (typeof date === 'string') {
date = new Date(date);
}
this.$dom.datepicker('setDate', date);
return this;
},
/**
* Get date format: 2015-03-12
* @returns string
*/
getDate: function () {
var date = this.$dom.datepicker('getDate');
return this._formattedDate(date);
},
/**
* Get number of month, start from 1 (January)
* @returns string
*/
getMonth: function () {
var date = this.$dom.datepicker('getDate'),
month = date.getMonth() + 1;
month = (month < 10) ? '0' + month : month;
return month;
},
/**
* Get year
* @returns {number|*}
*/
getYear: function () {
var date = this.$dom.datepicker('getDate');
return date.getFullYear();
},
/**
* Disable all dates before set date
* @param Date object date
*/
setStartDate: function (date) {
this.$dom.datepicker('setStartDate', date);
return this;
},
/**
* Disable all dates after set date
* @param Date object date
*/
setEndDate: function (date) {
this.$dom.datepicker('setEndDate', date);
return this;
},
/**
* Return the displayed dat
* @returns {*|jQuery}
*/
getViewDate:function() {
return $('input', this.$dom).val();
},
/**
* Set the date which will be shown when calendar is open by default
* @param Date object date
*/
setDefaultViewDate: function (date) {
if (!this.$dom.val().length) {
var object = this.$dom.data('datepicker');
object.viewDate = date;
object.fill();
}
},
/**
* Return formatted date: '2013-03-01'
* @param Date object date
* @returns string
* @private
*/
_formattedDate: function (date) {
var day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear();
month = (month < 10) ? '0' + month : month;
day = (day < 10) ? '0' + day : day;
return year + '-' + month + '-' + day;
}
};
define(['jquery', 'bootstrap-datepicker'],
function ($) {
$.fn.datepicker.dates.uk = {
days: ["Неділя", "Понеділок", "Вівторок", "Середа", "Четвер", "П'ятниця", "Субота"],
daysShort: ["Нед", "Пнд", "Втр", "Срд", "Чтв", "Птн", "Суб"],
daysMin: ["Нд", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"],
months: ["Cічень", "Лютий", "Березень", "Квітень", "Травень", "Червень", "Липень", "Серпень", "Вересень", "Жовтень", "Листопад", "Грудень"],
monthsShort: ["Січ", "Лют", "Бер", "Кві", "Тра", "Чер", "Лип", "Сер", "Вер", "Жов", "Лис", "Гру"],
today: "Сьогодні",
clear: "Очистити",
format: "dd.mm.yyyy",
weekStart: 1
};
return DatePicker;
}
);