-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
187 additions
and
2 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
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,70 @@ | ||
/*jslint browser: true, plusplus: true, vars: true, indent: 2 */ | ||
window.loadIndieConfig = (function () { | ||
'use strict'; | ||
|
||
// Indie-Config Loading script | ||
// by Pelle Wessman, voxpelli.com | ||
// MIT-licensed | ||
// http://indiewebcamp.com/indie-config | ||
|
||
var config, configFrame, configTimeout, | ||
callbacks = [], | ||
handleConfig, parseConfig; | ||
|
||
// When the configuration has been loaded – deregister all loading mechanics and call all callbacks | ||
handleConfig = function () { | ||
config = config || {}; | ||
|
||
configFrame.parentNode.removeChild(configFrame); | ||
configFrame = undefined; | ||
|
||
window.removeEventListener('message', parseConfig); | ||
|
||
clearTimeout(configTimeout); | ||
|
||
while (callbacks[0]) { | ||
callbacks.shift()(config); | ||
} | ||
}; | ||
|
||
// When we receive a message, check if the source is right and try to parse it | ||
parseConfig = function (message) { | ||
var correctSource = (configFrame && message.source === configFrame.contentWindow); | ||
|
||
if (correctSource && config === undefined) { | ||
try { | ||
config = JSON.parse(message.data); | ||
} catch (ignore) {} | ||
|
||
handleConfig(); | ||
} | ||
}; | ||
|
||
return function (callback) { | ||
// If the config is already loaded, call callback right away | ||
if (config) { | ||
callback(config); | ||
return; | ||
} | ||
|
||
// Otherwise add the callback to the queue | ||
callbacks.push(callback); | ||
|
||
// Are we already trying to load the Indie-Config, then wait | ||
if (configFrame) { | ||
return; | ||
} | ||
|
||
// Create the iframe that will load the Indie-Config | ||
configFrame = document.createElement('iframe'); | ||
configFrame.src = 'web+action:load'; | ||
document.getElementsByTagName('body')[0].appendChild(configFrame); | ||
configFrame.style.display = 'none'; | ||
|
||
// Listen for messages so we will catch the Indie-Config message | ||
window.addEventListener('message', parseConfig); | ||
|
||
// And if no such Indie-Config message has been loaded in a while, abort the loading | ||
configTimeout = setTimeout(handleConfig, 3000); | ||
}; | ||
}()); |
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,72 @@ | ||
/*jslint browser: true, plusplus: true, vars: true, indent: 2 */ | ||
(function () { | ||
'use strict'; | ||
|
||
var loadingClassRegexp = /(^|\s)indieconfig-loading(\s|$)/; | ||
|
||
var doTheAction = function (indieConfig) { | ||
var href, action, anchors; | ||
|
||
// Don't block the tag anymore as the queued action is now handled | ||
this.className = this.className.replace(loadingClassRegexp, ' '); | ||
|
||
// Pick the correct endpoint for the correct action | ||
action = this.getAttribute('do'); | ||
|
||
if ((action === 'reply' || action === 'post') && indieConfig.reply) { | ||
href = indieConfig.reply; | ||
} else if (action === 'like' && indieConfig.like) { | ||
href = indieConfig.like; | ||
} else if (action === 'repost' && indieConfig.repost) { | ||
href = indieConfig.repost; | ||
} else if (action === 'tip' && indieConfig.tip) { | ||
href = indieConfig.tip; | ||
} | ||
|
||
// If no endpoint is found, try the URL of the first a-tag within it | ||
if (!href) { | ||
anchors = this.getElementsByTagName('a'); | ||
if (anchors[0]) { | ||
href = anchors[0].href; | ||
} | ||
} | ||
|
||
// We have found an endpoint! | ||
if (href) { | ||
//Resolve a relative target | ||
var target = document.createElement('a'); | ||
target.href = this.getAttribute('with'); | ||
target = target.href; | ||
|
||
// Insert the target into the endpoint | ||
href = href.replace('{url}', encodeURIComponent(target || window.location.href)); | ||
|
||
// And redirect to it | ||
window.location = href; | ||
} | ||
}; | ||
|
||
// Event handler for a click on an indie-action tag | ||
var handleTheAction = function (e) { | ||
// Prevent the default of eg. any a-tag fallback within the indie-action tag | ||
e.preventDefault(); | ||
|
||
// Make sure this tag hasn't already been queued for the indieconfig-load | ||
if (!loadingClassRegexp.test(this.className)) { | ||
this.className += ' indieconfig-loading'; | ||
// Set "doTheAction" to be called when the indie-config has been loaded | ||
window.loadIndieConfig(doTheAction.bind(this)); | ||
} | ||
}; | ||
|
||
// Once the page is loased add click event listeners to all indie-action tags | ||
window.addEventListener('DOMContentLoaded', function () { | ||
var actions = document.querySelectorAll('indie-action'), | ||
i, | ||
length = actions.length; | ||
|
||
for (i = 0; i < length; i++) { | ||
actions[i].addEventListener('click', handleTheAction); | ||
} | ||
}); | ||
}()); |