TextWidgetParagraphUpgrade.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, 2021
  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(['../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils', 'jquery'], function (UpgradeBase, WidgetUpgradeUtils, $) {
  16. var TEXT_WIDGET_TYPE = 'text';
  17. var TextWidgetParagraphUpgrade = function (_UpgradeBase) {
  18. _inherits(TextWidgetParagraphUpgrade, _UpgradeBase);
  19. function TextWidgetParagraphUpgrade() {
  20. _classCallCheck(this, TextWidgetParagraphUpgrade);
  21. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  22. _this.VERSION = 1026;
  23. return _this;
  24. }
  25. /**
  26. * Perform upgrades for TextWidgets before support for numbered and unordered lists were added.
  27. * The default classes previously applied to the paragraph tag need to be moved up because adding
  28. * list replaces the paragraph tag.
  29. *
  30. * @param {object} spec - spec to perform upgrade on
  31. *
  32. * @return {Promise} Promise to be resolved when upgrade performed
  33. */
  34. TextWidgetParagraphUpgrade.prototype.up = function up(spec) {
  35. if (!spec) {
  36. return Promise.resolve(spec);
  37. }
  38. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  39. return Promise.resolve(spec);
  40. }
  41. return this._upgradeWidgets(spec);
  42. };
  43. TextWidgetParagraphUpgrade.prototype._upgradeWidgets = function _upgradeWidgets(spec) {
  44. for (var widget in spec.widgets) {
  45. var model = spec.widgets[widget];
  46. if (model.type === TEXT_WIDGET_TYPE && model.content) {
  47. this._upgradeTextWidget(model);
  48. }
  49. }
  50. return Promise.resolve(spec);
  51. };
  52. TextWidgetParagraphUpgrade.prototype._upgradeTextWidget = function _upgradeTextWidget(model) {
  53. var table = model.content.translationTable;
  54. if (table) {
  55. for (var lang in table) {
  56. table[lang] = this._upgradeContent(table[lang]);
  57. }
  58. } else if (typeof model.content === 'string') {
  59. model.content = this._upgradeContent(model.content);
  60. }
  61. };
  62. TextWidgetParagraphUpgrade.prototype._upgradeContent = function _upgradeContent(content) {
  63. var $content = $(content);
  64. var $paragraphs = $content.find('p');
  65. var $textFitted = $content.find('.textFitted');
  66. var className = $paragraphs.first().attr('class');
  67. $paragraphs.removeAttr('class');
  68. if ($textFitted.length) {
  69. $textFitted.addClass(className);
  70. } else {
  71. var $span = $('<span class=\'textFitted ' + className + '\'></span>');
  72. $paragraphs.wrapAll($span);
  73. }
  74. return $content.wrap('<div></div>').parent().html();
  75. };
  76. return TextWidgetParagraphUpgrade;
  77. }(UpgradeBase);
  78. return new TextWidgetParagraphUpgrade();
  79. });
  80. //# sourceMappingURL=TextWidgetParagraphUpgrade.js.map