Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature#bound date #446

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Override where the dropdown is appended.
Takes either a `string` to use as a selector, a `function` that gets passed the clicked input element as argument or a jquery `object` to use directly.
*default: "body"*

- **boundDate**
???
*default: null*

- **className**
A class name to apply to the HTML element that contains the timepicker dropdown.
*default: null*
Expand Down
134 changes: 76 additions & 58 deletions jquery.timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


(function (factory) {
if (typeof exports === "object" && exports &&
typeof module === "object" && module && module.exports === exports) {
// Browserify. Attach to jQuery module.
factory(require("jquery"));
} else if (typeof define === 'function' && define.amd) {
if (typeof exports === "object" && exports &&
typeof module === "object" && module && module.exports === exports) {
// Browserify. Attach to jQuery module.
factory(require("jquery"));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
Expand Down Expand Up @@ -41,7 +41,7 @@
// pick up settings from data attributes
var attributeOptions = [];
for (var key in $.fn.timepicker.defaults) {
if (self.data(key)) {
if (self.data(key)) {
attributeOptions[key] = self.data(key);
}
}
Expand All @@ -61,7 +61,7 @@
} else {
self.prop('autocomplete', 'off');
if (settings.showOn) {
for (i in settings.showOn) {
for (var i in settings.showOn) {
self.on(settings.showOn[i]+'.timepicker', methods.show);
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@
}

// check if list needs to be rendered
if (!list || list.length === 0 || typeof settings.durationTime === 'function') {
if (!list || list.length === 0 || typeof settings.durationTime === 'function' || settings.boundDate) {
_render(self);
list = self.data('timepicker-list');
}
Expand Down Expand Up @@ -142,7 +142,7 @@
verticalOrientation = 'b';
}

if (verticalOrientation == 't') {
if (verticalOrientation === 't') {
// position the dropdown on top
list.addClass('ui-timepicker-positioned-top');
listOffset.top = self.offset().top - list.outerHeight() + parseInt(list.css('marginTop').replace('px', ''), 10);
Expand Down Expand Up @@ -229,13 +229,13 @@
var settings = self.data('timepicker-settings');
var list = self.data('timepicker-list');

if (typeof key == 'object') {
if (typeof key === 'object') {
settings = $.extend(settings, key);

} else if (typeof key == 'string' && typeof value != 'undefined') {
} else if (typeof key === 'string' && typeof value !== 'undefined') {
settings[key] = value;

} else if (typeof key == 'string') {
} else if (typeof key === 'string') {
return settings[key];
}

Expand Down Expand Up @@ -293,7 +293,7 @@
var settings = self.data('timepicker-settings');

if (settings.forceRoundTime) {
var prettyTime = _roundAndFormatTime(_time2int(value), settings)
var prettyTime = _roundAndFormatTime(_time2int(value), settings);
} else {
var prettyTime = _int2time(_time2int(value), settings);
}
Expand Down Expand Up @@ -358,30 +358,26 @@
settings.maxTime = _time2int(settings.maxTime);
}

if (settings.durationTime && typeof settings.durationTime !== 'function') {
settings.durationTime = _time2int(settings.durationTime);
}

if (settings.scrollDefault == 'now') {
if (settings.scrollDefault === 'now') {
settings.scrollDefault = function() {
return settings.roundingFunction(_time2int(new Date()), settings);
}
} else if (settings.scrollDefault && typeof settings.scrollDefault != 'function') {
};
} else if (settings.scrollDefault && typeof settings.scrollDefault !== 'function') {
var val = settings.scrollDefault;
settings.scrollDefault = function() {
return settings.roundingFunction(_time2int(val), settings);
}
};
} else if (settings.minTime) {
settings.scrollDefault = function() {
return settings.roundingFunction(settings.minTime, settings);
}
};
}

if ($.type(settings.timeFormat) === "string" && settings.timeFormat.match(/[gh]/)) {
settings._twelveHourTime = true;
}

if (settings.showOnFocus === false && settings.showOn.indexOf('focus') != -1) {
if (settings.showOnFocus === false && settings.showOn.indexOf('focus') !== -1) {
settings.showOn.splice(settings.showOn.indexOf('focus'), 1);
}

Expand Down Expand Up @@ -411,6 +407,10 @@
}
}

if (settings.boundDate) {
settings.boundDate = _startOfDay(settings.boundDate);
}

return settings;
}

Expand All @@ -431,7 +431,7 @@
list = $('<ul />', { 'class': 'ui-timepicker-list' });

var wrapped_list = $('<div />', { 'class': 'ui-timepicker-wrapper', 'tabindex': -1 });
wrapped_list.css({'display':'none', 'position': 'absolute' }).append(list);
wrapped_list.css({'display': 'none', 'position': 'absolute' }).append(list);
}

if (settings.noneOption) {
Expand All @@ -441,7 +441,7 @@

if ($.isArray(settings.noneOption)) {
for (var i in settings.noneOption) {
if (parseInt(i, 10) == i){
if (parseInt(i, 10) === i){
var noneElement = _generateNoneElement(settings.noneOption[i], settings.useSelect);
list.append(noneElement);
}
Expand All @@ -457,16 +457,23 @@
}

if ((settings.minTime !== null || settings.durationTime !== null) && settings.showDuration) {
var stepval = typeof settings.step == 'function' ? 'function' : settings.step;
wrapped_list.addClass('ui-timepicker-with-duration');
wrapped_list.addClass('ui-timepicker-step-'+settings.step);
}

var durStart = settings.minTime;
if (typeof settings.durationTime === 'function') {
durStart = _time2int(settings.durationTime());
} else if (settings.durationTime !== null) {
durStart = settings.durationTime;
var durTime = settings.minTime, durDate;
if(settings.durationTime){
if (typeof settings.durationTime === 'function') {
durTime = settings.durationTime();
} else {
durTime = settings.durationTime;
}

// if boundDate is set, split durationTime into date (start of day) + time for duration computation.
if(settings.boundDate){
durDate = _startOfDay(durTime);
}
durTime = _time2int(durTime);
}
var start = (settings.minTime !== null) ? settings.minTime : 0;
var end = (settings.maxTime !== null) ? settings.maxTime : (start + _ONE_DAY - 1);
Expand All @@ -486,10 +493,10 @@
var drLen = dr.length;

var stepFunc = settings.step;
if (typeof stepFunc != 'function') {
if (typeof stepFunc !== 'function') {
stepFunc = function() {
return settings.step;
}
};
}

for (var i=start, j=0; i <= end; j++, i += stepFunc(j)*60) {
Expand All @@ -506,7 +513,15 @@
}

if ((settings.minTime !== null || settings.durationTime !== null) && settings.showDuration) {
var durationString = _int2duration(i - durStart, settings.step);
var current = i, compareTo = durTime;

// if boundDate is set, we can compute the duration in terms of days
if(settings.boundDate){
current += +settings.boundDate / 1000;
compareTo += +durDate / 1000;
}

var durationString = _int2duration(current - compareTo, settings.step);
if (settings.useSelect) {
row.text(row.text()+' ('+durationString+')');
} else {
Expand Down Expand Up @@ -598,11 +613,11 @@
{
var label, className, value;

if (typeof optionValue == 'object') {
if (typeof optionValue === 'object') {
label = optionValue.label;
className = optionValue.className;
value = optionValue.value;
} else if (typeof optionValue == 'string') {
} else if (typeof optionValue === 'string') {
label = optionValue;
} else {
$.error('Invalid noneOption value');
Expand Down Expand Up @@ -661,16 +676,16 @@

var settings = self.data('timepicker-settings');
var out = false;
var value = settings.roundingFunction(value, settings);
value = settings.roundingFunction(value, settings);

// loop through the menu items
list.find('li').each(function(i, obj) {
var jObj = $(obj);
if (typeof jObj.data('time') != 'number') {
if (typeof jObj.data('time') !== 'number') {
return;
}

if (jObj.data('time') == value) {
if (jObj.data('time') === value) {
out = jObj;
return false;
}
Expand Down Expand Up @@ -704,13 +719,13 @@

function _formatValue(e, origin)
{
if (this.value === '' || origin == 'timepicker') {
if (this.value === '' || origin === 'timepicker') {
return;
}

var self = $(this);

if (self.is(':focus') && (!e || e.type != 'change')) {
if (self.is(':focus') && (!e || e.type !== 'change')) {
return;
}

Expand Down Expand Up @@ -769,16 +784,16 @@
self.val(value);

var settings = self.data('timepicker-settings');
if (settings.useSelect && source != 'select' && source != 'initial') {
if (settings.useSelect && source !== 'select' && source !== 'initial') {
self.data('timepicker-list').val(_roundAndFormatTime(_time2int(value), settings));
}
}

if (self.data('ui-timepicker-value') != value) {
if (self.data('ui-timepicker-value') !== value) {
self.data('ui-timepicker-value', value);
if (source == 'select') {
if (source === 'select') {
self.trigger('selectTime').trigger('changeTime').trigger('change', 'timepicker');
} else if (source != 'error') {
} else if (source !== 'error') {
self.trigger('changeTime');
}

Expand All @@ -798,7 +813,7 @@
var list = self.data('timepicker-list');

if (!list || !_isVisible(list)) {
if (e.keyCode == 40) {
if (e.keyCode === 40) {
// show the list!
methods.show.call(self.get(0));
list = self.data('timepicker-list');
Expand Down Expand Up @@ -948,7 +963,7 @@
}

if (timeValue !== null) {
if (typeof timeValue != 'string') {
if (typeof timeValue !== 'string') {
timeValue = _int2time(timeValue, settings);
}

Expand All @@ -973,16 +988,16 @@
mins = minutes%60;

// Show decimal notation (eg: 1.5 hrs) for 30 minute steps
if (step == 30 && mins == 30) {
if (step === 30 && mins === 30) {
hours += _lang.decimal + 5;
}

duration.push(hours);
duration.push(hours == 1 ? _lang.hr : _lang.hrs);
duration.push(hours === 1 ? _lang.hr : _lang.hrs);

// Show remainder minutes notation (eg: 1 hr 15 mins) for non-30 minute steps
// and only if there are remainder minutes to show
if (step != 30 && mins) {
if (step !== 30 && mins) {
duration.push(mins);
duration.push(_lang.mins);
}
Expand Down Expand Up @@ -1076,16 +1091,16 @@
function _time2int(timeString, settings)
{
if (timeString === '') return null;
if (!timeString || timeString+0 == timeString) return timeString;
if (!timeString || timeString+0 === timeString) return timeString;

if (typeof(timeString) == 'object') {
if (typeof(timeString) === 'object') {
return timeString.getHours()*3600 + timeString.getMinutes()*60 + timeString.getSeconds();
}

timeString = timeString.toLowerCase().replace(/[\s\.]/g, '');

// if the last character is an "a" or "p", add the "m"
if (timeString.slice(-1) == 'a' || timeString.slice(-1) == 'p') {
if (timeString.slice(-1) === 'a' || timeString.slice(-1) === 'p') {
timeString += 'm';
}

Expand All @@ -1108,9 +1123,9 @@
var hours = hour;

if (hour <= 12 && ampm) {
var isPm = (ampm == _lang.pm || ampm == _lang.PM);
var isPm = (ampm === _lang.pm || ampm === _lang.PM);

if (hour == 12) {
if (hour === 12) {
hours = isPm ? 12 : 0;
} else {
hours = (hour + (isPm ? 12 : 0));
Expand All @@ -1132,8 +1147,10 @@
return timeInt;
}

function _pad2(n) {
return ("0" + n).slice(-2);
function _startOfDay(date) {
var clone = new Date(date);
clone.setHours(0, -clone.getTimezoneOffset(), 0, 0);
return clone;
}

// Plugin entry
Expand Down Expand Up @@ -1190,6 +1207,7 @@
typeaheadHighlight: true,
noneOption: false,
show2400: false,
stopScrollPropagation: false
stopScrollPropagation: false,
boundDate: null
};
}));