Repository navigation
Expand file tree
/
Copy pathwebaction.js
More file actions
82 lines (67 loc) · 2.55 KB
/
Copy pathwebaction.js
File metadata and controls
82 lines (67 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(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 (window.loadIndieConfig && !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));
}
};
// Used to activate all webactions in a context
var activateWebActionElements = function (context) {
var actions = context.querySelectorAll('indie-action'),
i,
length = actions.length;
for (i = 0; i < length; i++) {
actions[i].addEventListener('click', handleTheAction);
}
};
window.activateWebActionElements = activateWebActionElements;
// Once the page is loaded add click event listeners to all indie-action tags
if (document.body) {
activateWebActionElements(document);
} else {
window.addEventListener('DOMContentLoaded', function () {
activateWebActionElements(document);
});
}
}());