|
| 1 | +/* |
| 2 | + * jQuery UI Rating 1.9 |
| 3 | + * |
| 4 | + * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about) |
| 5 | + * Dual licensed under the MIT or GPL Version 2 licenses. |
| 6 | + * http://jquery.org/license |
| 7 | + * |
| 8 | + * http://docs.jquery.com/UI/Rating |
| 9 | + * |
| 10 | + * Depends: |
| 11 | + * jquery.ui.core.js |
| 12 | + * jquery.ui.widget.js |
| 13 | + */ |
| 14 | +(function( $, undefined ) { |
| 15 | + |
| 16 | +$.widget("ui.rating", { |
| 17 | + version: "@VERSION", |
| 18 | + |
| 19 | + options: { |
| 20 | + value: null, |
| 21 | + max: null, |
| 22 | + readOnly: false, |
| 23 | + |
| 24 | + // callbacks |
| 25 | + change: null |
| 26 | + }, |
| 27 | + |
| 28 | + min: 0, |
| 29 | + |
| 30 | + _create: function() { |
| 31 | + var self = this; |
| 32 | + |
| 33 | + var ratingDiv = $( "<div class='ui-rating ui-rating-current' />"); |
| 34 | + var ratingStar = $( "<a href='#' class='ui-icon ui-rating-star' />"); |
| 35 | + |
| 36 | + var element = self.element.wrap( ratingDiv ).parent(); |
| 37 | + |
| 38 | + $.extend(self, { |
| 39 | + originalElement: this.element, |
| 40 | + element: element |
| 41 | + }); |
| 42 | + |
| 43 | + if (isNaN(parseFloat(self.options.value))) |
| 44 | + { |
| 45 | + self.options.value = parseFloat(self.originalElement.val()); |
| 46 | + } |
| 47 | + |
| 48 | + if (isNaN(parseInt(self.options.max))) |
| 49 | + { |
| 50 | + self.options.max = 0; |
| 51 | + } |
| 52 | + |
| 53 | + $.each( $( "option", self.originalElement ), function( index, option ) { |
| 54 | + var val = parseInt( $( option ).val(), 0 ); |
| 55 | + |
| 56 | + self.options.max = ( val > self.options.max ) ? val : self.options.max; |
| 57 | + }); |
| 58 | + |
| 59 | + for ( index = 1; index <= self.options.max; index++ ) { |
| 60 | + ratingStar.clone().attr( 'id', 'rating_star_rating_' + index ).text( "Give it a " + index ).data( "rating", index ).appendTo( self.element ); |
| 61 | + } |
| 62 | + |
| 63 | + self.originalElement.hide(); |
| 64 | + |
| 65 | + // self.oldValue = self._getValue(); |
| 66 | + self._refreshValue(); |
| 67 | + |
| 68 | + // self.oldReadOnly = self._getReadOnly(); |
| 69 | + self._setReadOnly(); |
| 70 | + }, |
| 71 | + |
| 72 | + _destroy: function() { |
| 73 | + this.originalElement.siblings().remove().end().show().unwrap( this.element ); |
| 74 | + |
| 75 | + this.element = this.originalElement; |
| 76 | + |
| 77 | + delete this.originalElement; |
| 78 | + delete this.oldValue; |
| 79 | + delete this.oldReadOnly; |
| 80 | + }, |
| 81 | + |
| 82 | + _events: { |
| 83 | + 'click .ui-rating-star': function( event ) { |
| 84 | + event.preventDefault(); |
| 85 | + |
| 86 | + this.options.value = $( event.target ).data( "rating" ); |
| 87 | + |
| 88 | + this._refreshValue(); |
| 89 | + }, |
| 90 | + |
| 91 | + mouseover: function( event ) { |
| 92 | + var $target = $( event.target ); |
| 93 | + |
| 94 | + if ( $target.is( ".ui-rating-star" ) ) { |
| 95 | + this.element.removeClass( "ui-rating-current" ); |
| 96 | + |
| 97 | + // create referece object for stars |
| 98 | + var ratingStars = $( ".ui-rating-star", this.element ); |
| 99 | + |
| 100 | + // add hover class to the current and preceding stars |
| 101 | + ratingStars.removeClass( "ui-rating-hover" ).filter(function( index ) { |
| 102 | + return index <= ratingStars.index( $target ); |
| 103 | + }).addClass( "ui-rating-hover" ); |
| 104 | + |
| 105 | + this._trigger( "change", null, this.ui() ); |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + mouseleave: function( event ) { |
| 110 | + this.element.addClass( "ui-rating-current" ).find( ".ui-rating-hover" ).removeClass( "ui-rating-hover" ); |
| 111 | + |
| 112 | + this._trigger( "change", null, this.ui() ); |
| 113 | + } |
| 114 | + }, |
| 115 | + |
| 116 | + _eventsReadOnly: { |
| 117 | + 'click .ui-rating-star': function( event ) { |
| 118 | + event.preventDefault(); |
| 119 | + } |
| 120 | + }, |
| 121 | + |
| 122 | + _setOption: function( key, value ) { |
| 123 | + if ( key === "value" ) { |
| 124 | + this.options.value = value; |
| 125 | + |
| 126 | + this._refreshValue(); |
| 127 | + } |
| 128 | + |
| 129 | + if ( key === "readOnly") { |
| 130 | + this.options.readOnly = value; |
| 131 | + |
| 132 | + this._setReadOnly( value ); |
| 133 | + } |
| 134 | + |
| 135 | + this._super( "_setOption", key, value ); |
| 136 | + }, |
| 137 | + |
| 138 | + _getValue: function() { |
| 139 | + var val = this.options.value; |
| 140 | + |
| 141 | + // normalize invalid value |
| 142 | + if ( typeof val !== "number" ) { |
| 143 | + val = 0; |
| 144 | + } |
| 145 | + |
| 146 | + return Math.min( this.options.max, Math.max( this.min, val ) ); |
| 147 | + }, |
| 148 | + |
| 149 | + _refreshValue: function() { |
| 150 | + var value = this._getValue(); |
| 151 | + |
| 152 | + if ( this.oldValue !== value ) { |
| 153 | + this.oldValue = value; |
| 154 | + |
| 155 | + $( ".ui-rating-star", this.element ).removeClass( "ui-rating-full ui-rating-partial" ).each( function( index ) { |
| 156 | + index++; |
| 157 | + |
| 158 | + var starClass = ""; |
| 159 | + |
| 160 | + if ( index <= value ) { |
| 161 | + starClass = "ui-rating-full"; |
| 162 | + } else if ( index - value < 1 ) { |
| 163 | + starClass = "ui-rating-partial"; |
| 164 | + } |
| 165 | + |
| 166 | + $( this ).addClass( starClass ); |
| 167 | + }); |
| 168 | + |
| 169 | + this.originalElement.val( value ); |
| 170 | + |
| 171 | + this._trigger( "change", null, this.ui() ); |
| 172 | + } |
| 173 | + }, |
| 174 | + |
| 175 | + _getReadOnly: function() { |
| 176 | + var val = this.options.readOnly; |
| 177 | + |
| 178 | + // normalize invalid boolean |
| 179 | + if ( typeof val !== "boolean" ) |
| 180 | + { |
| 181 | + val = false; |
| 182 | + } |
| 183 | + |
| 184 | + return val; |
| 185 | + }, |
| 186 | + |
| 187 | + _setReadOnly: function() { |
| 188 | + var readOnly = this._getReadOnly(); |
| 189 | + |
| 190 | + if ( this.oldReadOnly !== readOnly ) { |
| 191 | + this.oldReadOnly = readOnly; |
| 192 | + |
| 193 | + if (readOnly === true) { |
| 194 | + // make read only |
| 195 | + this.element.addClass( "ui-rating-readonly ui-rating-current" ).find( ".ui-rating-hover" ).removeClass( "ui-rating-hover" ); |
| 196 | + |
| 197 | + this.bindings.unbind( "." + this.widgetName ); |
| 198 | + |
| 199 | + this._bind( this.element, this._eventsReadOnly); |
| 200 | + } else { |
| 201 | + // make non-read only |
| 202 | + this.element.removeClass('ui-rating-readonly'); |
| 203 | + |
| 204 | + this.bindings.unbind( "." + this.widgetName ); |
| 205 | + |
| 206 | + this._bind( this.element, this._events ); |
| 207 | + } |
| 208 | + |
| 209 | + this._trigger( "change", null, this.ui() ); |
| 210 | + } |
| 211 | + }, |
| 212 | + |
| 213 | + ui: function() { |
| 214 | + return { |
| 215 | + element: this.element, |
| 216 | + originalElement: this.originalElement, |
| 217 | + value: this.options.value, |
| 218 | + oldValue: this.oldValue, |
| 219 | + min: this.min, |
| 220 | + max: this.options.max, |
| 221 | + oldReadOnly: this.options.readOnly, |
| 222 | + readOnly: this.options.readOnly |
| 223 | + }; |
| 224 | + } |
| 225 | +}); |
| 226 | + |
| 227 | +}( jQuery )); |
0 commit comments