Newer
Older
});
};
CalendarApi.prototype.today = function () {
var state = this.getCurrentData();
this.unselect();
this.dispatch({
type: 'CHANGE_DATE',
dateMarker: getNow(state.calendarOptions.now, state.dateEnv),
});
};
CalendarApi.prototype.gotoDate = function (zonedDateInput) {
var state = this.getCurrentData();
this.unselect();
this.dispatch({
type: 'CHANGE_DATE',
dateMarker: state.dateEnv.createMarker(zonedDateInput),
});
};
CalendarApi.prototype.incrementDate = function (deltaInput) {
var state = this.getCurrentData();
var delta = createDuration(deltaInput);
if (delta) { // else, warn about invalid input?
this.unselect();
this.dispatch({
type: 'CHANGE_DATE',
dateMarker: state.dateEnv.add(state.currentDate, delta),
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
});
}
};
// for external API
CalendarApi.prototype.getDate = function () {
var state = this.getCurrentData();
return state.dateEnv.toDate(state.currentDate);
};
// Date Formatting Utils
// -----------------------------------------------------------------------------------------------------------------
CalendarApi.prototype.formatDate = function (d, formatter) {
var dateEnv = this.getCurrentData().dateEnv;
return dateEnv.format(dateEnv.createMarker(d), createFormatter(formatter));
};
// `settings` is for formatter AND isEndExclusive
CalendarApi.prototype.formatRange = function (d0, d1, settings) {
var dateEnv = this.getCurrentData().dateEnv;
return dateEnv.formatRange(dateEnv.createMarker(d0), dateEnv.createMarker(d1), createFormatter(settings), settings);
};
CalendarApi.prototype.formatIso = function (d, omitTime) {
var dateEnv = this.getCurrentData().dateEnv;
return dateEnv.formatIso(dateEnv.createMarker(d), { omitTime: omitTime });
};
// Date Selection / Event Selection / DayClick
// -----------------------------------------------------------------------------------------------------------------
// this public method receives start/end dates in any format, with any timezone
// NOTE: args were changed from v3
CalendarApi.prototype.select = function (dateOrObj, endDate) {
var selectionInput;
if (endDate == null) {
if (dateOrObj.start != null) {
selectionInput = dateOrObj;
}
else {
selectionInput = {
start: dateOrObj,
};
}
}
else {
selectionInput = {
start: dateOrObj,
};
}
var state = this.getCurrentData();
var selection = parseDateSpan(selectionInput, state.dateEnv, createDuration({ days: 1 }));
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
if (selection) { // throw parse error otherwise?
this.dispatch({ type: 'SELECT_DATES', selection: selection });
triggerDateSelect(selection, null, state);
}
};
// public method
CalendarApi.prototype.unselect = function (pev) {
var state = this.getCurrentData();
if (state.dateSelection) {
this.dispatch({ type: 'UNSELECT_DATES' });
triggerDateUnselect(pev, state);
}
};
// Public Events API
// -----------------------------------------------------------------------------------------------------------------
CalendarApi.prototype.addEvent = function (eventInput, sourceInput) {
if (eventInput instanceof EventApi) {
var def = eventInput._def;
var instance = eventInput._instance;
var currentData = this.getCurrentData();
// not already present? don't want to add an old snapshot
if (!currentData.eventStore.defs[def.defId]) {
this.dispatch({
type: 'ADD_EVENTS',
eventStore: eventTupleToStore({ def: def, instance: instance }),
});
this.triggerEventAdd(eventInput);
}
return eventInput;
}
var state = this.getCurrentData();
var eventSource;
if (sourceInput instanceof EventSourceApi) {
eventSource = sourceInput.internalEventSource;
}
else if (typeof sourceInput === 'boolean') {
if (sourceInput) { // true. part of the first event source
eventSource = hashValuesToArray(state.eventSources)[0];
}
}
else if (sourceInput != null) { // an ID. accepts a number too
var sourceApi = this.getEventSourceById(sourceInput); // TODO: use an internal function
if (!sourceApi) {
console.warn("Could not find an event source with ID \"" + sourceInput + "\""); // TODO: test
eventSource = sourceApi.internalEventSource;
}
var tuple = parseEvent(eventInput, eventSource, state, false);
if (tuple) {
var newEventApi = new EventApi(state, tuple.def, tuple.def.recurringDef ? null : tuple.instance);
this.dispatch({
type: 'ADD_EVENTS',
});
this.triggerEventAdd(newEventApi);
return newEventApi;
}
return null;
};
CalendarApi.prototype.triggerEventAdd = function (eventApi) {
var _this = this;
var emitter = this.getCurrentData().emitter;
emitter.trigger('eventAdd', {
event: eventApi,
relatedEvents: [],
revert: function () {
_this.dispatch({
type: 'REMOVE_EVENTS',
});
};
// TODO: optimize
CalendarApi.prototype.getEventById = function (id) {
var state = this.getCurrentData();
var _a = state.eventStore, defs = _a.defs, instances = _a.instances;
id = String(id);
for (var defId in defs) {
var def = defs[defId];
if (def.publicId === id) {
if (def.recurringDef) {
return new EventApi(state, def, null);
}
for (var instanceId in instances) {
var instance = instances[instanceId];
if (instance.defId === def.defId) {
return new EventApi(state, def, instance);
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
}
}
}
}
return null;
};
CalendarApi.prototype.getEvents = function () {
var currentData = this.getCurrentData();
return buildEventApis(currentData.eventStore, currentData);
};
CalendarApi.prototype.removeAllEvents = function () {
this.dispatch({ type: 'REMOVE_ALL_EVENTS' });
};
// Public Event Sources API
// -----------------------------------------------------------------------------------------------------------------
CalendarApi.prototype.getEventSources = function () {
var state = this.getCurrentData();
var sourceHash = state.eventSources;
var sourceApis = [];
for (var internalId in sourceHash) {
sourceApis.push(new EventSourceApi(state, sourceHash[internalId]));
}
return sourceApis;
};
CalendarApi.prototype.getEventSourceById = function (id) {
var state = this.getCurrentData();
var sourceHash = state.eventSources;
id = String(id);
for (var sourceId in sourceHash) {
if (sourceHash[sourceId].publicId === id) {
return new EventSourceApi(state, sourceHash[sourceId]);
}
}
return null;
};
CalendarApi.prototype.addEventSource = function (sourceInput) {
var state = this.getCurrentData();
if (sourceInput instanceof EventSourceApi) {
// not already present? don't want to add an old snapshot
if (!state.eventSources[sourceInput.internalEventSource.sourceId]) {
this.dispatch({
type: 'ADD_EVENT_SOURCES',
sources: [sourceInput.internalEventSource],
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
});
}
return sourceInput;
}
var eventSource = parseEventSource(sourceInput, state);
if (eventSource) { // TODO: error otherwise?
this.dispatch({ type: 'ADD_EVENT_SOURCES', sources: [eventSource] });
return new EventSourceApi(state, eventSource);
}
return null;
};
CalendarApi.prototype.removeAllEventSources = function () {
this.dispatch({ type: 'REMOVE_ALL_EVENT_SOURCES' });
};
CalendarApi.prototype.refetchEvents = function () {
this.dispatch({ type: 'FETCH_EVENT_SOURCES' });
};
// Scroll
// -----------------------------------------------------------------------------------------------------------------
CalendarApi.prototype.scrollToTime = function (timeInput) {
var time = createDuration(timeInput);
if (time) {
this.trigger('_scrollRequest', { time: time });
}
};
return CalendarApi;
}());
var EventApi = /** @class */ (function () {
// instance will be null if expressing a recurring event that has no current instances,
// OR if trying to validate an incoming external event that has no dates assigned
function EventApi(context, def, instance) {
this._context = context;
this._def = def;
this._instance = instance || null;
}
/*
TODO: make event struct more responsible for this
*/
EventApi.prototype.setProp = function (name, val) {
var _a, _b;
if (name in EVENT_DATE_REFINERS) {
console.warn('Could not set date-related prop \'name\'. Use one of the date-related methods instead.');
}
else if (name in EVENT_NON_DATE_REFINERS) {
val = EVENT_NON_DATE_REFINERS[name](val);
this.mutate({
standardProps: (_a = {}, _a[name] = val, _a),
});
}
else if (name in EVENT_UI_REFINERS) {
var ui = EVENT_UI_REFINERS[name](val);
if (name === 'color') {
ui = { backgroundColor: val, borderColor: val };
}
else if (name === 'editable') {
ui = { startEditable: val, durationEditable: val };
}
else {
ui = (_b = {}, _b[name] = val, _b);
}
this.mutate({
});
}
else {
console.warn("Could not set prop '" + name + "'. Use setExtendedProp instead.");
}
};
EventApi.prototype.setExtendedProp = function (name, val) {
var _a;
this.mutate({
extendedProps: (_a = {}, _a[name] = val, _a),
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
});
};
EventApi.prototype.setStart = function (startInput, options) {
if (options === void 0) { options = {}; }
var dateEnv = this._context.dateEnv;
var start = dateEnv.createMarker(startInput);
if (start && this._instance) { // TODO: warning if parsed bad
var instanceRange = this._instance.range;
var startDelta = diffDates(instanceRange.start, start, dateEnv, options.granularity); // what if parsed bad!?
if (options.maintainDuration) {
this.mutate({ datesDelta: startDelta });
}
else {
this.mutate({ startDelta: startDelta });
}
}
};
EventApi.prototype.setEnd = function (endInput, options) {
if (options === void 0) { options = {}; }
var dateEnv = this._context.dateEnv;
var end;
if (endInput != null) {
end = dateEnv.createMarker(endInput);
if (!end) {
return; // TODO: warning if parsed bad
}
}
if (this._instance) {
if (end) {
var endDelta = diffDates(this._instance.range.end, end, dateEnv, options.granularity);
this.mutate({ endDelta: endDelta });
}
else {
this.mutate({ standardProps: { hasEnd: false } });
}
}
};
EventApi.prototype.setDates = function (startInput, endInput, options) {
if (options === void 0) { options = {}; }
var dateEnv = this._context.dateEnv;
var standardProps = { allDay: options.allDay };
var start = dateEnv.createMarker(startInput);
var end;
if (!start) {
return; // TODO: warning if parsed bad
}
if (endInput != null) {
end = dateEnv.createMarker(endInput);
if (!end) { // TODO: warning if parsed bad
return;
}
}
if (this._instance) {
var instanceRange = this._instance.range;
// when computing the diff for an event being converted to all-day,
// compute diff off of the all-day values the way event-mutation does.
if (options.allDay === true) {
instanceRange = computeAlignedDayRange(instanceRange);
}
var startDelta = diffDates(instanceRange.start, start, dateEnv, options.granularity);
if (end) {
var endDelta = diffDates(instanceRange.end, end, dateEnv, options.granularity);
if (durationsEqual(startDelta, endDelta)) {
this.mutate({ datesDelta: startDelta, standardProps: standardProps });
}
else {
this.mutate({ startDelta: startDelta, endDelta: endDelta, standardProps: standardProps });
}
}
else { // means "clear the end"
standardProps.hasEnd = false;
this.mutate({ datesDelta: startDelta, standardProps: standardProps });
}
}
};
EventApi.prototype.moveStart = function (deltaInput) {
var delta = createDuration(deltaInput);
if (delta) { // TODO: warning if parsed bad
this.mutate({ startDelta: delta });
}
};
EventApi.prototype.moveEnd = function (deltaInput) {
var delta = createDuration(deltaInput);
if (delta) { // TODO: warning if parsed bad
this.mutate({ endDelta: delta });
}
};
EventApi.prototype.moveDates = function (deltaInput) {
var delta = createDuration(deltaInput);
if (delta) { // TODO: warning if parsed bad
this.mutate({ datesDelta: delta });
}
};
EventApi.prototype.setAllDay = function (allDay, options) {
if (options === void 0) { options = {}; }
var standardProps = { allDay: allDay };
var maintainDuration = options.maintainDuration;
if (maintainDuration == null) {
maintainDuration = this._context.options.allDayMaintainDuration;
}
if (this._def.allDay !== allDay) {
standardProps.hasEnd = maintainDuration;
}
this.mutate({ standardProps: standardProps });
};
EventApi.prototype.formatRange = function (formatInput) {
var dateEnv = this._context.dateEnv;
var instance = this._instance;
var formatter = createFormatter(formatInput);
if (this._def.hasEnd) {
return dateEnv.formatRange(instance.range.start, instance.range.end, formatter, {
forcedStartTzo: instance.forcedStartTzo,
return dateEnv.format(instance.range.start, formatter, {
forcedTzo: instance.forcedStartTzo,
});
};
EventApi.prototype.mutate = function (mutation) {
var instance = this._instance;
if (instance) {
var def = this._def;
var context_1 = this._context;
var eventStore_1 = context_1.getCurrentData().eventStore;
var relevantEvents = getRelevantEvents(eventStore_1, instance.instanceId);
var eventConfigBase = {
'': {
display: '',
startEditable: true,
durationEditable: true,
constraints: [],
overlap: null,
allows: [],
backgroundColor: '',
borderColor: '',
textColor: '',
relevantEvents = applyMutationToEventStore(relevantEvents, eventConfigBase, mutation, context_1);
var oldEvent = new EventApi(context_1, def, instance); // snapshot
this._def = relevantEvents.defs[def.defId];
this._instance = relevantEvents.instances[instance.instanceId];
context_1.dispatch({
type: 'MERGE_EVENTS',
});
context_1.emitter.trigger('eventChange', {
oldEvent: oldEvent,
event: this,
relatedEvents: buildEventApis(relevantEvents, context_1, instance),
revert: function () {
context_1.dispatch({
type: 'RESET_EVENTS',
eventStore: eventStore_1,
});
}
};
EventApi.prototype.remove = function () {
var context = this._context;
var asStore = eventApiToStore(this);
context.dispatch({
type: 'REMOVE_EVENTS',
});
context.emitter.trigger('eventRemove', {
event: this,
relatedEvents: [],
revert: function () {
context.dispatch({
type: 'MERGE_EVENTS',
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
});
};
Object.defineProperty(EventApi.prototype, "source", {
get: function () {
var sourceId = this._def.sourceId;
if (sourceId) {
return new EventSourceApi(this._context, this._context.getCurrentData().eventSources[sourceId]);
}
return null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "start", {
get: function () {
return this._instance ?
this._context.dateEnv.toDate(this._instance.range.start) :
null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "end", {
get: function () {
return (this._instance && this._def.hasEnd) ?
this._context.dateEnv.toDate(this._instance.range.end) :
null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "startStr", {
get: function () {
var instance = this._instance;
if (instance) {
return this._context.dateEnv.formatIso(instance.range.start, {
omitTime: this._def.allDay,
});
}
return '';
},
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "endStr", {
get: function () {
var instance = this._instance;
if (instance && this._def.hasEnd) {
return this._context.dateEnv.formatIso(instance.range.end, {
omitTime: this._def.allDay,
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
});
}
return '';
},
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "id", {
// computable props that all access the def
// TODO: find a TypeScript-compatible way to do this at scale
get: function () { return this._def.publicId; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "groupId", {
get: function () { return this._def.groupId; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "allDay", {
get: function () { return this._def.allDay; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "title", {
get: function () { return this._def.title; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "url", {
get: function () { return this._def.url; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "display", {
get: function () { return this._def.ui.display || 'auto'; } // bad. just normalize the type earlier
,
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "startEditable", {
get: function () { return this._def.ui.startEditable; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "durationEditable", {
get: function () { return this._def.ui.durationEditable; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "constraint", {
get: function () { return this._def.ui.constraints[0] || null; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "overlap", {
get: function () { return this._def.ui.overlap; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "allow", {
get: function () { return this._def.ui.allows[0] || null; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "backgroundColor", {
get: function () { return this._def.ui.backgroundColor; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "borderColor", {
get: function () { return this._def.ui.borderColor; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "textColor", {
get: function () { return this._def.ui.textColor; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "classNames", {
// NOTE: user can't modify these because Object.freeze was called in event-def parsing
get: function () { return this._def.ui.classNames; },
enumerable: false,
configurable: true
});
Object.defineProperty(EventApi.prototype, "extendedProps", {
get: function () { return this._def.extendedProps; },
enumerable: false,
configurable: true
});
EventApi.prototype.toPlainObject = function (settings) {
if (settings === void 0) { settings = {}; }
var def = this._def;
var ui = def.ui;
var _a = this, startStr = _a.startStr, endStr = _a.endStr;
var res = {};
if (def.title) {
res.title = def.title;
}
if (startStr) {
res.start = startStr;
}
if (endStr) {
res.end = endStr;
}
if (def.publicId) {
res.id = def.publicId;
}
if (def.groupId) {
res.groupId = def.groupId;
}
if (def.url) {
res.url = def.url;
}
if (ui.display && ui.display !== 'auto') {
res.display = ui.display;
}
// TODO: what about recurring-event properties???
// TODO: include startEditable/durationEditable/constraint/overlap/allow
if (settings.collapseColor && ui.backgroundColor && ui.backgroundColor === ui.borderColor) {
res.color = ui.backgroundColor;
}
else {
if (ui.backgroundColor) {
res.backgroundColor = ui.backgroundColor;
}
if (ui.borderColor) {
res.borderColor = ui.borderColor;
}
}
if (ui.textColor) {
res.textColor = ui.textColor;
}
if (ui.classNames.length) {
res.classNames = ui.classNames;
}
if (Object.keys(def.extendedProps).length) {
if (settings.collapseExtendedProps) {
__assign(res, def.extendedProps);
}
else {
res.extendedProps = def.extendedProps;
}
}
return res;
};
EventApi.prototype.toJSON = function () {
return this.toPlainObject();
};
return EventApi;
}());
function eventApiToStore(eventApi) {
var _a, _b;
var def = eventApi._def;
var instance = eventApi._instance;
return {
defs: (_a = {}, _a[def.defId] = def, _a),
instances: instance
? (_b = {}, _b[instance.instanceId] = instance, _b) : {},
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
};
}
function buildEventApis(eventStore, context, excludeInstance) {
var defs = eventStore.defs, instances = eventStore.instances;
var eventApis = [];
var excludeInstanceId = excludeInstance ? excludeInstance.instanceId : '';
for (var id in instances) {
var instance = instances[id];
var def = defs[instance.defId];
if (instance.instanceId !== excludeInstanceId) {
eventApis.push(new EventApi(context, def, instance));
}
}
return eventApis;
}
var calendarSystemClassMap = {};
function registerCalendarSystem(name, theClass) {
calendarSystemClassMap[name] = theClass;
}
function createCalendarSystem(name) {
return new calendarSystemClassMap[name]();
}
var GregorianCalendarSystem = /** @class */ (function () {
function GregorianCalendarSystem() {
}
GregorianCalendarSystem.prototype.getMarkerYear = function (d) {
return d.getUTCFullYear();
};
GregorianCalendarSystem.prototype.getMarkerMonth = function (d) {
return d.getUTCMonth();
};
GregorianCalendarSystem.prototype.getMarkerDay = function (d) {
return d.getUTCDate();
};
GregorianCalendarSystem.prototype.arrayToMarker = function (arr) {
return arrayToUtcDate(arr);
};
GregorianCalendarSystem.prototype.markerToArray = function (marker) {
return dateToUtcArray(marker);
};
return GregorianCalendarSystem;
}());
registerCalendarSystem('gregory', GregorianCalendarSystem);
var ISO_RE = /^\s*(\d{4})(-?(\d{2})(-?(\d{2})([T ](\d{2}):?(\d{2})(:?(\d{2})(\.(\d+))?)?(Z|(([-+])(\d{2})(:?(\d{2}))?))?)?)?)?$/;
function parse(str) {
var m = ISO_RE.exec(str);
if (m) {
var marker = new Date(Date.UTC(Number(m[1]), m[3] ? Number(m[3]) - 1 : 0, Number(m[5] || 1), Number(m[7] || 0), Number(m[8] || 0), Number(m[10] || 0), m[12] ? Number("0." + m[12]) * 1000 : 0));
if (isValidDate(marker)) {
var timeZoneOffset = null;
if (m[13]) {
timeZoneOffset = (m[15] === '-' ? -1 : 1) * (Number(m[16] || 0) * 60 +
Number(m[18] || 0));
}
return {
marker: marker,
isTimeUnspecified: !m[6],
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
};
}
}
return null;
}
var DateEnv = /** @class */ (function () {
function DateEnv(settings) {
var timeZone = this.timeZone = settings.timeZone;
var isNamedTimeZone = timeZone !== 'local' && timeZone !== 'UTC';
if (settings.namedTimeZoneImpl && isNamedTimeZone) {
this.namedTimeZoneImpl = new settings.namedTimeZoneImpl(timeZone);
}
this.canComputeOffset = Boolean(!isNamedTimeZone || this.namedTimeZoneImpl);
this.calendarSystem = createCalendarSystem(settings.calendarSystem);
this.locale = settings.locale;
this.weekDow = settings.locale.week.dow;
this.weekDoy = settings.locale.week.doy;
if (settings.weekNumberCalculation === 'ISO') {
this.weekDow = 1;
this.weekDoy = 4;
}
if (typeof settings.firstDay === 'number') {
this.weekDow = settings.firstDay;
}
if (typeof settings.weekNumberCalculation === 'function') {
this.weekNumberFunc = settings.weekNumberCalculation;
}
this.weekText = settings.weekText != null ? settings.weekText : settings.locale.options.weekText;
this.cmdFormatter = settings.cmdFormatter;
this.defaultSeparator = settings.defaultSeparator;
}
// Creating / Parsing
DateEnv.prototype.createMarker = function (input) {
var meta = this.createMarkerMeta(input);
if (meta === null) {
return null;
}
return meta.marker;
};
DateEnv.prototype.createNowMarker = function () {
if (this.canComputeOffset) {
return this.timestampToMarker(new Date().valueOf());
}
// if we can't compute the current date val for a timezone,
// better to give the current local date vals than UTC
return arrayToUtcDate(dateToLocalArray(new Date()));
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
};
DateEnv.prototype.createMarkerMeta = function (input) {
if (typeof input === 'string') {
return this.parse(input);
}
var marker = null;
if (typeof input === 'number') {
marker = this.timestampToMarker(input);
}
else if (input instanceof Date) {
input = input.valueOf();
if (!isNaN(input)) {
marker = this.timestampToMarker(input);
}
}
else if (Array.isArray(input)) {
marker = arrayToUtcDate(input);
}
if (marker === null || !isValidDate(marker)) {
return null;
}
return { marker: marker, isTimeUnspecified: false, forcedTzo: null };
};
DateEnv.prototype.parse = function (s) {
var parts = parse(s);
if (parts === null) {
return null;
}
var marker = parts.marker;
var forcedTzo = null;
if (parts.timeZoneOffset !== null) {
if (this.canComputeOffset) {
marker = this.timestampToMarker(marker.valueOf() - parts.timeZoneOffset * 60 * 1000);
}
else {
forcedTzo = parts.timeZoneOffset;
}
}
return { marker: marker, isTimeUnspecified: parts.isTimeUnspecified, forcedTzo: forcedTzo };
};
// Accessors
DateEnv.prototype.getYear = function (marker) {
return this.calendarSystem.getMarkerYear(marker);
};
DateEnv.prototype.getMonth = function (marker) {
return this.calendarSystem.getMarkerMonth(marker);
};
// Adding / Subtracting
DateEnv.prototype.add = function (marker, dur) {
var a = this.calendarSystem.markerToArray(marker);
a[0] += dur.years;
a[1] += dur.months;
a[2] += dur.days;
a[6] += dur.milliseconds;
return this.calendarSystem.arrayToMarker(a);
};
DateEnv.prototype.subtract = function (marker, dur) {
var a = this.calendarSystem.markerToArray(marker);
a[0] -= dur.years;
a[1] -= dur.months;
a[2] -= dur.days;
a[6] -= dur.milliseconds;
return this.calendarSystem.arrayToMarker(a);
};
DateEnv.prototype.addYears = function (marker, n) {
var a = this.calendarSystem.markerToArray(marker);
a[0] += n;
return this.calendarSystem.arrayToMarker(a);
};
DateEnv.prototype.addMonths = function (marker, n) {
var a = this.calendarSystem.markerToArray(marker);
a[1] += n;
return this.calendarSystem.arrayToMarker(a);
};
// Diffing Whole Units
DateEnv.prototype.diffWholeYears = function (m0, m1) {
var calendarSystem = this.calendarSystem;
if (timeAsMs(m0) === timeAsMs(m1) &&
calendarSystem.getMarkerDay(m0) === calendarSystem.getMarkerDay(m1) &&
calendarSystem.getMarkerMonth(m0) === calendarSystem.getMarkerMonth(m1)) {
return calendarSystem.getMarkerYear(m1) - calendarSystem.getMarkerYear(m0);
}
return null;
};
DateEnv.prototype.diffWholeMonths = function (m0, m1) {
var calendarSystem = this.calendarSystem;
if (timeAsMs(m0) === timeAsMs(m1) &&
calendarSystem.getMarkerDay(m0) === calendarSystem.getMarkerDay(m1)) {
return (calendarSystem.getMarkerMonth(m1) - calendarSystem.getMarkerMonth(m0)) +
(calendarSystem.getMarkerYear(m1) - calendarSystem.getMarkerYear(m0)) * 12;
}
return null;
};
// Range / Duration
DateEnv.prototype.greatestWholeUnit = function (m0, m1) {
var n = this.diffWholeYears(m0, m1);
if (n !== null) {
return { unit: 'year', value: n };
}
n = this.diffWholeMonths(m0, m1);
if (n !== null) {
return { unit: 'month', value: n };
}
n = diffWholeWeeks(m0, m1);
if (n !== null) {
return { unit: 'week', value: n };
}
n = diffWholeDays(m0, m1);
if (n !== null) {
return { unit: 'day', value: n };
}
n = diffHours(m0, m1);
if (isInt(n)) {
return { unit: 'hour', value: n };
}
n = diffMinutes(m0, m1);
if (isInt(n)) {
return { unit: 'minute', value: n };
}
n = diffSeconds(m0, m1);
if (isInt(n)) {
return { unit: 'second', value: n };
}
return { unit: 'millisecond', value: m1.valueOf() - m0.valueOf() };
};
DateEnv.prototype.countDurationsBetween = function (m0, m1, d) {
// TODO: can use greatestWholeUnit
var diff;
if (d.years) {
diff = this.diffWholeYears(m0, m1);
if (diff !== null) {
return diff / asRoughYears(d);
}
}
if (d.months) {
diff = this.diffWholeMonths(m0, m1);
if (diff !== null) {
return diff / asRoughMonths(d);
}
}
if (d.days) {
diff = diffWholeDays(m0, m1);
if (diff !== null) {
return diff / asRoughDays(d);
}
}
return (m1.valueOf() - m0.valueOf()) / asRoughMs(d);
};
// Start-Of
// these DON'T return zoned-dates. only UTC start-of dates
DateEnv.prototype.startOf = function (m, unit) {
if (unit === 'year') {
return this.startOfYear(m);
}
return this.startOfMonth(m);
}
return this.startOfWeek(m);
}
return startOfDay(m);
}
return startOfHour(m);
}
return startOfMinute(m);
}
return startOfSecond(m);
}
};
DateEnv.prototype.startOfYear = function (m) {
return this.calendarSystem.arrayToMarker([
]);
};
DateEnv.prototype.startOfMonth = function (m) {
return this.calendarSystem.arrayToMarker([
this.calendarSystem.getMarkerYear(m),
]);
};
DateEnv.prototype.startOfWeek = function (m) {
return this.calendarSystem.arrayToMarker([
this.calendarSystem.getMarkerYear(m),
this.calendarSystem.getMarkerMonth(m),
m.getUTCDate() - ((m.getUTCDay() - this.weekDow + 7) % 7),
]);
};
// Week Number
DateEnv.prototype.computeWeekNumber = function (marker) {
if (this.weekNumberFunc) {
return this.weekNumberFunc(this.toDate(marker));
}
return weekOfYear(marker, this.weekDow, this.weekDoy);
};
// TODO: choke on timeZoneName: long
DateEnv.prototype.format = function (marker, formatter, dateOptions) {
if (dateOptions === void 0) { dateOptions = {}; }
return formatter.format({
marker: marker,
timeZoneOffset: dateOptions.forcedTzo != null ?
dateOptions.forcedTzo :
}, this);
};
DateEnv.prototype.formatRange = function (start, end, formatter, dateOptions) {
if (dateOptions === void 0) { dateOptions = {}; }
if (dateOptions.isEndExclusive) {
end = addMs(end, -1);
}
return formatter.formatRange({
marker: start,
timeZoneOffset: dateOptions.forcedStartTzo != null ?
dateOptions.forcedStartTzo :
}, {
marker: end,
timeZoneOffset: dateOptions.forcedEndTzo != null ?
dateOptions.forcedEndTzo :
}, this, dateOptions.defaultSeparator);