浏览代码

Live load themes

Noah 7 年前
父节点
当前提交
86046d8218
共有 2 个文件被更改,包括 34 次插入11 次删除
  1. 20 9
      client/index.js
  2. 14 2
      server/index.js

+ 20 - 9
client/index.js 查看文件

@@ -1,11 +1,22 @@
1 1
 var d3 = require("d3"),
2 2
     $ = require("jquery"),
3
-    themes = require("../settings/themes.json"),
4 3
     preview = require("./preview.js"),
5 4
     video = require("./video.js"),
6 5
     audio = require("./audio.js");
7 6
 
8
-preloadImages();
7
+d3.json("/settings/themes.json", function(err, themes){
8
+
9
+  if (err) {
10
+    throw err;
11
+  }
12
+
13
+  for (var key in themes) {
14
+    themes[key] = $.extend({}, themes.default, themes[key]);
15
+  }
16
+
17
+  preloadImages(themes);
18
+
19
+});
9 20
 
10 21
 function submitted() {
11 22
 
@@ -192,11 +203,11 @@ function updateCaption() {
192 203
 }
193 204
 
194 205
 function updateTheme() {
195
-  var extended = $.extend({}, themes.default, d3.select(this.options[this.selectedIndex]).datum());
206
+  var extended = d3.select(this.options[this.selectedIndex]).datum();
196 207
   preview.theme(extended);
197 208
 }
198 209
 
199
-function preloadImages() {
210
+function preloadImages(themes) {
200 211
 
201 212
   // preload images
202 213
   var imageQueue = d3.queue();
@@ -215,22 +226,22 @@ function preloadImages() {
215 226
 
216 227
   imageQueue.awaitAll(initialize);
217 228
 
218
-  function getImage(theme,cb) {
229
+  function getImage(theme, cb) {
219 230
 
220 231
     if (!theme.backgroundImage) {
221
-      return cb(null,theme);
232
+      return cb(null, theme);
222 233
     }
223 234
 
224 235
     theme.backgroundImageFile = new Image();
225 236
     theme.backgroundImageFile.onload = function(){
226
-      return cb(null,theme);
237
+      return cb(null, theme);
227 238
     };
228 239
     theme.backgroundImageFile.onerror = function(e){
229 240
       console.warn(e);
230
-      return cb(null,theme);
241
+      return cb(null, theme);
231 242
     };
232 243
 
233
-    theme.backgroundImageFile.src = "img/" + theme.backgroundImage;
244
+    theme.backgroundImageFile.src = "/settings/backgrounds/" + theme.backgroundImage;
234 245
 
235 246
   }
236 247
 

+ 14 - 2
server/index.js 查看文件

@@ -62,8 +62,20 @@ if (!serverSettings.s3Bucket) {
62 62
 // Check the status of a current video
63 63
 app.get("/status/:id/", status);
64 64
 
65
-// Serve background images and everything else statically
66
-app.use("/img/", express.static(path.join(__dirname, "..", "settings", "backgrounds")));
65
+
66
+// Serve background images and themes JSON statically
67
+app.use("/settings/", function(req, res, next) {
68
+
69
+  // Keep server settings off limits
70
+  if (req.url.match(/\.js$/i)) {
71
+    return res.status(404).send("Cannot GET " + path.join("/settings", req.url));
72
+  }
73
+
74
+  next();
75
+
76
+}, express.static(path.join(__dirname, "..", "settings")));
77
+
78
+// Serve editor files statically
67 79
 app.use(express.static(path.join(__dirname, "..", "editor")));
68 80
 
69 81
 app.use(errorHandlers);