vendor/assets/javascripts/pikaday.js in pikaday-gem-1.0.0.2 vs vendor/assets/javascripts/pikaday.js in pikaday-gem-1.1.0.0
- old
+ new
@@ -1,21 +1,41 @@
/*!
* Pikaday
- * Copyright © 2012 David Bushell | BSD & MIT license | http://dbushell.com/
+ *
+ * Copyright © 2013 David Bushell | BSD & MIT license | https://github.com/dbushell/Pikaday
*/
-(function(window, document, undefined)
+(function (root, define, factory)
{
'use strict';
+ if (typeof define === 'function' && define.amd) {
+ // AMD. Register as an anonymous module.
+ define(function (req)
+ {
+ // Load moment.js as an optional dependency
+ var id = 'moment';
+ var moment = req.defined && req.defined(id) ? req(id) : undefined;
+ return factory(moment || root.moment);
+ });
+ } else {
+ // Browser global
+ root.Pikaday = factory(root.moment);
+ }
+}(window, window.define, function (moment)
+{
+ 'use strict';
+
/**
* feature detection and helper functions
*/
- var hasMoment = typeof window.moment === 'function',
+ var hasMoment = typeof moment === 'function',
hasEventListeners = !!window.addEventListener,
+ document = window.document,
+
sto = window.setTimeout,
addEvent = function(el, e, callback, capture)
{
if (hasEventListeners) {
@@ -172,16 +192,15 @@
// how many months are visible (not implemented yet)
numberOfMonths: 1,
// internationalization
i18n: {
- previousMonth : 'Previous Month',
- nextMonth : 'Next Month',
- months : ['January','February','March','April','May','June','July','August','September','October','November','December'],
- //monthsShort : ['Jan_Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
- weekdays : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
- weekdaysShort : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
+ previousMonth : 'Previous Month',
+ nextMonth : 'Next Month',
+ months : ['January','February','March','April','May','June','July','August','September','October','November','December'],
+ weekdays : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
+ weekdaysShort : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
},
// callback function
onSelect: null,
onOpen: null,
@@ -289,17 +308,17 @@
},
renderTable = function(opts, data)
{
return '<table cellpadding="0" cellspacing="0" class="pika-table">' + renderHead(opts) + renderBody(data) + '</table>';
- };
+ },
/**
* Pikaday constructor
*/
- window.Pikaday = function(options)
+ Pikaday = function(options)
{
var self = this,
opts = self.config(options);
self._onMouseDown = function(e)
@@ -332,11 +351,12 @@
}
if (!hasClass(target, 'pika-select')) {
if (e.preventDefault) {
e.preventDefault();
} else {
- return e.returnValue = false;
+ e.returnValue = false;
+ return false;
}
} else {
self._c = true;
}
};
@@ -362,33 +382,33 @@
if (e.firedBy === self) {
return;
}
if (hasMoment) {
- date = window.moment(opts.field.value, opts.format);
- date = date ? date.toDate() : null;
+ date = moment(opts.field.value, opts.format);
+ date = (date && date.isValid()) ? date.toDate() : null;
}
else {
date = new Date(Date.parse(opts.field.value));
}
self.setDate(isDate(date) ? date : null);
if (!self._v) {
self.show();
}
};
- self._onInputFocus = function(e)
+ self._onInputFocus = function()
{
self.show();
};
- self._onInputClick = function(e)
+ self._onInputClick = function()
{
self.show();
};
- self._onInputBlur = function(e)
+ self._onInputBlur = function()
{
if (!self._c) {
self._b = sto(function() {
self.hide();
}, 50);
@@ -435,11 +455,11 @@
}
addEvent(opts.field, 'change', self._onInputChange);
if (!opts.defaultDate) {
if (hasMoment && opts.field.value) {
- opts.defaultDate = window.moment(opts.field.value, opts.format).toDate();
+ opts.defaultDate = moment(opts.field.value, opts.format).toDate();
} else {
opts.defaultDate = new Date(Date.parse(opts.field.value));
}
opts.setDefaultDate = true;
}
@@ -471,11 +491,11 @@
/**
* public Pikaday API
*/
- window.Pikaday.prototype = {
+ Pikaday.prototype = {
/**
* configure functionality
*/
@@ -533,27 +553,27 @@
/**
* return a formatted string of the current selection (using Moment.js if available)
*/
toString: function(format)
{
- return !isDate(this._d) ? '' : hasMoment ? window.moment(this._d).format(format || this._o.format) : this._d.toDateString();
+ return !isDate(this._d) ? '' : hasMoment ? moment(this._d).format(format || this._o.format) : this._d.toDateString();
},
/**
* return a Moment.js object of the current selection (if available)
*/
getMoment: function()
{
- return hasMoment ? window.moment(this._d) : null;
+ return hasMoment ? moment(this._d) : null;
},
/**
* set the current selection from a Moment.js object (if available)
*/
setMoment: function(date)
{
- if (hasMoment && window.moment.isMoment(date)) {
+ if (hasMoment && moment.isMoment(date)) {
this.setDate(date.toDate());
}
},
/**
@@ -593,11 +613,11 @@
setToStartOfDay(this._d);
this.gotoDate(this._d);
if (this._o.field) {
this._o.field.value = this.toString();
- fireEvent(this._o.field, "change", { firedBy: this });
+ fireEvent(this._o.field, 'change', { firedBy: this });
}
if (!preventOnSelect && typeof this._o.onSelect === 'function') {
this._o.onSelect.call(this, this.getDate());
}
},
@@ -811,6 +831,8 @@
}
}
};
-})(window, window.document);
+ return Pikaday;
+
+}));