|
1 | | -/*require(["gitbook", "jquery"], function (gitbook, $) { |
2 | | - var matcher; |
3 | | - var localConfig = {jsfiddle:{}}; |
4 | | -
|
5 | | - function getQuery(querystring) { |
6 | | - var query = {}; |
7 | | -
|
8 | | - var pairs = querystring.split('&'), |
9 | | - length = pairs.length, |
10 | | - keyval = [], |
11 | | - i = 0; |
12 | | -
|
13 | | - for (; i < length; i++) { |
14 | | - keyval = pairs[i].split('=', 2); |
15 | | - try { |
16 | | - keyval[0] = decodeURIComponent(keyval[0]); // key |
17 | | - keyval[1] = decodeURIComponent(keyval[1]); // value |
18 | | - } catch (e) { |
19 | | - } |
20 | | -
|
21 | | - if (query[keyval[0]] === undefined) { |
22 | | - query[keyval[0]] = keyval[1]; |
23 | | - } else { |
24 | | - query[keyval[0]] += ',' + keyval[1]; |
25 | | - } |
26 | | - } |
27 | | -
|
28 | | - return query; |
29 | | - } |
30 | | -
|
31 | | - // <iframe width="100%" height="300" src="http://jsfiddle.net/taucharts/hmvwg1mn/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe> |
32 | | - function embed(link, config) { |
33 | | - var iframe = document.createElement('iframe'), |
34 | | - url = link.href.replace(/\?.+/, '') + 'embedded/' + config.tabs.join(',') + '/'; |
35 | | -
|
36 | | - iframe.src = url; |
37 | | - var $frame = $(iframe); |
38 | | - $frame.attr('allowfullscreen', 'allowfullscreen'); |
39 | | - $frame.attr('frameborder', 0); |
40 | | - iframe._src = url; // support for google slide embed |
41 | | - iframe.className = link.className; // inherit all the classes from the link |
42 | | - iframe.id = link.id; // also inherit, giving more style control to the user |
43 | | - iframe.style.border = '1px solid #aaa'; |
44 | | -
|
45 | | - var query = getQuery(link.search); |
46 | | - var widht = query.width || config.width || '100%'; |
47 | | - var height = query.height || config.height || '100%'; |
48 | | - $frame.attr('width', widht); |
49 | | - $frame.attr('height', height); |
50 | | - link.parentNode.replaceChild(iframe, link); |
51 | | -
|
52 | | - var onmessage = function (event) { |
53 | | - event || (event = window.event); |
54 | | - // * 1 to coerse to number, and + 2 to compensate for border |
55 | | - iframe.style.height = (event.data.height * 1 + 2) + 'px'; |
56 | | - }; |
57 | | -
|
58 | | - if (window.addEventListener) { |
59 | | - window.addEventListener('message', onmessage, false); |
60 | | - } else { |
61 | | - window.attachEvent('onmessage', onmessage); |
62 | | - } |
63 | | - } |
64 | | -
|
65 | | - function embedAllLink(config) { |
66 | | - localConfig.jsfiddle = config.jsfiddle || {}; |
67 | | - localConfig.tabs = config.tabs || ['result']; |
68 | | - $(".book-body a").each(function (index, link) { |
69 | | - if (link.href && matcher.test(link.href)) { |
70 | | - embed(link, localConfig.jsfiddle); |
71 | | - } |
72 | | - }); |
73 | | - } |
74 | | -
|
75 | | - gitbook.events.bind("start", function (e, config) { |
76 | | - matcher = /(http|https):\/\/jsfiddle.net\/.+/; |
77 | | - embedAllLink(config); |
78 | | - }); |
79 | | -
|
80 | | - gitbook.events.bind("page.change", function () { |
81 | | - if (matcher) { |
82 | | - embedAllLink(localConfig); |
83 | | - } |
84 | | - }); |
85 | | - });*/ |
86 | 1 | var htmlparser = require('htmlparser2'); |
87 | 2 | var DomHandler = require('domhandler'); |
88 | 3 | var domutils = require('domutils'); |
@@ -118,20 +33,36 @@ var generateAdditionalParams = function (config) { |
118 | 33 | if (config.theme) { |
119 | 34 | params += config.theme + '/'; |
120 | 35 | } |
121 | | - var colors = _.chain(config).omit('href', 'type', 'theme', 'tabs').reduce(function (colors, value, color) { |
| 36 | + var colors = _.chain(config).omit('href', 'type', 'theme', 'tabs', 'width', 'height').reduce(function (colors, value, color) { |
122 | 37 | colors += color + '=' + value + '&'; |
123 | 38 | return colors; |
124 | 39 | }, ''); |
| 40 | + colors = colors.replace(/&$/, ''); |
125 | 41 | return params + '?' + colors; |
126 | 42 | }; |
127 | 43 |
|
| 44 | +var generateUrl = function (config) { |
| 45 | + var additionalParam = generateAdditionalParams(config); |
| 46 | + var type = config.type == 'frame' ? 'embedded' : 'embed'; |
| 47 | + return config.href + type + '/' + config.tabs.join(',') + additionalParam; |
| 48 | +}; |
| 49 | + |
128 | 50 | var creator = { |
129 | 51 | script: function (config) { |
130 | | - var additionalParam = generateAdditionalParams(config); |
131 | | - return '<script async src="' + config.href + 'embed/' + config.tabs.join(',') + additionalParam + '" ></script>'; |
| 52 | + return '<script async src="' + generateUrl(config) + '" ></script>'; |
132 | 53 | }, |
133 | 54 | frame: function (config) { |
134 | | - |
| 55 | + return [ |
| 56 | + '<iframe', |
| 57 | + ' width=', |
| 58 | + '"' + (config.width ? config.width + 'px' : '100%') + '"', |
| 59 | + ' height=', |
| 60 | + '"' + (config.height ? config.height + 'px' : '100%') + '"', |
| 61 | + ' src="' + generateUrl(config) + '"', |
| 62 | + ' allowfullscreen="allowfullscreen" frameborder="0"', |
| 63 | + '>', |
| 64 | + '</iframe>' |
| 65 | + ].join(''); |
135 | 66 | } |
136 | 67 | }; |
137 | 68 |
|
|
0 commit comments