TextFitUtil.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2019
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['textfit'], function (textfit) {
  10. /**
  11. * This class is to wrap original TextFit with error handling
  12. *
  13. * @class TextFitUtil
  14. */
  15. var TextFitUtil = function () {
  16. function TextFitUtil() {
  17. _classCallCheck(this, TextFitUtil);
  18. }
  19. /**
  20. * This method adds addition error handling to TextFit
  21. * If TextFit throws errors, force the inner span font size to 0.1px
  22. *
  23. * @static
  24. * @param {*} el DOM single element
  25. * @param {*} [options={}] options to extend default TextFit options
  26. * @returns
  27. * @memberof TextFitUtil
  28. */
  29. TextFitUtil.fillText = function fillText(el) {
  30. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  31. try {
  32. textfit(el, options);
  33. } catch (e) {
  34. this._forceFitText(el);
  35. }
  36. };
  37. /**
  38. * This method is to set font size of inner span to 0.1px
  39. * If there is no span, add one and set the font size to 0.1px
  40. * If a span with class textFitted, set the font size to 0.1px
  41. * @static
  42. * @param {*} el DOM single element
  43. * @memberof TextFitUtil
  44. */
  45. TextFitUtil._forceFitText = function _forceFitText(el) {
  46. var originalHTML = el.innerHTML;
  47. var innerSpan = el.getElementsByClassName('textFitted');
  48. var defaultFontSize = '0.1px'; // Can't be 0 because IE freaks out.
  49. if (innerSpan[0]) {
  50. return innerSpan[0].style.fontSize = defaultFontSize;
  51. }
  52. innerSpan = document.createElement('span');
  53. innerSpan.className = 'textFitted';
  54. // Inline block ensure it takes on the size of its contents, even if they are enclosed
  55. // in other tags like <p>
  56. innerSpan.style['display'] = 'inline-block';
  57. innerSpan.innerHTML = originalHTML;
  58. innerSpan.style.fontSize = defaultFontSize;
  59. el.innerHTML = '';
  60. el.appendChild(innerSpan);
  61. };
  62. _createClass(TextFitUtil, null, [{
  63. key: '_textFit',
  64. // setter for mocking unit tests :(
  65. set: function set(func) {
  66. textfit = func;
  67. }
  68. }]);
  69. return TextFitUtil;
  70. }();
  71. return TextFitUtil;
  72. });
  73. //# sourceMappingURL=TextFitUtil.js.map