Skip to content

Commit ba2ed4b

Browse files
author
Gabriel Schulhof
committed
Demos: Reduce code duplication in view source
1 parent d661f62 commit ba2ed4b

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

docs/demos/_assets/js/jqm-demos.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ function attachPopupHandler( popup, sources ) {
9090

9191
var demoId = 0;
9292

93+
function getHeadSnippet( type, selector ) {
94+
if ( selector === "true" ) {
95+
selector = "";
96+
}
97+
return $( "<div></div>" ).append( $( "head" ).find( type + selector ).contents().clone() ).html();
98+
}
99+
93100
$.fn.viewSourceCode = function() {
94101

95102
return $( this ).each( function() {
@@ -127,24 +134,12 @@ $.fn.viewSourceCode = function() {
127134
}
128135

129136
if ( self.is( "[data-demo-js]" ) ) {
130-
var scriptId = self.attr( "data-demo-js" );
131-
132-
if ( scriptId === "true" ) {
133-
scriptId = "";
134-
}
135-
136-
data = $( "<div></div>" ).append( $( "head" ).find( "script" + scriptId ).contents().clone() ).html();
137+
data = getHeadSnippet( "script", self.attr( "data-demo-js" ) );
137138
sources.push( { title: "JS", theme: "f", brush: "js", data: data } );
138139
}
139140

140141
if ( self.is( "[data-demo-css]" ) ) {
141-
var styleId = self.attr( "data-demo-css" );
142-
143-
if ( styleId === "true" ) {
144-
styleId = "";
145-
}
146-
147-
data = $( "<div></div>" ).append( $( "head" ).find( "style" + styleId ).contents().clone() ).html();
142+
data = getHeadSnippet( "style", self.attr( "data-demo-css" ) );
148143
sources.push( { title: "CSS", theme: "e", brush: "css", data: data } );
149144
}
150145

0 commit comments

Comments
 (0)