-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements CTK.StarRating, a brand new widget for ratings. It's been
written from scratch. So far it seems to work fine, but further testing is definitely required. git-svn-id: svn://cherokee-project.com/CTK/trunk@5413 5dc97367-97f1-0310-9951-d761b3857238
- Loading branch information
Showing
9 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# CTK: Cherokee Toolkit | ||
# | ||
# Authors: | ||
# Alvaro Lopez Ortega <alvaro@alobbs.com> | ||
# | ||
# Copyright (C) 2010 Alvaro Lopez Ortega | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of version 2 of the GNU General Public | ||
# License as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
# 02110-1301, USA. | ||
# | ||
|
||
from Box import Box | ||
from Combobox import Combobox | ||
|
||
HEADERS = [ | ||
'<script type="text/javascript" src="/CTK/js/jquery-ui-1.7.2.custom.min.js"></script>', | ||
'<script type="text/javascript" src="/CTK/js/StarRating.js"></script>' | ||
] | ||
|
||
RATING_OPTIONS = [ | ||
('1', "Very poor"), | ||
('2', "Not that bad"), | ||
('3', "Average"), | ||
('4', "Good"), | ||
('5', "Excellent") | ||
] | ||
|
||
JS_INIT = """ | ||
$("#%(id)s").StarRating (%(selected)s); | ||
""" | ||
|
||
class StarRating (Box): | ||
def __init__ (self, props={}): | ||
Box.__init__ (self, {'class': 'star-rating'}) | ||
self.selected = props.get('selected') | ||
combo = Combobox (props.copy(), RATING_OPTIONS) | ||
self += combo | ||
|
||
def Render (self): | ||
render = Box.Render(self) | ||
|
||
render.headers += HEADERS | ||
render.js += JS_INIT %({'id': self.id, | ||
'selected': self.selected or "-1"}) | ||
return render |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* CTK: Cherokee Toolkit | ||
* | ||
* Authors: | ||
* Alvaro Lopez Ortega <alvaro@alobbs.com> | ||
* | ||
* Copyright (C) 2010 Alvaro Lopez Ortega | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of version 2 of the GNU General Public | ||
* License as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
(function($) { | ||
var StarRating = function (element) { | ||
var obj = this; // Object {} | ||
var self = $(element); // $(<div>) | ||
|
||
// PRIVATE | ||
// | ||
function get_star_number (star) { | ||
return $(star).find("a").text(); | ||
} | ||
|
||
function fill_to (idx, hover) { | ||
if (idx == -1) { | ||
for (var i=1; i<=5; i++) { | ||
var $star = obj.$parent.find('.star'+i); | ||
$star.addClass('ui-stars-star-disabled').removeClass('ui-stars-star-hover').removeClass('ui-stars-star-on'); | ||
} | ||
return; | ||
} | ||
|
||
var addClass = hover ? 'ui-stars-star-hover' : 'ui-stars-star-on'; | ||
var remClass = hover ? 'ui-stars-star-on' : 'ui-stars-star-hover'; | ||
|
||
for (var i=1; i<=5; i++) { | ||
var $star = obj.$parent.find('.star'+i); | ||
if (idx >= i) { | ||
$star.removeClass(remClass).addClass(addClass); | ||
} else { | ||
$star.removeClass(addClass).addClass(remClass); | ||
} | ||
} | ||
} | ||
|
||
// PUBLIC | ||
// | ||
this.init = function (self, selected) { | ||
obj.$select = self.find('select'); // <select> | ||
obj.$parent = obj.$select.parent(); // <div> | ||
obj.score = selected; | ||
|
||
/* Hide the <select> */ | ||
obj.$select.hide(); | ||
|
||
/* Add the stars */ | ||
var stars_html = ''; | ||
for (var i=1; i<=5; i++) { | ||
stars_html += '<div class="star'+i +' ui-stars-star"><a>'+ i +'</a></div>'; | ||
} | ||
obj.$parent.append (stars_html); | ||
|
||
/* Initial value */ | ||
fill_to (obj.score, true); | ||
|
||
/* Events */ | ||
obj.$parent.find ('.ui-stars-star') | ||
.bind('mouseover', function() { | ||
var idx = get_star_number (this); | ||
fill_to (idx, true); | ||
}) | ||
.bind('mouseout', function() { | ||
fill_to (obj.score, true); | ||
}) | ||
.bind('click', function() { | ||
var idx = get_star_number (this); | ||
obj.score = parseInt(idx); | ||
fill_to (idx, true); | ||
obj.$select.val(idx); | ||
obj.$select.trigger ({type: 'change', value: idx}) | ||
}); | ||
}; | ||
}; | ||
|
||
$.fn.StarRating = function (selected) { | ||
var self = this; | ||
return this.each (function() { | ||
if ($(this).data('starts')) return; | ||
|
||
var stars = new StarRating (this); | ||
$(this).data('stars', stars); | ||
stars.init (self, selected); | ||
}); | ||
|
||
}; | ||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import CTK | ||
|
||
def commit(): | ||
print CTK.post | ||
return {'ret': 'ok'} | ||
|
||
def default(): | ||
submit = CTK.Submitter('/commit') | ||
submit += CTK.RawHTML ("<h2>3rd selected</h2>") | ||
submit += CTK.StarRating ({'name': 'test_rate1', 'selected': '3'}) | ||
submit += CTK.RawHTML ("<h2>None selected</h2>") | ||
submit += CTK.StarRating ({'name': 'test_rate2'}) | ||
|
||
page = CTK.Page() | ||
page += CTK.RawHTML('<h1>Demo StarRating</h1>') | ||
page += submit | ||
return page.Render() | ||
|
||
|
||
CTK.publish ('', default) | ||
CTK.publish ('/commit', commit, method="POST") | ||
CTK.run (port=8000) |