Skip to content

Commit 136c570

Browse files
committed
Merge pull request #42 from javiertoledo/pr/41
Pr/41
2 parents 52395f7 + 6591b67 commit 136c570

File tree

6 files changed

+121
-95
lines changed

6 files changed

+121
-95
lines changed

README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ If you're using bower to manage your frontend dependencies you can install this
2222

2323
bower install bootstrap-rating-input --save
2424

25-
Else you can just download `build/bootstrap-rating-input.min.js`, put it wherever you usually put JavaScripts in your project and include it on pages where you want to have forms with ratings:
25+
Otherwise you can just download `build/bootstrap-rating-input.min.js`, put it wherever you usually put JavaScripts in your project and include it on pages where you want to have forms with ratings:
2626

2727
<script src="path/to/javascripts/bootstrap-rating-input.min.js" type="text/javascript"></script>
2828

2929
Now add a input of type *number* to your forms and add the class `rating` to it:
3030

3131
<input type="number" name="your_awesome_parameter" id="some_id" class="rating" />
3232

33-
That's all! When page loads, you'll find a few stars where you'd expect to find the input. It works just like most of rating plugins, but you don't need to learn anything about options or initializations, it just works out of the box.
33+
That's all! When page loads, you'll find a few stars where you'd expect to find the input. It works just like most of rating plugins, but for the basic usage you don't need to learn anything else about options or initializations, it just works out of the box.
3434

35-
### Wait, where has my input gone?
35+
### Wait, where is my input?
3636

37-
The plugin transforms your number input into a hidden field and wraps it inside a div with the interactive stars that will catch your clicks and save the selected values into the hidden field. In this way the form can be submitted or value readed by jQuery normally.
37+
The plugin transforms your number input into a hidden field and wraps it inside a `div` (Or a `span` with the inline option) along with the star icons that will catch your clicks and save the selected values into the hidden field. By doing this, the field still exist in your form and can be submitted or its current value read by jQuery normally. Any CSS class in addition to `rating` will be copied to the wrapper for further styling options.
3838

3939
### Nice, but I want to use a different number of stars
4040

@@ -44,13 +44,13 @@ Sure! You can set min and max values adding `data-min` and `data-max`:
4444

4545
### Can I set a default value?
4646

47-
Definitely, just set an integer value in your input that makes sense:
47+
Definitely, just set an integer value in your input that's within your min-max range':
4848

4949
<input type="number" name="your_awesome_parameter" id="some_id" class="rating" data-clearable="remove" value="3"/>
5050

5151
### Can I set a special value for empty ratings?
5252

53-
You can add the attribute `data-empty-value` to indicate which value should send the form when it have an empty rating. This can be used, for example, to have an special value indicating the user didn't select anything:
53+
You can add the attribute `data-empty-value` to indicate which value should send the form when it have an empty rating. This can be used, for example, to have an special value indicating the user didn't perform a selection:
5454

5555
<input class="rating" data-max="5" data-min="1" id="some_id" name="your_awesome_parameter" type="number" data-empty-value="0"/>
5656

@@ -62,7 +62,7 @@ By default once you set a value it remains set and you can only change it by ano
6262

6363
<input class="rating" data-clearable="remove" id="some_id" name="your_awesome_parameter" type="number" />
6464

65-
The content of `data-clearable` will appear as label for the link. You can set a space or a &amp;nbsp; to make it appear as a naked close icon.
65+
The content of `data-clearable` will appear as label for the link. If no value is provided the plugin will display just the clear icon.
6666

6767
### Can I use custom icon classes?
6868

@@ -78,29 +78,31 @@ If you want to use [FontAwesome](http://fontawesome.io/), remember to include th
7878
...
7979
</header>
8080

81-
### I don't want to be forced to add the `rating` class to the inputs
81+
### Inline render
8282

83-
The `rating` class is used in combination with `input[type=number]` to let you autoload the rating plugin without coding anything, but you can apply this plugin to a input of any type by executing the method `rating` on a jQuery selection:
83+
If you need to render the rating input inline with your text, use the `data-inline` option:
8484

85-
$('input.my_class').rating();
85+
<input type="number" name="your_awesome_parameter" id="some_id" class="rating" data-inline />
8686

87-
## Requirements
87+
This will wrap the input in a `span` element instead of the default `div`.
8888

89-
You know... [Twitter Bootstrap](http://getbootstrap.com) and [jQuery](http://jquery.com)!
89+
### Readonly mode
9090

91-
## Can I generate read-only stars for displaying?
91+
Thanks to the contribution by [iyedb](https://github.com/iyedb) this plugin now features a read-only mode. Just add the attribute `data-readonly` to do the trick:
9292

93-
If you think about it you don't want to use a plugin to generate static HTML code that is as simple as this:
93+
<input type="number" name="your_awesome_parameter" id="some_id" class="rating" value="2" data-readonly />
9494

95-
<i class='glyphicon glyphicon-star'></i><i class='glyphicon glyphicon-star'></i><i class='glyphicon glyphicon-star'></i><i class='glyphicon glyphicon-star-empty'></i><i class='glyphicon glyphicon-star-empty'></i>
95+
### I don't want to be forced to add the `rating` class to the inputs
9696

97-
You can easily generate such code with your favourite template engine and a loop. With Ruby and HAML it could look like this:
97+
The `rating` class is used in combination with `input[type=number]` to let you autoload the rating plugin without coding anything, but you can apply this plugin to a input of any type by executing the method `rating` on a jQuery selection and pass the options in an object:
9898

99-
/ Given a variable val with the value you want to represent and a variable max that contains the maximum number of stars:
100-
- max.times do |i|
101-
%i{class: "glyphicon glyphicon-star#{'-empty' if i>val}"}
99+
$('input.my_class').rating({
100+
clearable: true
101+
});
102102

103-
Well, HAML is awesome, but you are a programmer, so you'll be able to addapt this to your favorite language...
103+
## Requirements
104+
105+
You know... [Twitter Bootstrap](http://getbootstrap.com) and [jQuery](http://jquery.com)!
104106

105107
## It looks nice, but I want to complain because it doesn't fit my favorite use case
106108

@@ -112,4 +114,4 @@ Nice! You're awesome, fork the project, and do whatever changes you want into `s
112114

113115
$ npm install && grunt
114116

115-
Thanks!!
117+
Thanks!!

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bootstrap-rating-input",
33
"main": "build/bootstrap-rating-input.min.js",
4-
"version": "0.2.5",
4+
"version": "0.4.0",
55
"homepage": "https://github.com/javiertoledo/bootstrap-rating-input",
66
"authors": [
77
"Javier Toledo <[email protected]>"

build/bootstrap-rating-input.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo.html

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
font-size: 20px;
1313
padding: 50px;
1414
}
15+
16+
.myclass {
17+
color: red;
18+
font-size: 12px;
19+
}
20+
1521
</style>
1622
<script>
1723
$(function(){
@@ -24,16 +30,25 @@
2430
<body>
2531
<h1>The Original Bootstrap Rating Input <small>in action...</small></h1>
2632

27-
<p>My rating: <input type="number" name="your_awesome_parameter" id="some_id" class="rating" data-clearable="remove"/></p>
33+
<p>My rating: <input type="number" name="your_awesome_parameter" id="rating1" class="rating" data-clearable="remove"/></p>
2834

29-
<h1>Using a default value:</h1>
30-
<p>My rating: <input type="number" name="your_awesome_parameter" id="some_id" class="rating" data-clearable="remove" value="3"/></p>
35+
<h2>Using a default value:</h2>
36+
<p>My rating: <input type="number" name="your_awesome_parameter" id="rating-default" class="rating" data-clearable="remove" value="3"/></p>
3137

32-
<h1>With a minimum value and a special empty-value:</h1>
33-
<p>My rating: <input type="number" name="your_awesome_parameter" id="some_id" class="rating" data-clearable="remove" data-min="2" data-empty-value="1"/></p>
38+
<h2>With an empty clear button</h2>
39+
<p>My rating: <input type="number" name="your_awesome_parameter" id="rating-empty-clearable" class="rating" data-clearable/></p>
3440

35-
<h1>The Bootstrap Rating input with custom icon classes <small>by <a href="https://github.com/johncadigan">johncadigan</a></small></h1>
41+
<h2>With a minimum value and a special empty-value:</h2>
42+
<p>My rating: <input type="number" name="your_awesome_parameter" id="rating-minimum" class="rating" data-clearable="remove" data-min="2" data-empty-value="1"/></p>
3643

37-
<p>My rating: <input type="number" name="your_awesome_parameter" id="some_id" class="rating" data-clearable="remove" data-icon-lib="fa" data-active-icon="fa-heart" data-inactive-icon="fa-heart-o" data-clearable-icon="fa-trash-o"/></p>
44+
45+
<h1>Extra options <small>introduced by <a href="https://github.com/iyedb">iyedb</a></h1>
46+
<h2>Inline:</h2>
47+
<p>Inline rating: <input type="number" name="your_awesome_parameter" id="rating-inline" class="rating" data-clearable="remove" data-inline/></p>
48+
<h2>Read only:</h2>
49+
<p>Readonly: <input type="number" name="your_awesome_parameter" id="rating-readonly" class="rating" data-clearable="remove" value="2" data-readonly/></p>
50+
51+
<h1>The Bootstrap Rating input with custom icon classes <small>by <a href="https://github.com/johncadigan">johncadigan</a></small></h1>
52+
<p>My rating: <input type="number" name="your_awesome_parameter" id="rating-custom-icons" class="rating" data-clearable="remove" data-icon-lib="fa" data-active-icon="fa-heart" data-inactive-icon="fa-heart-o" data-clearable-icon="fa-trash-o" value="4"/></p>
3853
</body>
39-
</html>
54+
</html>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "bootstrap-rating-input",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
4+
"license": "MIT",
45
"repository": "https://github.com/javiertoledo/bootstrap-rating-input.git",
56
"main": "src/bootstrap-rating-input.js",
67
"devDependencies": {

src/bootstrap-rating-input.js

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,84 @@
1-
(function($) {
2-
3-
var clearClass = 'rating-clear';
4-
var clearSelector = '.' + clearClass;
5-
var hiddenClass = 'hidden';
1+
(function ($) {
2+
'use strict';
3+
4+
var clearClass = 'rating-clear',
5+
clearSelector = '.' + clearClass,
6+
hiddenClass = 'hidden',
7+
DEFAULTS = {
8+
'min': 1,
9+
'max': 5,
10+
'empty-value': 0,
11+
'iconLib': 'glyphicon',
12+
'activeIcon': 'glyphicon-star',
13+
'inactiveIcon': 'glyphicon-star-empty',
14+
'clearable': false,
15+
'clearableIcon': 'glyphicon-remove',
16+
'inline': false,
17+
'readonly': false
18+
};
619

7-
var starSelector = function(value) {
20+
function starSelector(value) {
821
return '[data-value' + (value ? ('=' + value) : '') + ']';
9-
};
22+
}
1023

11-
var toggleActive = function($el, active, options) {
12-
var activeClass = options['active-icon'];
13-
var inactiveClass = options['inactive-icon'];
24+
function toggleActive($el, active, options) {
25+
var activeClass = options.activeIcon,
26+
inactiveClass = options.inactiveIcon;
1427
$el.removeClass(active ? inactiveClass : activeClass).addClass(active ? activeClass : inactiveClass);
15-
};
28+
}
29+
30+
function parseOptions($input, options) {
31+
var data = $.extend({}, DEFAULTS, $input.data(), options);
32+
data.inline = data.inline === '' || data.inline;
33+
data.readonly = data.readonly === '' || data.readonly;
34+
if (data.clearable === false) {
35+
data.clearableLabel = '';
36+
} else {
37+
data.clearableLabel = data.clearable;
38+
}
39+
data.clearable = data.clearable === '' || data.clearable;
40+
return data;
41+
}
42+
43+
function createRatingEl($input, options) {
44+
// Inline option
45+
if (options.inline) {
46+
var $ratingEl = $('<span class="rating-input"></span>');
47+
} else {
48+
var $ratingEl = $('<div class="rating-input"></div>');
49+
}
50+
51+
// Copy original classes but the rating class
52+
$ratingEl.addClass($input.attr('class'));
53+
$ratingEl.removeClass('rating');
1654

17-
var createRatingEl = function($input, options) {
18-
var min = options.min;
19-
var max = options.max;
20-
var clearable = options.clearable;
21-
var $ratingEl = $('<div class="rating-input"></div>');
22-
for (var i = min; i <= max; i++) {
23-
$ratingEl.append('<i class="' + options['icon-lib'] + '" data-value="' + i + '"></i>');
55+
// Render rating icons
56+
for (var i = options.min; i <= options.max; i++) {
57+
$ratingEl.append('<i class="' + options.iconLib + '" data-value="' + i + '"></i>');
2458
}
25-
if (clearable) {
59+
60+
// Render clear link
61+
if (options.clearable && !options.readonly) {
2662
$ratingEl.append('&nbsp;').append(
2763
'<a class="' + clearClass + '">' +
28-
'<i class="' + options['icon-lib'] + ' ' + options['clearable-icon'] + '"/>' +
29-
clearable +
64+
'<i class="' + options.iconLib + ' ' + options.clearableIcon + '"/>' +
65+
options.clearableLabel +
3066
'</a>'
3167
);
3268
}
3369
return $ratingEl;
34-
};
35-
36-
var inputOptions = function($input) {
37-
var options = {};
38-
for (option in DEFAULTS) {
39-
options[option] = $input.data(option);
40-
};
41-
return options;
42-
};
43-
44-
var DEFAULTS = {
45-
'min': 1,
46-
'max': 5,
47-
'empty-value': 0,
48-
'icon-lib': 'glyphicon',
49-
'active-icon': 'glyphicon-star',
50-
'inactive-icon': 'glyphicon-star-empty',
51-
'clearable': '',
52-
'clearable-icon': 'glyphicon-remove'
53-
};
70+
}
5471

5572
var Rating = function(input, options) {
56-
var $input = this.$input = $(input);
57-
var ratingOptions = this.options = $.extend({}, DEFAULTS, inputOptions($input), options);
58-
var $ratingEl = this.$el = createRatingEl($input, ratingOptions);
73+
var $input = this.$input = input;
74+
this.options = parseOptions($input, options);
75+
var $ratingEl = this.$el = createRatingEl($input, this.options);
5976
$input.addClass(hiddenClass).before($ratingEl);
77+
$input.attr('type', 'hidden');
6078
this.highlight($input.val());
6179
};
6280

63-
Rating.VERSION = '0.3.0';
81+
Rating.VERSION = '0.4.0';
6482

6583
Rating.DEFAULTS = DEFAULTS;
6684

@@ -103,20 +121,17 @@
103121

104122
};
105123

106-
var Plugin = $.fn.rating = function(option) {
107-
return this.each(function() {
124+
var Plugin = $.fn.rating = function(options) {
125+
return this.filter('input[type=number]').each(function() {
108126
var $input = $(this);
109-
var dataKey = 'rating';
110-
var rating = $input.data(dataKey);
111-
var options = typeof option === 'object' && option;
112-
113-
if (!rating) {
114-
rating = new Rating($input, options);
127+
var optionsObject = typeof options === 'object' && options || {};
128+
var rating = new Rating($input, optionsObject);
129+
if (!rating.options.readonly) {
115130
rating.$el
116-
.on('mouseenter', starSelector(), function () {
131+
.on('mouseenter', starSelector(), function() {
117132
rating.highlight($(this).data('value'), true);
118133
})
119-
.on('mouseleave', starSelector(), function () {
134+
.on('mouseleave', starSelector(), function() {
120135
rating.highlight($input.val(), true);
121136
})
122137
.on('click', starSelector(), function() {
@@ -125,20 +140,13 @@
125140
.on('click', clearSelector, function() {
126141
rating.clear();
127142
});
128-
$input.data(dataKey, rating);
129-
}
130-
131-
if (option === 'clear') {
132-
rating.clear();
133-
} else if (option === 'setValue') {
134-
rating.setValue(arguments[1]);
135143
}
136144
});
137145
};
138146

139147
Plugin.Constructor = Rating;
140148

141-
$(function () {
149+
$(function() {
142150
$('input.rating[type=number]').each(function() {
143151
$(this).rating();
144152
});

0 commit comments

Comments
 (0)