WidgetTitleTruncate.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../WidgetTitleTruncateAPI'], function (APIFactory, WidgetTitleTruncateAPI) {
  9. 'use strict';
  10. var defaultValues = {
  11. truncateTitle: false
  12. };
  13. var WidgetTitleTruncate = function () {
  14. function WidgetTitleTruncate(options) {
  15. _classCallCheck(this, WidgetTitleTruncate);
  16. this._content = options.content;
  17. this._internal = options.features && options.features['Dashboard.internal'];
  18. this._dashboard = options.dashboardAPI;
  19. this._getProperty = this._getModelProp.bind(this);
  20. this._state = {
  21. initialWidgetRenderDone: false,
  22. truncateOn: false
  23. };
  24. }
  25. WidgetTitleTruncate.prototype._isSmartTitleFeatureEnabled = function _isSmartTitleFeatureEnabled() {
  26. return this._dashboard ? !this._dashboard.getGlassCoreSvc('.FeatureChecker').checkValue('dashboard', 'SmartTitle', 'disabled') : false;
  27. };
  28. WidgetTitleTruncate.prototype.getExtensionHooks = function getExtensionHooks() {
  29. if (!this._isSmartTitleFeatureEnabled()) {
  30. return undefined;
  31. }
  32. return {
  33. onInit: this._handleTitleTruncation.bind(this),
  34. onSetText: this._onSetText.bind(this),
  35. onEditStart: this._onEditStart.bind(this),
  36. onEditUpdate: this._onEditUpdate.bind(this),
  37. onEditEnd: this._onEditEnd.bind(this),
  38. //onModifyTextColor: this._onModifyTextColor.bind(this),
  39. isTextWrapOn: this._isTextWrapOn.bind(this),
  40. onTextWrapChanged: this._onTextWrapChanged.bind(this)
  41. };
  42. };
  43. WidgetTitleTruncate.prototype.setUpdateModelContent = function setUpdateModelContent(setter) {
  44. this._updateModelContent = setter;
  45. };
  46. WidgetTitleTruncate.prototype.setTitleHighlighter = function setTitleHighlighter(setter) {
  47. this.highlightTitle = setter;
  48. };
  49. WidgetTitleTruncate.prototype.highlightTitle = function highlightTitle() {
  50. if (this.highlightTitle) {
  51. this.highlightTitle();
  52. }
  53. };
  54. WidgetTitleTruncate.prototype.getState = function getState() {
  55. return {
  56. truncateTitleOn: this._content.getPropertyValue('truncateTitle'),
  57. truncateTemporarilyOff: this._truncateTemporarilyOff
  58. };
  59. };
  60. WidgetTitleTruncate.prototype.getAPI = function getAPI() {
  61. return APIFactory.createAPI(this, [WidgetTitleTruncateAPI]);
  62. };
  63. WidgetTitleTruncate.prototype._showTitle = function _showTitle() {
  64. var titleMode = this._getProperty('titleMode');
  65. if (titleMode !== 'noTitle') {
  66. return true;
  67. }
  68. return false;
  69. };
  70. WidgetTitleTruncate.prototype._isTextWrapOn = function _isTextWrapOn() {
  71. if (!this._isSmartTitleFeatureEnabled()) {
  72. return false;
  73. }
  74. var truncateTitle = this._getProperty('truncateTitle');
  75. return truncateTitle === true ? false : true;
  76. };
  77. WidgetTitleTruncate.prototype._onTextWrapChanged = function _onTextWrapChanged(textWrapFlag) {
  78. this._setProperty('truncateTitle', !textWrapFlag);
  79. };
  80. WidgetTitleTruncate.prototype._handleTitleTruncation = function _handleTitleTruncation(textEditor) {
  81. var _this = this;
  82. if (this._content) {
  83. this._onWidgetRenderDone();
  84. var showTitle = this._showTitle();
  85. var truncateTitle = this._getProperty('truncateTitle');
  86. if (showTitle && truncateTitle) {
  87. this._setTruncate(textEditor, true, true);
  88. }
  89. this._content.on('change:property:truncateTitle', function (event) {
  90. var showTitle = _this._showTitle();
  91. if (event.info.value === true) {
  92. if (showTitle && !_this._isTruncateStateSet()) {
  93. _this._setTruncate(textEditor, true);
  94. }
  95. } else {
  96. if (showTitle && _this._isTruncateStateSet()) {
  97. _this._setTruncate(textEditor, false);
  98. }
  99. }
  100. });
  101. }
  102. };
  103. WidgetTitleTruncate.prototype._onWidgetRenderDone = function _onWidgetRenderDone() {
  104. var _this2 = this;
  105. var stateAPI = this._content.getFeature('state');
  106. if (!this._state.initialWidgetRenderDone) {
  107. return stateAPI.whenStatusChanges('rendered').then(function () {
  108. _this2._getProperty = _this2._getContentProperty.bind(_this2);
  109. _this2._state.initialWidgetRenderDone = true;
  110. });
  111. }
  112. return Promise.resolve(true);
  113. };
  114. WidgetTitleTruncate.prototype._getContentProperty = function _getContentProperty(name) {
  115. return this._content.getPropertyValue(name);
  116. };
  117. WidgetTitleTruncate.prototype._getModelProp = function _getModelProp(name) {
  118. var boardModel = this._internal.getBoardModel();
  119. var id = this._content.getId();
  120. var widgetModel = boardModel.getWidgetModel(id);
  121. var value = widgetModel.get(name);
  122. return value !== undefined ? value : defaultValues[name];
  123. };
  124. WidgetTitleTruncate.prototype._setProperty = function _setProperty(name, value) {
  125. var _this3 = this;
  126. if (this._state.initialWidgetRenderDone) {
  127. this._content.setPropertyValue(name, value);
  128. } else {
  129. var stateAPI = this._content.getFeature('state');
  130. stateAPI.whenStatusChanges('rendered').then(function () {
  131. _this3._content.setPropertyValue(name, value);
  132. });
  133. }
  134. };
  135. WidgetTitleTruncate.prototype._isTruncateStateSet = function _isTruncateStateSet() {
  136. return this._state.truncateOn;
  137. };
  138. WidgetTitleTruncate.prototype._setTruncateState = function _setTruncateState(value) {
  139. this._state.truncateOn = value;
  140. };
  141. WidgetTitleTruncate.prototype._setTruncate = function _setTruncate(textEditor, truncateFlag) {
  142. var forcePropertySet = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  143. //used only when setting during initial widget rendering
  144. if (truncateFlag) {
  145. this._setTruncateState(true);
  146. textEditor.setTruncateOn();
  147. forcePropertySet && this._setProperty('truncateTitle', true); //ensure that its on on init
  148. } else if (truncateFlag === false) {
  149. this._setTruncateState(false);
  150. textEditor.setTruncateOff();
  151. this._updateModelContent && this._updateModelContent({}); //make sure titleHtml is in synch
  152. }
  153. };
  154. WidgetTitleTruncate.prototype._onSetText = function _onSetText(textEditor) {
  155. if (this._content) {
  156. var showTitle = this._showTitle();
  157. var truncateTitle = this._getProperty('truncateTitle');
  158. if (showTitle && truncateTitle) {
  159. this._setTruncate(textEditor, true, true);
  160. }
  161. }
  162. };
  163. WidgetTitleTruncate.prototype._onEditStart = function _onEditStart(textEditor) {
  164. //disable truncate when starting editing
  165. this._disableTruncateOnManualEditStart(textEditor);
  166. };
  167. WidgetTitleTruncate.prototype._onEditUpdate = function _onEditUpdate(currentTitle, currentTitleHtml, newTitle, transactionid) {
  168. //also disable smart title when editing manually
  169. if (currentTitle !== newTitle) {
  170. this._disableSmartTitleOnManualEdit(transactionid);
  171. }
  172. //title is the same, if partial formatting done, disable smart title
  173. var formattedPortionKey = newTitle + '</span>';
  174. var currentTitleHtml_ = currentTitleHtml.replace('</i>', '');
  175. currentTitleHtml_ = currentTitleHtml_.replace('</u>', '');
  176. currentTitleHtml_ = currentTitleHtml_.replace('</b>', '');
  177. if (currentTitleHtml_.indexOf(formattedPortionKey) === -1) {
  178. this._disableSmartTitleOnManualEdit(transactionid);
  179. }
  180. };
  181. WidgetTitleTruncate.prototype._onEditEnd = function _onEditEnd(textEditor) {
  182. //restore truncate when stopped editing
  183. this._restoreTruncateAfterManualEdit(textEditor);
  184. };
  185. WidgetTitleTruncate.prototype._disableTruncateOnManualEditStart = function _disableTruncateOnManualEditStart(textEditor) {
  186. if (this._content && !this._truncateTemporarilyOff) {
  187. var showTitle = this._showTitle();
  188. var truncateTitle = this._content.getPropertyValue('truncateTitle');
  189. if (showTitle) {
  190. if (truncateTitle) {
  191. this._setTruncate(textEditor, false);
  192. this._truncateTemporarilyOff = true;
  193. }
  194. }
  195. }
  196. };
  197. WidgetTitleTruncate.prototype._disableSmartTitleOnManualEdit = function _disableSmartTitleOnManualEdit(transactionid) {
  198. if (this._content) {
  199. var titleMode = this._content.getPropertyValue('titleMode');
  200. if (titleMode && titleMode === 'smartTitle') {
  201. this._content.setPropertyValue('titleMode', 'customTitle', { undoRedoTransactionId: transactionid });
  202. }
  203. }
  204. };
  205. WidgetTitleTruncate.prototype._restoreTruncateAfterManualEdit = function _restoreTruncateAfterManualEdit(textEditor) {
  206. if (this._content && this._truncateTemporarilyOff) {
  207. var showTitle = this._showTitle();
  208. var truncateTitle = this._content.getPropertyValue('truncateTitle');
  209. if (showTitle) {
  210. if (truncateTitle) {
  211. this._setTruncate(textEditor, true);
  212. this._truncateTemporarilyOff = false;
  213. }
  214. }
  215. }
  216. };
  217. // _onModifyTextColor(textEditor, addedClass, removedClass) {
  218. // textEditor.updateCssOnParagraphs(addedClass, removedClass);
  219. // }
  220. return WidgetTitleTruncate;
  221. }();
  222. return WidgetTitleTruncate;
  223. });
  224. //# sourceMappingURL=WidgetTitleTruncate.js.map