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

40 lines
1.5 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:[]};
9 years ago
const colors = setting => setting._id.indexOf('theme-color') > -1 ? `.${ setting._id.replace('theme-color-', '') }{${ setting.property }:${ setting.value }}` : '';
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 => list.filter(setting => setting.value && setting.property || setting.type !== 'color').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();
};