-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.js
More file actions
161 lines (125 loc) · 4.35 KB
/
widget.js
File metadata and controls
161 lines (125 loc) · 4.35 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(function(window, document, undefined) {
if (window.Synaps) {
Synaps.init();
return;
}
// Constants
var IFRAME_CLASS = '__synaps-iframe',
SUMMARY_CLASS = '__synaps-iframe-summary',
TOPBAR_CLASS = '__synaps-iframe-with-topbar',
DETAIL_CLASS = '__synaps-iframe-detail',
HEIGHT_FULL_CLASS = '__synaps-iframe-height-full',
ELECTION_ATTR = 'synaps-secim',
TOPBAR_ATTR = 'topbar',
DETAILURL_ATTR = 'detail-url',
HEIGHT_ATTR = 'height',
CITYCODE_ATTR = 'city-code',
DEBUG_INITIATOR = '__synaps_test',
BASE_URL = 'https://secim-demo.synaps.ly/#/';
// Ensures needed styles has injected
var ensureStylesReady = (function () {
var stylesReady = false;
return function () {
if (!stylesReady) {
var sheet = (function() {
// Create the <style> tag
var style = document.createElement("style");
// WebKit hack :(
style.appendChild(document.createTextNode(""));
// Add the <style> element to the page
document.head.appendChild(style);
return style.sheet;
})();
function addCSSClassRule(sheet, selector, rules, index) {
selector = '.' + selector;
if("insertRule" in sheet) {
sheet.insertRule(selector + "{" + rules + "}", index);
}
else if("addRule" in sheet) {
sheet.addRule(selector, rules, index);
}
}
// Add style rules for needed classes
addCSSClassRule(sheet, IFRAME_CLASS, "width: 100%; border: none;", 0);
addCSSClassRule(sheet, SUMMARY_CLASS, "height: 125px", 1);
addCSSClassRule(sheet, TOPBAR_CLASS, "height: 160px", 2);
addCSSClassRule(sheet, DETAIL_CLASS, "height: 400px", 3);
addCSSClassRule(sheet, HEIGHT_FULL_CLASS, "height: 100%", 4);
stylesReady = true;
}
};
})();
var getQuerystring = function (params) {
var parts = [];
for(var key in params) {
if (params[key] === true) {
parts.push(key);
} else {
parts.push(key + '=' + encodeURI(params[key]));
}
}
return parts.join('&');
};
var getAttribute = function (element, attribute) {
return element.getAttribute(attribute) || element.getAttribute('data-' + attribute);
};
var hasAttribute = function (element, attribute) {
return element.hasAttribute(attribute) || element.hasAttribute('data-' + attribute);
};
var init = function () {
var elements = document.querySelectorAll('['+ELECTION_ATTR+'],[data-'+ELECTION_ATTR+']');
if (elements.length > 0) {
ensureStylesReady();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var iframe = document.createElement('iframe');
var url = window.__synaps_election_url || BASE_URL;
iframe.classList.add(IFRAME_CLASS);
var params = {
iframe: true
};
var mode = getAttribute(element, ELECTION_ATTR);
if (mode == 'ozet') {
url += 'ozet/';
iframe.classList.add(SUMMARY_CLASS);
if (hasAttribute(element, TOPBAR_ATTR)) {
params.topbar = true;
iframe.classList.add(TOPBAR_CLASS);
}
} else {
iframe.classList.add(DETAIL_CLASS);
}
var height = getAttribute(element, HEIGHT_ATTR);
if (height == 'full') {
iframe.classList.add(HEIGHT_FULL_CLASS);
}
if (height == 'auto' || !height) {
params.height = 'auto';
window.addEventListener("message", function (event) {
if (event.data > 0) {
iframe.style.height = event.data + 'px';
}
}, false);
}
if (hasAttribute(element, DETAILURL_ATTR)) {
params['detailurl'] = getAttribute(element, DETAILURL_ATTR);
}
var cityCode = getAttribute(element, CITYCODE_ATTR);
if (cityCode) {
params['city'] = cityCode;
}
iframe.src = url + '?' + getQuerystring(params);
element.innerHTML = '';
element.appendChild(iframe);
}
}
}
var now = new Date(),
electionDate = new Date('2015-11-01');
if (now > electionDate || window[DEBUG_INITIATOR] || window.location.href.indexOf(DEBUG_INITIATOR) > -1) {
init();
}
window.Synaps = {
init: init
};
})(window, document);