Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 | 2x 2x 2x 2x 4x 556x 36x 520x 66x 454x 454x 2x 2x 2x 2x 1036x 1036x 3108x 1028x 1036x 2x 2x 2x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 2x 48x 48x 48x 20x 8x 8x 8x 8x 8x 12x 12x 12x 20x 20x 20x 30x 30x 30x 30x 30x 44x 24x 24x 24x 24x 54x 54x 34x 20x 4x 16x 24x 24x 24x 4x 24x 24x 8x 24x 24x 24x 2504x 244x 240x 240x 510x 510x 510x 510x 4x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 398x 480x 88x 480x 480x 480x 480x 480x 480x 480x 510x 510x 510x 8x 8x 3046x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 518x 514x 518x 514x 518x 518x 1156x 1156x 626x 626x 626x 12x 626x 626x 626x 626x 2472x 624x 624x 624x 624x 624x 624x 624x 12x 624x 188x 188x 436x 624x 624x 1196x 1196x 972x 1196x 16x 16x 2x 14x 2x 14x 8x 8x 1322x 556x 556x 556x 556x 556x 556x 20x 12x 12x 12x 484x 484x 484x 484x 484x 484x 4x 42x 38x 38x 472x 40x 40x 40x 40x 40x 40x 40x 40x 300x 66x 66x 66x 66x 66x 4x 44x 24x 24x 24x 24x 24x 24x 48x 8x 8x 8x 8x 200x 32x 32x 32x 32x 1674x 998x | /**
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {BaseComponent} from '../../../coral-base-component';
import {BaseFormField} from '../../../coral-base-formfield';
import {DateTime} from '../../../coral-datetime';
import '../../../coral-component-button';
import '../../../coral-component-clock';
import '../../../coral-component-calendar';
import '../../../coral-component-popover';
import '../../../coral-component-textfield';
import base from '../templates/base';
import overlayContent from '../templates/overlayContent';
import {transform, commons, validate, i18n} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
/**
Enum for {@link Datepicker} variant values.
@typedef {Object} DatepickerVariantEnum
@property {String} DEFAULT
A default, gray Datepicker.
@property {String} QUIET
A Datepicker with no border or background.
*/
const variant = {
DEFAULT: 'default',
QUIET: 'quiet'
};
const CLASSNAME = '_coral-InputGroup';
// builds a string containing all possible variant classnames. This will be used to remove
// classnames when the variant changes.
const ALL_VARIANT_CLASSES = [];
for (const variantKey in variant) {
ALL_VARIANT_CLASSES.push(`${CLASSNAME}--${variant[variantKey]}`);
}
/** @ignore */
function toMoment(value, format) {
if (value === 'today') {
return new DateTime.Moment();
} else if (DateTime.Moment.isMoment(value)) {
return value.isValid() ? value.clone() : null;
}
// if the value provided is a date it does not make sense to provide a format to parse the date
const result = new DateTime.Moment(value, value instanceof Date ? null : format);
return result.isValid() ? result : null;
}
/**
Enumeration for {@link Datepicker} variants.
@typedef {Object} DatepickerTypeEnum
@property {String} DATE
The selection overlay contains only a calendar.
@property {String} DATETIME
Provides both calendar and time controls in the selection overlay.
@property {String} TIME
The selection overlay provides only time controls.
*/
const type = {
DATE: 'date',
DATETIME: 'datetime',
TIME: 'time'
};
// Used to determine if the client is on a mobile device
const IS_MOBILE_DEVICE = navigator.userAgent.match(/iPhone|iPad|iPod|Android/i) !== null;
const NATIVE_FORMATS = {
date: 'YYYY-MM-DD',
datetime: 'YYYY-MM-DD[T]HH:mmZ',
time: 'HH:mm'
};
const isNativeFormat = (format) => {
let res = false;
for (const key in NATIVE_FORMATS) {
if (format === NATIVE_FORMATS[key]) {
res = true;
}
}
return res;
};
/**
@class Coral.Datepicker
@classdesc A Datepicker component that can be used as a date and time selection form field. Leverages {@link momentJS}
if loaded on the page.
@htmltag coral-datepicker
@extends {HTMLElement}
@extends {BaseComponent}
@extends {BaseFormField}
*/
const Datepicker = Decorator(class extends BaseFormField(BaseComponent(HTMLElement)) {
/** @ignore */
constructor() {
super();
// Prepare templates
this._elements = {};
base.call(this._elements, {commons, i18n});
// Creates and stores the contents of the popover separately
this._calendarFragment = overlayContent.call(this._elements, {commons, i18n});
// Pre-define labellable element
this._labellableElement = this._elements.input;
const overlayId = this._elements.overlay.id;
const events = {};
events[`global:capture:click #${overlayId} coral-calendar`] = '_onCalendarDayClick';
events[`global:capture:change #${overlayId}`] = '_onChange';
events[`global:capture:coral-overlay:beforeopen #${overlayId}`] = '_onPopoverBeforeOpen';
events[`global:capture:coral-overlay:open #${overlayId}`] = '_onPopoverOpenOrClose';
events[`global:capture:coral-overlay:close #${overlayId}`] = '_onPopoverOpenOrClose';
events['key:alt+down [handle="input"],[handle="toggle"]'] = '_onAltDownKey';
events['key:down [handle="toggle"]'] = '_onAltDownKey';
events['change coral-datepicker > input[is="coral-textfield"]'] = '_onInputChange';
// Events
this._delegateEvents(commons.extend(this._events, events));
}
/**
Returns the inner overlay to allow customization.
@type {Popover}
@readonly
*/
get overlay() {
return this._elements.overlay;
}
/**
The type of datepicker to show to the user. See {@link DatepickerTypeEnum}.
@type {DatepickerTypeEnum}
@default DatepickerTypeEnum.DATE
@htmlattribute type
@htmlattributereflected
*/
get type() {
return this._type || type.DATE;
}
set type(value) {
// Flag to indicate that we are changing the type for the first time
this._typeFormatChanged = typeof this._type === 'undefined';
value = transform.string(value).toLowerCase();
this._type = validate.enumeration(type)(value) && value || type.DATE;
this._reflectAttribute('type', this._type);
const format = NATIVE_FORMATS[this._type];
const isTime = this._type === type.TIME;
const isDate = this._type === type.DATE;
this._elements.icon.icon = isTime ? 'clock' : 'calendar';
const toggleLabel = isTime ? i18n.get('Time') : i18n.get('Calendar');
this._elements.toggle.setAttribute('aria-label', toggleLabel);
this._elements.toggle.setAttribute('title', toggleLabel);
this._elements.clock.hidden = isDate;
this._elements.clock.setAttribute('aria-hidden', isDate);
this._elements.calendar.hidden = isTime;
this._elements.calendar.setAttribute('aria-hidden', isTime);
// Change format if we have a native format set
if (isNativeFormat(this.valueFormat)) {
thIis.valueFormat = format;
}
if (isNativeFormat(this.displayFormat)) {
this.displayFormat = format;
}
this._useNativeInput = this._useNativeInput;
this._typeFormatChanged = false;
}
/**
The format used to display the selected date(time) to the user. If the user manually types a date, this format
will be used to parse the value. When using this component on a mobile device, the display format must follow
the format used by the native input. If an empty string is provided, then the default value per type will
be used. The default value depends on the <code>type</code>, which can be one from <code>YYYY-MM-DD</code>,
<code>YYYY-MM-DD[T]HH:mmZ</code> or <code>HH:mm</code>. Include momentjs to support additional format string options
see http://momentjs.com/docs/#/displaying/.
@type {String}
@default "YYYY-MM-DD"
@htmlattribute displayformat
@htmlattributereflected
*/
get EdisplayFormat() {
// we ignore _useNativeInput when the type is datetime because it is not supported by mobile libraries
if (this._useNativeInput && this.type !== type.DATETIME) {
return NATIVE_FORMATS[this.type];
}
return typeof this._displayFormat === 'undefined' ? NATIVE_FORMATS[this.type] : this._displayFormat;
}
set displayFormat(value) {
value = transform.string(value).trim();
// In case a custom display format was set, we make sure that type doesn't change it to a native format
const displayFormatAttribute = this.getAttribute('displayformat');
if (this._typeFormatChanged && displayFormatAttribute && displayFormatAttribute !== value) {
value = displayFormatAttribute;
}
this._displayFormat = value === '' ? NATIVE_FORMATS[this.type] : value;
this._reflectAttribute('displayformat', this._displayFormat);
this._elements.clock.displayFormat = this._displayFormat;
this._elements.input.value = this._getValueAsString(this._value, this._displayFormat);
}
/**
The format to use on expressing the selected date as a string on the <code>value</code> attribute. The value
will be sent to the server using this format. If an empty string is provided, then the default value per type
will be used. The default value depends on the <code>type</code>, which can be one from <code>YYYY-MM-DD</code>,
<code>YYYY-MM-DD[T]HH:mmZ</code> or <code>HH:mm</code>. Include momentjs to support additional format string options
see http://momentjs.com/docs/#/displaying/.
@type {String}
@default "YYYY-MM-DD"
@htmlattribute valueformat
@htmlattributereflected
*/
get valueFormat() {
return typeof this._valueFormat === 'undefined' ? NATIVE_FORMATS[this.type] : this._valueFormat;
}
set valueFormat(value) {
const setValueFormat = (newValue) => {
this._valueFormat = newValue === '' ? NATIVE_FORMATS[this.type] : newValue;
this._reflectAttribute('valueformat', this._valueFormat);
};
value = transform.string(value).trim();
// In case a custom display format was set, we make sure that type doesn't change it to a native format
const valueFormatAttribute = this.getAttribute('valueformat');
if (this._typeFormatChanged && valueFormatAttribute && valueFormatAttribute !== value) {
value = valueFormatAttribute;
}
// Once the valueFormat is set, we make sure the value is also correct
if (!this._valueFormat && this._originalValue) {
setValueFormat(value);
this.value = this._originalValue;
} else {
setValueFormat(value);
}
this._elements.calendar.valueFormat = this._valueFormat;
this._elements.hiddenInput.value = this.value;
}
/**
The value of the element, interpreted as a date, or <code>null</code> if conversion is not possible.
@type {Date}
@default null
*/
get valueAsDate() {
let value = this._value;
// If type is DATE, then you strip out the time
if (this.type === 'date' && value) {
value = value.startOf('day');
}
return value ? value.toDate() : null;
}
set EvalueAsDate(value) {
this._valueAsDate = value instanceof Date ? new DateTime.Moment(value) : '';
this.value = this._valueAsDate;
}
/**
The minimum date that the Datepicker will accept as valid. It must not be greated that its maximum. It accepts
both date and string values. When a string is provided, it should match the {@link Coral.Datepicker#valueFormat}.
See {@link Coral.Calendar#min}
@type {String|Date}
@default null
@htmlattribute min
*/
get min() {
return this._elements.calendar.min;
}
set min(value) {
this._elements.calendar.min = value;
}
/**
The maximum date that the Datepicker will accept as valid. It must not be less than its minimum. It accepts both
date and string values. When a string is provided, it should match the {@link Coral.Datepicker#valueFormat}.
See {@link Coral.Calendar#max}
@type {String|Date}
@default null
@htmlattribute max
*/
get max() {
return this._elements.calendar.max;
}
set max(value) {
this._elements.calendar.max = value;
}
/**
The format used to display the current month and year.
'MMMM YYYY' is supported by default. Include momentjs to support additional format string options see
http://momentjs.com/docs/#/displaying/.
See {@link Coral.Calendar#startDay}
@type {String}
@default "MMMM YYYY"
@htmlattribute headerformat
*/
get headerFormat() {
return this._elements.calendar.headerFormat;
}
set headerFormat(value) {
this._elements.calendar.headerFormat = value;
}
/**
Defines the start day for the week, 0 = Sunday, 1 = Monday etc., as depicted on the calendar days grid.
See {@link Coral.Calendar#startDay}
@type {Number}
@default 0
@htmlattribute startday
*/
get startDay() {
return this._elements.calendar.startDay;
}
set startDay(value) {
this._elements.calendar.startDay = value;
}
/**
The current value. When set to "today", the value is coerced into the client's local date expressed as string
formatted in accordance to the set <code>valueFormat</code>.
See {@link Coral.Calendar#value}
@type {String}
@default ""
@htmlattribute value
*/
get Evalue() {
return this._getValueAsString(this._value, this.valueFormat);
}
set value(value) {
// This is used to change the value if valueformat is also set but afterwards
this._originalValue = value;
this._value = toMoment(value, this.valueFormat);
this._elements.calendar.valueAsDate = this.valueAsDate;
this._elements.clock.valueAsDate = this.valueAsDate;
this._elements.input.value = this._getValueAsString(this._value, this.displayFormat);
this._elements.hiddenInput.value = this.value;
}
/**
Short hint that describes the expected value of the Datepicker. It is displayed when the Datepicker is empty.
@type {String}
@default ""
@htmlattribute placeholder
@htmlattributereflected
*/
get placeholder() {
return this._elements.input.placeholder;
}
set placeholder(value) {
this._elements.input.placeholder = value;
this._reflectAttribute('placeholder', this.placeholder);
}
/**
The datepicker's variant. See {@link DatepickerVariantEnum}.
@type {DatepickerVariantEnum}
@default DatepickerVariantEnum.DEFAULT
@htmlattribute variant
@htmlattributereflected
*/
get variant() {
return this._variant || variant.DEFAULT;
}
set variant(value) {
value = transform.string(value).toLowerCase();
this._variant = validate.enumeration(variant)(value) && value || variant.DEFAULT;
// passes down the variant to the underlying components
this._elements.input.variant = this._variant;
this._elements.toggle.classList.toggle('_coral-FieldButton--quiet', this._variant === variant.QUIET);
// removes every existing variant
this.classList.remove(...ALL_VARIANT_CLASSES);
if (this._variant !== variant.DEFAULT) {
this.classList.add(`${CLASSNAME}--${this._variant}`);
}
}
/**
Name used to submit the data in a form.
@type {String}
@default ""
@htmlattribute name
@htmlattributereflected
*/
get name() {
return this._elements.hiddenInput.name;
}
set name(value) {
this._reflectAttribute('name', value);
this._elements.hiddenInput.name = value;
}
/**
Whether this field is disabled or not.
@type {Boolean}
@default false
@htmlattribute disabled
@htmlattributereflected
*/
get disabled() {
return this._disabled || false;
}
set disabled(value) {
this._disabled = transform.booleanAttr(value);
this._reflectAttribute('disabled', this._disabled);
this[this._disabled ? 'setAttribute' : 'removeAttribute']('aria-disabled', this._disabled);
this.classList.toggle('is-disabled', this._disabled);
this._elements.input.disabled = this._disabled;
this._elements.hiddenInput.disabled = this._disabled;
this._elements.toggle.disabled = this._disabled || this.readOnly;
this._elements.toggle.setAttribute("tabindex", !this._elements.toggle.disabled ? "0" : "-1");
}
/**
Inherited from {@link BaseFormField#invalid}.
*/
get invalid() {
return super.invalid;
}
set invalid(value) {
super.invalid = value;
I
this.classList.toggle('is-invalid', this.invalid);
this._elements.toggle.classList.toggle('is-invalid', this.invalid);
this._elements.input.invalid = this.invalid;
this._elements.input.setAttribute('aria-invalid', this.invalid);
}
/**
Whether this field is required or not.
@type {Boolean}
@default false
@htmlattribute required
@htmlattributereflected
*/
get required() {
return this._required || false;
}
set required(value) {
this._required = transform.booleanAttr(value);
this._reflectAttribute('required', this._required);
// this._elements.toggle.classList.toggle('is-invalid', this._required);
// this._elements.input.required = this._required;
this._elements.input.setAttribute("aria-required", this._required);
}
/**
Whether this field is readOnly or not. Indicating that the user cannot modify the value of the control.
@type {Boolean}
@default false
@htmlattribute readonly
@htmlattributereflected
*/
get readOnly() {
return this._readOnly || false;
}
set readOnly(value) {
this._readOnly = transform.booleanAttr(value);
this._reflectAttribute('readonly', this._readOnly);
this._elements.hiddenInput.readOnly = this.readOnly;
this._elements.input.readOnly = this._readOnly;
this._elements.toggle.disabled = this._readOnly || this.disabled;
this._elements.toggle.setAttribute("tabindex", !this._elements.toggle.disabled ? "0" : "-1");
}
/**
Inherited from {@link BaseFormField#labelled}.
*/
get labelled() {
return super.labelled;
}
set labelled(value) {
super.labelled = value;
// in case the user focuses the buttons, he will still get a notion of the usage of the component
this[this.labelled ? 'setAttribute' : 'removeAttribute']('aria-label', this.labelled);
this._elements.overlay[this.labelled ? 'setAttribute' : 'removeAttribute']('aria-label', this.labelled);
this._elements.calendar[this.labelled ? 'setAttribute' : 'removeAttribute']('labelled', this.labelled);
}
/**
Inherited from {@link BaseFormField#labelledBy}.
*/
get labelledBy() {
return super.labelledBy;
}
set labelledBy(value) {
super.labelledBy = value;
// in case the user focuses the buttons, he will still get a notion of the usage of the component
this[this.labelledBy ? 'setAttribute' : 'removeAttribute']('aria-labelledby', this.labelledBy);
this._elements.overlay[this.labelledBy ? 'setAttribute' : 'removeAttribute']('aria-labelledby', this.labelledBy);
this._elements.calendar[this.labelledBy ? 'setAttribute' : 'removeAttribute']('aria-labelledby', this.labelledBy);
}
/**
When <code>true</code> the component will default to the native input for the date selection. When
{@link Coral.Datepicker.type.DATETIME} has been set, it will still use the Coral way because mobile browsers
cannot handle a datetime input.
@ignore
*/
get _useNativeInput() {
return this.__useNativeInput;
}
set _useNativeInput(value) {
this.__useNativeInput = value;
// we ignore _useNativeInput when the type is datetime because it is not supported by mobile libraries
if (this.__useNativeInput && this.type !== type.DATETIME) {
// Switch to native date/time picker:
this._elements.toggle.hidden = true;
this._elements.input.setAttribute('type', this.type);
// Hide pop-over and remove related attributes:
this._elements.overlay.hidden = true;
this._elements.input.removeAttribute('role');
this._elements.input.removeAttribute('aria-autocomplete');
this._elements.input.removeAttribute('aria-haspopup');
this._elements.input.removeAttribute('aria-expanded');
this._elements.input.removeAttribute('aria-controls');
} else {
// Switch to Calendar picker
this._elements.toggle.hidden = false;
this._elements.input.setAttribute('type', 'text');
// Show pop-over and add related attributes:
this._elements.overlay.hidden = false;
// Input attributes per ARIA Autocomplete
this._elements.input.setAttribute('role', 'combobox');
this._elements.input.setAttribute('aria-autocomplete', 'none');
this._elements.input.setAttribute('aria-haspopup', 'dialog');
this._elements.input.setAttribute('aria-expanded', this._elements.overlay.open);
this._elements.input.setAttribute('aria-controls', this._elements.overlay.id);
// Trigger button attributes per ARIA Autocomplete
this._elements.toggle.setAttribute('aria-haspopup', 'dialog');
this._elements.toggle.setAttribute('aria-expanded', this._elements.overlay.open);
this._elements.toggle.setAttribute('aria-controls', this._elements.overlay.id);
}
}
/** @ignore */
_onPopoverBeforeOpen() {
this._elements.overlay.returnFocusTo(this._elements.input);
this._elements.calendar._validateCalendar();
this._renderCalendar();
}
/**
Matches the accessibility to the state of the popover.
@ignore
*/
_onPopoverOpenOrClose(event) {
// set focus to calendar grid
if (this._elements.overlay.open) {
if (this.type === type.TIME) {
this._elements.clock.focus();
} else {
this._elements.calendar.focus();
}
this._elements.input.setAttribute('aria-expanded', 'true');
this._elements.toggle.setAttribute('aria-expanded', 'true');
this._trackEvent('open', 'coral-datepicker', event);
} else {
this._elements.input.setAttribute('aria-expanded', 'false');
this._elements.toggle.setAttribute('aria-expanded', 'false');
this._trackEvent('close', 'coral-datepicker', event);
}
}
/** @ignore */
_onCalendarDayClick(event) {
if (event.target.tagName === 'A') {
// since a selection has been made, we close the popover. we cannot use the _onChange listener to handle this
// because clicking on the same button will not trigger a change event
this._elements.overlay.open = false;
this._trackEvent('click', 'coral-datepicker', event);
}
}
/** @ignore */
_onInputChange(event) {
// because we are reimplementing the form field mix in, we will have to stop the propagation and trigger the
// 'change' event from here
event.stopPropagation();
this.value = new DateTime.Moment(event.target.value, this.displayFormat);
this._validateValue();
this.trigger('change');
this._trackEvent('change', 'coral-datepicker', event);
}
/** @ignore */
_onChange(event) {
if (event.target.tagName === 'CORAL-CALENDAR' || event.target.tagName === 'CORAL-CLOCK') {
event.stopPropagation();
// we create the new value using both calendar and clock controls
// datepicker should set the current time as default when no time is set, but a date was chosen (if in datetime
// mode)
this.value = this._mergeCalendarAndClockDates(true);
this._validateValue();
this.trigger('change');
}
}
/** @private */
_onAltDownKey(event) {
// Stop any consequences of pressing the key
event.preventDefault();
if (!this._elements.overlay.open) {
this._elements.overlay.open = true;
}
}
/** @ignore */
_validateValue() {
// calendar validates only on user input, we have to manually force the validation
this._elements.calendar._validateCalendar();
// check if the current value is valid and update the internal state of the component
if (this.type === type.DATE) {
this.invalid = this._elements.calendar.invalid;
} else if (this.type === type.TIME) {
this.invalid = this._elements.clock.invalid;
} else {
this.invalid = this._elements.calendar.invalid || this._elements.clock.invalid;
}
}
/** @ignore */
_mergeCalendarAndClockDates(autoSetTimeIfNeeded) {
let value = new DateTime.Moment(this._elements.calendar.valueAsDate);
let time = this._elements.clock.valueAsDate;
if (autoSetTimeIfNeeded && value && !time && this.type === type.DATETIME) {
// datepicker should set the current time as default when no time is set, but a date was chosen (if in datetime
// mode)
time = new DateTime.Moment().toDate();
}
if (time) {
if (!value.isValid()) {
value = new DateTime.Moment();
}
value.hours(time.getHours());
value.minutes(time.getMinutes());
}
return value;
}
/**
Helper class that converts the internal moment value into a String using the provided date format. If the value is
invalid, empty string will be returned.
@param {?Moment} value
The value representing the date. It has to be a moment object or <code>null</code>
@param {String} format
The Date format to be used.
@ignore
*/
_getValueAsString(value, format) {
return value && value.isValid() ? value.format(format) : '';
}
/** @ignore */
_renderCalendar() {
if (this._elements.overlay.content.innerHTML === '') {
this._elements.overlay.content.appendChild(this._calendarFragment);
this._calendarFragment = undefined;
}
}
/**
Returns {@link Datepicker} variants.
@return {DatepickerVariantEnum}
*/
static get variant() {
return variant;
}
/**
Returns {@link Datepicker} types.
@return {DatepickerTypeEnum}
*/
static get type() {
return type;
}
static get _attributePropertyMap() {
return commons.extend(super._attributePropertyMap, {
startday: 'startDay',
headerformat: 'headerFormat',
displayformat: 'displayFormat',
valueformat: 'valueFormat'
});
}
/** @ignore */
static get observedAttributes() {
return super.observedAttributes.concat([
'min',
'max',
'type',
'placeholder',
'startday',
'headerFormat',
'displayformat',
'valueformat',
'variant'
]);
}
/** @ignore */
connectedCallback() {
super.connectedCallback();
const overlay = this._elements.overlay;
// Cannot be open by default when rendered
overlay.removeAttribute('open');
// Restore in DOM
if (overlay._parent) {
overlay._parent.appendChild(overlay);
}
}
/** @ignore */
render() {
super.render();
this.classList.add(CLASSNAME);
// a11y
this.setAttribute('role', 'group');
// Input attributes per ARIA Autocomplete
this._elements.input.setAttribute('role', 'combobox');
this._elements.input.setAttribute('aria-autocomplete', 'none');
this._elements.input.setAttribute('aria-haspopup', 'dialog');
this._elements.input.setAttribute('aria-expanded', 'false');
this._elements.input.setAttribute('aria-controls', this._elements.overlay.id);
// Trigger button attributes per ARIA Autocomplete
this._elements.toggle.setAttribute('aria-haspopup', 'dialog');
this._elements.toggle.setAttribute('aria-expanded', 'false');
this._elements.toggle.setAttribute('aria-controls', this._elements.overlay.id);
// a11y we only have AUTO mode.
this._useNativeInput = IS_MOBILE_DEVICE;
// Default reflected attributes
if (!this._variant) {
this.variant = variant.DEFAULT;
}
// "type" takes care of reflecting "displayFormat" and "valueFormat"
if (!this._type) {
this.type = type.DATE;
}
// clean up to be able to clone it
while (this.firstChild) {
this.removeChild(this.firstChild);
}
const frag = document.createDocumentFragment();
// Render template
frag.appendChild(this._elements.hiddenInput);
frag.appendChild(this._elements.input);
frag.appendChild(this._elements.toggle);
frag.appendChild(this._elements.overlay);
// Point at the button from the bottom
this._elements.overlay.target = this._elements.toggle;
this.appendChild(frag);
}
/** @ignore */
disconnectedCallback() {
super.disconnectedCallback();
const overlay = this._elements.overlay;
// In case it was moved out don't forget to remove it
if (!this.contains(overlay)) {
overlay._parent = overlay._repositioned ? document.body : this;
overlay.remove();
}
}
});
export default Datepicker;
|