-
Notifications
You must be signed in to change notification settings - Fork 32
/
Stylesheet.js
277 lines (238 loc) · 9.36 KB
/
Stylesheet.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// Hatch.js is a CMS and social website building framework built in Node.js
// Copyright (C) 2013 Inventures Software Ltd
//
// This file is part of Hatch.js
//
// Hatch.js is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation, version 3
//
// Hatch.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Affero General Public License for more details. You should have received a copy of the GNU
// General Public License along with Hatch.js. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Marcus Greenwood, Anatoliy Chakkaev and others
//
'use strict';
var webrequest = require('request');
var async = require('async');
var fs = require("fs");
var less = require("less");
var finder = require('file-finder');
var _ = require('underscore');
var path = require('path');
module.exports = function (compound, Stylesheet) {
var Group = compound.models.Group;
// define the stylesheet cache
Stylesheet.cache = {};
/**
* Save and compile the stylesheet with any changes applied.
*
* @param {Function} callback - callback function
*/
Stylesheet.prototype.saveAndCompile = function (callback) {
var self = this;
self.compile(function(err) {
if(err) {
console.log(err);
throw err;
}
self.version ++;
self.lastUpdate = new Date();
self.save(function(err) {
if (self.groupId) {
Group.find(self.groupId, function (err, group) {
group.cssVersion = self.version + '-' + new Date().getTime();
group.save(function(err) {
callback(err, {
version: group.cssVersion,
url: group.cssUrl
});
});
});
} else {
callback(err, {
version: self.version
});
}
});
});
};
/**
* Compile the CSS from this less stylesheet from the stored LESS.
*
* @param {Function} callback - callback function
*/
Stylesheet.prototype.compile = function (callback) {
var self = this;
var tree, css;
var templatePath = path.join(__dirname, '/../../public/less/theme_template.less');
fs.readFile(templatePath, 'utf-8', function (err, str) {
if (err) {
return callback(err);
}
var moduleCss = '';
// get the module and widget stylesheets
async.forEach(Object.keys(compound.hatch.modules), function(key, done) {
var module = compound.hatch.modules[key];
if (!module.path) {
return done();
}
//search for stylesheets
finder.findFiles(module.path, '.less', function (err, results) {
(results || []).forEach(function(modulePath) {
var css = fs.readFileSync(modulePath, 'utf-8');
moduleCss += css + '\n';
});
done();
});
}, function (err) {
//replace the variables, bootswatch and the module Css
str = str.replace("@import \"theme-template-variables.less\";", self.less.variables);
str = str.replace("@import \"theme-template-bootswatch.less\";", self.less.bootswatch);
str = str.replace("@import \"theme-template-modules.less\";", moduleCss);
//add the custom less onto the end
str += '\n' + self.less.custom;
new(less.Parser)({
paths: [path.dirname(templatePath)],
optimization: 0
}).parse(str, function (err, tree) {
if (err) {
callback(err);
} else {
try {
css = tree.toCSS({
compress: false
});
//css should be a string
if(typeof css == "object") css = css[0];
//store in the stylesheet
self.css = css;
self.version ++;
self.lastUpdate = new Date();
if(self.groupId) {
Group.find(self.groupId, function (err, group) {
var path = compound.app.get('upload path') + '/' + group.cssVersion + '.css';
//save the file
fs.writeFile(path, css, function (err) {
group.cssUrl = '/upload/' + group.cssVersion + '.css';
group.save(function (err) {
if (callback) {
callback(null, group.cssUrl);
}
});
});
});
} else {
//success - callback!
callback(null);
}
} catch (err) {
callback(err);
}
}
});
});
});
};
/**
* Set some custom rules on this stylesheet.
*
* @param {Object} rules - the rules to be set
*/
Stylesheet.prototype.setRules = function(rules, callback) {
var css = '';
for(var selector in rules) {
for(var rule in rules[selector]) {
var value = rules[selector][rule];
//exception for font
if (selector == "@" && rule == "import") {
css = selector + rule + " " + value + ";\n" + css;
}
//standard rule
else css += "\n" + selector + " { " + rule + " : " + value + " }";
}
}
this.less.custom += '\n' + css;
this.saveAndCompile(callback);
};
/**
* Set the LESS directly for this stylesheet.
*
* @param {Object} less - less object
*/
Stylesheet.prototype.setLess = function (less, callback) {
this.less = less;
this.saveAndCompile(callback);
};
/**
* Set the theme for this stylesheet.
*
* @param {String} name - theme to load.
* @param {Function} callback - continuation function.
*/
Stylesheet.prototype.setTheme = function (name, callback) {
var self = this;
//get the cached version
if (!Stylesheet.cache[name]) {
//load the template
var theme = compound.hatch.themes.getTheme(name);
//if no theme found, use the default theme settings
if (!theme) {
return callback(new Error('Theme "' + name + '" not defined!'));
}
if (theme.variables.indexOf('/') === 0) {
theme.variables = 'http://localhost:3000' + theme.variables;
}
if (theme.bootswatch.indexOf('/') === 0) {
theme.bootswatch = 'http://localhost:3000' + theme.bootswatch;
}
var variables = '';
var bootswatch = '';
//load the theme data and then process the less
async.parallel([
function(callback) {
webrequest.get({ uri: theme.variables }, function (err, response, body) {
variables = body;
callback(err, body);
});
},
function(callback) {
webrequest.get({ uri: theme.bootswatch }, function (err, response, body) {
bootswatch = body;
callback(err, body);
});
}
], function(err, results) {
if(err) {
return callback(err);
}
//save to cache
Stylesheet.cache[name] = {
variables: variables,
bootswatch: bootswatch
};
done();
});
}
else {
//call the response function
var theme = Stylesheet.cache[name];
variables = theme.variables;
bootswatch = theme.bootswatch;
done();
}
// when everything is done, compile the stylesheet and save
function done() {
self.less = {};
self.name = name;
self.less.variables = variables;
self.less.bootswatch = bootswatch;
self.less.custom = '/* put your custom css here */';
// compile the stylesheet to a file
self.saveAndCompile(callback);
}
}
};