This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathimages.js
71 lines (61 loc) · 1.97 KB
/
images.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
// Exported methods adds image accessors and loadImagesFrom method to
// constructor.
var File = require("fs");
var Path = require("path");
// Supported images.
var IMAGES = [ "background", "footer", "icon", "logo", "strip", "thumbnail" ];
function applyImageMethods(constructor) {
var prototype = constructor.prototype;
// Accessor methods for images (logo, strip, etc).
//
// Call with an argument to set the image and return self, call with no
// argument to get image value.
//
// pass.icon(function(callback) { ... };
// console.log(pass.icon());
//
// The 2x suffix is used for high resolution version (file name uses @2x
// suffix).
//
// pass.icon2x("[email protected]");
// console.log(pass.icon2x());
IMAGES.forEach(function(key) {
prototype[key] = function(value) {
if (arguments.length === 0) {
return this.images[key];
} else {
this.images[key] = value;
return this;
}
};
var retina = key + "2x";
prototype[retina] = function(value) {
if (arguments.length === 0) {
return this.images[retina];
} else {
this.images[retina] = value;
return this;
}
};
});
// Load all images from the specified directory. Only supported images are
// loaded, nothing bad happens if directory contains other files.
//
// path - Directory containing images to load
prototype.loadImagesFrom = function(path) {
var self = this;
var files = File.readdirSync(path);
files.forEach(function(filename) {
var basename = Path.basename(filename, ".png");
if (/@2x$/.test(basename) && ~IMAGES.indexOf(basename.slice(0, -3))) {
// High resolution
self.images[basename.replace(/@2x$/, "2x")] = Path.resolve(path, filename);
} else if (~IMAGES.indexOf(basename)) {
// Normal resolution
self.images[basename] = Path.resolve(path, filename);
}
});
return this;
};
}
module.exports = applyImageMethods;