-
Notifications
You must be signed in to change notification settings - Fork 7
/
avWidgets.js
74 lines (65 loc) · 2.39 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* This file is part of common-ui.
* Copyright (C) 2015-2016 Sequent Tech Inc <legal@sequentech.io>
* common-ui is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License.
* common-ui is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with common-ui. If not, see <http://www.gnu.org/licenses/>.
**/
/* 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 || !e.data || 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("sequent-voting-booth", function (link) {
var funcName = link.getAttribute("data-authorization-funcname");
window.avRequestAuthorizationFuncName = funcName;
});
processLinks("sequent-ballot-locator");
processLinks("sequent-results");
})();
/* jshint ignore:end */