From a724fd2940b515369e537eca9268d91d530792b6 Mon Sep 17 00:00:00 2001 From: washingtonbotelho Date: Thu, 8 Jul 2010 16:34:35 -0300 Subject: [PATCH] Added support for cancel button. --- LICENSE.txt | 21 +++++ README | 46 ---------- README.txt | 53 +++++++++++ changelog.txt | 7 ++ img/cancel-off.png | Bin 0 -> 699 bytes img/cancel-on.png | Bin 0 -> 715 bytes index.html | 213 ++++++++++++++++++++++++++++++--------------- js/jquery.raty.js | 136 ++++++++++++++++++----------- 8 files changed, 311 insertions(+), 165 deletions(-) create mode 100644 LICENSE.txt delete mode 100644 README create mode 100644 README.txt create mode 100644 img/cancel-off.png create mode 100644 img/cancel-on.png diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 00000000..babac6d0 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2010 Washington Botelho dos Santos + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README b/README deleted file mode 100644 index 05526647..00000000 --- a/README +++ /dev/null @@ -1,46 +0,0 @@ -/** - * jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty - * -------------------------------------------------------------------------- - * - * Licensed under The MIT License - * - * @version 0.4 - * @since 11.06.2010 - * @author Washington Botelho dos Santos - * @link http://wbotelhos.com/raty - * @twitter http://twitter.com/wbotelhos - * @license http://www.opensource.org/licenses/mit-license.php MIT - * @package jQuery Plugins - * - * Thanks to: - * -------------------------------------------------------------------------- - * @glbenz - * @franciscosouza - * - * Usage (default values): - * -------------------------------------------------------------------------- - * $('div#star').raty({ - * hintList: ['bad', 'poor', 'regular', 'good', 'gorgeous'], // A hint information for default 5 stars. - * number: 5, // Number of star. - * path: 'img, // Path of images. - * readOnly: false, // read-only or not. - * scoreName: 'score', // The name of target score. - * showHalf: false, // Active the half star. - * starHalf: 'star-half.png', // The image of the half star. - * start: 0, // Start with a score value. - * starOff: 'star-off.png', // The image of the off star. - * starOn: 'star-on.png' // The image of the on star. - * //onClick: function() { alert('clicked!'); } // A default function can to be setted here. - * }); - * - *
- * - * Public functions: - * -------------------------------------------------------------------------- - * $.fn.raty.start(3); // Starting the rating with 3 stars later. - * $.fn.raty.readOnly(true); // Adjusts the rating for read-only later. - * $.fn.raty.click(2); // Click in a star later. - * - * Should come after the current raty and before the anothers one. Because it takes the last called raty. - * - */ \ No newline at end of file diff --git a/README.txt b/README.txt new file mode 100644 index 00000000..d3f97938 --- /dev/null +++ b/README.txt @@ -0,0 +1,53 @@ +/** + * jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty + * -------------------------------------------------------------------------- + * + * jQuery Raty is a plugin that generates a customizable star rating automatically. + * + * Licensed under The MIT License + * + * @version 0.5 + * @since 11.06.2010 + * @author Washington Botelho dos Santos + * @link http://wbotelhos.com/raty + * @twitter http://twitter.com/wbotelhos + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @package jQuery Plugins + * + * Thanks to: + * -------------------------------------------------------------------------- + * @glbenz + * @franciscosouza + * + * Usage (default values): + * -------------------------------------------------------------------------- + * $('div#star').raty({ + * cancelHint: 'cancel this rate!', // The hint information. + * cancelOff: 'cancel-off.png' // Name of the cancel image off. + * cancelOn: 'cancel-on.png' // Name of the cancel image on. + * cancelPlace: 'left', // Position of the cancel button. + * hintList: ['bad', 'poor', 'regular', 'good', 'gorgeous'], // A hint information for default 5 stars. + * number: 5, // Number of star. + * path: 'img, // Path of images. + * readOnly: false, // read-only or not. + * scoreName: 'score', // The name of target score. + * showCancel: false, // If will be showed a button to cancel the rate. + * showHalf: false, // Active the half star. + * starHalf: 'star-half.png', // The image of the half star. + * start: 0, // Start with a score value. + * starOff: 'star-off.png', // Name of the star image on. + * starOn: 'star-on.png' // Name of the star image on. + * //onClick: function() { alert('clicked!'); } // A default function can to be setted here. + * }); + * + *
+ * + * Public functions: + * -------------------------------------------------------------------------- + * $.fn.raty.start(3); // Starting the rating with 3 stars later. + * $.fn.raty.readOnly(true); // Adjusts the rating for read-only later. + * $.fn.raty.click(2); // Click in a star later. + * + * Should come after the current raty and before the anothers one. Because it takes the last called raty. + * + */ \ No newline at end of file diff --git a/changelog.txt b/changelog.txt index 19ecaaed..447d7a94 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,6 +2,13 @@ * jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty * -------------------------------------------------------------------------- * + * 07.08.2010 + * - Now you can put a cancel button to cancel your rating; + * - Choose the cancel image off; + * - Choose the cancel image on; + * - Choose de left or right side position for the cancel button; + * - Change de hint of the cancel button. + * * 07.02.2010 * - Added support to display half star. * diff --git a/img/cancel-off.png b/img/cancel-off.png new file mode 100644 index 0000000000000000000000000000000000000000..a3031f055375716b848c29191cabf6253b936379 GIT binary patch literal 699 zcmV;s0!00ZP)LlQBzMVHAd+8!Cx7n4ko2BpKR*l8~R!U*RTd z-KC3@i{RwscE?Or>7D!)-4qlQMGUEfL=(Km2!<>7e((2nscB0J9em*|hwptkhx2@% zbB?#@9f0R~!$uf}AB-`J&bbvyGuB!pb*#0!lD@62t+ig?I_L15bHjiz3>S+9fx3`mlMX0u7HR>SkWlB6Yo%&%RRecz|k z>G1UQM7!Ol*X!~4`1n@=JUl#5tyYn=JiLOW*=#mTx7)=S!|LklKTpX-R`T3bfqw!C(=Xn$g1v;G$#+deS0%Ocx zv)QCjD3GQpV`F0@W@{|~#bS};<71q2dw;I5Z>_z(xVT_`ejb2KCc`T%)><-|4E1`Q z!^1=3IKGv%5BvscXJ=4|#1P83Dl-```crC2O7H8sW2(GjgyYar=!5CjJ! zC~t3XFGvaizgDY}&*uS%qKK1|6XH0&lk_DBf>D%TwY9bNLDHfzX2m%-BMD01y4qo3(5`U!bUg@nP|I3oZ6002ovPDHLkV1f)gL$Ck< literal 0 HcmV?d00001 diff --git a/img/cancel-on.png b/img/cancel-on.png new file mode 100644 index 0000000000000000000000000000000000000000..08f249365afd29594b51210c6e21ba253897505d GIT binary patch literal 715 zcmV;+0yO=JP)C4}Mrzlg<+1Y8PEBfUp0jJpx4B>@E+cy3`^(Gw`Mf+2&yxZm<$to~Vpgvg&QKNR z_f#1(r6svZt%iF?s+n<8X?B&!h3g9Dbb8_=MX}!;HiQSAh`bp^WMl~Z-44teO7W_Y zV4thSL{h;rJY7!l3%5J4H1!tIzB`Dv+YxO(haWeausGZYkI8^hWj6mzo=L0{%;yxzh{5!Htr?51 zvG|W62MzC8BZ76hRpCyO2zOn<%e)K>NHge!-~)Ap33OdWw6hsLYbCxGNt0%wk_2z7 zfyYvXheSG)5HRK1VB~%mq7Dmurw#bi@hEcOr3&G1ZiF*$M=&9nB#VNf&Q^r$4G5kp zTURh&s)E0%5&hyVD}sp<72~zmAY`Y(9aqO6CXF%=zFHGzO-A&I(pE}v70YQxCPJ{Y z4L+?5-crdLn3ZRPEs!A4ehEY3ZRpL~w9>@aMN+{F4dI@v&>(QDHQum!mG~E^$OS8l z!7?%Uwib*ROP67Hw`ika)gX-(8Ia`-u_IEhxG7U<13kSsMW+$lbb2dUMm5p6pa}cjgA+U$^mJ^AjD?&bdi)8~y+Q002ovPDHLkV1g8IMc@Dc literal 0 HcmV?d00001 diff --git a/index.html b/index.html index c232c639..287152a4 100644 --- a/index.html +++ b/index.html @@ -6,10 +6,9 @@ jQuery Raty - A Star Rating Plugin - - + - + - - + span#version { + color: #777; + font: 10px verdana; + } + -
jQuery Raty - A Star Rating Plugin
+
jQuery Raty - A Star Rating Plugin (current version: 0.5)
-
+
@@ -200,7 +175,7 @@
$('#star').raty();

- <div id="fixed"></div> + <div id="star"></div>
Started with a score and read only value:
@@ -208,8 +183,8 @@
$('#fixed').raty({
-   readOnly: true,
-   start: 2
+   readOnly:  true,
+   start:     2
});

<div id="fixed"></div> @@ -220,8 +195,8 @@
$('#custom').raty({
-   scoreName: 'entity.score',
-   number: 10
+   scoreName:  'entity.score',
+   number:     10
});

<div id="custom"></div> @@ -258,27 +233,72 @@
$('#half').raty({
-   start: 3.3,
-   showHalf: true
+   start:     3.3,
+   showHalf:  true
});

<div id="half"></div>
+
With a default cancel button:
+
+ +
+ $('#cancel').raty({
+   showCancel: true
+ });

+ + <div id="cancel"></div> +
+ +
With a custom cancel button:
+
+ +
+ $('#cancel-custom').raty({
+   cancelHint:   'remove my rating!',
+   cancelPlace:  'right',
+   showCancel:   true
+ });

+ + <div id="cancel-custom"></div> +
+
Default options:
- hintList:   ['bad', 'poor', 'regular', 'good', 'gorgeous']
- number:     5
- path:       'img/'
- readOnly:   false
- scoreName:  'score'
- showHalf:   false,
- starHalf:   'star-half.png',
- start:      0
- starOff:    'star-off.png'
- starOn:     'star-on.png'
- //onClick:    function() { alert('clicked!'); } + cancelHint:   'cancel this rating!'
+
The hint information.
+ cancelOff:    'cancel-off.png'
+
Name of the cancel image off.
+ cancelOn:     'cancel-on.png'
+
Name of the cancel image on.
+ cancelPlace:  'left'
+
Position of the cancel button.
+ hintList:     ['bad', 'poor', 'regular', 'good', 'gorgeous']
+
List of names that will be used as a hint on each star.
+ number:       5
+
Number of stars that will be presented.
+ path:         'img/'
+
Path where are the images of the stars.
+ readOnly:     false
+
If the stars will be read-only.
+ scoreName:    'score'
+
Name of the hidden field that holds the score value.
+ showCancel:   false
+
If will be showed a button to cancel the rating.
+ showHalf:     false
+
If will show half star if come a float number as score.
+ starHalf:     'star-half.png'
+
The name of the half star image.
+ start:        0
+
Number of stars to be selected.
+ starOff:      'star-off.png'
+
Name of the star image off.
+ starOn:       'star-on.png'
+
Name of the star image on.
+ //onClick:    function() { alert('clicked!'); } +
Function that returns the selected value.
Public functions: *
@@ -304,4 +324,59 @@
+ + \ No newline at end of file diff --git a/js/jquery.raty.js b/js/jquery.raty.js index 322c4063..08e66156 100755 --- a/js/jquery.raty.js +++ b/js/jquery.raty.js @@ -2,9 +2,11 @@ * jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty * -------------------------------------------------------------------------- * + * jQuery Raty is a plugin that generates a customizable star rating automatically. + * * Licensed under The MIT License * - * @version 0.4 + * @version 0.5 * @since 11.06.2010 * @author Washington Botelho dos Santos * @link http://wbotelhos.com/raty @@ -14,29 +16,34 @@ * * Usage (default values): * -------------------------------------------------------------------------- - * $('div#star').raty({ - * hintList: ['bad', 'poor', 'regular', 'good', 'gorgeous'], // A hint information for default 5 stars. - * number: 5, // Number of star. - * path: 'img, // Path of images. - * readOnly: false, // read-only or not. - * scoreName: 'score', // The name of target score. - * showHalf: false, // Active the half star. - * starHalf: 'star-half.png', // The image of the half star. - * start: 0, // Start with a score value. - * starOff: 'star-off.png', // The image of the off star. - * starOn: 'star-on.png' // The image of the on star. - * //onClick: function() { alert('clicked!'); } // A default function can to be setted here. +* $('div#star').raty({ + * cancelHint: 'cancel this rating!', // The hint information. + * cancelOff: 'cancel-off.png' // The image of the off cancel. + * cancelOn: 'cancel-on.png' // The image of the on cancel. + * cancelPlace: 'left', // Position of the cancel button. + * hintList: ['bad', 'poor', 'regular', 'good', 'gorgeous'], // A hint information for default 5 stars. + * number: 5, // Number of star. + * path: 'img, // Path of images. + * readOnly: false, // read-only or not. + * scoreName: 'score', // The name of target score. + * showCancel: false, // If will be showed a button to cancel the rating. + * showHalf: false, // Active the half star. + * starHalf: 'star-half.png', // The image of the half star. + * start: 0, // Start with a score value. + * starOff: 'star-off.png', // The image of the off star. + * starOn: 'star-on.png' // The image of the on star. + * //onClick: function() { alert('clicked!'); } // A default function can to be setted here. * }); * - *
- * + *
+ * * Public functions: * -------------------------------------------------------------------------- - * $.fn.raty.start(3); // Starting the rating with 3 stars later. - * $.fn.raty.readOnly(true); // Adjusts the rating for read-only later. - * $.fn.raty.click(2); // Click in a star later. + * $.fn.raty.start(3); // Starting the rating with 3 stars later. + * $.fn.raty.readOnly(true); // Adjusts the rating for read-only later. + * $.fn.raty.click(2); // Click in a star later. * - * Should come after the current raty and before the anothers one. Because it takes the last called raty. + * Should come after the current raty and before the anothers one. Because it takes the last called raty. * */ @@ -56,26 +63,28 @@ if (options.number > 20) { options.number = 20; } - + if (options.path.substring(options.path.length - 1, options.path.length) != '/') { options.path += '/'; } - + // TODO: Using var for values that will be used into a function later (live/bind), to keep the current value and not the last one. Why, Mr. Anderson? Why? - var containerId = $this.attr('id'); - var path = options.path; - var showHalf = options.showHalf; - var starHalf = options.starHalf; - var starOff = options.starOff; - var starOn = options.starOn; - var onClick = options.onClick; - - var start = 0; + var containerId = $this.attr('id'), + path = options.path, + cancelOff = options.cancelOff, + cancelOn = options.cancelOn, + showHalf = options.showHalf, + starHalf = options.starHalf, + starOff = options.starOff, + starOn = options.starOn, + onClick = options.onClick, + start = 0, + hint = ''; + if (!isNaN(options.start) && options.start > 0) { start = (options.start > options.number) ? options.number : options.start; } - var hint = ''; for (var i = 1; i <= options.number; i++) { hint = (options.number <= options.hintList.length && options.hintList[i - 1] !== null) ? options.hintList[i - 1] : i; // Avoids a nonexistent index (undefined) and ensures that the hint will be applied, it means different from null. Otherwise applies the current number. @@ -86,16 +95,14 @@ .append((i < options.number) ? ' ' : ''); } - $this - .css('width', options.number * 20) // star: 16px and space 4px. A little bit more for safety Unbuntu's FF. - .append(''); + $this.append(''); $('#' + containerId + '-score').val(start); if (showHalf) { - var score = $('input#' + containerId + '-score').val(); // [x ... x.25] || [x.26 ... x.75] || [x.76 ... (x + 1)] - var rounded = Math.ceil(score); // (x + 1) - var diff = (rounded - score).toFixed(1); // [0.9 ... 0.8] || [0.7 ... 0.3] || [0.2 ... 0] + var score = $('input#' + containerId + '-score').val(), // [x ... x.25] || [x.26 ... x.75] || [x.76 ... (x + 1)] + rounded = Math.ceil(score), // (x + 1) + diff = (rounded - score).toFixed(1); // [0.9 ... 0.8] || [0.7 ... 0.3] || [0.2 ... 0] if (diff >= 0.3 && diff <= 0.7) { // 3.5 In the center interval the score is with a next star. rounded = rounded - 0.5; // 3 . half @@ -108,8 +115,38 @@ } if (!options.readOnly) { + if (options.showCancel) { + var cancel = 'x'; + + if (options.cancelPlace == 'left') { + $this.prepend(cancel + ' '); + } else { + $this.append(' ').append(cancel); + } + + $this.css('width', options.number * 20 + 20); + + $('#' + containerId + ' img.button-cancel') + .live('mouseenter', function() { + $(this).attr('src', path + cancelOn); + $('img.' + containerId).attr('src', path + starOff); + }) + .live('mouseleave', function() { + $(this).attr('src', path + cancelOff); + $('img.' + containerId).trigger('mouseout'); + }) + .live('click', function() { + $('input#' + containerId + '-score').val(0); + if (onClick) { + onClick(this.alt); + } + }); + } else { + $this.css('width', options.number * 20); + } - $('img.' + containerId).live('mouseenter', function() { + $('img.' + containerId) + .live('mouseenter', function() { var qtyStar = $('img.' + containerId).length; for (var i = 1; i <= qtyStar; i++) { @@ -119,20 +156,16 @@ $('img#' + containerId + '-' + i).attr('src', path + starOff); } } - }); - - $('img.' + containerId).live('click', function() { + }) + .live('click', function() { $('input#' + containerId + '-score').val(this.alt); - if (onClick) { onClick(this.alt); } }); $this.live('mouseleave', function() { - var id = $(this).attr('id'); - var qtyStar = $('img.' + id).length; - var score = $('input#' + id + '-score').val(); + var id = $(this).attr('id'), qtyStar = $('img.' + id).length, score = $('input#' + id + '-score').val(); for (var i = 1; i <= qtyStar; i++) { if (i <= score) { @@ -143,9 +176,7 @@ } if (showHalf) { - var score = $('input#' + id + '-score').val(); - var rounded = Math.ceil(score); - var diff = (rounded - score).toFixed(1); + var score = $('input#' + id + '-score').val(), rounded = Math.ceil(score), diff = (rounded - score).toFixed(1); if (diff >= 0.3 && diff <= 0.7) { rounded = rounded - 0.5; @@ -165,11 +196,16 @@ }; $.fn.raty.defaults = { + cancelHint: 'cancel this rating!', + cancelOff: 'cancel-off.png', + cancelOn: 'cancel-on.png', + cancelPlace: 'left', hintList: ['bad', 'poor', 'regular', 'good', 'gorgeous'], number: 5, path: 'img/', readOnly: false, scoreName: 'score', + showCancel: false, showHalf: false, starHalf: 'star-half.png', start: 0, @@ -247,8 +283,8 @@ }; function initialize(start) { - var id = $this.attr('id'); - var qtyStar = $('img.' + id).length; + var id = $this.attr('id'), + qtyStar = $('img.' + id).length; $('input#' + id + '-score').val(start); for (var i = 1; i <= qtyStar; i++) {