PageSourceSearch

https://schmickclub.com/wp-content/themes/schremp/assets/slabText/js/jquery.slabtext.js?ver=1.0

js schmickclub.com collected 2026-09-24 13:14:57 UTC 11,913 bytes, 249 lines download raw bytes

1/*! jQuery slabtext plugin v2.4.0 MIT/GPL2 @freqdec */
2(function( $ ){
3
4    $.fn.slabText = function(options) {
5
6        // Add the slabtexted classname to the body to initiate the styling of
7        // the injected spans
8        $("body").addClass("slabtexted");
9
10        return this.each(function(){
11
12            if(options) {
13                $.extend(settings, options);
14            };
15
16            var $this               = $(this),
17                self                = this,
18                settings            = $.extend({}, $.fn.slabText.defaults, options),
19                keepSpans           = $("span.slabtext", $this).length,
20                words               = keepSpans ? [] : String($.trim($this.text())).replace(/\s{2,}/g, " ").split(" "),
21                origFontSize        = null,
22                idealCharPerLine    = null,
23                resizeThrottle      = null,
24                viewportWidth       = $(window).width(),
25                headLink            = $this.find("a:first").attr("href") || $this.attr("href"),
26                linkTitle           = headLink ? $this.find("a:first").attr("title") : "";
27
28            if(!keepSpans && settings.minCharsPerLine && words.join(" ").length < settings.minCharsPerLine) {
29                return;
30            };
31
32            // Calculates the pixel equivalent of 1em within the current header
33            var grabPixelFontSize = function() {
34                var dummy = jQuery('<div style="display:none;font-size:1em;margin:0;padding:0;height:auto;line-height:1;border:0;">&nbsp;</div>').appendTo($this),
35                    emH   = dummy.height();
36                dummy.remove();
37                return emH;
38            };
39
40            // Most of this function is a (very) stripped down AS3 to JS port of
41            // the slabtype algorithm by Eric Loyer with the original comments
42            // left intact
43            // http://erikloyer.com/index.php/blog/the_slabtype_algorithm_part_1_background/
44            var resizeSlabs = function resizeSlabs() {
45
46                // Cache the parent containers width
47                var parentWidth = $this.width(),
48                    fs;
49
50               // Sanity check to prevent infinite loop
51                if(parentWidth == 0) {
52                    return;
53                };
54
55                // Remove the slabtextdone and slabtextinactive classnames to enable the inline-block shrink-wrap effect
56                $this.removeClass("slabtextdone slabtextinactive");
57
58                if(settings.viewportBreakpoint && settings.viewportBreakpoint > viewportWidth
59                   ||
60                   settings.headerBreakpoint && settings.headerBreakpoint > parentWidth) {
61                    // Add the slabtextinactive classname to set the spans as inline
62                    // and to reset the font-size to 1em (inherit won't work in IE6/7)
63                    $this.addClass("slabtextinactive");
64                    return;
65                };
66
67                fs = grabPixelFontSize();
68                // If the parent containers font-size has changed or the "forceNewCharCount" option is true (the default),
69                // then recalculate the "characters per line" count and re-render the inner spans
70                // Setting "forceNewCharCount" to false will save CPU cycles...
71                if(!keepSpans && (settings.forceNewCharCount || fs != origFontSize)) {
72
73                    origFontSize = fs;
74
75                    var newCharPerLine      = Math.min(60, Math.floor(parentWidth / (origFontSize * settings.fontRatio))),
76                        wordIndex           = 0,
77                        lineText            = [],
78                        counter             = 0,
79                        preText             = "",
80                        postText            = "",
81                        finalText           = "",
82                        lineLength,
83                        slice,
84                        preDiff,
85                        postDiff;
86
87                    if(newCharPerLine != 0 && newCharPerLine != idealCharPerLine) {
88                        idealCharPerLine = newCharPerLine;
89
90                        while (wordIndex < words.length) {
91
92                            postText = "";
93
94                            // build two strings (preText and postText) word by word, with one
95                            // string always one word behind the other, until
96                            // the length of one string is less than the ideal number of characters
97                            // per line, while the length of the other is greater than that ideal
98                            while (postText.length < idealCharPerLine) {
99                                preText   = postText;
100                                postText += words[wordIndex] + " ";
101                                if(++wordIndex >= words.length) {
102                                    break;
103                                };
104                            };
105
106                            // This bit hacks in a minimum characters per line test
107                            // on the last line
108                            if(settings.minCharsPerLine) {
109                                slice = words.slice(wordIndex).join(" ");
110                                if(slice.length < settings.minCharsPerLine) {
111                                    postText += slice;
112                                    preText = postText;
113                                    wordIndex = words.length + 2;
114                                };
115                            };
116
117                            // calculate the character difference between the two strings and the
118                            // ideal number of characters per line
119                            preDiff  = idealCharPerLine - preText.length;
120                            postDiff = postText.length - idealCharPerLine;
121
122                            // if the smaller string is closer to the length of the ideal than
123                            // the longer string, and doesn’t contain less than minCharsPerLine
124                            // characters, then use that one for the line
125                            if((preDiff < postDiff) && (preText.length >= (settings.minCharsPerLine || 2))) {
126                                finalText = preText;
127                                wordIndex--;
128                            // otherwise, use the longer string for the line
129                            } else {
130                                finalText = postText;
131                            };
132
133                            lineLength = $.trim(finalText).length;
134
135                            // HTML-escape the text
136                            finalText = $('<div/>').text(finalText).html()
137
138                            // Wrap ampersands in spans with class `amp` for specific styling
139                            if(settings.wrapAmpersand) {
140                                finalText = finalText.replace(/&amp;/g, '<span class="amp">&amp;</span>');
141                            };
142
143                            finalText = $.trim(finalText);
144
145                            lineText.push('<span class="slabtext slabtext-linesize-' + Math.floor(lineLength / 10) + ' slabtext-linelength-' + lineLength + '">' + finalText + "</span>");
146                        };
147
148                        $this.html(lineText.join(" "));
149                        // If we have a headLink, add it back just inside our target, around all the slabText spans
150                        if(headLink) {
151                            $this.wrapInner('<a href="' + headLink + '" ' + (linkTitle ? 'title="' + linkTitle + '" ' : '') + '/>');
152                        };
153                    };
154                } else {
155                    // We only need the font-size for the resize-to-fit functionality
156                    // if not injecting the spans
157                    origFontSize = fs;
158                };
159
160                $("span.slabtext", $this).each(function() {
161                    var $span       = $(this),
162                        innerText   = $span.text(),
163                        wordSpacing = innerText.split(" ").length > 1,
164                        diff,
165                        ratio,
166                        fontSize;
167
168                    if(settings.postTweak) {
169                        $span.css({
170                            "word-spacing":0,
171                            "letter-spacing":0
172                        });
173                    };
174
175                    ratio    = parentWidth / $span.width();
176                    fontSize = parseFloat(this.style.fontSize) || origFontSize;
177
178                    $span.css("font-size", Math.min((fontSize * ratio).toFixed(settings.precision), settings.maxFontSize) + "px");
179
180                    // Do we still have space to try to fill or crop
181                    diff = !!settings.postTweak ? parentWidth - $span.width() : false;
182
183                    // A "dumb" tweak in the blind hope that the browser will
184                    // resize the text to better fit the available space.
185                    // Better "dumb" and fast...
186                    if(diff) {
187                        $span.css((wordSpacing ? 'word' : 'letter') + '-spacing', (diff / (wordSpacing ? innerText.split(" ").length - 1 : innerText.length)).toFixed(settings.precision) + "px");
188                    };
189                });
190
191                // Add the class slabtextdone to set a display:block on the child spans
192                // and avoid styling & layout issues associated with inline-block
193                $this.addClass("slabtextdone");
194
195                // Fire the callback if required
196                if(typeof settings.onRender == 'function') {
197                    settings.onRender.call(self);
198                };
199            };
200
201            // Immediate resize
202            resizeSlabs();
203
204            if(!settings.noResizeEvent) {
205                $(window).resize(function() {
206                    // Only run the resize code if the viewport width has changed.
207                    // we ignore the viewport height as it will be constantly changing.
208                    if($(window).width() == viewportWidth) {
209                        return;
210                    };
211
212                    viewportWidth = $(window).width();
213
214                    clearTimeout(resizeThrottle);
215                    resizeThrottle = setTimeout(resizeSlabs, settings.resizeThrottleTime);
216                });
217            };
218        });
219    };
220
221    $.fn.slabText.defaults = {
222            // The ratio used when calculating the characters per line
223            // (parent width / (font-size * fontRatio)).
224            "fontRatio"             : 0.78,
225            // Always recalculate the characters per line, not just when the
226            // font-size changes? Defaults to true (CPU intensive)
227            "forceNewCharCount"     : true,
228            // Do we wrap ampersands in <span class="amp">
229            "wrapAmpersand"         : true,
230            // Under what pixel width do we remove the slabtext styling?
231            "headerBreakpoint"      : null,
232            "viewportBreakpoint"    : null,
233            // Don't attach a resize event
234            "noResizeEvent"         : false,
235            // By many milliseconds do we throttle the resize event
236            "resizeThrottleTime"    : 300,
237            // The maximum pixel font size the script can set
238            "maxFontSize"           : 999,
239            // Do we try to tweak the letter-spacing or word-spacing?
240            "postTweak"             : true,
241            // Decimal precision to use when setting CSS values
242            "precision"             : 3,
243            // The min num of chars a line has to contain
244            "minCharsPerLine"       : 0,
245            // Callback function fired after the headline is redrawn
246            "onRender"              : null
247        };
248
249})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.