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

Update index.js #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

DayScheduleSelector.DEFAULTS = {
days : [0, 1, 2, 3, 4, 5, 6], // Sun - Sat
stringDays : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
startTime : '08:00', // HH:mm format
endTime : '20:00', // HH:mm format
timeFormat : 'hh:mm', // hh:mm or HH:MM
interval : 30, // minutes
template : '<div class="day-schedule-selector">' +
'<table class="schedule-table">' +
Expand All @@ -37,7 +39,7 @@
* @public
*/
DayScheduleSelector.prototype.renderHeader = function () {
var stringDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
var stringDays = this.options.stringDays
Copy link
Member

Choose a reason for hiding this comment

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

This is a good idea, and we should figure out a way to do internationalization for other labels, too. 👍

, days = this.options.days
, html = '';

Expand All @@ -52,6 +54,7 @@
DayScheduleSelector.prototype.renderRows = function () {
var start = this.options.startTime
, end = this.options.endTime
, timeFormat = this.options.timeFormat
, interval = this.options.interval
, days = this.options.days
, $el = this.$el.find('.schedule-rows');
Expand All @@ -61,7 +64,7 @@
return '<td class="time-slot" data-time="' + hhmm(d) + '" data-day="' + days[i] + '"></td>'
}).join();

$el.append('<tr><td class="time-label">' + hmmAmPm(d) + '</td>' + daysInARow + '</tr>');
$el.append('<tr><td class="time-label">' + hmmAmPm(d,timeFormat) + '</td>' + daysInARow + '</tr>');
Copy link
Member

Choose a reason for hiding this comment

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

See the comment below about using a formatTimeLabel option.

});
};

Expand Down Expand Up @@ -242,11 +245,20 @@
* @private
* @returns {String} Time in H:mm format with am/pm, e.g. '9:30am'
*/
function hmmAmPm(date) {
function hmmAmPm(date,timeFormat) {
Copy link
Member

Choose a reason for hiding this comment

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

There is a hhmm helper does what you need here, I believe.

Instead of the timeFormat options, IMHO it will be even better if we provide a formatTimeLabel option which is a function users can specify that will take a Date object and return whatever they want to format it. If the option is not specified, simply use the default hmmAmPm(d).

How do you think about this?

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, was too busy but flagged this to respond :) Your idea is good. Additionally it would be good to add a 'locale' option if you want to further improve it. I just needed those two options that's why I made it as simple as with timeFormat :)

Thanks!

var hours = date.getHours()
, minutes = date.getMinutes()
, ampm = hours >= 12 ? 'pm' : 'am';
return hours + ':' + ('0' + minutes).slice(-2) + ampm;


if (timeFormat == 'hh:mm') {
var hours = hours >= 13 ? hours - 12 : hours;
var time = hours + ':' + ('0' + minutes).slice(-2) + ampm;
} else {
var time = hours + ':' + ('0' + minutes).slice(-2);
}

return time
}

/**
Expand Down