Skip to content

Instantly share code, notes, and snippets.

@netroy
Forked from mokus80/gist:e48d9f92bbcac8d6a5fa
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netroy/3a5275c4b33e2a2ba078 to your computer and use it in GitHub Desktop.
Save netroy/3a5275c4b33e2a2ba078 to your computer and use it in GitHub Desktop.
(function(i) {
function q(e) {
var j = e.originalEvent.changedTouches[0],
f = "";
switch (e.type) {
case "touchmove":
f = "mousemove";
break;
case "touchend":
f = "mouseup";
break;
default:
return
}
var l = document.createEvent("MouseEvent");
l.initMouseEvent(f, true, true, window, 1, j.screenX, j.screenY, j.clientX, j.clientY, false, false, false, false, 0, null);
j.target.dispatchEvent(l);
e.preventDefault()
}
i.fn.rateit = function(e, j) {
var f = {},
l = "init";
if (this.length == 0) return this;
var m = i.type(e);
if (m == "object" || e === undefined || e == null) f = i.extend({},
i.fn.rateit.defaults, e);
else if (m == "string" && j === undefined) return this.data("rateit-" + e);
else if (m == "string") l = "setvalue";
return this.each(function() {
var c = i(this),
a = function(g, h) {
return c.data("rateit-" + g, h)
};
a1 = function(g) {
return c.data("rateit-" + g)
};
c.hasClass("rateit") || c.addClass("rateit");
var n = c.css("direction") != "rtl";
if (l == "setvalue") {
if (!a1("init")) throw "Can't set value before init";
if (e == "readonly" && !a1("readonly")) {
c.find(".rateit-range").unbind();
a("wired", false)
}
if (a1("backingfld")) {
var b = i(a1("backingfld"));
e == "value" && b.val(j);
if (e == "min" && b[0].min) b[0].min = j;
if (e == "max" && b[0].max) b[0].max = j;
if (e == "step" && b[0].step) b[0].step = j
}
a(e, j)
}
if (!a1("init")) {
a("min", a1("min") || f.min);
a("max", a1("max") || f.max);
a("step", a1("step") || f.step);
a("readonly", a1("readonly") !== undefined ? a1("readonly") : f.readonly);
a("resetable", a1("resetable") !== undefined ? a1("resetable") : f.resetable);
a("backingfld", a1("backingfld") || f.backingfld);
a("starwidth", a1("starwidth") || f.starwidth);
a("starheight", a1("starheight") || f.starheight);
a("value", a1("value") || f.min);
if (a1("backingfld")) {
b =
i(a1("backingfld"));
a("value", b.hide().val());
if (b[0].nodeName == "INPUT")
if (b[0].type == "range" || b[0].type == "text") {
a("min", parseInt(b.attr("min")) || a1("min"));
a("max", parseInt(b.attr("max")) || a1("max"));
a("step", parseInt(b.attr("step")) || a1("step"))
}
if (b[0].nodeName == "SELECT" && b[0].options.length > 1) {
a("min", Number(b[0].options[0].value));
a("max", Number(b[0].options[b[0].length - 1].value));
a("step", Number(b[0].options[1].value) - Number(b[0].options[0].value))
}
}
c.append('<div class="rateit-reset"></div><div class="rateit-range"><div class="rateit-selected" style="height:' +
a1("starheight") + 'px"></div><div class="rateit-hover" style="height:' + a1("starheight") + 'px"></div></div>');
if (!n) {
c.find(".rateit-reset").css("float", "right");
c.find(".rateit-selected").addClass("rateit-selected-rtl");
c.find(".rateit-hover").addClass("rateit-hover-rtl")
}
a("init", true)
}
var d = c.find(".rateit-range");
d.width(a1("starwidth") * (a1("max") - a1("min"))).height(a1("starheight"));
if (a1("value")) {
var o = (a1("value") - a1("min")) * a1("starwidth");
c.find(".rateit-selected").width(o)
}
b = c.find(".rateit-reset");
var p = function(g, h) {
var k = (h.changedTouches ? h.changedTouches[0].pageX : h.pageX) - i(g).offset().left;
n || (k = d.width() - k);
if (k > d.width()) k = d.width();
if (k < 0) k = 0;
return o = Math.ceil(k / a1("starwidth") * (1 / a1("step")))
};
if (a1("readonly")) b.hide();
else {
a1("resetable") ? b.click(function() {
a("value", a1("min"));
d.find(".rateit-hover").hide().width(0);
d.find(".rateit-selected").width(0).show();
a1("backingfld") && i(a1("backingfld")).val(a1("min"));
c.trigger("reset")
}) : b.hide();
if (!a1("wired")) {
d.bind("touchmove touchend", q);
d.mousemove(function(g) {
g = p(this, g);
var h = g * a1("starwidth") * a1("step"),
k = d.find(".rateit-hover");
if (k.data("width") != h) {
d.find(".rateit-selected").hide();
k.width(h).show().data("width", h);
c.trigger("hover", [g * a1("step") + a1("min")])
}
});
d.mouseleave(function() {
d.find(".rateit-hover").hide().width(0).data("width", "");
c.trigger("hover", [null]);
d.find(".rateit-selected").show()
});
d.mouseup(function(g) {
g = p(this, g);
var h = g * a1("step") + a1("min");
a("value", h);
a("backingfld") && i(a1("backingfld")).val(h);
d.find(".rateit-hover").hide();
d.find(".rateit-selected").width(g * a1("starwidth") * a1("step")).show();
c.trigger("hover", [null]).trigger("rated", [h])
});
a("wired", true)
}
a1("resetable") && b.show()
}
})
};
i.fn.rateit.defaults = {
min: 0,
max: 5,
step: 0.5,
starwidth: 16,
starheight: 16,
readonly: false,
resetable: true
};
i("div.rateit").rateit()
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment