@luftborn/custom-elements 2.8.4 → 2.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/demo/index.min.js CHANGED
@@ -3006,6 +3006,7 @@ var CustomDatepicker = /** @class */ (function () {
3006
3006
  CustomDatepicker.prototype.createDatePickerElements = function () {
3007
3007
  var _this = this;
3008
3008
  this.datepicker = document.createElement('div');
3009
+ this.datepicker.id = this.input.id + '-datepicker';
3009
3010
  this.datepicker.classList.add('datepicker');
3010
3011
  this.header = document.createElement('div');
3011
3012
  this.header.classList.add('header');
@@ -3049,12 +3050,7 @@ var CustomDatepicker = /** @class */ (function () {
3049
3050
  today.value = 'Today';
3050
3051
  today.onclick = function () { return _this.setDateForToday(); };
3051
3052
  actions.appendChild(today);
3052
- if (this.input.parentElement) {
3053
- this.input.parentElement.appendChild(this.datepicker);
3054
- }
3055
- else {
3056
- document.body.appendChild(this.datepicker);
3057
- }
3053
+ document.body.appendChild(this.datepicker);
3058
3054
  this.applyStyles();
3059
3055
  };
3060
3056
  CustomDatepicker.prototype.applyStyles = function () {
@@ -3129,15 +3125,7 @@ var CustomDatepicker = /** @class */ (function () {
3129
3125
  _this.selectedYear = _this.currentYear;
3130
3126
  moveMonthSteps = 1;
3131
3127
  }
3132
- _this.selectedDate = new Date(_this.selectedYear, _this.selectedMonth, _this.selectedDay);
3133
- var selectedDayElement = _this.days.querySelector('.selected-day');
3134
- if (selectedDayElement) {
3135
- selectedDayElement.classList.remove('selected-day');
3136
- }
3137
- span.classList.add('selected-day');
3138
- var formattedDate = (0, CustomDatepickerUtils_1.formatDate)(_this.selectedDate, _this.dateFormat, _this.language);
3139
- _this.input.value = formattedDate;
3140
- _this.showPicker(false);
3128
+ _this.setSelectedDate(moveMonthSteps, span);
3141
3129
  };
3142
3130
  this_1.days.appendChild(span);
3143
3131
  };
@@ -3147,8 +3135,24 @@ var CustomDatepicker = /** @class */ (function () {
3147
3135
  }
3148
3136
  this.positionPicker();
3149
3137
  };
3138
+ CustomDatepicker.prototype.setSelectedDate = function (moveMonthSteps, span) {
3139
+ if (moveMonthSteps === void 0) { moveMonthSteps = 0; }
3140
+ this.selectedDate = new Date(this.selectedYear, this.selectedMonth, this.selectedDay);
3141
+ var selectedDayElement = this.days.querySelector('.selected-day');
3142
+ if (selectedDayElement) {
3143
+ selectedDayElement.classList.remove('selected-day');
3144
+ }
3145
+ span.classList.add('selected-day');
3146
+ var formattedDate = (0, CustomDatepickerUtils_1.formatDate)(this.selectedDate, this.dateFormat, this.language);
3147
+ this.input.value = formattedDate;
3148
+ this.showPicker(false);
3149
+ if (moveMonthSteps !== 0) {
3150
+ this.moveMonth(moveMonthSteps);
3151
+ }
3152
+ };
3150
3153
  CustomDatepicker.prototype.initSelectMonthYear = function () {
3151
3154
  var _this = this;
3155
+ var tempCurrentYear = this.currentYear;
3152
3156
  this.selectMonthYear.style.display = 'none';
3153
3157
  this.selectMonthYear.style.height = '200px';
3154
3158
  if ((0, CustomDatepickerUtils_1.isMobileDevice)()) {
@@ -3163,8 +3167,8 @@ var CustomDatepicker = /** @class */ (function () {
3163
3167
  span.classList.add('select-month');
3164
3168
  monthsContainer.appendChild(span);
3165
3169
  span.onclick = function () {
3166
- _this.selectedMonth = _this.months.findIndex(function (month) { return month.substring(0, 3) === span.innerHTML; });
3167
- var moveMonthSteps = (_this.selectedYear - _this.currentYear) * 12 + _this.selectedMonth - _this.currentMonth;
3170
+ var tempCurrentMonth = _this.months.findIndex(function (month) { return month.substring(0, 3) === span.innerHTML; });
3171
+ var moveMonthSteps = (tempCurrentYear - _this.currentYear) * 12 + tempCurrentMonth - _this.currentMonth;
3168
3172
  _this.moveMonth(moveMonthSteps);
3169
3173
  _this.hideSelectMonthYear();
3170
3174
  };
@@ -3183,16 +3187,14 @@ var CustomDatepicker = /** @class */ (function () {
3183
3187
  }
3184
3188
  }
3185
3189
  var years = this.selectMonthYear.querySelectorAll('.year');
3186
- var tempSelectedYear = this.selectedYear;
3187
3190
  var _loop_3 = function (i) {
3188
3191
  years[i].addEventListener('click', function () {
3189
3192
  var year = parseInt(years[i].innerHTML);
3190
- if (year === tempSelectedYear) {
3193
+ if (year === tempCurrentYear) {
3191
3194
  return;
3192
3195
  }
3193
3196
  else {
3194
- tempSelectedYear = year;
3195
- _this.selectedYear = year;
3197
+ tempCurrentYear = year;
3196
3198
  var monthsContainers = _this.selectMonthYear.querySelectorAll('.months');
3197
3199
  for (var i_1 = 0; i_1 < monthsContainers.length; i_1++) {
3198
3200
  monthsContainers[i_1].remove();
@@ -3230,15 +3232,36 @@ var CustomDatepicker = /** @class */ (function () {
3230
3232
  CustomDatepicker.prototype.showPicker = function (showpicker) {
3231
3233
  if (showpicker === void 0) { showpicker = true; }
3232
3234
  if (showpicker) {
3235
+ this.closeOtherDatepickers();
3236
+ localStorage.setItem('shownDatepicker', this.datepicker.id);
3237
+ localStorage.setItem('focusedInput', this.input.id);
3233
3238
  this.positionPicker();
3234
3239
  this.datepicker.style.display = 'block';
3240
+ this.navigateToSelectedMonth();
3235
3241
  }
3236
3242
  else {
3237
3243
  this.datepicker.style.display = 'none';
3244
+ this.closeOtherDatepickers();
3238
3245
  this.hideSelectMonthYear();
3246
+ this.input.placeholder = this.dateFormat;
3239
3247
  return;
3240
3248
  }
3241
3249
  };
3250
+ CustomDatepicker.prototype.closeOtherDatepickers = function () {
3251
+ var shownDatepickerId = localStorage.getItem('shownDatepicker');
3252
+ if (shownDatepickerId) {
3253
+ var shownDatepicker = document.getElementById(shownDatepickerId);
3254
+ if (shownDatepicker) {
3255
+ shownDatepicker.style.display = 'none';
3256
+ }
3257
+ }
3258
+ localStorage.removeItem('shownDatepicker');
3259
+ localStorage.removeItem('focusedInput');
3260
+ };
3261
+ CustomDatepicker.prototype.navigateToSelectedMonth = function () {
3262
+ var moveMonthSteps = (this.selectedYear - this.currentYear) * 12 + this.selectedMonth - this.currentMonth;
3263
+ this.moveMonth(moveMonthSteps);
3264
+ };
3242
3265
  CustomDatepicker.prototype.positionPicker = function () {
3243
3266
  this.datepicker.style.position = 'fixed';
3244
3267
  var datepickerHeight = 250;
@@ -3276,27 +3299,56 @@ var CustomDatepicker = /** @class */ (function () {
3276
3299
  this.input.value = (0, CustomDatepickerUtils_1.formatDate)(this.selectedDate, this.dateFormat, this.language);
3277
3300
  this.showPicker(false);
3278
3301
  };
3302
+ CustomDatepicker.prototype.setDateUsingInputValue = function () {
3303
+ var _this = this;
3304
+ var date = (0, CustomDatepickerUtils_1.convertStringToDate)(this.input.value, this.dateFormat, this.language);
3305
+ if (date) {
3306
+ this.selectedDate = date;
3307
+ this.selectedMonth = date.getMonth();
3308
+ this.selectedYear = date.getFullYear();
3309
+ this.selectedDay = date.getDate();
3310
+ this.renderCalendar();
3311
+ this.input.value = (0, CustomDatepickerUtils_1.formatDate)(this.selectedDate, this.dateFormat, this.language);
3312
+ this.input.blur();
3313
+ }
3314
+ else {
3315
+ this.input.value = 'Invalid Date';
3316
+ setTimeout(function () {
3317
+ _this.input.value = '';
3318
+ }, 300);
3319
+ }
3320
+ this.showPicker(false);
3321
+ };
3279
3322
  CustomDatepicker.prototype.addEventListeners = function () {
3280
3323
  var _this = this;
3281
3324
  this.input.addEventListener('click', function () { return _this.showPicker(true); });
3282
3325
  this.input.addEventListener('focus', function () { return _this.showPicker(true); });
3283
3326
  this.input.addEventListener('change', function () {
3284
- var date = new Date(_this.input.value);
3285
- if (date.toString() !== 'Invalid Date') {
3286
- _this.selectedDate = date;
3287
- _this.selectedMonth = date.getMonth();
3288
- _this.selectedYear = date.getFullYear();
3289
- _this.selectedDay = date.getDate();
3290
- _this.renderCalendar();
3327
+ _this.setDateUsingInputValue();
3328
+ });
3329
+ this.input.addEventListener('keydown', function (event) {
3330
+ if (event.key === 'Enter') {
3331
+ _this.setDateUsingInputValue();
3291
3332
  }
3292
3333
  });
3293
3334
  this.otherTriggers.forEach(function (trigger) {
3294
3335
  trigger.addEventListener('click', function () { return _this.showPicker(true); });
3295
3336
  });
3296
3337
  document.onclick = function (event) {
3297
- var elementsToNeglect = [_this.input, _this.datepicker, _this.monthYear, _this.weekdays, _this.days, _this.header, _this.selectMonthYear];
3298
3338
  var element = event.target;
3299
- var isNeglected = elementsToNeglect.includes(element) || elementsToNeglect.includes(element.parentElement) || (element).classList.contains('select-month') || element === _this.parentElement;
3339
+ var shownDatepicker = document.getElementById(localStorage.getItem('shownDatepicker'));
3340
+ if (shownDatepicker) {
3341
+ var elementInsideDatepicker = shownDatepicker.querySelector(element.tagName);
3342
+ if (elementInsideDatepicker) {
3343
+ return;
3344
+ }
3345
+ }
3346
+ var elementsToNeglect = [_this.input, _this.datepicker, _this.monthYear, _this.weekdays, _this.days, _this.header, _this.selectMonthYear];
3347
+ var isNeglected = elementsToNeglect.includes(element)
3348
+ || elementsToNeglect.includes(element.parentElement)
3349
+ || (element).classList.contains('select-month')
3350
+ || element === _this.parentElement
3351
+ || element.tagName === 'CUSTOM-FORMAT-DATE-ELEMENT';
3300
3352
  if (!isNeglected) {
3301
3353
  _this.showPicker(false);
3302
3354
  }
@@ -3315,7 +3367,7 @@ exports.CustomDatepickerStyles = "\n .datepicker {\n position: fixed;\
3315
3367
  },{}],17:[function(require,module,exports){
3316
3368
  "use strict";
3317
3369
  Object.defineProperty(exports, "__esModule", { value: true });
3318
- exports.isMobileDevice = exports.formatDate = exports.supportedDateFormats = exports.defaultDateFormat = exports.daysOfWeek = exports.months = void 0;
3370
+ exports.convertStringToDate = exports.isMobileDevice = exports.formatDate = exports.supportedDateFormats = exports.defaultDateFormat = exports.daysOfWeek = exports.months = void 0;
3319
3371
  var DatepickerTranslations_1 = require("./DatepickerTranslations");
3320
3372
  exports.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
3321
3373
  exports.daysOfWeek = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
@@ -3335,24 +3387,20 @@ exports.supportedDateFormats = [
3335
3387
  ];
3336
3388
  function formatDate(date, dateFormat, language) {
3337
3389
  if (language === void 0) { language = 'en'; }
3390
+ debugger;
3338
3391
  var year = date.getFullYear();
3339
3392
  var month = (date.getMonth() + 1).toString().padStart(2, '0');
3340
3393
  var day = date.getDate().toString().padStart(2, '0');
3341
3394
  var monthName = (0, DatepickerTranslations_1.GetMonths)(language)[date.getMonth()];
3342
3395
  var capitalizedMonthName = monthName.charAt(0).toUpperCase() + monthName.slice(1);
3343
- var dateFormats = {
3344
- 'ddmmyyyy': "" + day + month + year,
3345
- 'mmddyyyy': "" + month + day + year,
3346
- 'dd/mm/yyyy': day + "/" + month + "/" + year,
3347
- 'mm/dd/yyyy': month + "/" + day + "/" + year,
3348
- 'dd-mm-yyyy': day + "-" + month + "-" + year,
3349
- 'mm-dd-yyyy': month + "-" + day + "-" + year,
3350
- 'yyyy-mm-dd': year + "-" + month + "-" + day,
3351
- 'yyyy-dd-mm': year + "-" + day + "-" + month,
3352
- 'Month dd, yyyy': capitalizedMonthName + " " + day + ", " + year,
3353
- 'mm/dd/yy': month + "/" + day + "/" + year.toString().slice(-2),
3354
- 'dd/mm/yy': day + "/" + month + "/" + year.toString().slice(-2),
3355
- };
3396
+ var dateFormats = exports.supportedDateFormats.reduce(function (acc, format) {
3397
+ acc[format] = format.replace('yyyy', year.toString())
3398
+ .replace('yy', year.toString().slice(-2))
3399
+ .replace('mm', month)
3400
+ .replace('dd', day)
3401
+ .replace('Month', capitalizedMonthName);
3402
+ return acc;
3403
+ }, {});
3356
3404
  return dateFormats[dateFormat];
3357
3405
  }
3358
3406
  exports.formatDate = formatDate;
@@ -3360,6 +3408,88 @@ function isMobileDevice() {
3360
3408
  return /Android|webOS|iPhone|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth < 480;
3361
3409
  }
3362
3410
  exports.isMobileDevice = isMobileDevice;
3411
+ function convertStringToDate(dateString, dateFormat, language) {
3412
+ var _a, _b, _c;
3413
+ if (language === void 0) { language = 'en'; }
3414
+ debugger;
3415
+ var year, month, day;
3416
+ if (!validateDateString(dateString, dateFormat, language)) {
3417
+ return null;
3418
+ }
3419
+ var getMonthIndex = function (monthName) {
3420
+ var months = (0, DatepickerTranslations_1.GetMonths)(language).map(function (month) { return month.toLowerCase(); });
3421
+ var englishMonths = (0, DatepickerTranslations_1.GetMonths)('en').map(function (month) { return month.toLowerCase(); });
3422
+ var index = months.indexOf(monthName.toLowerCase());
3423
+ if (index === -1) {
3424
+ var englishMonthIndex = englishMonths.indexOf(monthName.toLowerCase());
3425
+ return englishMonthIndex === -1 ? NaN : englishMonthIndex;
3426
+ }
3427
+ return index + 1;
3428
+ };
3429
+ switch (dateFormat) {
3430
+ case 'ddmmyyyy':
3431
+ day = parseInt(dateString.substring(0, 2));
3432
+ month = parseInt(dateString.substring(2, 4));
3433
+ year = parseInt(dateString.substring(4, 8));
3434
+ break;
3435
+ case 'mmddyyyy':
3436
+ month = parseInt(dateString.substring(0, 2));
3437
+ day = parseInt(dateString.substring(2, 4));
3438
+ year = parseInt(dateString.substring(4, 8));
3439
+ break;
3440
+ case 'dd/mm/yyyy':
3441
+ case 'dd-mm-yyyy':
3442
+ _a = dateString.split(/\/|-/).map(Number), day = _a[0], month = _a[1], year = _a[2];
3443
+ break;
3444
+ case 'mm/dd/yyyy':
3445
+ case 'mm-dd-yyyy':
3446
+ _b = dateString.split(/\/|-/).map(Number), month = _b[0], day = _b[1], year = _b[2];
3447
+ break;
3448
+ case 'yyyy-mm-dd':
3449
+ case 'yyyy-dd-mm':
3450
+ _c = dateFormat === 'yyyy-mm-dd' ? dateString.split('-').map(Number) : [dateString.slice(0, 4), dateString.slice(8, 10), dateString.slice(5, 7)].map(Number), year = _c[0], month = _c[1], day = _c[2];
3451
+ break;
3452
+ case 'Month dd, yyyy':
3453
+ var parts = dateString.split(' ');
3454
+ year = parseInt(parts[2]);
3455
+ month = getMonthIndex(parts[0]);
3456
+ day = parseInt(parts[1].replace(',', ''));
3457
+ break;
3458
+ case 'mm/dd/yy':
3459
+ var _d = dateString.split('/'), monthSt = _d[0], daySt = _d[1], yearSt = _d[2];
3460
+ month = parseInt(monthSt);
3461
+ day = parseInt(daySt);
3462
+ year = parseInt(yearSt) + 2000;
3463
+ break;
3464
+ case 'dd/mm/yy':
3465
+ var _e = dateString.split('/'), dayStr = _e[0], monthStr = _e[1], yearStr = _e[2];
3466
+ day = parseInt(dayStr);
3467
+ month = parseInt(monthStr);
3468
+ year = parseInt(yearStr) + 2000;
3469
+ break;
3470
+ default:
3471
+ return null;
3472
+ }
3473
+ if (!isNaN(year) && !isNaN(month) && !isNaN(day)) {
3474
+ return new Date(year, month - 1, day);
3475
+ }
3476
+ else {
3477
+ return null;
3478
+ }
3479
+ }
3480
+ exports.convertStringToDate = convertStringToDate;
3481
+ function validateDateString(dateString, dateFormat, language) {
3482
+ if (language === void 0) { language = 'en'; }
3483
+ var dateFormatsRegex = exports.supportedDateFormats.reduce(function (acc, format) {
3484
+ acc[format] = new RegExp("^" + format.replace('yyyy', '\\d{4}')
3485
+ .replace('yy', '\\d{2}')
3486
+ .replace('mm', '(0[1-9]|1[0-2])')
3487
+ .replace('dd', '(0[1-9]|[12][0-9]|3[01])')
3488
+ .replace('Month', (0, DatepickerTranslations_1.GetMonths)(language).join('|') + '|' + (0, DatepickerTranslations_1.GetMonths)('en').join('|')) + "$", 'i');
3489
+ return acc;
3490
+ }, {});
3491
+ return dateFormatsRegex[dateFormat].test(dateString);
3492
+ }
3363
3493
 
3364
3494
  },{"./DatepickerTranslations":18}],18:[function(require,module,exports){
3365
3495
  "use strict";
@@ -3614,6 +3744,7 @@ var CustomFormatDateFieldElement = /** @class */ (function (_super) {
3614
3744
  CustomFormatDateFieldElement.prototype.initChildInputs = function () {
3615
3745
  var _a;
3616
3746
  this.date = _super.prototype.getChildInput.call(this, '#date-field');
3747
+ this.date.id = "date-field-" + Date.now();
3617
3748
  this.pickerTrigger = this.getChildElement('#picker-trigger');
3618
3749
  this.dateFormat = CustomDatepickerUtils_1.supportedDateFormats.includes(this.dateFormat) ? this.dateFormat : CustomDatepickerUtils_1.defaultDateFormat;
3619
3750
  this.date.placeholder = this.dateFormat;
@@ -3662,7 +3793,7 @@ var CustomFormatDateFieldElement = /** @class */ (function (_super) {
3662
3793
  CustomFormatDateFieldElement = __decorate([
3663
3794
  (0, custom_element_decorator_1.default)({
3664
3795
  selector: 'custom-format-date-element',
3665
- template: "\n\t\t<div class=\"wrapper\">\n\t\t\t<input type=\"text\" id=\"date-field\" readonly />\n\t\t\t<svg id=\"picker-trigger\" data-toggle viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><rect x=\"2\" y=\"4\" width=\"20\" height=\"18\" rx=\"1\" fill=\"#000\"/><rect x=\"4\" y=\"8\" width=\"16\" height=\"12\" fill=\"white\"/><path d=\"M4 10H20\" stroke=\"#000\" stroke-width=\"1\"/><circle cx=\"16\" cy=\"14\" r=\"2\" fill=\"#F44336\"/><rect x=\"6\" y=\"2\" width=\"3\" height=\"4\" rx=\".5\" fill=\"#000\"/><rect x=\"15\" y=\"2\" width=\"3\" height=\"4\" rx=\".5\" fill=\"#000\"/></svg>\n\t\t</div>",
3796
+ template: "\n\t\t<div class=\"wrapper\">\n\t\t\t<input type=\"text\" id=\"date-field\" />\n\t\t\t<svg id=\"picker-trigger\" data-toggle viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><rect x=\"2\" y=\"4\" width=\"20\" height=\"18\" rx=\"1\" fill=\"#000\"/><rect x=\"4\" y=\"8\" width=\"16\" height=\"12\" fill=\"white\"/><path d=\"M4 10H20\" stroke=\"#000\" stroke-width=\"1\"/><circle cx=\"16\" cy=\"14\" r=\"2\" fill=\"#F44336\"/><rect x=\"6\" y=\"2\" width=\"3\" height=\"4\" rx=\".5\" fill=\"#000\"/><rect x=\"15\" y=\"2\" width=\"3\" height=\"4\" rx=\".5\" fill=\"#000\"/></svg>\n\t\t</div>",
3666
3797
  style: "\n\t\t:host{\n\t\t\twidth:100%;\n\t\t}\n\t\t* {\n\t\t\tbox-sizing: border-box;\n\t\t}\n\t\t.wrapper{\n\t\t\tdisplay:flex;\n\t\t\tposition: relative;\n\t\t}\n\t\tinput{\n\t\t\tbox-sizing: border-box;\n\t\t\twidth: 100% !important;\n\t\t\tborder: none;\n\t\t\tbackground-color: #f1f4ff;\n\t\t\tmargin: 2px;\n\t\t\tresize: none;\n\t\t}\n\t\t#date-field::after {\n\t\t\tcontent: url('path/to/datepicker-icon.png'); /* Path to your datepicker icon */\n\t\t\tcursor: pointer;\n\t\t\tright: 10px;\n\t\t}\n\t\t#picker-trigger {\n\t\t\tcursor: pointer;\n\t\t\twidth: 15px;\n\t\t\theight: 15px;\n\t\t\tposition:absolute;\n\t\t\tright: 2px;\n\t\t\ttop: 15%;\n\t\t}\n\t",
3667
3798
  useShadow: true,
3668
3799
  })