Path

ez projects / ezstarrating / forum / general / jquery code for voting


jQuery code for voting

You need to be logged in to post messages in the forums. New users may register here.

Carlos Revillo

Member since:
31 January 2007

Posts: 53

Sunday 30 August 2009 12:23:50 pm

Hi.

I really like jQuery, so i have written some code to do voting with this library. it's just a 'translation' of the yui3 functions.

please, let me know if you think this code could be useful for you.

regarding to this, it would be nice to choose wich library to use via ini file or something...

best regards.

Twitter: @crevillo
Skype ID: crevillo1976
http://www.tantacom.com
eZ Diff Squad member

Up

André R

Member since:
20 August 2005

Posts: 171

Tuesday 01 September 2009 3:00:36 pm

Feel free to post it and we'll look into ways of incorporation it.
Would be cool with a jquery / yui setting, but I think it should be in ezjscore then*.

Note: As of today, the loading of scripts are a bit different as well as the access checks**, and the schema changes I mentioned in the other post also happened today***.
So look out for that when you svn update.

* http://projects.ez.no/ezjscore/subversion/14
** http://projects.ez.no/ezjscore/subversion/12
*** http://projects.ez.no/ezstarrating/subversion/43

--
ar

Up

Carlos Revillo

Member since:
31 January 2007

Posts: 53

Tuesday 01 September 2009 5:02:33 pm

Hi. After getting rev 47, this is the piece of code that is working for me when choosing jquery as my PreferredLibrary




{run-once}


{ezscript_require(array( 'ezjsc::jquery', 'ezjsc::jqueryio') )}


<script type="text/javascript">


$('head').append('<link rel="stylesheet" href={"stylesheets/star_rating.css"|ezdesign()} type="text/css" />');


 

{if has_access_to_limitation( 'ezjscore', 'call', hash( 'FunctionList', 'ezstarrating_rate' ) )}


{literal}


        $().ready(function() {


       $('ul.ezsr-star-rating').addClass('ezsr-star-rating-enabled');


       $('li a', 'ul.ezsr-star-rating').click( _rate );        

   });


    

   function _rate( e )


   {


       var args = $(this).attr('id').split('_');


       $('#ezsr_rating_' + args[1]).removeClass('ezsr-star-rating-enabled');


       $('li a', '#ezsr_rating_' + args[1]).unbind( 'click' );        

       jQuery.ez( 'ezstarrating::rate::' + args[1] + '::' + args[2] + '::' + args[3], function(data){ _callBack(data) } );


       return false;


   }


    

   function _callBack( data )


   {


       if ( data && data.content !== '' )


       {


           if ( data.content.rated )


           {


               $('#ezsr_just_rated_' + data.content.id).removeClass('hide');


               $('#ezsr_rating_percent_' + data.content.id).css('width', (( data.content.stats.rounded_average / 5 ) * 100 ) + '%' );


               $('#ezsr_average_' + data.content.id).text( data.content.stats.rounded_average );


               $('#ezsr_total_' + data.content.id).text( data.content.stats.rating_count );


           }


           else if ( data.content.already_rated )


               $('#ezsr_has_rated_' + data.content.id).removeClass('hide');


           //else alert('Invalid input variables, could not rate!');


       }


       else


       {


           // This shouldn't happen as we have already checked access in the template..


           // Unless this is inside a aggressive cache-block of course.


           alert( data.content.error_text );


       }


   }


{/literal}


{/if}


 

</script>


{/run-once}




About the preferred setting, i was thinking in something like the following.
1.- Call to the ezscript_require operator getting your preferred Library.

Something like



{def $preferred_library = ezini('eZJSCore', 'PreferredLibrary', 'ezjscore.ini')}


{ezscript_require(array( concat( 'ezjsc::', $preferred_library ), concat( 'ezjsc::', $preferred_library, 'io' ) ) )}




Then, add a javascript folder under design folder, and one more folder for every library. so we can have some like



design


  |--------javascript


  |------------jquery


  |------------yui3




Under each 'library' folder we can add a star_rating.tpl or whatever. then we'll only have to call the apropiatte tpl usign preferred_library again, something like



{include uri=concat( "design:javascript/", $preferred_library, "/star_rating.tpl")}





P.S. I have more suggestions to add. Would you prefer them in a new post?

Twitter: @crevillo
Skype ID: crevillo1976
http://www.tantacom.com
eZ Diff Squad member

Up

André R

Member since:
20 August 2005

Posts: 171

Thursday 03 September 2009 2:07:03 pm

I think your example would have to be something like:



{def $preferred_library = ezini('eZJSCore', 'PreferredLibrary', 'ezjscore.ini')}


{ezscript_require(array( concat( 'ezjsc::', $preferred_library ), concat( 'ezjsc::', $preferred_library, 'io' ), concat( 'ezstarrating_template::', $preferred_library ), ) )}




But yeah, could work.

--
ar

Up

Carlos Revillo

Member since:
31 January 2007

Posts: 53

Thursday 03 September 2009 3:56:45 pm

great. thank you.

Twitter: @crevillo
Skype ID: crevillo1976
http://www.tantacom.com
eZ Diff Squad member

Up

André R

Member since:
20 August 2005

Posts: 171

Tuesday 08 September 2009 8:05:39 pm

Hi, this is now implemented in svn, thanks for sharing the jQuery code!
I only did some small tweaks:
* not using $ in global scope in case user uses moo tools or prototype
* changes to be able to change vote
* support for "ezsr-star-rating-disabled" class on <ul id="ezsr_rating_{$attribute.id}" class="ezsr-star-rating"> so user can signal disabled rating for instance based on recently added 'current_user_has_rated' attribute on datatype content (Like we discussed earlier, not used by default since it's not cache safe).

--
ar

Up

Carlos Revillo

Member since:
31 January 2007

Posts: 53

Wednesday 09 September 2009 1:00:16 am

Glad it helpel André. Thanks for reviewing the code.

If i can help with some jquery core for another extensions, please let me know.

I will share a spanish translation file asap.

Best Regards.

Twitter: @crevillo
Skype ID: crevillo1976
http://www.tantacom.com
eZ Diff Squad member

Up

You need to be logged in to post messages in the forums. New users may register here.