/greek/media/js/poll.js

https://github.com/cid2105/virtual_greek · JavaScript · 121 lines · 70 code · 21 blank · 30 comment · 11 complexity · 8864f21acfea720c8d0b978ad7ec971f MD5 · raw file

  1. /*
  2. jPoll jQuery plugin version 1.0
  3. Copyright (c) 2009 Dan Wellman
  4. Dual licensed under the MIT and GPL licenses:
  5. http://www.opensource.org/licenses/mit-license.php
  6. http://www.gnu.org/licenses/gpl.html
  7. */
  8. (function($) {
  9. //define jPoll object with some default properties
  10. $.jPoll = {
  11. defaults: {
  12. ajaxOpts: {
  13. url: "poll"
  14. },
  15. groupName: "choices",
  16. groupIDs: ["choice0", "choice1", "choice2", "choice3", "choice4"],
  17. pollHeading: "Please choose your favourite:",
  18. rowClass: "row",
  19. errors: true
  20. }
  21. };
  22. //extend jquery with the plugin
  23. $.fn.extend({
  24. jPoll:function(config) {
  25. //use defaults or properties supplied by user
  26. config = $.extend({}, $.jPoll.defaults, config);
  27. //init widget
  28. $("<h2>").text(config.pollHeading).appendTo($(this));
  29. $("<form>").attr({
  30. id: "pollForm",
  31. action: config.ajaxOpts.url
  32. }).appendTo($(this));
  33. for(var x = 0; x < config.groupIDs.length; x++) {
  34. $("<div>").addClass(config.rowClass).appendTo($(this).find("form"));
  35. $("<input type='radio' name='" + config.groupName + "' id='" + config.groupIDs[x] + "'>").addClass("choice").appendTo($(this).find("form").children(":last")).click(function() {
  36. ($(".error").length != 0) ? $(".error").slideUp("slow") : null ;
  37. });
  38. $("<label>").text(config.groupIDs[x]).attr("for", config.groupIDs[x]).appendTo($(this).find("form").children(":last"));
  39. }
  40. $("<div>").attr("id", "buttonRow").addClass(config.rowClass).appendTo($(this).find("form"));
  41. $("<button type='submit'>").text("Vote!").appendTo("#buttonRow").click(function(e) {
  42. e.preventDefault();
  43. //record which radio was selected
  44. var selected;
  45. $(".choice").each(function() {
  46. ($(this).attr("checked") == true) ? selected = $(this).attr("id") : null ;
  47. });
  48. //print message if no radio selected and errors enabled
  49. if (config.errors == true) {
  50. (selected == null && $(".error").length == 0) ? $("<p>").addClass("error").text("Please make a selection!").css({display:"none"}).insertAfter("#pollForm").slideDown("slow") : null ;
  51. }
  52. //add additional request options
  53. var addOpts = {
  54. type: "post",
  55. data: "&choice=" + selected,
  56. dataType:"json",
  57. success: function(data) {
  58. //add all votes to get total
  59. var total = 0;
  60. for (var x = 0; x < data.length; x++) {
  61. total += parseInt(data[x].votes);
  62. }
  63. //change h2
  64. $("div#pollContainer").find("h2").text("Results, out of " + total + " votes:");
  65. //remove form
  66. $("form#pollForm").slideUp("slow");
  67. //create results container
  68. $("<div>").attr("id", "results").css({ display:"none" }).insertAfter("#pollForm");
  69. //create results
  70. for (var x = 0; x < data.length; x++) {
  71. //create row elment
  72. $("<div>").addClass("row").attr("id", "row" + x).appendTo("#results");
  73. //create label and result
  74. $("<label>").text(config.groupIDs[x]).appendTo("#row" + x);
  75. $("<div>").attr("title", Math.round(data[x].votes / total * 100) + "%").addClass("result").css({ display:"none" }).appendTo("#row" + x);
  76. }
  77. //show results container
  78. $("#results").slideDown("slow", function() {
  79. //animate each result
  80. $(".result").each(function(i) {
  81. $(this).animate({ width: Math.round(data[i].votes / total * 100) }, "slow");
  82. });
  83. //create and show thanks message
  84. $("<p>").attr("id", "thanks").text("Thanks for voting!").css({ display:"none" }).insertAfter("#results").fadeIn("slow");
  85. });
  86. }
  87. };
  88. //merge ajaxOpts widget properties and additional options objects
  89. ajaxOpts = $.extend({}, addOpts, config.ajaxOpts);
  90. //make request if radio selected
  91. return (selected == null) ? false : $.ajax(ajaxOpts) ;
  92. });
  93. //return the jquery object for chaining
  94. return this;
  95. }
  96. });
  97. })(jQuery);