-
Notifications
You must be signed in to change notification settings - Fork 80
/
embed2.js
116 lines (99 loc) · 3.12 KB
/
embed2.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
var jsonp = require('jsonp')
var url = require('url')
var iframe = require('iframe')
var getGistFiles = require('./get-gist-files')
var $ = window.jQuery
var hljs = window.hljs
var parsedURL = url.parse(window.location.href, true)
var gistID = parsedURL.query.gist
var $codeEls = $('#output > div')
var $links = $('#links a')
var binURL = '?gist=' + gistID
if (gistID.indexOf('/') > -1) gistID = gistID.split('/')[1]
run()
function run () {
updateUIBeforeGistLoad()
loadFromAPI(gistID)
}
function updateUIBeforeGistLoad () {
// update the links to requirebin
$('.requirebin-link').attr('href', 'http://requirebin.com/' + binURL)
// tabs state
var tabs = (parsedURL.query.tabs || '')
.split(',')
.filter(Boolean)
if (tabs.length) {
$('#result-link').addClass('visible')
$('#nav').show()
tabs.forEach(function (tab) {
$('#' + tab + '-link').addClass('visible')
})
} else {
// if no tab is enabled then plain mode is activated
$(document.body).addClass('plain')
}
}
function loadFromAPI (gistID) {
jsonp('https://api.github.com/gists/' + gistID, function (err, gist) {
if (err) return console.log(err)
getGistFiles(gist, ['page-head.html', 'page-body.html', 'head.html', 'minified.js', 'package.json', 'index.js'], function (err) {
if (err) return console.log(err)
var files = gist.data.files
var content = {}
var headFile = files['page-head.html'] || files['head.html']
if (headFile) {
content.head = headFile.content
}
if (files['page-body.html']) {
content.body = files['page-body.html'].content
}
if (files['minified.js']) {
content.bundle = files['minified.js'].content
content.code = files['index.js'].content
}
if (files['package.json']) {
content.meta = files['package.json'].content
}
updateUI(content)
setUpUIController(content)
render(content)
})
})
}
function render (content) {
if (!content.bundle || !content.meta) {
content.bundle = 'document.write("not a valid requirebin gist - missing minified.js")'
}
// disable default styling on the iframe
if (content.head) {
content.head = '<style> html, body{ margin: 0; padding: 0; border: 0; }</style>' + content.head
}
iframe({
container: document.getElementById('result'),
head: content.head,
body: content.body + '<script type="text/javascript">' +
'setTimeout(function(){\n;' + content.bundle + '\n;}, 0)</script>'
})
}
function updateUI (content) {
// highlight the code
['code', 'head', 'body', 'meta'].forEach(function (key) {
var box = document.querySelector('#' + key + ' code')
box.textContent = box.innerText = content[key]
})
hljs.initHighlightingOnLoad()
}
function setUpUIController (content) {
window.onpopstate = function () {
var hash = window.location.hash.substr(1)
if (content[hash] || hash === 'result') {
changeEditor(hash)
}
}
}
function changeEditor (hash) {
$codeEls.removeClass('active')
$links.removeClass('btn-primary')
$('#' + hash).addClass('active')
$('#' + hash + '-link').addClass('btn-primary')
}