Skip to content

Instantly share code, notes, and snippets.

@rococodogs
Last active December 2, 2015 18:32
Show Gist options
  • Select an option

  • Save rococodogs/c290a15b07093ceab3fc to your computer and use it in GitHub Desktop.

Select an option

Save rococodogs/c290a15b07093ceab3fc to your computer and use it in GitHub Desktop.
new library hours js

new library hours js

replacement for the old hours, now using LibCal.

testing

make a bookmarklet w/ this as the location:

javascript:(function () { $('#hoursWidget, #hoursTable, #todaysHours').html('');function handleTodaysHours(e){var t=e.locations[0],a=t.times;return t&&a?$("#todaysHours").text(getTimestr(a)):void 0}function handleWeeksHours(e){var t=e.locations[0].weeks[0],a=$("#hoursTable"),r=$('<table class="table nolines"/>'),s=$("<tbody/>");a.prepend(r),r.append(s),buildTableWeek(s,t)}function handleHoursWidget(e){function t(e){return r(e,-1)}function a(e){return r(e,1)}function r(e,t){e&&e.preventDefault(),h+=t,o.css("visibility",0>=h?"hidden":"visible"),d.css("visibility",h>=l?"hidden":"visible"),buildTableWeek(n,u[h])}var s=$hoursWidget||$("#hoursWidget"),i=$('<table class="table table-bordered"/>'),n=$("<tbody/>"),o=$("<a/>"),d=$("<a/>"),u=e.locations[0].weeks,l=u.length,h=0;i.append(n),s.append(i),s.append(o),s.append(d),o.attr({id:"back",href:"#"}),o.addClass("pull-left"),o.css("visibility","hidden"),o.text("Previous Week"),o.on("click",t),d.attr({id:"back",href:"#"}),d.addClass("pull-right"),d.text("Next Week"),d.on("click",a),r(null,0)}function buildTableWeek(e,t){e.html("");for(var a in t){var r=buildTableRow(t[a]);e.append(r),t[a].times.currently_open&&r.addClass("highlight")}}function buildTableRow(e){var t=$("<tr/>"),a=e.times,r=getTimestr(a),s=e.date.split("-").map(function(e){return parseInt(e,10)}),i=new Date(s[0],s[1]-1,s[2]),n=getDaystr(i);return t.html("<td>"+n+"</td><td>"+r+"</td>"),t}function getDaystr(e){var t=["January","February","March","April","May","June","July","August","September","October","November","December"],a=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],r=e.getDay(),s=e.getMonth(),i=e.getDate();return a[r]+", "+t[s]+" "+i}function getTimestr(e){if("closed"===e.status)return"Closed";if("24hours"===e.status)return"Open 24 Hours";if("not-set"===e.status)return"TBD";var t=e.hours[0],a=formatTime(t.from),r=formatTime(t.to);return a&&r?a+" - "+r:r&&!a?"Close at "+r:!r&&a?"Open at "+a:"TBD"}function formatTime(e){return e.replace(/^(\d{1,2})([ap]m)/,"$1:00$2")}var proxyPath="http://libappdev.muhlenberg.edu/hours-proxy",todaysPath="/api_hours_today.php?iid=814&format=json",weeksPath="/api_hours_grid.php?iid=814&format=json&weeks=1",yearsPath="/api_hours_grid.php?iid=814&format=json&weeks=52",$hoursWidget=$("#hoursWidget");$.get(proxyPath+todaysPath,handleTodaysHours),$.get(proxyPath+weeksPath,handleWeeksHours),$hoursWidget.length>0&&$.get(proxyPath+yearsPath,handleHoursWidget);})()
var proxyPath = 'http://libappdev.muhlenberg.edu/hours-proxy'
var todaysPath = '/api_hours_today.php?iid=814&format=json'
var weeksPath = '/api_hours_grid.php?iid=814&format=json&weeks=1'
var yearsPath = '/api_hours_grid.php?iid=814&format=json&weeks=52'
var $hoursWidget = $('#hoursWidget')
// these are on every page
$.get(proxyPath + todaysPath, handleTodaysHours)
$.get(proxyPath + weeksPath, handleWeeksHours)
if ($hoursWidget.length > 0)
$.get(proxyPath + yearsPath, handleHoursWidget)
function handleTodaysHours (d) {
var loc = d.locations[0]
var times = loc.times
if (loc && times)
return $('#todaysHours').text(getTimestr(times))
}
function handleWeeksHours (d) {
var days = d.locations[0].weeks[0]
var $target = $('#hoursTable')
var $table = $('<table class="table nolines"/>')
var $tbody = $('<tbody/>')
$target.prepend($table)
$table.append($tbody)
buildTableWeek($tbody, days)
}
function handleHoursWidget (d) {
// table pieces
var $widget = $hoursWidget || $('#hoursWidget')
var $table = $('<table class="table table-bordered"/>')
var $tbody = $('<tbody/>')
var $back = $('<a/>')
var $frwd = $('<a/>')
// data
var weeks = d.locations[0].weeks
var weeksLen = weeks.length
// etc
var counter = 0
// set up table
$table.append($tbody)
$widget.append($table)
$widget.append($back)
$widget.append($frwd)
// set up buttons
$back.attr({'id': 'cal-back', 'href': '#'})
$back.addClass('pull-left')
$back.css('visibility', 'hidden')
$back.text('Previous Week')
$back.on('click', calBackwards)
$frwd.attr({'id': 'cal-forward', 'href': '#'})
$frwd.addClass('pull-right')
$frwd.text('Next Week')
$frwd.on('click', calForwards)
// get started
resetCal(null, 0)
// helpers
function calBackwards (e) { return resetCal(e, -1) }
function calForwards (e) { return resetCal(e, 1) }
function resetCal(ev, val) {
if (ev) ev.preventDefault()
counter = counter + val
$back.css('visibility', (counter <= 0 ? 'hidden' : 'visible'))
$frwd.css('visibility', (counter >= weeksLen ? 'hidden' : 'visible'))
buildTableWeek($tbody, weeks[counter])
}
}
function buildTableWeek ($el, week) {
// clear out the el first
$el.html('')
for (var day in week) {
var $row = buildTableRow(week[day])
$el.append($row)
if (week[day].times.currently_open)
$row.addClass('highlight')
}
}
function buildTableRow (data) {
var $row = $('<tr/>')
var block = data.times
var timestr = getTimestr(block)
var d = data.date.split('-').map(function (d) {
return parseInt(d, 10)
})
var date = new Date(d[0], d[1] - 1, d[2])
var daystr = getDaystr(date)
$row.html('<td>' + daystr + '</td><td>' + timestr + '</td>')
return $row
}
// for compatability, we'll do this the long way
function getDaystr (date) {
var mon = [
'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'
]
var day = [
'Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday'
]
var d = date.getDay()
var m = date.getMonth()
var n = date.getDate()
return day[d] + ', ' + mon[m] + ' ' + n
}
function getTimestr (times) {
if (times.status === 'closed')
return 'Closed'
if (times.status === '24hours')
return 'Open 24 Hours'
if (times.status === 'not-set')
return 'TBD'
var hours = times.hours[0]
var from = formatTime(hours.from)
var to = formatTime(hours.to)
if (from && to)
return from + ' - ' + to
else if (to && !from)
return 'Close at ' + to
else if (!to && from)
return 'Open at ' + from
else
return 'TBD'
}
function formatTime (t) {
return t.replace(/^(\d{1,2})([ap]m)/, '$1:00$2')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment