Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Theme Package to JS #6491

Merged
merged 7 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/rocketchat-theme/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Package.onUse(function(api) {
api.use('rocketchat:lib');
api.use('rocketchat:logger');
api.use('rocketchat:assets');
api.use('coffeescript');
api.use('ecmascript');
api.use('less');
api.use('underscore');
Expand All @@ -18,8 +17,8 @@ Package.onUse(function(api) {
api.use('templating', 'client');

// Server side files
api.addFiles('server/server.coffee', 'server');
api.addFiles('server/variables.coffee', 'server');
api.addFiles('server/server.js', 'server');
api.addFiles('server/variables.js', 'server');

// Colorpicker
api.addFiles('client/vendor/jscolor.js', 'client');
Expand Down
159 changes: 0 additions & 159 deletions packages/rocketchat-theme/server/server.coffee

This file was deleted.

189 changes: 189 additions & 0 deletions packages/rocketchat-theme/server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/* globals WebAppHashing */

const less = Npm.require('less');
const Autoprefixer = Npm.require('less-plugin-autoprefix');
const crypto = Npm.require('crypto');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import

const logger = new Logger('rocketchat:theme', {
methods: {
stop_rendering: {
type: 'info'
}
}
});

WebApp.rawConnectHandlers.use(function(req, res, next) {
let css;
let hash;

const path = req.url.split('?')[0];
const prefix = __meteor_runtime_config__.ROOT_URL_PATH_PREFIX || '';
if (path === (`${ prefix }/__cordova/theme.css`) || path === (`${ prefix }/theme.css`)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for these parenthesis

css = RocketChat.theme.getCss();
hash = crypto.createHash('sha1').update(css).digest('hex');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare css and hash here

res.setHeader('Content-Type', 'text/css; charset=UTF-8');
res.setHeader('ETag', `"${ hash }"`);
res.write(css);
return res.end();
} else {
return next();
}
});

const calculateClientHash = WebAppHashing.calculateClientHash;

WebAppHashing.calculateClientHash = function(manifest, includeFilter, runtimeConfigOverride) {
let hash;
let themeManifestItem;
const css = RocketChat.theme.getCss();
if (css.trim() !== '') {
hash = crypto.createHash('sha1').update(css).digest('hex');
themeManifestItem = _.find(manifest, function(item) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare hash and themeManifestItem here

return item.path === 'app/theme.css';
});
if (themeManifestItem == null) {
themeManifestItem = {};
manifest.push(themeManifestItem);
}
themeManifestItem.path = 'app/theme.css';
themeManifestItem.type = 'css';
themeManifestItem.cacheable = true;
themeManifestItem.where = 'client';
themeManifestItem.url = `/theme.css?${ hash }`;
themeManifestItem.size = css.length;
themeManifestItem.hash = hash;
}
return calculateClientHash.call(this, manifest, includeFilter, runtimeConfigOverride);
};

RocketChat.theme = new class {
constructor() {
this.variables = {};
this.packageCallbacks = [];
this.files = ['server/lesshat.less', 'server/colors.less'];
this.customCSS = '';
RocketChat.settings.add('css', '');
RocketChat.settings.addGroup('Layout');
RocketChat.settings.onload('css', Meteor.bindEnvironment(() => {
return function(key, value, initialLoad) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for this function, pass the params directly to the above arrow function

if (!initialLoad) {
return Meteor.startup(function() {
return process.emit('message', {
refresh: 'client'
});
});
}
};
})(this));
this.compileDelayed = _.debounce(Meteor.bindEnvironment(this.compile.bind(this)), 100);
Meteor.startup(() => {
return function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

return RocketChat.settings.onAfterInitialLoad(function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use arrow function here

return RocketChat.settings.get('*', Meteor.bindEnvironment(function(key, value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use arrow function here

let name;
if (key === 'theme-custom-css' && value != null) {
this.customCSS = value;
} else if (/^theme-.+/.test(key) === true) {
name = key.replace(/^theme-[a-z]+-/, '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare name here

if (this.variables[name] != null) {
this.variables[name].value = value;
}
} else {
return;
}
return this.compileDelayed();
}));
});
};
});
}

compile() {

let content = [this.getVariablesAsLess(), ...Object.keys(this.files || []).map((key) => {
const file = this.files[key];
return Assets.getText(file);
}), ...Object.keys(this.packageCallbacks || []).map((key)=>{
const value = this.packageCallback[key];
return value();
}).filter(value => _.isString(value))];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, split this code in parts.

this.files is not an object, it's and array, this.packageCallback is an array as well


content.push(this.customCSS);
content = content.join('\n');
const options = {
compress: true,
plugins: [new Autoprefixer()]
};
const start = Date.now();
return less.render(content, options, function(err, data) {
logger.stop_rendering(Date.now() - start);
if (err != null) {
return console.log(err);
}
RocketChat.settings.updateById('css', data.css);
return Meteor.startup(function() {
return Meteor.setTimeout(function() {
return process.emit('message', {
refresh: 'client'
});
}, 200);
});
});
}

addVariable(type, name, value, section, persist, editor, allowedTypes) {
let config;
if (persist == null) {
persist = true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use default values

this.variables[name] = {
type,
value
};
if (persist === true) {
config = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare config here

group: 'Layout',
type,
editor: editor || type,
section,
'public': false,
allowedTypes
};
return RocketChat.settings.add(`theme-${ type }-${ name }`, value, config);
}
}

addPublicColor(name, value, section, editor) {
if (editor == null) {
editor = 'color';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use default values

return this.addVariable('color', name, value, section, true, editor, ['color', 'expression']);
}

addPublicFont(name, value) {
return this.addVariable('font', name, value, 'Fonts', true);
}

getVariablesAsObject() {
return Object.keys(this.variables).reduce((obj, name) => {
obj[name] = this.variables[name].value;
return obj;
}, {});
}

getVariablesAsLess() {
return Object.keys(this.variables).map((obj, name) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map((obj, name) => { is wrong, the correct is map((name) => {

const variable = this.variables[name];
return `@${ name }: ${ variable.value };`;
}).join('\n');
}

addPackageAsset(cb) {
this.packageCallbacks.push(cb);
return this.compileDelayed();
}

getCss() {
return RocketChat.settings.get('css') || '';
}

};
Loading