-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from david-storm/main
feat: add script for view Location Hours today
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Behaviors for the 'Today's hours' functionality. | ||
*/ | ||
(function ($, Drupal) { | ||
Drupal.behaviors.today_hours = { | ||
/** | ||
* Used to track the setInveral. | ||
*/ | ||
refreshTimer: false, | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
getDayOfWeek: function (tz) { | ||
// ISO day of week. returns a string of day of week, e.g. Mon, Tue, etc. | ||
return moment().tz(tz).format('ddd'); | ||
}, | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
getDate: function (tz) { | ||
return moment().tz(tz).format('YYYY-MM-DD'); | ||
}, | ||
|
||
/** | ||
* Primary method for updating the today hours. | ||
*/ | ||
updateTodayHours: function (todayHours) { | ||
var nid = todayHours.parents('article.node').attr('data-openy-map-location-id'); | ||
if (typeof drupalSettings.lb_branch_hours_blocks === 'undefined') { | ||
drupalSettings.lb_branch_hours_blocks = {}; | ||
} | ||
|
||
var hoursData = drupalSettings.lb_branch_hours_blocks.branch_hours[nid] || {}; | ||
var tz = drupalSettings.lb_branch_hours_blocks.tz || 'America/New York'; | ||
tz = tz.replace(/ /g, "_"); | ||
console.log(hoursData); | ||
|
||
if (Object.keys(hoursData).length) { | ||
var todayString = Drupal.behaviors.today_hours.getDate(tz); | ||
var dayOfWeek = Drupal.behaviors.today_hours.getDayOfWeek(tz); | ||
var exceptions = drupalSettings.lb_branch_hours_blocks.exceptions; // Holidays and other day exceptions will come later. | ||
|
||
if (typeof exceptions[todayString] != 'undefined') { | ||
todayHours.html(exceptions[todayString]); | ||
} | ||
else { | ||
todayHours.html(hoursData[dayOfWeek]); | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Drupal behavior attach. | ||
* | ||
* @param context | ||
* @param settings | ||
*/ | ||
attach: function (context, settings) { | ||
var $todayHours = $('.hours .field-branch-hours'); | ||
var onceClass = 'refresh-interval-set'; | ||
|
||
$todayHours.each((i, element) => { | ||
// Bail out if there's already refresh action set. | ||
if (!$(element).hasClass(onceClass)) { | ||
|
||
// This will ensure that if people leave the tab open or the page comes back | ||
// into memory on a phone the hour will always be correct. | ||
this.refreshTimer = setInterval(this.updateTodayHours($(element)), 60 * 1000); | ||
|
||
// Run for the first time. | ||
this.updateTodayHours($(element)); | ||
$(element).addClass(onceClass); | ||
} | ||
}); | ||
} | ||
|
||
}; | ||
})(jQuery, Drupal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters