3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
17 Ext.ux.Rating = Ext.extend(Ext.util.Observable, {
18 // Configuration options
26 // Our class constructor
27 constructor : function(element, config) {
28 Ext.apply(this, config);
29 Ext.ux.Rating.superclass.constructor.call(this);
36 this.el = Ext.get(element);
43 // Some arrays we are going to store data in
48 // We create a container to put all our stars into
49 this.container = this.el.createChild({
50 cls: 'ux-rating-container ux-rating-clearfix'
54 this.resetEl = this.container.createChild({
55 cls: 'ux-rating-reset',
58 title: this.showTitles ? (this.resetTitle || 'Reset your vote') : '',
62 this.resetEl.visibilityMode = Ext.Element.DISPLAY;
64 this.resetEl.hover(function() {
65 Ext.fly(this).addClass('ux-rating-reset-hover');
67 Ext.fly(this).removeClass('ux-rating-reset-hover');
70 this.resetEl.on('click', this.reset, this);
73 // We use DomQuery to select the radio buttons
74 this.radioBoxes = this.el.select('input[type=radio]');
75 // Then we can loop over the CompositeElement using each
76 this.radioBoxes.each(this.initStar, this);
78 // We use DomHelper to create our hidden input
79 this.input = this.container.createChild({
83 value: this.values[this.defaultSelected] || this.resetValue
86 // Lets remove all the radio buttons from the DOM
87 this.radioBoxes.remove();
89 this.select(this.defaultSelected === undefined ? false : this.defaultSelected)
94 // Enable will set up our event listeners
99 initStar : function(item, all, i) {
100 var sw = Math.floor(this.starWidth / this.split);
102 // We use the name and disabled attributes of the first radio button
104 this.name = item.dom.name;
105 this.disabled = item.dom.disabled;
108 // Saving the value and title for this star
109 this.values[i] = item.dom.value;
110 this.titles[i] = item.dom.title;
112 if(item.dom.checked) {
113 this.defaultSelected = i;
116 // Now actually create the star!
117 var star = this.container.createChild({
118 cls: 'ux-rating-star'
121 var starLink = star.createChild({
123 html: this.values[i],
124 title: this.showTitles ? this.titles[i] : ''
127 // Prepare division settings
129 var odd = (i % this.split);
131 starLink.setStyle('margin-left', '-' + (odd * sw) + 'px');
134 // Save the reference to this star so we can easily access it later
135 this.stars.push(star.dom);
138 onStarClick : function(ev, t) {
140 this.select(this.stars.indexOf(t));
144 onStarOver : function(ev, t) {
146 this.fillTo(this.stars.indexOf(t), true);
150 onStarOut : function(ev, t) {
152 this.fillTo(this.selected, false);
156 reset : function(ev, t) {
160 select : function(index) {
161 if(index === false || index === -1) {
162 // remove current selection in el
163 this.value = this.resetValue;
165 this.input.dom.value = '';
168 this.resetEl.setOpacity(0.5);
173 if(this.selected !== -1) {
174 this.fireEvent('change', this, this.values[index], this.stars[index]);
178 else if(index !== this.selected) {
179 // Update some properties
180 this.selected = index;
181 this.value = this.values[index];
182 this.title = this.titles[index];
184 // Set the value of our hidden input so the rating can be submitted
185 this.input.dom.value = this.value;
188 this.resetEl.setOpacity(0.99);
191 // the fillTo() method will fill the stars up until the selected one
192 this.fillTo(index, false);
194 // Lets also not forget to fire our custom event!
195 this.fireEvent('change', this, this.values[index], this.stars[index]);
199 fillTo : function(index, hover) {
201 var addClass = hover ? 'ux-rating-star-hover' : 'ux-rating-star-on';
202 var removeClass = hover ? 'ux-rating-star-on' : 'ux-rating-star-hover';
204 // We add a css class to each star up until the selected one
205 Ext.each(this.stars.slice(0, index+1), function() {
206 Ext.fly(this).removeClass(removeClass).addClass(addClass);
209 // And then remove the same class from all the stars after this one
210 Ext.each(this.stars.slice(index+1), function() {
211 Ext.fly(this).removeClass([removeClass, addClass]);
219 fillNone : function() {
220 this.container.select('.ux-rating-star').removeClass(['ux-rating-star-hover', 'ux-rating-star-on']);
223 enable : function() {
228 this.input.dom.disabled = null;
229 this.disabled = false;
231 this.container.removeClass('ux-rating-disabled');
233 // We will be using the technique of event delegation by listening
234 // for bubbled up events on the container
236 click: this.onStarClick,
237 mouseover: this.onStarOver,
238 mouseout: this.onStarOut,
240 delegate: 'div.ux-rating-star'
244 disable : function() {
249 this.input.dom.disabled = true;
250 this.disabled = true;
252 this.container.addClass('ux-rating-disabled');
255 click: this.onStarClick,
256 mouseover: this.onStarOver,
257 mouseout: this.onStarOut,
259 delegate: 'div.ux-rating-star'
263 getValue : function() {
264 return this.values[this.selected] || this.resetValue;
267 destroy : function() {
269 this.container.remove();
270 this.radioBoxes.appendTo(this.el);
271 if(this.selected !== -1) {
272 this.radioBoxes.elements[this.selected].checked = true;