forked from alial04/sl-vux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed.js
192 lines (172 loc) · 6.24 KB
/
embed.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(function() {
var Embed, Utils,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Utils = (function() {
function Utils() {}
Utils.prototype.eachElement = function(array, callback, scope) {
var i, results;
if (scope == null) {
scope = this;
}
i = 0;
results = [];
while (i < array.length) {
callback.call(scope, array[i], i);
results.push(i++);
}
return results;
};
Utils.prototype.pushMessage = function(name, value) {
if (value == null) {
value = {};
}
return window.parent.postMessage([name, value], "*");
};
Utils.prototype.addEvent = function(element, event, fn, useCapture) {
if (useCapture == null) {
useCapture = false;
}
return element.addEventListener(event, fn, useCapture);
};
Utils.prototype.setStyles = function(element, styles) {
var key, results;
results = [];
for (key in styles) {
results.push(element.style[key] = styles[key]);
}
return results;
};
return Utils;
})();
Embed = (function(superClass) {
extend(Embed, superClass);
function Embed() {
this.switchTab = bind(this.switchTab, this);
this.loadResult = bind(this.loadResult, this);
this.repositionHighlight = bind(this.repositionHighlight, this);
this.setHeight = bind(this.setHeight, this);
this.setupEvents = bind(this.setupEvents, this);
this.setupDefaults = bind(this.setupDefaults, this);
this.elements = {
tabs: document.querySelectorAll("#tabs .tCont"),
actions: document.querySelectorAll("#actions a"),
hl: document.querySelector(".hl"),
pre: document.querySelectorAll("pre")
};
this.setupDefaults();
}
Embed.prototype.setupDefaults = function() {
this.setupEvents();
this.eachElement(this.elements.pre, function(element) {
return hljs.highlightBlock(element);
});
this.repositionHighlight(this.elements.actions[0], false);
return this.setHeight(this.elements.tabs[0]);
};
Embed.prototype.setupEvents = function() {
var ref;
this.eachElement(this.elements.actions, (function(_this) {
return function(action, index) {
return action.addEventListener("click", function(event) {
return _this.switchTab(event, action, index);
});
};
})(this));
if (((ref = this.elements.actions[0]) != null ? ref.dataset.triggerType : void 0) === "result") {
return this.loadResult();
}
};
Embed.prototype.setHeight = function(element) {
var activeTab, height;
activeTab = element.getBoundingClientRect();
height = activeTab.height;
return this.pushMessage("embed", {
slug: slug,
height: height
});
};
Embed.prototype.predender = function() {
var head, prefetch, prerender;
head = document.getElementsByTagName("head")[0];
prefetch = document.createElement("link");
prefetch.setAttribute("rel", "prefetch");
prefetch.setAttribute("href", show_src);
head.appendChild(prefetch);
prerender = document.createElement("link");
prerender.setAttribute("rel", "prerender");
prerender.setAttribute("href", show_src);
return head.appendChild(prerender);
};
Embed.prototype.repositionHighlight = function(action, animated) {
var position;
if (animated == null) {
animated = true;
}
if (action) {
position = action.getBoundingClientRect();
if (animated) {
this.elements.hl.classList.add("animated");
}
return this.setStyles(this.elements.hl, {
left: position.left + "px",
width: position.width + "px"
});
}
};
Embed.prototype.loadResult = function(callback) {
var iframes, resultCont, resultsFrame, sandboxAttrs;
iframes = document.querySelectorAll("#result iframe");
resultsFrame = document.createElement("iframe");
resultCont = document.querySelector("#result");
this.eachElement(iframes, function(iframe) {
return iframe.parentNode.removeChild(iframe);
});
sandboxAttrs = ["allow-forms", "allow-scripts", "allow-same-origin"];
if (document.location.search.indexOf("disableModals") === -1) {
sandboxAttrs.push("allow-modals", "allow-popups");
}
resultsFrame.src = show_src;
resultsFrame.allowtransparency = true;
resultsFrame.allowfullscreen = true;
resultsFrame.allowpaymentrequest = true;
resultsFrame.frameBorder = "0";
resultsFrame.sandbox = sandboxAttrs.join(" ");
resultsFrame.allow = "midi; geolocation; microphone; camera";
resultCont.appendChild(resultsFrame);
if (callback) {
return resultsFrame.addEventListener("load", (function(_this) {
return function() {
return callback.apply([_this]);
};
})(this));
}
};
Embed.prototype.switchTab = function(event, action, index) {
var actionParent;
event.preventDefault();
event.stopPropagation();
this.repositionHighlight(action);
actionParent = action.parentElement.parentElement.querySelectorAll("li");
this.eachElement(this.elements.tabs, function(element) {
return element.classList.remove("active");
});
this.elements.tabs[index].classList.add("active");
if (actionParent) {
this.eachElement(actionParent, function(element) {
return element.classList.remove("active");
});
action.parentElement.classList.add("active");
}
this.setHeight(this.elements.tabs[index]);
if (action.dataset.triggerType === "result") {
return this.loadResult();
}
};
return Embed;
})(Utils);
window.addEventListener("DOMContentLoaded", function(event) {
return this.EmbedManager = new Embed;
});
}).call(this);