Permalink
Browse files

Added color coding

  • Loading branch information...
1 parent 66c3791 commit 4e2f31ea072a8688f46cf79cd6e1ec6f67a9441f @brymck committed Jun 23, 2011
Showing with 44 additions and 11 deletions.
  1. +29 −9 src/js/content.js
  2. +9 −0 src/js/xlcolorscale-0.5.min.js
  3. +6 −2 src/manifest.json
View
@@ -1,6 +1,11 @@
var VimRatings = (function() {
var RPD_DECIMALS = 3;
+ var RPD_MIN = -0.05;
+ var RPD_MAX = 0.25;
+
var RPR_DECIMALS = 2;
+ var RPR_MIN = -1;
+ var RPR_MAX = 4;
function stripTags(str) {
return str.replace(/<[^>]+>/g, "");
@@ -10,6 +15,15 @@ var VimRatings = (function() {
return parseInt($node.attr("colspan") || 1);
}
+ function getColor(value, decimals, min, max, tag) {
+ var html = "<" + tag;
+ if (xlcs) {
+ html += " style='background-color:" + xlcs.convert(value, min, max) + "'";
+ }
+ html += ">" + value.toFixed(decimals) + "</" + tag + ">";
+ return html;
+ }
+
function matchUrl(search) {
return (window.location.href.match(search) !== null);
}
@@ -39,7 +53,6 @@ var VimRatings = (function() {
}
});
- // Place new ratings/download column after both have appeared
var isOddRow = true;
$("tr", $results).each(function(i, tr) {
@@ -48,6 +61,7 @@ var VimRatings = (function() {
var rating;
var downloads;
var $lastTd;
+ var scriptId;
var counter = 0;
var $rpd;
@@ -70,16 +84,22 @@ var VimRatings = (function() {
});
if (typeof $tds.eq(0).attr("colspan") !== "undefined") {
- $rpd = $("<td>");
+ $lastTd.after($("<td>")).after($("<td>"));
} else if ($tr.hasClass("tableheader")) {
- $rpd = $("<th>").html("Rating/<br>Download");
+ $lastTd.after($("<th>").html("Rating/<br>Download"))
+ .after($("<th>").html("Average<br>Rating"));
} else {
- $rpd = $("<td>").addClass(isOddRow ? "rowodd" : "roweven").attr("align", "right")
- .text((rating / downloads).toFixed(RPD_DECIMALS));
+ scriptId = $("a:first", $tr).attr("href").match(/script_id=(\d+)/)[1];
+ $lastTd.after($(getColor(rating / downloads, RPD_DECIMALS, RPD_MIN, RPD_MAX, "td")).attr("align", "right"))
+ .after($("<td>").addClass(isOddRow ? "rowodd" : "roweven"));
+ $.get("http://www.vim.org/scripts/script.php", { script_id: scriptId }, function(data) {
+ var numbers = data.match(/<b>(\d+)\/(\d+)<\/b>/);
+ $lastTd.next("td").replaceWith(
+ $(getColor(parseInt(numbers[1], 10) / parseInt(numbers[2], 10), RPR_DECIMALS, RPR_MIN, RPR_MAX, "td")).attr("align", "right")
+ );
+ });
isOddRow = !isOddRow;
}
-
- $lastTd.after($rpd);
});
}
}
@@ -95,8 +115,8 @@ var VimRatings = (function() {
while (match = re.exec(text)) {
numbers.push(match);
}
- $rating.html($rating.html().replace(",", " (" + (numbers[0] / numbers[1]).toFixed(RPR_DECIMALS) + "),") +
- " (" + (numbers[0] / numbers[2]).toFixed(RPD_DECIMALS) + ")");
+ $rating.html($rating.html().replace(",", " (" + getColor(numbers[0] / numbers[1], RPD_DECIMALS, RPD_MIN, RPD_MAX, "span") + "),") +
+ " (" + getColor(numbers[0] / numbers[2], RPR_DECIMALS, RPR_MIN, RPR_MAX, "span") + ")");
}
}
@@ -0,0 +1,9 @@
+/*
+ xlColorScale v0.5
+ http://www.brymck.com
+
+ Copyright 2011, Bryan McKelvey
+ Licensed under the MIT License
+ http://www.brymck.com/license
+*/
+var xlColorScale=xlcs=function(){var e=[[248,105,107],[255,235,132],[99,190,123]];return{convert:function(c,b,a){var d=e.length-1;b==null&&(b=0);a==null&&(a=a||100);c<b?c=b:c>a&&(c=a);b=(c-b)/(a-b);a=parseInt(b*(e.length-1),10);a>=d&&(a=d-1);for(var c=a,d=(b-a/d)/((a+1)/d-a/d),f="#",b=0;b<3;b++)a=parseInt(e[c][b]*(1-d)+e[c+1][b]*d,10),a=a.toString(16),f+=(a.length===1?"0":"")+a;return f}}}();
View
@@ -1,6 +1,6 @@
{
"name": "vim_ratings",
- "version": "0.5",
+ "version": "0.8",
"description": "Show additional rating metrics for vim plugins at vim.org",
"icons": {
"16": "img/icon_16.png",
@@ -10,7 +10,11 @@
"content_scripts": [
{
"matches": ["http://www.vim.org/*"],
- "js": ["js/jquery-1.6.1.min.js", "js/content.js"]
+ "js": [
+ "js/jquery-1.6.1.min.js",
+ "js/xlcolorscale-0.5.min.js",
+ "js/content.js"
+ ]
}
]
}

0 comments on commit 4e2f31e

Please sign in to comment.