1
- var template = '<ul>' +
2
- '<li class="tab" data-tab="demo">Demo</li>' +
3
- '<li class="tab" data-tab="html">HTML</li>' +
4
- '<li class="tab" data-tab="js" style="display:none;">JS</li>' +
5
- '</ul>' +
6
- '<div class="tab-content" data-for="demo">' +
7
- '<iframe></iframe>' +
8
- '</div>' +
9
- '<div class="tab-content" data-for="html">' +
10
- '<pre class="prettyprint"></pre>' +
11
- '</div>' +
12
- '<div class="tab-content" data-for="js">' +
13
- '<pre class="prettyprint lang-js"></pre>' +
14
- '</div>' ;
15
-
16
- function render ( node , docObject ) {
1
+ var template = require ( "./demo_tpl" ) ;
2
+
3
+ function render ( node , docObject ) {
17
4
var demoDiv = document . createElement ( "div" ) ;
18
5
demoDiv . className = "demo" ;
19
6
demoDiv . innerHTML = template ;
20
- var demoSrc = ( docObject . pathToRoot || ".." ) + "/" + ( node . dataset ? node . dataset . demoSrc : node . getAttribute ( "data-demo-src" ) ) ;
7
+ var demoSrc =
8
+ ( docObject . pathToRoot || ".." ) +
9
+ "/" +
10
+ ( node . dataset ? node . dataset . demoSrc : node . getAttribute ( "data-demo-src" ) ) ;
21
11
demoDiv . getElementsByTagName ( "iframe" ) [ 0 ] . src = demoSrc ;
22
12
23
13
node . innerHTML = "" ;
@@ -26,69 +16,73 @@ function render(node, docObject){
26
16
return demoDiv ;
27
17
}
28
18
29
-
30
- module . exports = function ( node ) {
19
+ module . exports = function ( node ) {
31
20
var docObject = window . docObject || { } ;
32
21
render ( node , docObject ) ;
33
22
34
23
var iframe = node . getElementsByTagName ( "iframe" ) [ 0 ] ;
35
24
36
25
iframe . addEventListener ( "load" , process ) ;
37
26
38
- function process ( ) {
39
- var demoEl = this . contentDocument . getElementById ( ' demo-html' ) ,
40
- sourceEl = this . contentDocument . getElementById ( ' demo-source' ) ;
27
+ function process ( ) {
28
+ var demoEl = this . contentDocument . getElementById ( " demo-html" ) ,
29
+ sourceEl = this . contentDocument . getElementById ( " demo-source" ) ;
41
30
42
- var html = getHTML . call ( this , demoEl ) ;
43
- var js = getJS . call ( this , sourceEl ) ;
31
+ var html = getHTML . call ( this , demoEl ) ;
32
+ var js = getJS . call ( this , sourceEl ) ;
44
33
45
- var dataForHtml = node . querySelector ( "[data-for=html] > pre" ) ;
46
- dataForHtml . innerHTML = prettyify ( html ) ;
34
+ var dataForHtml = node . querySelector ( "[data-for=html] > pre" ) ;
35
+ dataForHtml . innerHTML = prettyify ( html ) ;
47
36
48
- if ( js ) {
49
- var dataForJS = node . querySelector ( "[data-for=js] > pre" ) ;
50
- dataForJS . innerHTML = prettyify ( js . replace ( / \t / g, " " ) ) ;
51
- show ( node . querySelector ( "[data-tab=js]" ) ) ;
52
- }
37
+ if ( js ) {
38
+ var dataForJS = node . querySelector ( "[data-for=js] > pre" ) ;
39
+ dataForJS . innerHTML = prettyify ( js . replace ( / \t / g, " " ) ) ;
40
+ show ( node . querySelector ( "[data-tab=js]" ) ) ;
41
+ }
53
42
54
- tabs ( ) ;
43
+ resizeIframe ( ) ;
44
+ tabs ( ) ;
55
45
}
56
46
57
47
function getHTML ( demoEl ) {
58
- var html = demoEl ? demoEl . innerHTML : this . contentWindow . DEMO_HTML ;
59
-
60
- if ( ! html ) {
61
- // try to make from body
62
- var clonedBody = this . contentDocument . body . cloneNode ( true ) ;
63
- var scripts = [ ] . slice . call ( clonedBody . getElementsByTagName ( "script" ) ) ;
64
- scripts . forEach ( function ( script ) {
65
- if ( ! script . type || script . type . indexOf ( "javascript" ) === - 1 ) {
66
- script . parentNode . removeChild ( script ) ;
67
- }
68
- } ) ;
69
- var styles = [ ] . slice . call ( clonedBody . getElementsByTagName ( "style" ) ) ;
70
- styles . forEach ( function ( style ) {
71
- style . parentNode . removeChild ( style ) ;
72
- } ) ;
73
- html = clonedBody . innerHTML ;
74
- }
75
- return html ;
48
+ var html = demoEl ? demoEl . innerHTML : this . contentWindow . DEMO_HTML ;
49
+
50
+ if ( ! html ) {
51
+ // try to make from body
52
+ var clonedBody = this . contentDocument . body . cloneNode ( true ) ;
53
+ var scripts = [ ] . slice . call ( clonedBody . getElementsByTagName ( "script" ) ) ;
54
+ scripts . forEach ( function ( script ) {
55
+ if ( ! script . type || script . type . indexOf ( "javascript" ) === - 1 ) {
56
+ script . parentNode . removeChild ( script ) ;
57
+ }
58
+ } ) ;
59
+ var styles = [ ] . slice . call ( clonedBody . getElementsByTagName ( "style" ) ) ;
60
+ styles . forEach ( function ( style ) {
61
+ style . parentNode . removeChild ( style ) ;
62
+ } ) ;
63
+ html = clonedBody . innerHTML ;
64
+ }
65
+ return html ;
76
66
}
77
67
78
- function getJS ( sourceEl ) {
79
- var source = sourceEl ? sourceEl . innerHTML : this . contentWindow . DEMO_SOURCE ;
80
- if ( ! source ) {
81
- var scripts = [ ] . slice . call ( this . contentDocument . querySelectorAll ( "script" ) ) ;
82
- // get the first one that is JS
83
- for ( var i = 0 ; i < scripts . length ; i ++ ) {
84
- if ( ! scripts [ i ] . type || ( scripts [ i ] . type . indexOf ( "javascript" ) >= 0 &&
85
- ! scripts [ i ] . src ) ) {
86
- source = scripts [ i ] . innerHTML ;
87
- break ;
88
- }
68
+ function getJS ( sourceEl ) {
69
+ var source = sourceEl ? sourceEl . innerHTML : this . contentWindow . DEMO_SOURCE ;
70
+ if ( ! source ) {
71
+ var scripts = [ ] . slice . call (
72
+ this . contentDocument . querySelectorAll ( "script" )
73
+ ) ;
74
+ // get the first one that is JS
75
+ for ( var i = 0 ; i < scripts . length ; i ++ ) {
76
+ if (
77
+ ! scripts [ i ] . type ||
78
+ ( scripts [ i ] . type . indexOf ( "javascript" ) >= 0 && ! scripts [ i ] . src )
79
+ ) {
80
+ source = scripts [ i ] . innerHTML ;
81
+ break ;
89
82
}
90
83
}
91
- return ( source ? source . trim ( ) : '' ) ;
84
+ }
85
+ return source ? source . trim ( ) : "" ;
92
86
}
93
87
94
88
function show ( el ) {
@@ -100,32 +94,30 @@ module.exports = function(node){
100
94
}
101
95
102
96
function tabs ( ) {
103
- node . querySelector ( "ul" ) . addEventListener ( "click" , function ( ev ) {
97
+ node . querySelector ( "ul" ) . addEventListener ( "click" , function ( ev ) {
104
98
var el = ev . target ;
105
- if ( el . className === "tab" ) {
99
+ if ( el . className === "tab" ) {
106
100
toggle ( el . dataset ? el . dataset . tab : el . getAttribute ( "data-tab" ) ) ;
107
101
}
108
102
} ) ;
109
103
toggle ( "demo" ) ;
110
104
111
105
function toggle ( tabName ) {
112
- each ( ".tab" , function ( el ) {
113
- if ( el . classList ) {
106
+ each ( ".tab" , function ( el ) {
107
+ if ( el . classList ) {
114
108
el . classList . remove ( "active" ) ;
115
109
} else {
116
110
el . className = "tab" ;
117
111
}
118
-
119
112
} ) ;
120
113
121
114
each ( ".tab-content" , hide ) ;
122
- each ( ".tab[data-tab='" + tabName + "']" , function ( el ) {
123
- if ( el . classList ) {
115
+ each ( ".tab[data-tab='" + tabName + "']" , function ( el ) {
116
+ if ( el . classList ) {
124
117
el . classList . add ( "active" ) ;
125
118
} else {
126
119
el . className = "tab active" ;
127
120
}
128
-
129
121
} ) ;
130
122
each ( "[data-for='" + tabName + "']" , show ) ;
131
123
}
@@ -134,13 +126,36 @@ module.exports = function(node){
134
126
var tabs = [ ] . slice . call ( node . querySelectorAll ( selector ) ) ;
135
127
tabs . forEach ( cb ) ;
136
128
}
137
-
138
129
}
139
130
140
- function prettyify ( txt ) {
141
- txt = txt . replace ( / < / g, '<' ) ;
142
- return typeof prettyPrintOne !== "undefined" ?
143
- prettyPrintOne ( txt ) : txt ;
131
+ function prettyify ( txt ) {
132
+ txt = txt . replace ( / < / g, "<" ) ;
133
+ return typeof prettyPrintOne !== "undefined" ? prettyPrintOne ( txt ) : txt ;
144
134
}
145
135
136
+ function resizeIframe ( ) {
137
+ var frame = node . getElementsByTagName ( "iframe" ) [ 0 ] ;
138
+ var height = frame . contentWindow . document . body . scrollHeight ;
139
+
140
+ var tolerance = 5 ; // pixels
141
+ var low = height - tolerance ;
142
+ var high = height + tolerance ;
143
+
144
+ // turns "150px" to 150, and "" to 0
145
+ var getCssHeight = function ( ) {
146
+ var h = frame . style . height ;
147
+ return Number ( h . substr ( 0 , h . length - 2 ) || 0 ) ;
148
+ } ;
149
+
150
+ var cssHeight = getCssHeight ( ) ;
151
+
152
+ // Setting the height causes the next resizeIframe call to get a different
153
+ // height reading (lower); The range/tolerance logic is added to prevent the
154
+ // continous shrinking of the iframe
155
+ if ( cssHeight < low || cssHeight > high ) {
156
+ iframe . style . height = Math . min ( high , 600 ) + "px" ;
157
+ }
158
+
159
+ setTimeout ( resizeIframe , 1000 ) ;
160
+ }
146
161
} ;
0 commit comments