PHP Classes
elePHPant
Icontem

File: js/Fr.star.js

Recommend this page to a friend!
  Classes of Subin Siby  >  Francium PHP 5 Star Rating System  >  js/Fr.star.js  >  Download  
File: js/Fr.star.js
Role: Auxiliary data
Content type: text/plain
Description: JavaScript library to implement rating system. Contains jQuery plugin too.
Class: Francium PHP 5 Star Rating System
Record star ratings of items in a MySQL with AJAX
Author: By
Last change:
Date: 3 years ago
Size: 1,886 bytes
 

Contents

Class file image Download
/**
.-------------------------------------.
|  Software: Francium Star            |
|  Version: 0.1  (2015-06-15)         |
|  http://subinsb.com/Francium-Star   |
'-------------------------------------'
*/

function log(m){
  console.log(m);
}

(function(window){
  window.Fr = window.Fr || {};
  
  Fr.star = function(elem, rated_callback){
    rated_callback = rated_callback || function(){};
    
    elem.onmousemove = function(e) {
      if(elem.getAttribute("data-disabled") == null){
        var xCoor = e.offsetX;
        var width = elem.offsetWidth;
        
        percent = (xCoor/width) * 100;
        if(percent < 101){
          rating_decimal = ("" + (percent / 100) * 5 + "").substr(0, 3);
          if(rating_decimal.substr(-2) == ".9"){
            rating_decimal = Math.round(rating_decimal, 2);
          }
          elem.setAttribute("data-title", "Set a rating of " + rating_decimal);
          
          elem.querySelector(".Fr-star-value").style.width = percent + "%";
        }
      }
    };
    
    elem.onmouseout = function(){
      original_rating = elem.getAttribute("data-rating");      
      percent = (original_rating / 5) * 100;
      elem.querySelector(".Fr-star-value").style.width = percent + "%";
      
      elem.removeAttribute("data-disabled");
    };
    
    elem.onclick = function(){
      width = elem.querySelector(".Fr-star-value").style.width.replace("%", "");
      rating = ("" + (width/100) * 5 + "").substr(0, 3);
      
      if(rating.substr(-2) == ".9"){
        rating = Math.round(rating, 2);
      }
      
      elem.setAttribute("data-rating", rating);
      elem.setAttribute("data-disabled", 1);
      rated_callback(rating);
    };
  };
})(window);

(function($){  
  $.fn.Fr_star = function(rated_callback){
    return this.each(function(){
      Fr.star($(this).get(0), rated_callback);
    });
  };
})(jQuery);