|
| 1 | +(function ($) { |
| 2 | + var Quickfit, QuickfitHelper, defaults, pluginName; |
| 3 | + |
| 4 | + pluginName = 'quickfit'; |
| 5 | + |
| 6 | + defaults = { |
| 7 | + min: 8, |
| 8 | + max: 12, |
| 9 | + tolerance: 0.02, |
| 10 | + truncate: false, |
| 11 | + width: null, |
| 12 | + sampleNumberOfLetters: 10, |
| 13 | + sampleFontSize: 12 |
| 14 | + }; |
| 15 | + QuickfitHelper = (function () { |
| 16 | + |
| 17 | + var sharedInstance = null; |
| 18 | + |
| 19 | + QuickfitHelper.instance = function (options) { |
| 20 | + if (!sharedInstance) { |
| 21 | + sharedInstance = new QuickfitHelper(options); |
| 22 | + } |
| 23 | + return sharedInstance; |
| 24 | + }; |
| 25 | + |
| 26 | + function QuickfitHelper(options) { |
| 27 | + this.options = options; |
| 28 | + |
| 29 | + this.item = $('<span id="meassure"></span>'); |
| 30 | + this.item.css({ |
| 31 | + position: 'absolute', |
| 32 | + left: '-1000px', |
| 33 | + top: '-1000px', |
| 34 | + 'font-size': "" + this.options.sampleFontSize + "px" |
| 35 | + }); |
| 36 | + $('body').append(this.item); |
| 37 | + |
| 38 | + this.meassures = {}; |
| 39 | + } |
| 40 | + |
| 41 | + QuickfitHelper.prototype.getMeassure = function (letter) { |
| 42 | + var currentMeassure; |
| 43 | + currentMeassure = this.meassures[letter]; |
| 44 | + if (!currentMeassure) { |
| 45 | + currentMeassure = this.setMeassure(letter); |
| 46 | + } |
| 47 | + return currentMeassure; |
| 48 | + }; |
| 49 | + |
| 50 | + QuickfitHelper.prototype.setMeassure = function (letter) { |
| 51 | + var currentMeassure, index, sampleLetter, text, _ref; |
| 52 | + |
| 53 | + text = ''; |
| 54 | + sampleLetter = letter === ' ' ? ' ' : letter; |
| 55 | + |
| 56 | + for (index = 0, _ref = this.options.sampleNumberOfLetters - 1; 0 <= _ref ? index <= _ref : index >= _ref; 0 <= _ref ? index++ : index--) { |
| 57 | + text += sampleLetter; |
| 58 | + } |
| 59 | + |
| 60 | + this.item.html(text); |
| 61 | + currentMeassure = this.item.width() / this.options.sampleNumberOfLetters / this.options.sampleFontSize; |
| 62 | + this.meassures[letter] = currentMeassure; |
| 63 | + |
| 64 | + return currentMeassure; |
| 65 | + }; |
| 66 | + |
| 67 | + return QuickfitHelper; |
| 68 | + |
| 69 | + })(); |
| 70 | + |
| 71 | + Quickfit = (function () { |
| 72 | + |
| 73 | + function Quickfit(element, options) { |
| 74 | + this.$element = element; |
| 75 | + this.options = $.extend({}, defaults, options); |
| 76 | + this.$element = $(this.$element); |
| 77 | + this._defaults = defaults; |
| 78 | + this._name = pluginName; |
| 79 | + this.quickfitHelper = QuickfitHelper.instance(this.options); |
| 80 | + } |
| 81 | + |
| 82 | + Quickfit.prototype.fit = function () { |
| 83 | + var elementWidth; |
| 84 | + if (!this.options.width) { |
| 85 | + elementWidth = this.$element.width(); |
| 86 | + this.options.width = elementWidth - this.options.tolerance * elementWidth; |
| 87 | + } |
| 88 | + if (this.text = this.$element.attr('data-quickfit')) { |
| 89 | + this.previouslyTruncated = true; |
| 90 | + } else { |
| 91 | + this.text = this.$element.text(); |
| 92 | + } |
| 93 | + this.calculateFontSize(); |
| 94 | + |
| 95 | + if (this.options.truncate) this.truncate(); |
| 96 | + |
| 97 | + return { |
| 98 | + $element: this.$element, |
| 99 | + size: this.fontSize |
| 100 | + }; |
| 101 | + }; |
| 102 | + |
| 103 | + Quickfit.prototype.calculateFontSize = function () { |
| 104 | + var letter, textWidth, i; |
| 105 | + |
| 106 | + textWidth = 0; |
| 107 | + for (i = 0; i < this.text.length; ++i) { |
| 108 | + letter = this.text.charAt(i); |
| 109 | + textWidth += this.quickfitHelper.getMeassure(letter); |
| 110 | + } |
| 111 | + |
| 112 | + this.targetFontSize = parseInt(this.options.width / textWidth); |
| 113 | + return this.fontSize = Math.max(this.options.min, Math.min(this.options.max, this.targetFontSize)); |
| 114 | + }; |
| 115 | + |
| 116 | + Quickfit.prototype.truncate = function () { |
| 117 | + var index, lastLetter, letter, textToAdd, textWidth; |
| 118 | + |
| 119 | + if (this.fontSize > this.targetFontSize) { |
| 120 | + textToAdd = ''; |
| 121 | + textWidth = 3 * this.quickfitHelper.getMeassure('.') * this.fontSize; |
| 122 | + |
| 123 | + index = 0; |
| 124 | + while (textWidth < this.options.width && index < this.text.length) { |
| 125 | + letter = this.text[index++]; |
| 126 | + if (lastLetter) textToAdd += lastLetter; |
| 127 | + textWidth += this.fontSize * this.quickfitHelper.getMeassure(letter); |
| 128 | + lastLetter = letter; |
| 129 | + } |
| 130 | + |
| 131 | + if (textToAdd.length + 1 === this.text.length) { |
| 132 | + textToAdd = this.text; |
| 133 | + } else { |
| 134 | + textToAdd += '...'; |
| 135 | + } |
| 136 | + this.textWasTruncated = true; |
| 137 | + |
| 138 | + return this.$element.attr('data-quickfit', this.text).html(textToAdd); |
| 139 | + |
| 140 | + } else { |
| 141 | + if (this.previouslyTruncated) { |
| 142 | + return this.$element.html(this.text); |
| 143 | + } |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + return Quickfit; |
| 148 | + |
| 149 | + })(); |
| 150 | + |
| 151 | + return $.fn.quickfit = function (options) { |
| 152 | + var measurements = []; |
| 153 | + |
| 154 | + // Separate measurements from repaints |
| 155 | + // First calculate all measurements... |
| 156 | + var $elements = this.each(function () { |
| 157 | + var measurement = new Quickfit(this, options).fit(); |
| 158 | + measurements.push(measurement); |
| 159 | + return measurement.$element; |
| 160 | + }); |
| 161 | + |
| 162 | + // ... then apply the measurements. |
| 163 | + for (var i = 0; i < measurements.length; i++) { |
| 164 | + var measurement = measurements[i]; |
| 165 | + |
| 166 | + measurement.$element.css({ fontSize: measurement.size + 'px' }); |
| 167 | + } |
| 168 | + |
| 169 | + return $elements; |
| 170 | + }; |
| 171 | + |
| 172 | +})(jQuery, window); |
0 commit comments