Skip to content

Commit

Permalink
Fix #4: Add exception when invalid language found
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Aug 2, 2014
1 parent 5b06456 commit 8f0f661
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 60 deletions.
125 changes: 65 additions & 60 deletions assets/js/daterangepicker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @version: 1.3.7
* @author: Dan Grossman http://www.dangrossman.info/
* @date: 2014-04-29
* @copyright: Copyright (c) 2012-2014 Dan Grossman. All rights reserved.
* @license: Licensed under Apache License v2.0. See http://www.apache.org/licenses/LICENSE-2.0
* @website: http://www.improvely.com/
*/
* @version: 1.3.7
* @author: Dan Grossman http://www.dangrossman.info/
* @date: 2014-04-29
* @copyright: Copyright (c) 2012-2014 Dan Grossman. All rights reserved.
* @license: Licensed under Apache License v2.0. See http://www.apache.org/licenses/LICENSE-2.0
* @website: http://www.improvely.com/
*/
!function ($, moment) {

var DateRangePicker = function (element, options, cb) {
Expand All @@ -17,24 +17,25 @@
this.element = $(element);

//create the picker HTML object
var DRPTemplate = '<div class="daterangepicker dropdown-menu">' +
'<div class="calendar left"></div>' +
'<div class="calendar right"></div>' +
'<div class="ranges">' +
'<div class="range_inputs">' +
'<div class="daterangepicker_start_input">' +
'<label for="daterangepicker_start"></label>' +
'<input class="input-mini" type="text" name="daterangepicker_start" value="" readonly="readonly" />' +
'</div>' +
'<div class="daterangepicker_end_input">' +
'<label for="daterangepicker_end"></label>' +
'<input class="input-mini" type="text" name="daterangepicker_end" value="" readonly="readonly" />' +
'</div>' +
'<button class="applyBtn" disabled="disabled"></button>&nbsp;' +
'<button class="cancelBtn"></button>' +
'</div>' +
'</div>' +
'</div>';
var DRPTemplate = '\n' +
'<div class="daterangepicker dropdown-menu">\n' +
' <div class="calendar left"></div>\n' +
' <div class="calendar right"></div>\n' +
' <div class="ranges">\n' +
' <div class="range_inputs">\n' +
' <div class="daterangepicker_start_input">\n' +
' <label for="daterangepicker_start"></label>\n' +
' <input class="input-mini" type="text" name="daterangepicker_start" value="" readonly="readonly" />\n' +
' </div>\n' +
' <div class="daterangepicker_end_input">\n' +
' <label for="daterangepicker_end"></label>\n' +
' <input class="input-mini" type="text" name="daterangepicker_end" value="" readonly="readonly" />\n' +
' </div>\n' +
' <button class="applyBtn" disabled="disabled"></button>&nbsp;\n' +
' <button class="cancelBtn"></button>\n' +
' </div>\n' +
' </div>\n' +
'</div>';

//custom options
if (typeof options !== 'object' || options === null)
Expand Down Expand Up @@ -95,7 +96,7 @@

constructor: DateRangePicker,

setOptions: function(options, callback) {
setOptions: function (options, callback) {

this.startDate = moment().startOf('day');
this.endDate = moment().endOf('day');
Expand Down Expand Up @@ -134,7 +135,8 @@
firstDay: 0
};

this.cb = function () { };
this.cb = function () {
};

if (typeof options.format === 'string')
this.format = options.format;
Expand Down Expand Up @@ -185,7 +187,7 @@
}

if (typeof options.locale.monthNames === 'object') {
this.locale.monthNames = options.locale.monthNames.slice();
this.locale.monthNames = options.locale.monthNames.slice();
}

if (typeof options.locale.firstDay === 'number') {
Expand All @@ -198,27 +200,27 @@
}

if (typeof options.locale.applyLabel === 'string') {
this.locale.applyLabel = options.locale.applyLabel;
this.locale.applyLabel = options.locale.applyLabel;
}

if (typeof options.locale.cancelLabel === 'string') {
this.locale.cancelLabel = options.locale.cancelLabel;
this.locale.cancelLabel = options.locale.cancelLabel;
}

if (typeof options.locale.fromLabel === 'string') {
this.locale.fromLabel = options.locale.fromLabel;
this.locale.fromLabel = options.locale.fromLabel;
}

if (typeof options.locale.toLabel === 'string') {
this.locale.toLabel = options.locale.toLabel;
this.locale.toLabel = options.locale.toLabel;
}

if (typeof options.locale.weekLabel === 'string') {
this.locale.weekLabel = options.locale.weekLabel;
this.locale.weekLabel = options.locale.weekLabel;
}

if (typeof options.locale.customRangeLabel === 'string') {
this.locale.customRangeLabel = options.locale.customRangeLabel;
this.locale.customRangeLabel = options.locale.customRangeLabel;
}
}

Expand Down Expand Up @@ -368,7 +370,7 @@

},

setStartDate: function(startDate) {
setStartDate: function (startDate) {
if (typeof startDate === 'string')
this.startDate = moment(startDate, this.format);

Expand All @@ -385,7 +387,7 @@
this.updateInputText();
},

setEndDate: function(endDate) {
setEndDate: function (endDate) {
if (typeof endDate === 'string')
this.endDate = moment(endDate, this.format);

Expand Down Expand Up @@ -427,7 +429,7 @@
start = null,
end = null;

if(dateString.length === 2) {
if (dateString.length === 2) {
start = moment(dateString[0], this.format);
end = moment(dateString[1], this.format);
}
Expand Down Expand Up @@ -506,14 +508,16 @@
this.move();

// Create a click proxy that is private to this instance of datepicker, for unbinding
this._outsideClickProxy = $.proxy(function (e) { this.outsideClick(e); }, this);
this._outsideClickProxy = $.proxy(function (e) {
this.outsideClick(e);
}, this);
// Bind global datepicker mousedown for hiding and
$(document)
.on('mousedown.daterangepicker', this._outsideClickProxy)
// also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
.on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
// and also close when focus changes to outside the picker (eg. tabbing between controls)
.on('focusin.daterangepicker', this._outsideClickProxy);
.on('mousedown.daterangepicker', this._outsideClickProxy)
// also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
.on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
// and also close when focus changes to outside the picker (eg. tabbing between controls)
.on('focusin.daterangepicker', this._outsideClickProxy);

this.element.trigger('show.daterangepicker', this);
},
Expand All @@ -524,17 +528,17 @@
// itself then call this.hide()
if (
target.closest(this.element).length ||
target.closest(this.container).length ||
target.closest('.calendar-date').length
target.closest(this.container).length ||
target.closest('.calendar-date').length
) return;
this.hide();
},

hide: function (e) {
$(document)
.off('mousedown.daterangepicker', this._outsideClickProxy)
.off('click.daterangepicker', this._outsideClickProxy)
.off('focusin.daterangepicker', this._outsideClickProxy);
.off('mousedown.daterangepicker', this._outsideClickProxy)
.off('click.daterangepicker', this._outsideClickProxy)
.off('focusin.daterangepicker', this._outsideClickProxy);

this.element.removeClass('active');
this.container.hide();
Expand All @@ -560,18 +564,18 @@
}
},

showCalendars: function() {
showCalendars: function () {
this.container.addClass('show-calendar');
this.move();
this.element.trigger('showCalendar.daterangepicker', this);
},

hideCalendars: function() {
hideCalendars: function () {
this.container.removeClass('show-calendar');
this.element.trigger('hideCalendar.daterangepicker', this);
},

updateInputText: function() {
updateInputText: function () {
if (this.element.is('input') && !this.singleDatePicker) {
this.element.val(this.startDate.format(this.format) + this.separator + this.endDate.format(this.format));
} else if (this.element.is('input')) {
Expand Down Expand Up @@ -720,17 +724,17 @@
updateMonthYear: function (e) {
var isLeft = $(e.target).closest('.calendar').hasClass('left'),
leftOrRight = isLeft ? 'left' : 'right',
cal = this.container.find('.calendar.'+leftOrRight);
cal = this.container.find('.calendar.' + leftOrRight);

// Month must be Number for new moment versions
var month = parseInt(cal.find('.monthselect').val(), 10);
var year = cal.find('.yearselect').val();

this[leftOrRight+'Calendar'].month.month(month).year(year);
this[leftOrRight + 'Calendar'].month.month(month).year(year);
this.updateCalendars();
},

updateTime: function(e) {
updateTime: function (e) {

var cal = $(e.target).closest('.calendar'),
isLeft = cal.hasClass('left');
Expand Down Expand Up @@ -933,8 +937,12 @@
}
} else if (calendar[row][col] >= this.startDate && calendar[row][col] <= this.endDate) {
cname += ' in-range ';
if (calendar[row][col].isSame(this.startDate)) { cname += ' start-date '; }
if (calendar[row][col].isSame(this.endDate)) { cname += ' end-date '; }
if (calendar[row][col].isSame(this.startDate)) {
cname += ' start-date ';
}
if (calendar[row][col].isSame(this.endDate)) {
cname += ' end-date ';
}
}

var title = 'r' + row + 'c' + col;
Expand Down Expand Up @@ -1004,15 +1012,12 @@
}

return html;

},

remove: function() {

remove: function () {
this.container.remove();
this.element.off('.daterangepicker');
this.element.removeData('daterangepicker');

}

};
Expand Down
17 changes: 17 additions & 0 deletions daterange/DateRangePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public function init()
{
parent::init();
$this->initI18N();
if (!$this->checkLocale()) {
throw new InvalidConfigException("The locales have not been setup for your language '{$this->language}'. \n
You can submit the translations by reading the DateRangePicker documentation. \n
Alternatively, to continue without using any translations set the 'language' property to 'en' in your widget configuration.");
}
if ($this->convertFormat && isset($this->pluginOptions['format'])) {
$this->pluginOptions['format'] = static::convertDateFormat($this->pluginOptions['format']);
}
Expand Down Expand Up @@ -167,6 +172,18 @@ protected function initRange()
$this->pluginOptions['ranges'] = $range;
}

/**
* Check if the locale has been setup
*/
protected function checkLocale() {
if (substr($this->language, 0, 2) == 'en') {
return true;
}
$s = DIRECTORY_SEPARATOR;
$file = __DIR__ . "{$s}..{$s}assets{$s}js{$s}locales{$s}daterange-{$this->language}.js";
return (file_exists($file));
}

/**
* Parses and returns a JsExpression
*
Expand Down

1 comment on commit 8f0f661

@Agrumas
Copy link
Contributor

@Agrumas Agrumas commented on 8f0f661 Aug 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe better solution would be silently change locale to en. As user i aspect that extension would work out-of-the-box. Mandatory language definition makes no sense, it is just workaround and makes more work in the future, e.g. after translating i would have to find all widget calls and alter config.

Please sign in to comment.