Skip to content

Instantly share code, notes, and snippets.

@ranwahle
ranwahle / define-custom-element.js
Created May 12, 2019 14:04
Define custom element
customElements.define('self-routing-anchor', SelfRoutingAnchor, {extends: 'a'})
@ranwahle
ranwahle / self-routing-anchor.js
Created May 12, 2019 14:06
Self routing anchor
import Router from './router.js';
// router.js is where our router implementation will be
export default class SelfRoutingAnchor extends HTMLAnchorElement {
connectedCallback() {
const router = Router.router;
this.onclick = evt => {
evt.preventDefault();
@ranwahle
ranwahle / self-routing-anchor-skeleton.js
Created May 12, 2019 14:07
Self Routing Anchor skeleton
export default class SelfRoutingAnchor extends HTMLAnchorElement {
}
@ranwahle
ranwahle / Route-configuration-example.js
Created May 20, 2019 12:43
Route configuration example
[
{path: '/', element: 'div', attributes: {is: 'images-container'}}
,
{
path: '/addImage',
element: 'div',
attributes: {is: 'add-image'}
}, {
path: 'image/:index',
element: 'div',
@ranwahle
ranwahle / render-element.js
Last active May 21, 2019 08:51
Render element inside router outlet
const newElement = document.createElement(newRouteData.element, newRouteData.attributes);
Object.keys(newRouteData.attributes || {}).forEach(key => {
newElement.setAttribute(key, newRouteData.attributes[key]);
});
this.appendChild(newElement)
@ranwahle
ranwahle / build-route-parameters.js
Created May 22, 2019 07:25
Build route paramenters
buildParameters(routeCandidate, urlFragments) {
const result = {};
const pathFragments = this.getFragments(routeCandidate.path);
pathFragments.forEach((fragment, index) => {
if (fragment.startsWith(':')) {
const fragmentName = fragment.substring(1);
result[fragmentName] = urlFragments[index];
}
})
return result;
@ranwahle
ranwahle / use-route-paramenters.js
Created May 22, 2019 08:59
Use route parameters
const parameters = router.currentSnapshot.params;
//for images/:id route - Get the image content
this.getImage(parameters.id);
@ranwahle
ranwahle / request-notification-permission.js
Created May 30, 2019 09:19
Request notification permission.
Notification.requestPermission().then(function(result) {
// Do something with the permission
});
@ranwahle
ranwahle / send-notification.js
Created May 30, 2019 09:43
Send notification
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
const notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
@ranwahle
ranwahle / query-notification-permission.js
Created May 30, 2019 11:03
Query Notification Permission
async function mayINotify() {
return await navigator.permission.query({name: 'notifications'});
}
// This will return a promise of PermssionStatus object