This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/open 57: Openingsuren component (STIJ-238) #164
Open
Jeroen005
wants to merge
5
commits into
develop
Choose a base branch
from
feature/OPEN-57
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e85d845
OPEN-57: Added openinghours widget.
Jeroen005 3f496a2
Merge commit '44cf2eb50f683470f3da3f6dbfa1807661e524a7' into feature/…
Jeroen005 785a7c1
Merge commit 'b70ee404a148804673c0c8adc5f44d1716af70a0' into feature/…
Jeroen005 c29f235
OPEN-57: Refactored opening hours js lib.
Jeroen005 e391c25
OPEN-57: Removed preview since this value is default.
Jeroen005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
Currently in Alpha since Openinghours isn't in production yet. |
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 @@ | ||
<div class="openinghours" data-date="Mon Jan 01 2018 00:00:00 GMT+0100 (CET)" data-service="1" data-channel="12" data-type="month"></div> |
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 @@ | ||
<div class="openinghours" data-service="1" data-channel="12" data-type="week"></div> |
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,21 @@ | ||
{ | ||
"title": "Openinghours", | ||
"name": "openinghours", | ||
"status": "alpha", | ||
"handle": "openinghours", | ||
"default": "open-now", | ||
"variants": [ | ||
{ | ||
"name": "open-now", | ||
"title": "Open now" | ||
}, | ||
{ | ||
"name": "week", | ||
"title": "Open now" | ||
}, | ||
{ | ||
"name": "month", | ||
"title": "Open now" | ||
} | ||
] | ||
} |
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,98 @@ | ||
window.openingHours = (function () { | ||
'use strict'; | ||
|
||
var defaults = { | ||
endpoint: 'https://openingsuren.gentgrp.gent.be' | ||
}; | ||
|
||
function OpeningHours(items, options) { | ||
this.s = Object.assign({}, defaults, options); | ||
this.items = items; | ||
|
||
for (var i = 0; i < items.length; i++) { | ||
this._current = items[i]; | ||
this.init(items[i]); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
OpeningHours.prototype.init = function () { | ||
if (isNaN(this._current.dataset.service) || isNaN(this._current.dataset.channel)) { | ||
this._current.innerHTML = 'Error: Please provide a service and channel.'; | ||
return false; | ||
} | ||
|
||
if (typeof this._current.dataset.date === 'string' && this._current.dataset.type === 'open-now') { | ||
this._current.innerHTML = 'Error: Date is not supported in the "open now" widget.'; | ||
return false; | ||
} | ||
|
||
if (typeof this._current.dataset.date === 'undefined') { | ||
this._current.dataset.date = new Date().toUTCString(); | ||
} | ||
|
||
var url = this.constructRequest(); | ||
this.request(url, this.print); | ||
}; | ||
|
||
OpeningHours.prototype.formattedDate = function (d) { | ||
var date; | ||
if (!d) { | ||
date = new Date(); | ||
} | ||
else { | ||
date = new Date(d); | ||
} | ||
|
||
return date.getDate() + '-' + (date.getMonth() + 1) + '-' + date.getFullYear(); | ||
}; | ||
|
||
OpeningHours.prototype.constructRequest = function () { | ||
switch (this._current.dataset.type) { | ||
case 'open-now': | ||
return this.s.endpoint + '/api/v1/services/' + this._current.dataset.service + '/channels/' + this._current.dataset.channel + '/open-now'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd put these strings in a function variable, so it's easy to change version numbers etc. |
||
case 'month': | ||
return this.s.endpoint + '/api/v1/services/' + this._current.dataset.service + '/channels/' + this._current.dataset.channel + '/openinghours/month?date=' + this.formattedDate(this._current.dataset.date); | ||
default: | ||
var until = new Date(this._current.dataset.date); | ||
until.setDate(until.getDate() + 6); | ||
return this.s.endpoint + '/api/v1/services/' + this._current.dataset.service + '/channels/' + this._current.dataset.channel + '/openinghours?from=' + this.formattedDate(this._current.dataset.date) + '&until=' + this.formattedDate(until); | ||
} | ||
}; | ||
|
||
OpeningHours.prototype.request = function (url, callback) { | ||
var xmlhttp; | ||
xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.element = this._current; | ||
xmlhttp.onreadystatechange = function () { | ||
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { | ||
callback(xmlhttp.element, xmlhttp.responseText); | ||
} | ||
}; | ||
xmlhttp.open('GET', url, true); | ||
xmlhttp.setRequestHeader('Accept', 'text/html'); | ||
xmlhttp.send(); | ||
}; | ||
|
||
OpeningHours.prototype.print = function (element, data) { | ||
element.innerHTML = data; | ||
}; | ||
|
||
var openinghours = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. variable is redundant |
||
load: function (options) { | ||
var items = document.querySelectorAll('.openinghours'); | ||
new OpeningHours(items, options); | ||
} | ||
}; | ||
|
||
return openinghours; | ||
}()); | ||
|
||
document.onreadystatechange = function () { | ||
'use strict'; | ||
|
||
if (document.readyState === 'interactive') { | ||
window.openingHours.load(); | ||
} | ||
}; |
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 @@ | ||
<div class="openinghours" data-service="1" data-channel="12" data-type="open-now"></div> |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try for(var i = items.length; i--;)
This will only calculate the length of your array once, plus comparing to zero is faster.
Only really matters for very big arrays, but it's a nice habit ;)
https://stackoverflow.com/questions/1340589/are-loops-really-faster-in-reverse