config.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. rules[".chatmsg-author-name::before"] = [ "content:'<'" ];
  60. rules[".chatmsg-author-name::after"] = [ "content:'>'" ];
  61. }
  62. if (!this.isColorfulNames()) {
  63. rules[".chatmsg-authorGroup .chatmsg-item .chatmsg-link-user, .chatsystem-content a.chatmsg-author-name, .chatmsg-author-name"] = ["background-color: transparent !important;"];
  64. }
  65. for (var i in rules) {
  66. css += i +'{';
  67. rules[i].forEach(function(rule) {
  68. css += rule +';';
  69. });
  70. css += '}';
  71. }
  72. return css;
  73. };
  74. CONFIG = new Config([]);