/*!
* jQuery UI date range picker widget
* Copyright (c) 2017 Tamble, Inc.
* Licensed under MIT (https://github.com/tamble/jquery-ui-daterangepicker/raw/master/LICENSE.txt)
*
* Depends:
* - jQuery 1.8.3+
* - jQuery UI 1.9.0+ (widget factory, position utility, button, menu, datepicker)
* - moment.js 2.3.0+
*/
(function($, window, undefined) {
var uniqueId = 0; // used for unique ID generation within multiple plugin instances
$.widget('comiseo.daterangepicker', {
version: '0.6.0-beta.1',
options: {
// presetRanges: array of objects; each object describes an item in the presets menu
// and must have the properties: text, dateStart, dateEnd.
// dateStart, dateEnd are functions returning a moment object
presetRanges: [
{text: 'Today', dateStart: function() { return moment() }, dateEnd: function() { return moment() } },
{text: 'Yesterday', dateStart: function() { return moment().subtract('days', 1) }, dateEnd: function() { return moment().subtract('days', 1) } },
{text: 'Last 7 Days', dateStart: function() { return moment().subtract('days', 6) }, dateEnd: function() { return moment() } },
{text: 'Last Week (Mo-Su)', dateStart: function() { return moment().subtract('days', 7).isoWeekday(1) }, dateEnd: function() { return moment().subtract('days', 7).isoWeekday(7) } },
{text: 'Month to Date', dateStart: function() { return moment().startOf('month') }, dateEnd: function() { return moment() } },
{text: 'Previous Month', dateStart: function() { return moment().subtract('month', 1).startOf('month') }, dateEnd: function() { return moment().subtract('month', 1).endOf('month') } },
{text: 'Year to Date', dateStart: function() { return moment().startOf('year') }, dateEnd: function() { return moment() } }
],
initialText: 'Select date range...', // placeholder text - shown when nothing is selected
icon: 'ui-icon-triangle-1-s',
applyButtonText: 'Apply', // use '' to get rid of the button
clearButtonText: 'Clear', // use '' to get rid of the button
cancelButtonText: 'Cancel', // use '' to get rid of the button
rangeSplitter: ' - ', // string to use between dates
dateFormat: 'M d, yy', // displayed date format. Available formats: http://api.jqueryui.com/datepicker/#utility-formatDate
altFormat: 'yy-mm-dd', // submitted date format - inside JSON {"start":"...","end":"..."}
verticalOffset: 0, // offset of the dropdown relative to the closest edge of the trigger button
mirrorOnCollision: true, // reverse layout when there is not enough space on the right
autoFitCalendars: true, // override datepicker's numberOfMonths option in order to fit widget width
applyOnMenuSelect: true, // whether to auto apply menu selections
open: null, // callback that executes when the dropdown opens
close: null, // callback that executes when the dropdown closes
change: null, // callback that executes when the date range changes
clear: null, // callback that executes when the clear button is used
cancel: null, // callback that executes when the cancel button is used
onOpen: null, // @deprecated callback that executes when the dropdown opens
onClose: null, // @deprecated callback that executes when the dropdown closes
onChange: null, // @deprecated callback that executes when the date range changes
onClear: null, // @deprecated callback that executes when the clear button is used
datepickerOptions: { // object containing datepicker options. See http://api.jqueryui.com/datepicker/#options
numberOfMonths: 3,
// showCurrentAtPos: 1 // bug; use maxDate instead
maxDate: 0 // the maximum selectable date is today (also current month is displayed on the last position)
}
},
_create: function() {
this._dateRangePicker = buildDateRangePicker(this.element, this, this.options);
},
_destroy: function() {
this._dateRangePicker.destroy();
},
_setOptions: function(options) {
this._super(options);
this._dateRangePicker.enforceOptions();
},
open: function() {
this._dateRangePicker.open();
},
close: function() {
this._dateRangePicker.close();
},
setRange: function(range) {
this._dateRangePicker.setRange(range);
},
getRange: function() {
return this._dateRangePicker.getRange();
},
clearRange: function() {
this._dateRangePicker.clearRange();
},
widget: function() {
return this._dateRangePicker.getContainer();
}
});
/**
* factory for the trigger button (which visually replaces the original input form element)
*
* @param {jQuery} $originalElement jQuery object containing the input form element used to instantiate this widget instance
* @param {String} classnameContext classname of the parent container
* @param {Object} options
*/
function buildTriggerButton($originalElement, classnameContext, options) {
var $self, id;
function fixReferences() {
id = 'drp_autogen' + uniqueId++;
$('label[for="' + $originalElement.attr('id') + '"]')
.attr('for', id);
}
function fixButton() {
if ($.fn.button.noConflict) {
var btn = $.fn.button.noConflict(); // reverts $.fn.button to jqueryui btn
$.fn.btn = btn; // assigns bootstrap button functionality to $.fn.btn
}
}
function init() {
fixReferences();
fixButton();
$self = $('')
.addClass(classnameContext + '-triggerbutton')
.attr({'title': $originalElement.attr('title'), 'tabindex': $originalElement.attr('tabindex'), id: id})
.button({
icons: {
secondary: options.icon
},
icon: options.icon,
iconPosition: 'end',
label: options.initialText
});
}
function getLabel() {
return $self.button('option', 'label');
}
function setLabel(value) {
$self.button('option', 'label', value);
}
function reset() {
$originalElement.val('').change();
setLabel(options.initialText);
}
function enforceOptions() {
$self.button('option', {
icons: {
secondary: options.icon
},
icon: options.icon,
iconPosition: 'end',
label: options.initialText
});
}
init();
return {
getElement: function() { return $self; },
getLabel: getLabel,
setLabel: setLabel,
reset: reset,
enforceOptions: enforceOptions
};
}
/**
* factory for the presets menu (containing built-in date ranges)
*
* @param {String} classnameContext classname of the parent container
* @param {Object} options
* @param {Function} onClick callback that executes when a preset is clicked
*/
function buildPresetsMenu(classnameContext, options, onClick) {
var $self,
$menu,
menuItemWrapper;
function init() {
$self = $('