forked from agoravoting/agora-core-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avWidgets.js
57 lines (52 loc) · 1.67 KB
/
avWidgets.js
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
/* jshint ignore:start */
(function () {
function createElement(name, attrs) {
var el = document.createElement(name);
for (var key in attrs) {
el.setAttribute(key, attrs[key]);
}
return el;
}
function processLinks(className, callback) {
var links = document.getElementsByClassName(className);
for (var i = 0; i < links.length; i++) {
var link = links[i];
var href = link.getAttribute("href");
if (callback !== undefined) {
callback(link, i, className);
}
var iframe = createElement("iframe", {
"class": className + "-iframe",
"src": href,
"style": "border: 0; width: 100%; height: 100%",
"seamless": ""
});
link.parentNode.insertBefore(iframe, link);
link.parentNode.removeChild(link);
}
}
// generic interface for html5 messaging api
function requestAuthorization(e) {
var reqAuth = "avRequestAuthorization:";
if (e.data.substr(0, reqAuth.length) !== reqAuth) {
return;
}
function callback(khmac) {
e.source.postMessage('avPostAuthorization:' + khmac, '*');
}
var args = [
JSON.parse(e.data.substr(reqAuth.length, e.data.length)),
callback
];
window[window.avRequestAuthorizationFuncName].apply(window, args);
}
window.addEventListener('message', requestAuthorization, false);
// convert links into widgets
processLinks("agoravoting-voting-booth", function (link) {
var funcName = link.getAttribute("data-authorization-funcname");
window.avRequestAuthorizationFuncName = funcName;
});
processLinks("agoravoting-ballot-locator");
processLinks("agoravoting-results");
})();
/* jshint ignore:end */