-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (57 loc) · 2.08 KB
/
index.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
"use strict";
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var functionize = require("./lib/functionize");
var planTasks = require("./lib/plan-tasks");
var Preset = require("./lib/preset");
var clear = require("./lib/clear");
var express = require("express");
var Resizer = function (publicDir) {
if (!this instanceof Resizer) throw "Resizer must be invoked with new";
var self = this;
this.app = express();
this.presets = [];
this.publicDir = publicDir;
this.app.on("mount", function () {
self.addHelpers();
self.addUpdateHelpers();
});
};
util.inherits(Resizer, EventEmitter);
Resizer.prototype.attach = function (name) {
var self = this;
var preset = new Preset(name);
preset.publicDir(this.publicDir);
preset.once("done", function (preset) {
if (!preset.target) throw "Preset needs a target to write to";
self.generateRoute(preset);
self.presets.push(preset);
});
return preset;
};
Resizer.prototype.generateRoute = function (preset) {
this.app.get(preset.target + "/*", planTasks(preset));
};
Resizer.prototype.addHelpers = function () {
var self = this;
this.presets.forEach(function (preset) {
self.app.parent.locals[functionize(preset.name) + "Path"] = function (src) {
return src.replace(preset.from, preset.target, src);
};
self.app.parent.locals[functionize(preset.name) + "Image"] = function (src, alt) {
return "<img src=\"" + src.replace(preset.from, preset.target, src) + "\” alt=\"" + alt + "\">";
};
});
};
Resizer.prototype.addUpdateHelpers = function () {
var self = this;
if (self.app.parent.resizer) {
throw "Resizre can not add deletion und update helpers because resizer is already defined in app.";
}
self.app.parent.resizer = {};
this.presets.forEach(function (preset) {
self.app.parent.resizer["clear" + functionize(preset.name, true)] = clear.all(preset);
});
self.app.parent.resizer.clear = clear.file(this.presets);
};
module.exports = Resizer;