platform/plugins/Q/web/js/fn/textfill.js - Q Platform
Show:

File: platform/plugins/Q/web/js/fn/textfill.js

  1. (function (Q, $, window, document, undefined) {
  2.  
  3. /**
  4. * Q Tools
  5. * @module Q-tools
  6. */
  7.  
  8. /**
  9. * Adjusts the font size of the context text until it fills the element's width and height
  10. * @class Q textfill
  11. * @constructor
  12. * @param {Object} [options] options object that contains function parameters
  13. * @param {Number} [options.maxFontPixels] Maximum size of text font,
  14. * set this if your text container is large and you don't want to have extra large text on page
  15. * @param {Number} [options.maxLines=null] Maximum number of lines,
  16. * set this if you'd like to have a maximum number of lines.
  17. * @param {boolean} [options.refreshOnLayout=true] Whether to refresh the textfill on any layout change that affects its container
  18. * @param {boolean} [options.fillPadding=false] Whether to have the text extend into the padding as well
  19. */
  20. Q.Tool.jQuery('Q/textfill',
  21.  
  22. function _Q_textfill(options) {
  23.  
  24. var $this = $(this);
  25. $this.plugin('Q/textfill', 'refresh', options);
  26. if (options.refreshOnLayout) {
  27. $this.state('Q/textfill').layoutEventKey
  28. = Q.onLayout(this[0]).set(function () {
  29. $this.plugin('Q/textfill', 'refresh');
  30. });
  31. }
  32.  
  33. },
  34.  
  35. {},
  36.  
  37. {
  38. refresh: function (options) {
  39. var o = Q.extend({}, this.state('Q/textfill'), options);
  40. var ourElement, ourText = "";
  41. this.children(':visible').each(function () {
  42. var $t = $(this);
  43. if ($t.text().length > ourText.length) {
  44. ourElement = $t;
  45. ourText = $t.text();
  46. }
  47. });
  48. if (!ourElement) {
  49. var e = new Q.Error("Q/textfill missing a visible element inside the container");
  50. console.warn(e);
  51. return false;
  52. }
  53. var $this = $(this);
  54. var fontSize = o.maxFontPixels || ($this.height() + 10);
  55. var lastGoodFontSize = 0, lastBadFontSize = fontSize, jump;
  56. var maxHeight = o.fillPadding ? $this.innerHeight() : $this.height();
  57. var maxWidth = o.fillPadding ? $this.innerWidth() : $this.width();
  58. var textHeight, textWidth, lines, tooBig;
  59. ourElement.addClass('Q_textfill_resized');
  60. for (var i=0; i<100; ++i) {
  61. ourElement.css('font-size', fontSize + 'px');
  62. textHeight = ourElement.outerHeight(true);
  63. textWidth = ourElement.outerWidth(true);
  64. if (o.maxLines) {
  65. lines = textHeight / Math.floor(fontSize * 1.5);
  66. }
  67. if (tooBig = (textHeight > maxHeight || textWidth > maxWidth
  68. || (o.maxLines && lines > o.maxLines))) {
  69. lastBadFontSize = fontSize;
  70. jump = (lastGoodFontSize - fontSize) / 2;
  71. } else {
  72. lastGoodFontSize = fontSize;
  73. jump = (lastBadFontSize - fontSize) / 2
  74. }
  75. if (Math.abs(jump) < 1) {
  76. break;
  77. }
  78. fontSize = Math.floor(fontSize + jump);
  79. if (fontSize < 3) {
  80. lastGoodFontSize = 3;
  81. break; // container is super small
  82. }
  83. };
  84. ourElement.css('font-size', lastGoodFontSize + 'px');
  85. return this;
  86. },
  87. remove: function () {
  88. Q.onLayout(this[0]).remove(this.state('Q/textfill').layoutEventKey);
  89. }
  90. }
  91.  
  92. );
  93.  
  94. })(Q, jQuery, window, document);