vendor: 6,988 bytes, lines 1-270
1/**! 2 * trunk8 v1.3.2 3 * https://github.com/rviscomi/trunk8 4 * 5 * Copyright 2012 Rick Viscomi 6 * Released under the MIT License. 7 * 8 * Date: October 21, 2012 9 */ 10 11(function ($) { 12 var methods, 13 utils, 14 SIDES = { 15 /* cen...ter */ 16 center: 'center', 17 /* ...left */ 18 left: 'left', 19 /* right... */ 20 right: 'right' 21 }, 22 WIDTH = { 23 auto: 'auto' 24 }; 25 26 function trunk8(element) { 27 this.$element = $(element); 28 this.original_text = this.$element.html(); 29 this.settings = $.extend({}, $.fn.trunk8.defaults); 30 } 31 32 trunk8.prototype.updateSettings = function (options) { 33 this.settings = $.extend(this.settings, options); 34 }; 35 36 function truncate() { 37 var data = this.data('trunk8'), 38 settings = data.settings, 39 width = settings.width, 40 side = settings.side, 41 fill = settings.fill, 42 line_height = utils.getLineHeight(this) * settings.lines, 43 str = data.original_text, 44 length = str.length, 45 max_bite = '', 46 lower, upper, 47 bite_size, 48 bite; 49 50 /* Reset the field to the original string. */ 51 this.html(str); 52 53 if (width === WIDTH.auto) { 54 /* Assuming there is no "overflow: hidden". */ 55 if (this.height() <= line_height) { 56 /* Text is already at the optimal trunkage. */ 57 return; 58 } 59 60 /* Binary search technique for finding the optimal trunkage. */ 61 /* Find the maximum bite without overflowing. */ 62 lower = 0; 63 upper = length - 1; 64 65 while (lower <= upper) { 66 bite_size = lower + ((upper - lower) >> 1); 67 68 bite = utils.eatStr(str, side, length - bite_size, fill); 69 70 this.html(bite); 71 72 /* Check for overflow. */ 73 if (this.height() > line_height) { 74 upper = bite_size - 1; 75 } 76 else { 77 lower = bite_size + 1; 78 79 /* Save the bigger bite. */ 80 max_bite = (max_bite.length > bite.length) ? max_bite : bite; 81 } 82 } 83 84 /* Reset the content to eliminate possible existing scroll bars. */ 85 this.html(''); 86 87 /* Display the biggest bite. */ 88 this.html(max_bite); 89 90 if (settings.tooltip) { 91 this.attr('title', str); 92 } 93 } 94 else if (!isNaN(width)) { 95 bite_size = length - width; 96 97 bite = utils.eatStr(str, side, bite_size, fill); 98 99 this.html(bite); 100 101 if (settings.tooltip) { 102 this.attr('title', str); 103 } 104 } 105 else { 106 $.error('Invalid width "' + width + '".'); 107 } 108 } 109 110 methods = { 111 init: function (options) { 112 return this.each(function () { 113 var $this = $(this), 114 data = $this.data('trunk8'); 115 116 if (!data) { 117 $this.data('trunk8', (data = new trunk8(this))); 118 } 119 120 data.updateSettings(options); 121 122 truncate.call($this); 123 }); 124 }, 125 126 /** Updates the text value of the elements while maintaining truncation. */ 127 update: function (new_string) { 128 return this.each(function () { 129 var $this = $(this); 130 131 /* Update text. */ 132 if (new_string) { 133 $this.data('trunk8').original_text = new_string; 134 } 135 136 /* Truncate accordingly. */ 137 truncate.call($this); 138 }); 139 }, 140 141 revert: function () { 142 return this.each(function () { 143 /* Get original text. */ 144 var text = $(this).data('trunk8').original_text; 145 146 /* Revert element to original text. */ 147 $(this).html(text); 148 }); 149 }, 150 151 /** Returns this instance's settings object. NOT CHAINABLE. */ 152 getSettings: function () { 153 return this.get(0).data('trunk8').settings; 154 } 155 }; 156 157 utils = { 158 /** Replaces [bite_size] [side]-most chars in [str] with [fill]. */ 159 eatStr: function (str, side, bite_size, fill) { 160 var length = str.length, 161 key = utils.eatStr.generateKey.apply(null, arguments), 162 half_length, 163 half_bite_size; 164 165 /* If the result is already in the cache, return it. */ 166 if (utils.eatStr.cache[key]) { 167 return utils.eatStr.cache[key]; 168 } 169 170 /* Common error handling. */ 171 if ((typeof str !== 'string') || (length === 0)) { 172 $.error('Invalid source string "' + str + '".'); 173 } 174 if ((bite_size < 0) || (bite_size > length)) { 175 $.error('Invalid bite size "' + bite_size + '".'); 176 } 177 else if (bite_size === 0) { 178 /* No bite should show no truncation. */ 179 return str; 180 } 181 if (typeof (fill + '') !== 'string') { 182 $.error('Fill unable to be converted to a string.'); 183 } 184 185 /* Compute the result, store it in the cache, and return it. */ 186 switch (side) { 187 case SIDES.right: 188 /* str... */ 189 return utils.eatStr.cache[key] = 190 $.trim(str.substr(0, length - bite_size)) + fill; 191 192 case SIDES.left: 193 /* ...str */ 194 return utils.eatStr.cache[key] = 195 fill + $.trim(str.substr(bite_size)); 196 197 case SIDES.center: 198 /* Bit-shift to the right by one === Math.floor(x / 2) */ 199 half_length = length >> 1; // halve the length 200 half_bite_size = bite_size >> 1; // halve the bite_size 201 202 /* st...r */ 203 return utils.eatStr.cache[key] = 204 $.trim(utils.eatStr(str.substr(0, length - half_length), SIDES.right, bite_size - half_bite_size, '')) + 205 fill + 206 $.trim(utils.eatStr(str.substr(length - half_length), SIDES.left, half_bite_size, '')); 207 208 default: 209 $.error('Invalid side "' + side + '".'); 210 } 211 }, 212 213 getLineHeight: function (elem) { 214 var $elem = $(elem), 215 floats = $elem.css('float'), 216 position = $elem.css('position'), 217 html = $elem.html(), 218 wrapper_id = 'line-height-test', 219 line_height; 220 221 if (floats !== 'none') { 222 $elem.css('float', 'none'); 223 } 224 225 if (position === 'absolute') { 226 $elem.css('position', 'static'); 227 } 228 229 /* Set the content to a small single character and wrap. */ 230 $elem.html('i').wrap('<div id="' + wrapper_id + '" />'); 231 232 /* Calculate the line height by measuring the wrapper.*/ 233 line_height = $('#' + wrapper_id).innerHeight(); 234 235 /* Remove the wrapper and reset the style/content. */ 236 $elem.html(html).css({ 237 'float': floats, 238 'position': position 239 }).unwrap(); 240 241 return line_height; 242 } 243 }; 244 245 utils.eatStr.cache = {}; 246 utils.eatStr.generateKey = function () { 247 return Array.prototype.join.call(arguments, ''); 248 }; 249 250 $.fn.trunk8 = function (method) { 251 if (methods[method]) { 252 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 253 } 254 else if (typeof method === 'object' || !method) { 255 return methods.init.apply(this, arguments); 256 } 257 else { 258 $.error('Method ' + method + ' does not exist on jQuery.trunk8'); 259 } 260 }; 261 262 /* Default trunk8 settings. */ 263 $.fn.trunk8.defaults = { 264 fill: '…', 265 lines: 1, 266 side: SIDES.right, 267 tooltip: true, 268 width: WIDTH.auto 269 }; 270})(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.