b2evolution PHP Cross Reference Blogging Systems

Source: /rsc/js/jquery/jquery.raty.js - 452 lines - 11074 bytes - Summary - Text - Print

Description: jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty ---------------------------------------------------------------------------------

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

title

Description

title

Description

title

Description

title

title

Body