2022-09-08 08:04:32 -07:00

150 lines
3.6 KiB
JavaScript

/**
* Datepicker wrapper.
*
* @author Htmlstream
* @version 1.0
*
*/
;(function ($) {
'use strict';
$.HSCore.components.HSDatepicker = {
/**
*
*
* @var Object _baseConfig
*/
_baseConfig: {
dateFormat: 'dd.mm.yy',
dayNamesMin: [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
],
prevText: '<i class="fa fa-angle-left"></i>',
nextText: '<i class="fa fa-angle-right"></i>'
},
/**
*
*
* @var jQuery pageCollection
*/
pageCollection: $(),
/**
* Initialization of Datepicker wrapper.
*
* @param String selector (optional)
* @param Object config (optional)
*
* @return jQuery pageCollection - collection of initialized items.
*/
init: function (selector, config) {
this.collection = selector && $(selector).length ? $(selector) : $();
if (!$(selector).length) return;
this.config = config && $.isPlainObject(config) ?
$.extend({}, this._baseConfig, config) : this._baseConfig;
this.config.itemSelector = selector;
this.initDatepicker();
return this.pageCollection;
},
initDatepicker: function () {
//Variables
var $self = this,
config = $self.config,
collection = $self.pageCollection;
//Actions
this.collection.each(function (i, el) {
//Variables
var $this = $(el),
to = $this.data('to'),
rangeBoolean = $this.data('range');
if (rangeBoolean == 1) {
var dateFrom = $this.datepicker({
dateFormat: config['dateFormat'],
defaultDate: '+1w',
dayNamesMin: config['dayNamesMin'],
numberOfMonths: 1,
showOtherMonths: true,
prevText: config['prevText'],
nextText: config['nextText'],
beforeShow: $self.datepickerCustomClass
}).on('change', function () {
dateTo.datepicker('option', 'minDate', $self.getDate(this));
});
var dateTo = $('#' + to).datepicker({
dateFormat: config['dateFormat'],
defaultDate: '+1w',
dayNamesMin: config['dayNamesMin'],
numberOfMonths: 1,
showOtherMonths: true,
prevText: config['prevText'],
nextText: config['nextText'],
beforeShow: $self.datepickerCustomClass
}).on('change', function () {
dateFrom.datepicker('option', 'maxDate', $self.getDate(this));
});
} else {
$this.datepicker({
dateFormat: config['dateFormat'],
dayNamesMin: config['dayNamesMin'],
showOtherMonths: true,
prevText: config['prevText'],
nextText: config['nextText'],
beforeShow: $self.datepickerCustomClass
});
}
//Actions
collection = collection.add($this);
});
},
datepickerCustomClass: function (el, attr) {
var arrayOfClasses, customClass, i;
arrayOfClasses = attr.input[0].className.split(' ');
for (i = 0; arrayOfClasses.length > i; i++) {
if (arrayOfClasses[i].substring(0, 6) == 'u-date') {
customClass = arrayOfClasses[i];
}
}
$('#ui-datepicker-div').addClass(customClass);
},
getDate: function (element) {
var $self = this,
date,
config = $self.config;
try {
date = $.datepicker.parseDate(config['dateFormat'], element.value);
} catch (error) {
date = null;
}
return date;
}
};
})(jQuery);