﻿/*
* jQuery UI Stars v2.0.0
*
* Copyright (c) 2009 Orkan (orkans@gmail.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* $Rev: 50 $
* $Date:: 2009-05-05 #$
* $Build: 3 (2009-05-05)
*
* Depends:
*  ui.core.js
*
*/
(function(A) { A.widget("ui.stars", { _init: function() { var B = this, E = this.options; E.isSelect = E.inputType == "select"; this.$selec = E.isSelect ? A("select", this.element) : null; this.$rboxs = E.isSelect ? A("option", this.$selec) : A(":radio", this.element); this.$stars = this.$rboxs.map(function(H) { if (H == 0) { E.split = typeof E.split != "number" ? 0 : E.split; E.val2id = []; E.id2val = []; E.id2title = []; E.name = E.isSelect ? B.$selec.get(0).name : this.name; E.disabled = E.disabled || (E.isSelect ? A(B.$selec).attr("disabled") : A(this).attr("disabled")); E.items = 0 } E.items++; E.val2id[this.value] = H; E.id2val[H] = this.value; E.id2title[H] = (E.isSelect ? this.text : this.title) || this.value; if (E.selected == H || (E.selected == -1 && (E.isSelect ? this.defaultSelected : this.defaultChecked))) { E.checked = H; E.value = E.id2val[H]; E.title = E.id2title[H] } var G = A("<div/>").addClass(E.starClass); var I = A("<a/>").attr("title", E.showTitles ? E.id2title[H] : "").text(this.value); if (E.split) { var F = (H % E.split); var J = Math.floor(E.starWidth / E.split); G.width(J); I.css("margin-left", "-" + (F * J) + "px") } return G.append(I).get(0) }); this.$cancel = A("<div/>").addClass(E.cancelClass).append(A("<a/>").attr("title", E.showTitles ? E.cancelTitle : "").text(E.cancelValue)); this.$value = A('<input type="hidden" name="' + E.name + '" value="' + E.value + '" />'); E.cancelShow &= !E.disabled && !E.oneVoteOnly; if (E.cancelShow) { this.element.append(this.$cancel) } this.element.append(this.$stars); this.element.append(this.$value); E.isSelect ? this.$selec.remove() : this.$rboxs.remove(); if (E.checked === undefined) { E.checked = -1; E.value = E.cancelValue; E.title = ""; if (E.cancelShow) { this._disableCancel() } } else { C(E.checked, false) } E.disabled && this.disable(); A(window).bind("unload", function() { B.$cancel.unbind(".stars"); B.$stars.unbind(".stars"); B.$selec = B.$rboxs = B.$stars = B.$value = B.$cancel = null }); function D() { B.$stars.removeClass(E.starOnClass + " " + E.starHoverClass); B._showCap("") } function C(F, H) { if (F != -1) { var I = H ? E.starHoverClass : E.starOnClass; var G = H ? E.starOnClass : E.starHoverClass; B.$stars.eq(F).prevAll("." + E.starClass).andSelf().removeClass(G).addClass(I); B.$stars.eq(F).nextAll("." + E.starClass).removeClass(E.starHoverClass + " " + E.starOnClass); B._showCap(E.id2title[F]) } else { D() } } this.$stars.bind("click.stars", function() { if (!E.forceSelect && E.disabled) { return false } var F = B.$stars.index(this); E.checked = F; E.value = E.id2val[F]; E.title = E.id2title[F]; B.$value.attr({ disabled: E.disabled ? "disabled" : "", value: E.value }); C(F, false); B._disableCancel(); if (!E.forceSelect) { B.callback("star") } }).bind("mouseover.stars", function() { if (E.disabled) { return false } var F = B.$stars.index(this); C(F, true) }).bind("mouseout.stars", function() { if (E.disabled) { return false } C(B.options.checked, false) }); this.$cancel.bind("click.stars", function() { if (!E.forceSelect && (E.disabled || (E.value == E.cancelValue))) { return false } E.checked = -1; E.value = E.cancelValue; E.title = ""; B.$value.attr({ disabled: "disabled", value: E.value }); D(); B._disableCancel(); (!E.forceSelect) && B.callback("cancel") }).bind("mouseover.stars", function() { if (B._disableCancel()) { return false } B.$cancel.addClass(E.cancelHoverClass); D(); B._showCap(E.cancelTitle) }).bind("mouseout.stars", function() { if (B._disableCancel()) { return false } B.$cancel.removeClass(E.cancelHoverClass); B.$stars.triggerHandler("mouseout.stars") }) }, _disableCancel: function() { var C = this.options, B = C.disabled || C.oneVoteOnly || (C.value == C.cancelValue); if (B) { this.$cancel.removeClass(C.cancelHoverClass).addClass(C.cancelDisabledClass) } else { this.$cancel.removeClass(C.cancelDisabledClass) } this.$cancel.css("opacity", B ? 0.5 : 1); return B }, _disableAll: function() { var B = this.options; this._disableCancel(); if (B.disabled) { this.$stars.filter("div").addClass(B.starDisabledClass) } else { this.$stars.filter("div").removeClass(B.starDisabledClass) } }, _showCap: function(B) { var C = this.options; if (C.captionEl) { C.captionEl.text(B) } }, value: function() { return this.options.value }, select: function(D) { var C = this.options, B = (D == C.cancelValue) ? this.$cancel : this.$stars.eq(C.val2id[D]); C.forceSelect = true; B.triggerHandler("click.stars"); C.forceSelect = false }, selectID: function(D) { var C = this.options, B = (D == -1) ? this.$cancel : this.$stars.eq(D); C.forceSelect = true; B.triggerHandler("click.stars"); C.forceSelect = false }, enable: function() { this.options.disabled = false; this._disableAll() }, disable: function() { this.options.disabled = true; this._disableAll() }, destroy: function() { this.options.isSelect ? this.$selec.appendTo(this.element) : this.$rboxs.appendTo(this.element); this.$cancel.unbind(".stars").remove(); this.$stars.unbind(".stars").remove(); this.$value.remove(); this.element.unbind(".stars").removeData("stars") }, callback: function(B) { var C = this.options; C.callback(this, B, C.value); C.oneVoteOnly && !C.disabled && this.disable() } }); A.extend(A.ui.stars, { version: "2.0.0", getter: "value", defaults: { inputType: "radio", split: 0, selected: -1, disabled: false, cancelTitle: "Cancel Rating", cancelValue: 0, cancelShow: true, oneVoteOnly: false, showTitles: false, captionEl: null, callback: function(C, B, D) { }, starWidth: 16, cancelClass: "ui-stars-cancel", starClass: "ui-stars-star", starOnClass: "ui-stars-star-on", starHoverClass: "ui-stars-star-hover", starDisabledClass: "ui-stars-star-disabled", cancelHoverClass: "ui-stars-cancel-hover", cancelDisabledClass: "ui-stars-cancel-disabled"} }) })(jQuery);