The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-ui-master/server/dynamic-css.js

61 lines
1.9 KiB

9 years ago
/* global DynamicCss */
9 years ago
'use strict';
export default () => {
const debounce = (func, wait, immediate) => {
let timeout;
return function(...args) {
const later = () => {
timeout = null;
!immediate && func.apply(this, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
callNow && func.apply(this, args);
};
};
const style = document.createElement('style');
style.type = 'text/css';
9 years ago
style.id = 'rocketchat-dynamic-css';
9 years ago
DynamicCss = typeof DynamicCss !=='undefined'? DynamicCss : {list:[]};
const colors = setting => {
if (setting._id.indexOf('theme-color') > -1) {
return setting.properties.map(property => {
const temp = setting._id.replace('theme-color-', '').split(':');
const settingName = temp[0];
const pseudo = temp[1] || '';
const propertyName = property.replace('color', '').replace('-', '');
const className = propertyName ? `${ settingName }-${ propertyName }` : settingName;
return `.${ className }${ pseudo ? ':' : '' }${ pseudo }{${ property }:${ setting.value }}`;
}).join('\n');
}
return '';
};
9 years ago
const customCss = setting => setting._id.indexOf('theme-custom-css') > -1 ? setting.value : '';
const fontFamily = setting => setting._id.indexOf('theme-font-body-font-family') > -1 ? `body{${ setting.value }}` : '';
9 years ago
9 years ago
const properties = [fontFamily, colors, customCss];
const run = list => {
return list.filter(setting => setting.value && setting.properties || setting.type !== 'color')
9 years ago
.sort((a, b) => a._id.length - b._id.length)
.map(setting => properties.reduce((ret, f) => ret || f(setting), ''))
.join('\n');
};
9 years ago
DynamicCss.run = debounce(() => {
const list = typeof RocketChat !== 'undefined' ? RocketChat.settings.collection.find({_id:/theme/}).fetch() : [];
return style.innerHTML = run(list.length && list || DynamicCss.list);
}, 1000);
document.head.appendChild(style);
DynamicCss.run();
};