1/* 2 RateIt 3 version 0.99.2 4 03/07/2011 5 http://rateit.codeplex.com 6 Twitter: @gjunge 7 8*/ 9(function ($) { 10 $.fn.rateit = function (p1, p2) { 11 //quick way out. 12 var options = {}; var mode = 'init'; 13 if (this.length == 0) return this; 14 15 16 var tp1 = $.type(p1); 17 if (tp1 == 'object' || p1 === undefined || p1 == null) { 18 options = $.extend({}, $.fn.rateit.defaults, p1); //wants to init new rateit plugin(s). 19 } 20 else if (tp1 == 'string' && p2 === undefined) { 21 return this.data('rateit-' + p1); //wants to get a value. 22 } 23 else if (tp1 == 'string') { 24 mode = 'setvalue' 25 } 26 27 return this.each(function () { 28 var item = $(this); 29 30 //shorten all the item.data('rateit-XXX'), will save space in closure compiler, will be like item.data('XXX') will become x('XXX') 31 var itemdata = function (k, v) { 32 k = 'rateit-' + k; 33 34 if (arguments.length === 1) 35 return item.data(k); 36 37 item.data(k, v); 38 return v; 39 }; 40 41 //add the rate it class. 42 if (!item.hasClass('rateit')) item.addClass('rateit'); 43 44 var ltr = item.css('direction') != 'rtl'; 45 46 // set value mode 47 if (mode == 'setvalue') { 48 if (!itemdata('init')) throw 'Can\'t set value before init'; 49 50 51 //if readonly now and it wasn't readonly, remove the eventhandlers. 52 if (p1 == 'readonly' && !itemdata('readonly')) { 53 item.find('.rateit-range').unbind(); 54 itemdata('wired', false); 55 } 56 57 if (itemdata('backingfld')) { 58 //if we have a backing field, check which fields we should update. 59 //In case of input[type=range], although we did read its attributes even in browsers that don't support it (using fld.attr()) 60 //we only update it in browser that support it (&& fld[0].min only works in supporting browsers), not only does it save us from checking if it is range input type, it also is unnecessary. 61 var fld = $(itemdata('backingfld')); 62 if (p1 == 'value') fld.val(p2); 63 if (p1 == 'min' && fld[0].min) fld[0].min = p2; 64 if (p1 == 'max' && fld[0].max) fld[0].max = p2; 65 if (p1 == 'step' && fld[0].step) fld[0].step = p2; 66 } 67 68 itemdata(p1, p2); 69 } 70 71 //init rateit plugin 72 if (!itemdata('init')) { 73 74 //get our values, either from the data-* html5 attribute or from the options. 75 itemdata('min', itemdata('min') || options.min); 76 itemdata('max', itemdata('max') || options.max); 77 itemdata('step', itemdata('step') || options.step); 78 itemdata('readonly', itemdata('readonly') !== undefined ? itemdata('readonly') : options.readonly); 79 itemdata('resetable', itemdata('resetable') !== undefined ? itemdata('resetable') : options.resetable); 80 itemdata('backingfld', itemdata('backingfld') || options.backingfld); 81 itemdata('starwidth', itemdata('starwidth') || options.starwidth); 82 itemdata('starheight', itemdata('starheight') || options.starheight); 83 itemdata('value', itemdata('value') || options.min);
84 //are we LTR or RTL? 85 86 if (itemdata('backingfld')) { 87 //if we have a backing field, hide it, and get its value, and override defaults if range. 88 var fld = $(itemdata('backingfld')); 89 itemdata('value', fld.hide().val()); 90 91 if (fld[0].nodeName == 'INPUT') { 92 if (fld[0].type == 'range' || fld[0].type == 'text') { //in browsers not support the range type, it defaults to text 93 94 itemdata('min', parseInt(fld.attr('min')) || itemdata('min')); //if we would have done fld[0].min it wouldn't have worked in browsers not supporting the range type. 95 itemdata('max', parseInt(fld.attr('max')) || itemdata('max')); 96 itemdata('step', parseInt(fld.attr('step')) || itemdata('step')); 97 } 98 } 99 if (fld[0].nodeName == 'SELECT' && fld[0].options.length > 1) { 100 itemdata('min', Number(fld[0].options[0].value)); 101 itemdata('max', Number(fld[0].options[fld[0].length - 1].value)); 102 itemdata('step', Number(fld[0].options[1].value) - Number(fld[0].options[0].value)); 103 } 104 } 105 106 //Create the necessart tags. 107 item.append('<div class="rateit-reset"></div><div class="rateit-range"><div class="rateit-selected" style="height:' + itemdata('starheight') + 'px"></div><div class="rateit-hover" style="height:' + itemdata('starheight') + 'px"></div></div>'); 108 109 //if we are in RTL mode, we have to change the float of the "reset button" 110 if (!ltr) { 111 item.find('.rateit-reset').css('float', 'right'); 112 item.find('.rateit-selected').addClass('rateit-selected-rtl'); 113 item.find('.rateit-hover').addClass('rateit-hover-rtl'); 114 } 115 itemdata('init', true); 116 } 117 118 119 //set the range element to fit all the stars. 120 var range = item.find('.rateit-range'); 121 range.width(itemdata('starwidth') * (itemdata('max') - itemdata('min'))).height(itemdata('starheight')); 122 123 124 //set the value if we have it. 125 if (itemdata('value')) { 126 var score = (itemdata('value') - itemdata('min')) * itemdata('starwidth'); 127 item.find('.rateit-selected').width(score); 128 } 129 130 var resetbtn = item.find('.rateit-reset'); 131 132 var calcRawScore = function (element, event) { 133 var pageX = (event.changedTouches) ? event.changedTouches[0].pageX : event.pageX; 134 135 var offsetx = pageX - $(element).offset().left; 136 if (!ltr) offsetx = range.width() - offsetx; 137 if (offsetx > range.width()) offsetx = range.width(); 138 if (offsetx < 0) offsetx = 0; 139 140 return score = Math.ceil(offsetx / itemdata('starwidth') * (1 / itemdata('step'))); 141 }; 142 143 144 // 145 146 if (!itemdata('readonly')) { 147 //if we are not read only, add all the events 148 149 //if we have a reset button, set the event handler. 150 if (itemdata('resetable')) { 151 resetbtn.click(function () { 152 itemdata('value', itemdata('min')); 153 range.find('.rateit-hover').hide().width(0); 154 range.find('.rateit-selected').width(0).show(); 155 if (itemdata('backingfld')) $(itemdata('backingfld')).val(itemdata('min')); 156 item.trigger('reset'); 157 }); 158 159 } 160 else { 161 resetbtn.hide(); 162 } 163 164 165 166 //when the mouse goes over the range div, we set the "hover" stars. 167 if (!itemdata('wired')) { 168 range.bind('touchmove touchend', touchHandler); //bind touch events 169 range.mousemove(function (e) { 170 var score = calcRawScore(this, e); 171 var w = score * itemdata('starwidth') * itemdata('step'); 172 var h = range.find('.rateit-hover'); 173 if (h.data('width') != w) { 174 range.find('.rateit-selected').hide(); 175 h.width(w).show().data('width', w); 176 item.trigger('hover', [(score * itemdata('step')) + itemdata('min')]); 177 } 178 }); 179 //when the mouse leaves the range, we have to hide the hover stars, and show the current value. 180 range.mouseleave(function (e) { 181 range.find('.rateit-hover').hide().width(0).data('width', ''); 182 item.trigger('hover', [null]); 183 range.find('.rateit-selected').show(); 184 }); 185 //when we click on the range, we have to set the value, hide the hover. 186 range.mouseup(function (e) { 187 var score = calcRawScore(this, e); 188 189 var newvalue = (score * itemdata('step')) + itemdata('min'); 190 itemdata('value', newvalue); 191 if (itemdata('backingfld')) { 192 $(itemdata('backingfld')).val(newvalue); 193 } 194 range.find('.rateit-hover').hide(); 195 range.find('.rateit-selected').width(score * itemdata('starwidth') * itemdata('step')).show(); 196 item.trigger('hover', [null]).trigger('rated', [newvalue]); 197 });
198 199 itemdata('wired', true); 200 } 201 if (itemdata('resetable')) { 202 resetbtn.show(); 203 } 204 } 205 else { 206 resetbtn.hide(); 207 } 208 }); 209 }; 210 211 //touch converter http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript/ 212 function touchHandler(event) { 213 214 var touches = event.originalEvent.changedTouches, 215 first = touches[0], 216 type = ""; 217 switch (event.type) { 218 case "touchmove": type = "mousemove"; break; 219 case "touchend": type = "mouseup"; break; 220 default: return; 221 } 222 223 var simulatedEvent = document.createEvent("MouseEvent"); 224 simulatedEvent.initMouseEvent(type, true, true, window, 1, 225 first.screenX, first.screenY, 226 first.clientX, first.clientY, false, 227 false, false, false, 0/*left*/, null); 228 229 first.target.dispatchEvent(simulatedEvent); 230 event.preventDefault(); 231 }; 232 233 234 //some default values. 235 $.fn.rateit.defaults = { min: 0, max: 5, step: 0.5, starwidth: 16, starheight: 16, readonly: false, resetable: true }; 236 eds3_5_jq(document).ready(function ($) { 237 //invoke it on all div.rateit elements. This could be removed if not wanted. 238 $('div.rateit').rateit(); 239 }); 240})(eds3_5_jq);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.