/
jquery.flexcal.format.js
244 lines (229 loc) · 8.31 KB
/
jquery.flexcal.format.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Extend flexcal with better formatting
//
// for flexcal version 3.5
//
// Copyright (c) 2015 Daniel Wachsstock
// MIT license:
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
/*
Designed to be flexible in parsing (http://en.wikipedia.org/wiki/Robustness_principle).
Proposal: similar to http://keith-wood.name/calendars.html#format , using ' to mark literals and '' for a single quote,
d - day of the month (no leading zero)
dd - day of the month (two digit)
dddd - day number formatted (result of l10n.dates(d))
D - day name, min
DD - day name, short
DDDD - day name, full
m - month of the year (no leading zero)
mm - month of the year (two digit)
M - month name, short
MM - month name, long
yy - year (full year but not padded digit; no two-year dates)
yyyy - year (four digit)
YYYY - formatted year (result of l10n.years(yyyy))
no julian dates or timestamps; no day of the year or week of the year for now.
No predefined formats for now.
*/
(function($, f){
// from http://stackoverflow.com/a/1268377 . Assumes whole positive numbers; too-long numbers are left as is
function pad(n, p) {
var zeros = Math.max(0, p - n.toString().length );
return Math.pow(10,zeros).toString().substr(1) + n;
}
f.localize = function (name, l10n){
return l10n[name+'Text'] || '';
}
f.format = function (d, format, l10n){
// I am aware that regular expressions are not a parser, but this will serve for now.
// http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
d = l10n.calendar(d); // convert to the localized date
var dow = (d.dow + d.d - 1) % l10n.dayNames.length; // d.dow is the day of week of the first day of the month
// replace text in braces
var ret = format.replace(/{(\w+)}/g, function(match, name){
return f.localize(name, l10n);
});
return ret.replace(/'((?:[^']|'')*)'|[dmy]{4}|[dmy]{1,2}/ig, function(match, part1){
switch (match){
case 'dddd': return l10n.dates(d.d);
case 'dd': return pad(d.d, 2);
case 'd': return d.d;
case 'DDDD': return l10n.dayNames[dow];
case 'DD': return l10n.dayNamesShort[dow];
case 'D': return l10n.dayNamesMin[dow];
case 'mm': return pad(d.m+1, 2);
case 'm': return d.m+1;
case 'MM': return l10n.monthNames[d.m];
case 'M': return l10n.monthNamesShort[d.m];
case 'yyyy': return pad(d.y, 4);
case 'yy': return d.y; // jQuery UI datepicker uses yy for the 4-digit year
case 'YYYY': return l10n.years(d.y);
case "''": return "'";
default: return part1 ? part1.replace (/''/g, "'") : match; // remove doubled quotes
}
});
};
f.parse = function (s, format, l10n){
// replace text in braces
var dateFormat = format.replace(/{(\w+)}/g, function(match, name){
return f.localize(name, l10n);
});
// to allow for as generous a parse as possible, simplify the format to just look for date parts
// note that we ignore capital D; no way to parse days of the week meaningfully
dateFormat = dateFormat.replace(/'((?:[^']|'')*)'|[dmMyY]+|./g, function(match, part1){
if (/^d+$/.test(match)){
return 'd';
}else if (/^m+$/i.test(match)){
return 'm';
}else if (/^y+$/i.test(match)){
return 'y';
}else{
return ' ';
}
}).replace (/[^dmy]+/g, '');
var d, m, y;
var index = 0; // track how far we have parsed
// parse date format somehow
dateFormat.split('').forEach(function(which){
switch (which){
case 'd': d = parseDate(); break;
case 'm': m = parseMonth(); break;
case 'y': y = parseYear(); break;
}
});
return l10n.calendar(new Date).toDate({d: d, m: m, y: y});
function parseDate (){
if (checkError()) return;
var d = parseNumber();
if (d != undefined) return d;
d = parseDateString(s.length);
if (d != undefined) return d;
++index; // skip this character and try again
return parseDate();
}
function parseDateString(length){
// look for the longest string that actually corresponds to a date string
if (!l10n.fromDates) return; // no algorithm for parsing date strings
if (length == index) return;
var d = l10n.fromDates(s.substring(index, length));
if (d != undefined) return d;
return parseDateString(length-1);
}
function parseMonth (){
if (checkError()) return;
var m = parseNumber();
if (m != undefined) return m-1;
m = parseMonthString(s.length);
if (m != undefined) return m;
++index;
return parseMonth();
}
function parseMonthString(length){
if (length == index) return;
// do this
var m = findMonthName(s.substring(index, length));
if (m != undefined) return m;
return parseMonthString(length-1);
}
function findMonthName(name){
for (key in l10n) if (key.indexOf('month') == 0 && $.isArray(l10n[key])){
var m = l10n[key].indexOf(name);
if (m >= 0) return m;
}
}
function parseYear (){
if (checkError()) return;
var y = parseNumber();
if (y != undefined) return y;
y = parseYearString(s.length);
if (y != undefined) return y;
++index;
return parseYear();
}
function parseYearString(length){
// look for the longest string that actually corresponds to a year string
if (!l10n.fromYears) return; // no algorithm for parsing date strings
if (length == index) return;
var d = l10n.fromYears(s.substring(index, length));
if (d != undefined) return d;
return parseYearString(length-1);
}
function checkError(){
return index >= s.length;
}
function parseNumber(){
var ret = undefined;
while (/\d/.test(s.charAt(index))){
ret = (ret == undefined) ? 0 : ret*10;
ret += parseInt(s.charAt(index), 10);
++index;
}
return ret;
}
};
// extend the defaults to allow use of the new formatting
$.extend ($.bililite.flexcal.prototype.options.l10n, {
nextButtonText: "'{next}'",
prevButtonText: "'{prev}'",
captionText: "'<span class=ui-datepicker-month>'MM'</span>' "+
"'<span class=ui-datepicker-year>'YYYY'</span>'",
dayOfWeekText: 'D',
dateText: 'dddd'
});
$.extend (true, $.bililite.flexcal.l10n, {
jewish: {
dayNames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','שבת'],
dayNamesShort: ['Sun','Mon','Tue','Wed','Thu','Fri','שבת'],
monthNamesShort: $.bililite.flexcal.l10n.jewish.monthNames,
},
'he-jewish':{
dayNames: ['יום א׳','יום ב׳','יום ג׳','יום ד׳','יום ה׳','יום ו׳','שבת'],
dayNamesShort: $.bililite.flexcal.l10n['he-jewish'].dayNamesMin,
monthNamesShort: $.bililite.flexcal.l10n['he-jewish'].monthNames
}
});
// fancier text generators
$.extend($.bililite.flexcal.prototype,{
_generateCaptionText: function(d, cal){
return this.format(d, this._l10n.captionText, this._l10n);
},
_generateDateText: function (d, i){
return this.format(d, this._l10n.dateText, this._l10n);
},
_generateGoText: function(which, cal){
var text = this._l10n[which+'ButtonText'];
return this.format(cal[which], text, this._l10n);
},
_listDaysOfWeek: function (d, dow){
// returns an array of days of the week, starting at the localized first day of the week
// uses the week centered around d, which is on dow day of the week.
function addDay (d, n){
return new Date (d.getTime()+n*86400000); // msec/day
}
var daysInWeek = this._l10n.dayNames.length;
d = addDay (d, this._l10n.firstDay - dow); // positive or negative should not matter
var dayNames = [];
for (var i = 0; i < daysInWeek; ++i){
dayNames.push(this.format(d, this._l10n.dayOfWeekText, this._l10n));
d = addDay (d, 1);
}
return dayNames;
}
});
})(jQuery, jQuery.bililite.flexcal);