config.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /** @type Config */
  2. var CONFIG;
  3. /** @constructor */
  4. function Config(configData) {
  5. this.deviceId = null;
  6. /** @type {Object.<string, Object<string, string>>} serviceType => { serviceId => serviceName } */
  7. this.services = {};
  8. /** @type {string|undefined} */
  9. this.emojiProvider;
  10. /** @type{boolean|undefined} */
  11. this.displayAvatar;
  12. /** @type{boolean|undefined} */
  13. this.colorfulNames;
  14. // Load global configurations
  15. for (var i =0, nbConfig = configData.length; i < nbConfig; i++)
  16. if (configData[i]["service"] === null && configData[i]["device"] === null)
  17. this.mergeConfig(JSON.parse(configData[i]["config"]));
  18. }
  19. Config.prototype.mergeConfig = function(configData) {
  20. if (configData["services"])
  21. for (var i in configData["services"]) {
  22. this.services[i] = configData["services"][i];
  23. }
  24. if (configData["emojiProvider"] !== undefined)
  25. this.emojiProvider = configData["emojiProvider"];
  26. if (configData["displayAvatar"] !== undefined)
  27. this.displayAvatar = configData["displayAvatar"];
  28. if (configData["colorfulNames"] !== undefined)
  29. this.colorfulNames = configData["colorfulNames"];
  30. };
  31. /** @return {string|undefined} */
  32. Config.prototype.getEmojiProvider = function() {
  33. return this.emojiProvider;
  34. };
  35. Config.prototype.isDisplayAvatars = function() {
  36. return this.displayAvatar !== false;
  37. };
  38. Config.prototype.isColorfulNames = function() {
  39. return this.colorfulNames === true;
  40. };
  41. Config.prototype.commitNewSettings = function(newSettings) {
  42. for (var i in newSettings) {
  43. // Just to check not empty object
  44. // TODO only global config atm
  45. this.mergeConfig(newSettings);
  46. onConfigUpdated();
  47. new HttpRequest(HttpRequestMethod.POST, "api/settings?service=null&device=null").send(JSON.stringify(newSettings));
  48. return;
  49. }
  50. };
  51. Config.prototype.compileCSS = function() {
  52. var rules = {},
  53. css = '';
  54. if (!this.isDisplayAvatars()) {
  55. rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author-img-wrapper"] = ["display: none"];
  56. rules[".chatsystem-content .chatmsg-authorGroup"] = ["display: flex"];
  57. rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author"] = ["position:initial", "vertical-align:top", "min-width:75px"];
  58. rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author-messages"] = ["padding-top:0", "padding-left:0", "display:inline-block", "margin-top:-4px", "flex:1"];
  59. }
  60. if (!this.isColorfulNames()) {
  61. rules[".chatsystem-content .chatmsg-authorGroup .chatmsg-author .chatmsg-author-name"] = ["background-color: transparent !important;"];
  62. }
  63. for (var i in rules) {
  64. css += i +'{';
  65. rules[i].forEach(function(rule) {
  66. css += rule +';';
  67. });
  68. css += '}';
  69. }
  70. return css;
  71. };
  72. CONFIG = new Config([]);