TextWidgetResponsiveUpgradeHelper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /*
  6. *+------------------------------------------------------------------------+
  7. *| Licensed Materials - Property of IBM
  8. *| IBM Cognos Products: BI Dashboard
  9. *| (C) Copyright IBM Corp. 2018
  10. *|
  11. *| US Government Users Restricted Rights - Use, duplication or disclosure
  12. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  13. *+------------------------------------------------------------------------+
  14. */
  15. define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils'], function (_, UpgradeBase, WidgetUpgradeUtils) {
  16. var TEXT_WIDGET_TYPE = 'text';
  17. var TextWidgetResponsiveUpgradeHelper = function (_UpgradeBase) {
  18. _inherits(TextWidgetResponsiveUpgradeHelper, _UpgradeBase);
  19. function TextWidgetResponsiveUpgradeHelper() {
  20. _classCallCheck(this, TextWidgetResponsiveUpgradeHelper);
  21. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  22. _this.VERSION = 1022;
  23. return _this;
  24. }
  25. /**
  26. * Perform upgrades for TextWidgets that might have been saved in a state where it was both isResponsive
  27. * and had font-sizes set for inner <span> elements
  28. *
  29. * @param {object} spec - spec to perform upgrade on
  30. *
  31. * @return {Promise} Promise to be resolved when upgrade performed
  32. */
  33. TextWidgetResponsiveUpgradeHelper.prototype.up = function up(spec) {
  34. if (!spec) {
  35. return Promise.resolve(spec);
  36. }
  37. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  38. return Promise.resolve(spec);
  39. }
  40. return this._upgradeWidgets(spec);
  41. };
  42. TextWidgetResponsiveUpgradeHelper.prototype.down = function down(spec) {
  43. // no downgrade at this time; return as is:
  44. // we don't save backups of specs
  45. return Promise.resolve(spec);
  46. };
  47. TextWidgetResponsiveUpgradeHelper.prototype._upgradeWidgets = function _upgradeWidgets(spec) {
  48. var _this2 = this;
  49. try {
  50. _.each(spec.widgets, function (model) {
  51. if (model.type === TEXT_WIDGET_TYPE) {
  52. _this2._upgradeTextWidget(model);
  53. }
  54. });
  55. return Promise.resolve(spec);
  56. } catch (error) {
  57. throw error;
  58. }
  59. };
  60. TextWidgetResponsiveUpgradeHelper.prototype._upgradeTextWidget = function _upgradeTextWidget(model) {
  61. if (!model.content) {
  62. return;
  63. }
  64. var table = model.content.translationTable;
  65. if (table) {
  66. for (var lang in table) {
  67. table[lang] = this._upgradeContent(table[lang], model.isResponsive);
  68. }
  69. } else if (typeof model.content === 'string') {
  70. model.content = this._upgradeContent(model.content, model.isResponsive);
  71. }
  72. };
  73. TextWidgetResponsiveUpgradeHelper.prototype._upgradeContent = function _upgradeContent(content, isResponsive) {
  74. if (isResponsive) {
  75. content = content.replace(/\s*rt_fontSize[0-9]*/gi, '');
  76. content = content.replace(/\s*font-size\s*:[^;]*;*/gi, '');
  77. }
  78. if (content.indexOf('contenteditable="true"') !== -1) {
  79. content = content.replace(/\s*contenteditable="true"/gi, '');
  80. }
  81. if (content.indexOf('style=""') !== -1) {
  82. content = content.replace(/\s*style=""/gi, '');
  83. }
  84. return content;
  85. };
  86. return TextWidgetResponsiveUpgradeHelper;
  87. }(UpgradeBase);
  88. return new TextWidgetResponsiveUpgradeHelper();
  89. });
  90. //# sourceMappingURL=TextWidgetResponsiveUpgradeHelper.js.map