1/** 2 * jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty 3 * --------------------------------------------------------------------------------- 4 * 5 * jQuery Raty is a plugin that generates a customizable star rating automatically. 6 * 7 * Licensed under The MIT License 8 * 9 * @version 1.4.3 10 * @since 06.11.2010 11 * @author Washington Botelho dos Santos 12 * @documentation http://wbotelhos.com/raty 13 * @twitter http://twitter.com/wbotelhos 14 * @license http://opensource.org/licenses/mit-license.php 15 * @package jQuery Plugins 16 * 17 * Usage with default values: 18 * --------------------------------------------------------------------------------- 19 * $('#star').raty(); 20 * 21 * <div id="star"></div> 22 * 23 * 24 * $('.star').raty(); 25 * 26 * <div class="star"></div> 27 * <div class="star"></div> 28 * <div class="star"></div> 29 * 30 */ 31 32;(function($) { 33 34 $.fn.raty = function(settings) { 35 36 if (this.length == 0) { 37 debug('Selector invalid or missing!'); 38 return; 39 } else if (this.length > 1) { 40 return this.each(function() { 41 $.fn.raty.apply($(this), [settings]); 42 }); 43 } 44 45 var opt = $.extend({}, $.fn.raty.defaults, settings), 46 $this = $(this), 47 id = this.attr('id'), 48 start = 0, 49 starFile = opt.starOn, 50 hint = '', 51 target = opt.target, 52 width = (opt.width) ? opt.width : (opt.number * opt.size + opt.number * 4); 53 54 if (id === undefined) { 55 id = 'raty-' + $this.index(); 56 $this.attr('id', id); 57 } 58 59 if (opt.number > 20) { 60 opt.number = 20; 61 } else if (opt.number < 0) { 62 opt.number = 0; 63 } 64 65 if (opt.path.substring(opt.path.length - 1, opt.path.length) != '/') { 66 opt.path += '/'; 67 } 68 69 $this.data('options', opt); 70 71 if (!isNaN(parseInt(opt.start)) && opt.start > 0) { 72 start = (opt.start > opt.number) ? opt.number : opt.start; 73 } 74 75 for (var i = 1; i <= opt.number; i++) { 76 starFile = (start >= i) ? opt.starOn : opt.starOff; 77 78 hint = (i <= opt.hintList.length && opt.hintList[i - 1] !== null) ? opt.hintList[i - 1] : i; 79 80 $this 81 .append('<img id="' + id + '-' + i + '" src="' + opt.path + starFile + '" alt="' + i + '" title="' + hint + '" class="' + id + '"/>') 82 .append((i < opt.number) ? ' ' : ''); 83 } 84 85 if (opt.iconRange && start > 0) { 86 fillStar(id, start, opt); 87 } 88 89 var $score = $('<input/>', { 90 id: id + '-score', 91 type: 'hidden', 92 name: opt.scoreName 93 }).appendTo($this); 94 95 if (start > 0) { 96 $score.val(start); 97 } 98 99 if (opt.half) { 100 splitStar($this, $('input#' + id + '-score').val(), opt); 101 } 102 103 if (!opt.readOnly) { 104 if (target !== null) { 105 target = $(target); 106 107 if (target.length == 0) { 108 debug('Target selector invalid or missing!'); 109 } 110 } 111 112 if (opt.cancel) { 113 var stars = $('img.' + id), 114 cancel = '<img src="' + opt.path + opt.cancelOff + '" alt="x" title="' + opt.cancelHint + '" class="button-cancel"/>'; 115 116 if (opt.cancelPlace == 'left') { 117 $this.prepend(cancel + ' '); 118 } else { 119 $this.append(' ').append(cancel); 120 } 121 122 $('#' + id + ' img.button-cancel').mouseenter(function() { 123 $(this).attr('src', opt.path + opt.cancelOn); 124 stars.attr('src', opt.path + opt.starOff); 125 setTarget(target, '', opt); 126 }).mouseleave(function() { 127 $(this).attr('src', opt.path + opt.cancelOff); 128 $this.mouseout(); 129 }).click(function(evt) { 130 $('input#' + id + '-score').removeAttr('value'); 131 132 if (opt.click) { 133 opt.click.apply($this, [null, evt]); 134 } 135 }); 136 137 $this.css('width', width + opt.size + 4); 138 } else { 139 $this.css('width', width); 140 } 141 142 $this.css('cursor', 'pointer'); 143 bindAll($this, opt, target); 144 } else { 145 $this.css('cursor', 'default'); 146 fixHint($this, start, opt); 147 } 148 149 return $this; 150 }; 151 152 function bindAll(context, opt, target) { 153 var id = context.attr('id'), 154 score = $('input#' + id + '-score'), 155 stars = context.children('img.' + id); 156 157 context.mouseleave(function() { 158 initialize(context, score.val(), opt); 159 clearTarget(target, score, opt); 160 }); 161 162 stars.bind(((opt.half) ? 'mousemove' : 'mouseover'), function(e) { 163 fillStar(id, this.alt, opt); 164 165 if (opt.half) { 166 var percent = parseFloat(((e.pageX - $(this).offset().left) / opt.size).toFixed(1)); 167 percent = (percent >= 0 && percent < 0.5) ? 0.5 : 1; 168 169 context.data('score', parseFloat(this.alt) + percent - 1); 170 171 splitStar(context, context.data('score'), opt); 172 } else { 173 fillStar(id, this.alt, opt); 174 } 175 176 setTarget(target, this.alt, opt); 177 }).click(function(evt) { 178 score.val(opt.half ? context.data('score') : this.alt); 179 180 if (opt.click) { 181 opt.click.apply(context, [score.val(), evt]); 182 } 183 }); 184 }; 185 186 function clearTarget(target, score, opt) { 187 if (target !== null) { 188 var value = ''; 189 190 if (opt.targetKeep) { 191 value = score.val(); 192 193 if (opt.targetType == 'hint') { 194 if (score.val() == '' && opt.cancel) { 195 value = opt.cancelHint; 196 } else { 197 value = opt.hintList[Math.ceil(score.val()) - 1]; 198 } 199 } 200 } 201 202 if (isField(target)) { 203 target.val(value); 204 } else { 205 target.html(value); 206 } 207 } 208 }; 209 210 function fillStar(id, score, opt) { 211 var qtyStar = $('img.' + id).length, 212 item = 0, 213 range = 0, 214 star, 215 starOn; 216 217 for (var i = 1; i <= qtyStar; i++) { 218 star = $('img#' + id + '-' + i); 219 220 if (i <= score) { 221 if (opt.iconRange && opt.iconRange.length > item) { 222 223 starOn = opt.iconRange[item][0]; 224 range = opt.iconRange[item][1]; 225 226 if (i <= range) { 227 star.attr('src', opt.path + starOn); 228 } 229 230 if (i == range) { 231 item++; 232 } 233 } else { 234 star.attr('src', opt.path + opt.starOn); 235 } 236 } else { 237 star.attr('src', opt.path + opt.starOff); 238 } 239 } 240 }; 241 242 function fixHint(context, score, opt) { 243 var hint = ''; 244 245 if (score != 0) { 246 score = parseInt(score); 247 hint = (score > 0 && opt.number <= opt.hintList.length && opt.hintList[score - 1] !== null) ? opt.hintList[score - 1] : score; 248 } else { 249 hint = opt.noRatedMsg; 250 } 251 252 context.attr('title', hint).children('img').attr('title', hint); 253 }; 254 255 function isField(target) { 256 return target.is('input') || target.is('select') || target.is('textarea'); 257 }; 258 259 function initialize(context, score, opt) { 260 var id = context.attr('id'); 261 262 if (isNaN(parseInt(score))) { 263 context.children('img.' + id).attr('src', opt.path + opt.starOff); 264 $('input#' + id + '-score').removeAttr('value'); 265 return; 266 } 267 268 if (score < 0) { 269 score = 0; 270 } else if (score > opt.number) { 271 score = opt.number; 272 } 273 274 fillStar(id, score, opt); 275 276 if (score > 0) { 277 $('input#' + id + '-score').val(score); 278 279 if (opt.half) { 280 splitStar(context, score, opt); 281 } 282 } 283 284 if (opt.readOnly || context.css('cursor') == 'default') { 285 fixHint(context, score, opt); 286 } 287 }; 288 289 function setTarget(target, alt, opt) { 290 if (target !== null) { 291 var value = alt; 292 293 if (opt.targetType == 'hint') { 294 if (alt == 0 && opt.cancel) { 295 value = opt.cancelHint; 296 } else { 297 value = opt.hintList[alt - 1]; 298 } 299 } 300 301 if (isField(target)) { 302 target.val(value); 303 } else { 304 target.html(value); 305 } 306 } 307 }; 308 309 function splitStar(context, score, opt) { 310 var id = context.attr('id'),
311 rounded = Math.ceil(score), 312 diff = (rounded - score).toFixed(1); 313 314 if (diff > 0.25 && diff <= 0.75) { 315 rounded = rounded - 0.5; 316 $('img#' + id + '-' + Math.ceil(rounded)).attr('src', opt.path + opt.starHalf); 317 } else if (diff > 0.75) { 318 rounded--; 319 } else { 320 $('img#' + id + '-' + rounded).attr('src', opt.path + opt.starOn); 321 } 322 }; 323 324 $.fn.raty.cancel = function(idOrClass, isClickIn) { 325 var isClick = (isClickIn === undefined) ? false : true; 326 327 if (isClick) { 328 return $.fn.raty.click('', idOrClass, 'cancel'); 329 } else { 330 return $.fn.raty.start('', idOrClass, 'cancel'); 331 } 332 }; 333 334 $.fn.raty.click = function(score, idOrClass) { 335 var context = getContext(score, idOrClass, 'click'), 336 options = $(idOrClass).data('options'); 337 338 if (idOrClass.indexOf('.') >= 0) { 339 return; 340 } 341 342 initialize(context, score, options); 343 344 if (options.click) { 345 options.click.apply(context, [score]); 346 } else { 347 debug('You must add the "click: function(score, evt) { }" callback.'); 348 } 349 350 return context; 351 }; 352 353 $.fn.raty.readOnly = function(boo, idOrClass) { 354 var context = getContext(boo, idOrClass, 'readOnly'), 355 options = $(idOrClass).data('options'), 356 cancel = context.children('img.button-cancel'); 357 358 if (idOrClass.indexOf('.') >= 0) { 359 return; 360 } 361 362 if (cancel[0]) { 363 (boo) ? cancel.hide() : cancel.show(); 364 } 365 366 if (boo) { 367 $('img.' + context.attr('id')).unbind(); 368 context.css('cursor', 'default').unbind(); 369 } else { 370 bindAll(context, options); 371 context.css('cursor', 'pointer'); 372 } 373 374 return context; 375 }; 376 377 $.fn.raty.start = function(score, idOrClass) { 378 var context = getContext(score, idOrClass, 'start'), 379 options = $(idOrClass).data('options'); 380 381 if (idOrClass.indexOf('.') >= 0) { 382 return; 383 } 384 385 initialize(context, score, options); 386 387 return context; 388 }; 389 390 function getContext(value, idOrClass, name) { 391 var context = undefined; 392 393 if (idOrClass == undefined) { 394 debug('Specify an ID or class to be the target of the action.'); 395 return; 396 } 397 398 if (idOrClass) { 399 if (idOrClass.indexOf('.') >= 0) { 400 var idEach; 401 402 return $(idOrClass).each(function() { 403 idEach = '#' + $(this).attr('id'); 404 405 if (name == 'start') { 406 $.fn.raty.start(value, idEach); 407 } else if (name == 'click') { 408 $.fn.raty.click(value, idEach); 409 } else if (name == 'readOnly') { 410 $.fn.raty.readOnly(value, idEach); 411 } 412 }); 413 } 414 415 context = $(idOrClass); 416 417 if (!context.length) { 418 debug('"' + idOrClass + '" is a invalid identifier for the public funtion $.fn.raty.' + name + '().'); 419 return; 420 } 421 } 422 423 return context; 424 }; 425 426 function debug(message) { 427 if (window.console && window.console.log) { 428 window.console.log(message); 429 } 430 }; 431 432 $.fn.raty.defaults = { 433 cancel: false, 434 cancelHint: 'cancel this rating!', 435 cancelOff: 'cancel-off.png', 436 cancelOn: 'cancel-on.png', 437 cancelPlace: 'left', 438 click: null, 439 half: false, 440 hintList: ['bad', 'poor', 'regular', 'good', 'gorgeous'], 441 noRatedMsg: 'not rated yet', 442 number: 5, 443 path: 'images/icons/', 444 iconRange: [], 445 readOnly: false, 446 scoreName: 'testimonial_score', 447 size: 16, 448 starHalf: 'star-half.png', 449 starOff: 'star-off.png', 450 starOn: 'star-on.png', 451 start: 0, 452 target: null, 453 targetKeep: false, 454 targetType: 'hint', 455 width: null 456 }; 457 458})(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.