WidgetTitleAriaLabelUpgrade.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. 2019
  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 LIVE_WIDGET_TYPE = 'live';
  17. var TEXT_WIDGET_TYPE = 'text';
  18. var WidgetTitleAriaLabelUpgrade = function (_UpgradeBase) {
  19. _inherits(WidgetTitleAriaLabelUpgrade, _UpgradeBase);
  20. function WidgetTitleAriaLabelUpgrade() {
  21. _classCallCheck(this, WidgetTitleAriaLabelUpgrade);
  22. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  23. _this.VERSION = 1402;
  24. return _this;
  25. }
  26. /**
  27. * Perform upgrade for the aria label by removing the old one so that the new
  28. * label will not be duplicated by the new code.
  29. * This is needed to solve the issue of titles getting duplicate text when
  30. * being pinned in the dashboard.
  31. *
  32. *
  33. * @param {object} spec - The widget spec that the upgrade is performed on
  34. *
  35. * @return {Promise} Promise to be resolved with upgrade performed.
  36. */
  37. WidgetTitleAriaLabelUpgrade.prototype.up = function up(spec) {
  38. if (!spec) {
  39. return Promise.resolve(spec);
  40. }
  41. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  42. return Promise.resolve(spec);
  43. }
  44. return this._upgradeWidgets(spec);
  45. };
  46. WidgetTitleAriaLabelUpgrade.prototype._upgradeWidgets = function _upgradeWidgets(spec) {
  47. for (var widget in spec.widgets) {
  48. var model = spec.widgets[widget];
  49. if (model.titleHtml && model.type == LIVE_WIDGET_TYPE) {
  50. model.titleHtml = this._upgradeLiveVisualizations(model.titleHtml);
  51. model.name = this._updateTranslationTable(model.titleHtml);
  52. } else if (model.type == TEXT_WIDGET_TYPE) {
  53. model.name = this._updateTranslationTable(model.content);
  54. }
  55. }
  56. return Promise.resolve(spec);
  57. };
  58. WidgetTitleAriaLabelUpgrade.prototype._generateTitle = function _generateTitle(html) {
  59. var $titleHtml = $(html);
  60. var title = '';
  61. $titleHtml.find('p, li').each(function (index, el) {
  62. title = title + ' ' + el.textContent;
  63. });
  64. return title.trim();
  65. };
  66. WidgetTitleAriaLabelUpgrade.prototype._updateTranslationTable = function _updateTranslationTable(table) {
  67. var new_table = JSON.parse(JSON.stringify(table));
  68. if (table.translationTable) {
  69. for (var locale in table.translationTable) {
  70. new_table.translationTable[locale] = this._generateTitle(table.translationTable[locale]);
  71. }
  72. } else {
  73. //No translation table
  74. new_table = this._generateTitle(table);
  75. }
  76. return new_table;
  77. };
  78. WidgetTitleAriaLabelUpgrade.prototype._removeAriaLabelNode = function _removeAriaLabelNode(html) {
  79. var $titleHtml = $(html);
  80. var $ariaLabel = $titleHtml.find('.ariaLabelNode');
  81. $ariaLabel.remove();
  82. return $titleHtml[0].outerHTML;
  83. };
  84. WidgetTitleAriaLabelUpgrade.prototype._upgradeLiveVisualizations = function _upgradeLiveVisualizations(titleHtml) {
  85. if (titleHtml.translationTable) {
  86. for (var locale in titleHtml.translationTable) {
  87. titleHtml.translationTable[locale] = this._removeAriaLabelNode(titleHtml.translationTable[locale]);
  88. }
  89. } else {
  90. titleHtml = this._removeAriaLabelNode(titleHtml);
  91. }
  92. return titleHtml;
  93. };
  94. return WidgetTitleAriaLabelUpgrade;
  95. }(UpgradeBase);
  96. return new WidgetTitleAriaLabelUpgrade();
  97. });
  98. //# sourceMappingURL=WidgetTitleAriaLabelUpgrade.js.map