1
0

config.js 4.0 KB

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