Skip to content

Commit

Permalink
Merge pull request #53 from david-storm/main
Browse files Browse the repository at this point in the history
feat: add script for view Location Hours today
  • Loading branch information
podarok authored Aug 28, 2023
2 parents 20f5ccc + a73f64d commit 00f8227
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
80 changes: 80 additions & 0 deletions modules/openy_map_lb/assets/js/hours_today.js
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);
33 changes: 33 additions & 0 deletions modules/openy_map_lb/openy_map_lb.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,36 @@ openy_map:
- core/drupal
- core/drupalSettings
- openy_map/jquery.multiselect

hours_today:
version: 1.0.0
js:
assets/js/hours_today.js: { minified: false }
dependencies:
- core/jquery
- core/once
- core/drupal
- core/drupalSettings
- openy_map_lb/moment
- openy_map_lb/moment-timezone

moment:
remote: https://github.com/moment/moment/
version: 2.24.0
license:
name: MIT License
url: https://github.com/moment/moment/blob/master/LICENSE
gpl-compatible: true
js:
//cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js: { type: external, minified: true }

moment-timezone:
remote: https://github.com/moment/moment-timezone
version: 0.5.27
license:
name: MIT License
url: https://github.com/moment/moment-timezone/blob/master/LICENSE
gpl-compatible: true
js:
//cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.27/moment-timezone.min.js: { type: external, minified: true }
//cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.27/moment-timezone-with-data.min.js: { type: external, minified: true }
28 changes: 28 additions & 0 deletions modules/openy_map_lb/openy_map_lb.module
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,31 @@ function openy_map_lb_theme_suggestions_node_alter(array &$suggestions, array $v
}
}
}


/**
* Implements hook_preprocess_HOOK().
*/
function openy_map_lb_preprocess_node__location_type__lb_teaser(&$variables) {
$hoursHelper = Drupal::getContainer()->get('y_branch.hours_helper');
$node = $variables['elements']['#node'];
if (!$node) {
return;
}

$branch_hours = $hoursHelper->getBranchHours($node);
$holiday_hours = $hoursHelper->getBranchHolidayHours($node);
unset($variables['content']['field_branch_hours'][0]['table']);
$variables['#attached'] = [
'library' => [
'openy_map_lb/hours_today',
],
'drupalSettings' => [
'lb_branch_hours_blocks' => [
'exceptions' => $holiday_hours['js_settings'],
'tz' => $hoursHelper->getTimezone(),
]
]
];
$variables['#attached']['drupalSettings']['lb_branch_hours_blocks']['branch_hours'][$node->id()] = $branch_hours['js_settings'];
}

0 comments on commit 00f8227

Please sign in to comment.