-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy paththemeroller.js
110 lines (99 loc) · 3.36 KB
/
themeroller.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
"use strict";
var fs = require( "node:fs" ),
Handlebars = require( "handlebars" ),
Image = require( "./lib/themeroller-image" ),
JqueryUi = require( "./lib/jquery-ui" ),
querystring = require( "querystring" ),
textures = require( "./lib/themeroller-textures" ),
themeGallery = require( "./lib/themeroller-themegallery" )(),
ThemeRoller = require( "./lib/themeroller" );
function renderImage( filename, response, callback ) {
new Image( filename ).get( function( err, filename, data ) {
if ( err ) {
return callback( err );
}
response.setHeader( "Content-Type", "image/png" );
response.end( data );
callback();
} );
}
// Returns 'selected="selected"' if param == value
Handlebars.registerHelper( "selected", function( param, value ) {
return new Handlebars.SafeString( param === value ? "selected=\"selected\"" : "" );
} );
Handlebars.registerHelper( "themeParams", function( serializedVars ) {
return serializedVars.length > 0 ? "?themeParams=" + querystring.escape( serializedVars ) : "";
} );
var appinterfaceTemplate = Handlebars.compile( fs.readFileSync( __dirname + "/template/themeroller/appinterface.html", "utf8" ) ),
demoTemplate = Handlebars.compile( fs.readFileSync( __dirname + "/template/themeroller/demo.html", "utf8" ) ),
helpTemplate = Handlebars.compile( fs.readFileSync( __dirname + "/template/themeroller/help.html", "utf8" ) ),
indexTemplate = Handlebars.compile( fs.readFileSync( __dirname + "/template/themeroller/index.html", "utf8" ) ),
themegalleryTemplate = Handlebars.compile( fs.readFileSync( __dirname + "/template/themeroller/themegallery.html", "utf8" ) ),
wrapTemplate = Handlebars.compile( fs.readFileSync( __dirname + "/template/themeroller/wrap.html", "utf8" ) );
var Frontend = function( args ) {
Object.assign( this, args, {
jqueryUiForThemeroller: JqueryUi.find( args.resources.jqueryuiVersionForThemeroller )
} );
};
Frontend.prototype = {
index: function( vars, options ) {
if ( vars && "zThemeParams" in vars ) {
delete vars.zThemeParams;
}
var production = this.env.toLowerCase() === "production";
options = options || {};
if ( options.wrap ) {
options = {
...options,
wrap: false
};
return wrapTemplate( {
body: this.index( vars, options ),
resources: this.resources
} );
}
return indexTemplate( {
appinterface: appinterfaceTemplate( {
help: helpTemplate(),
themegallery: themegalleryTemplate( {
production: production,
themeGallery: themeGallery
} )
} ),
baseVars: themeGallery[ 0 ].serializedVars,
demo: demoTemplate( {
production: production
} ),
host: this.host,
lzmaWorker: production ? "/resources/external/lzma_worker.min.js" : "/node_modules/lzma/src/lzma_worker.js",
production: production,
resources: this.resources,
textures: JSON.stringify( textures )
} );
},
css: function( vars ) {
var theme = new ThemeRoller( {
jqueryUi: this.jqueryUiForThemeroller,
vars: Object.assign( {
dynamicImage: true,
dynamicImageHost: this.host
}, vars )
} );
return theme.css();
},
icon: function( filename, response, error ) {
renderImage( filename, response, function( err ) {
if ( err ) {
error( err, response );
}
} );
},
texture: function( filename, response, error ) {
renderImage( filename, response, function( err ) {
if ( err ) {
error( err, response );
}
} );
}
};
module.exports = Frontend;