diff --git README.txt README.txt index 281d173..4fef693 100644 --- README.txt +++ README.txt @@ -43,7 +43,7 @@ From plus1.module theme_plus1_widget function header: * default JavaScript file, simply assign different values to * the following variables: * $widget_class (The wrapper element for the voting widget.) -* $link_class (The anchor element to cast a vote.) +* $vote_class (The anchor element to cast a vote.) * $message_class (The wrapper element for the anchor element. May contain feedback when the vote has been cast.) * $score_class (The placeholder element for the score.) * The JavaScript looks for these CSS hooks to diff --git jquery.plus1.js jquery.plus1.js index 5e51f43..8f4768d 100644 --- jquery.plus1.js +++ jquery.plus1.js @@ -5,13 +5,18 @@ // Global killswitch: only run if we are in a supported browser. if (Drupal.jsEnabled) { // Documentation on Drupal JavaScript behaviors can be found here: http://drupal.org/node/114774#javascript-behaviors - Drupal.behaviors.plus1 = function(context){ - jQuery('.'+ Drupal.settings.plus1.widget_class +':not(.plus1-processed)', context).addClass('plus1-processed').each(function(){ - var plus1_widget = jQuery(this); - plus1_widget.find('.'+ Drupal.settings.plus1.link_class).attr('href', function(){ return jQuery(this).attr('href') + '&json=true'; }).click(function(){ - jQuery.getJSON(jQuery(this).attr('href'), function(json){ - plus1_widget.find('.'+ Drupal.settings.plus1.score_class).hide().fadeIn('slow').html(json.score); - plus1_widget.find('.'+ Drupal.settings.plus1.message_class).html(json.voted); + Drupal.behaviors.plus1 = function (context) { + jQuery('.' + Drupal.settings.plus1.widget_class, context).each(function () { + var plus1_widget = jQuery(this), plus1_form = plus1_widget.find('form.' + Drupal.settings.plus1.vote_class); + plus1_form.attr('action', plus1_form.attr('action') + '&json=true').submit(function () { + jQuery.ajax({ + 'type': 'POST', + 'dataType': 'json', + 'url': jQuery(this).attr('action'), + 'success': function (json) { + plus1_widget.find('.' + Drupal.settings.plus1.score_class).hide().fadeIn('slow').html(json.score); + plus1_widget.find('.' + Drupal.settings.plus1.message_class).html(json.voted); + } }); // Preventing the /plus1/vote/ target from being triggered. return false; diff --git plus1.css plus1.css index e353a4b..a4e9a06 100644 --- plus1.css +++ plus1.css @@ -1,6 +1,6 @@ /* $Id: plus1.css,v 1.1.4.1 2008/09/07 22:43:46 chill35 Exp $ */ div.plus1-widget { - padding: 0.2em 0.2em 0.6em; + padding: 0.2em 0.2em 0; border-top: 1px #fbf389 solid; border-bottom: 1px #fbf389 solid; background-color: #fbfbe6; @@ -16,13 +16,18 @@ div.plus1-widget .plus1-score { padding-top: 0.4em; font-size: 150%; } -div.plus1-widget .plus1-vote { - background: url(images/up.png) no-repeat center bottom; +div.plus1-widget .plus1-vote button { + background: url(images/up.png) no-repeat center center; text-indent: -999em; -} -div.plus1-widget .plus1-vote a { display: block; + overflow: visible; width: 100%; - height: 100%; + height: 2em; outline: none; + border: 0; + padding: 0; } +div.plus1-widget .plus1-msg { + line-height: 2em; + height: 2em; +} \ No newline at end of file diff --git plus1.module plus1.module index ca5ae35..370fcb0 100644 --- plus1.module +++ plus1.module @@ -76,13 +76,27 @@ function plus1_settings() { '#title' => t('Text settings'), ); + $form['plus1_text']['plus1_vote'] = array( + '#type' => 'textfield', + '#title' => t('Text of the button to click to vote'), + '#default_value' => variable_get('plus1_vote', 'Vote'), + '#description' => t('Enter, for example, Digg, Vote, or Like.'), + ); + $form['plus1_text']['plus1_you_voted'] = array( '#type' => 'textfield', '#title' => t('Feedback provided to voter when he already voted'), - '#default_value' => variable_get('plus1_you_voted', t('You voted')), + '#default_value' => variable_get('plus1_you_voted', 'You voted'), '#description' => t('Enter, for example, Dugg, You voted, or Voted.'), ); - + + $form['plus1_text']['plus1_login_to_vote'] = array( + '#type' => 'textfield', + '#title' => t('Text to show if user needs to log in'), + '#default_value' => variable_get('plus1_login_to_vote', 'Log in to vote'), + '#description' => t('Enter, for example, Log in to vote.'), + ); + $form['plus1_weight'] = array( '#type' => 'fieldset', '#title' => t('Weight settings'), @@ -240,7 +254,15 @@ function plus1_nodeapi(&$node, $op, $teaser, $page) { function plus1_theme() { return array( 'plus1_widget' => array( - 'arguments' => array('node', 'score', 'logged_in', 'is_author', 'voted', 'teaser', 'page'), + 'arguments' => array( + 'node', + 'score' => 0, + 'logged_in' => FALSE, + 'is_author' => FALSE, + 'voted' => FALSE, + 'teaser' => FALSE, + 'page' => TRUE, + ), ), ); } @@ -257,7 +279,7 @@ function plus1_theme() { * default JavaScript file, simply assign different values to * the following variables: * $widget_class (The wrapper element for the voting widget.) -* $link_class (The anchor element to cast a vote.) +* $vote_class (The anchor element to cast a vote.) * $message_class (The wrapper element for the anchor element. May contain feedback when the vote has been cast.) * $score_class (The placeholder element for the score.) * The JavaScript looks for these CSS hooks to @@ -266,48 +288,56 @@ function plus1_theme() { * The JavaScript adds presentation, ie: fade in. * */ -function theme_plus1_widget($node, $score, $logged_in, $is_author, $voted, $teaser, $page) { +function theme_plus1_widget($node, $score = 0, $logged_in = FALSE, $is_author = FALSE, $voted = FALSE, $teaser = FALSE, $page = TRUE) { static $javascript_added = FALSE; - // Load the JavaScript and CSS files. - // You are free to load your own JavaScript files in your theming function to override. - drupal_add_js(drupal_get_path('module', 'plus1') .'/jquery.plus1.js'); - drupal_add_css(drupal_get_path('module', 'plus1') .'/plus1.css'); - // Defining CSS hooks to be used in the JavaScript. $widget_class = 'plus1-widget'; - $link_class = 'plus1-link'; + $vote_class = 'plus1-vote'; $message_class = 'plus1-msg'; $score_class = 'plus1-score'; - // Attaching these hooks names to the Drupal.settings.plus1 JavaScript object. - // So these class names are NOT hard-coded in the JavaScript. if (!$javascript_added) { - drupal_add_js(array('plus1' => array('widget_class' => $widget_class, 'link_class' => $link_class, 'message_class' => $message_class, 'score_class' => $score_class)), 'setting'); + // Load the JavaScript and CSS files. + // You are free to load your own JavaScript files in your theming function to override. + drupal_add_js(drupal_get_path('module', 'plus1') .'/jquery.plus1.js'); + drupal_add_css(drupal_get_path('module', 'plus1') .'/plus1.css'); + + // Attaching these hooks names to the Drupal.settings.plus1 JavaScript object. + // So these class names are NOT hard-coded in the JavaScript. + drupal_add_js(array('plus1' => array('widget_class' => $widget_class, 'vote_class' => $vote_class, 'message_class' => $message_class, 'score_class' => $score_class)), 'setting'); + $javascript_added = TRUE; } // And now we output the HTML. - $output = '
'; - if (!$logged_in || user_access('vote on content')) { - $output .= '
'; - if (!$logged_in && !user_access('vote on content')) { - $output .= ''. l(t('Log in
to vote'), 'user', array('html' => TRUE)) .'
'; + $output_content = ''; + if (!$logged_in && !user_access('vote on content')) { + if ($login_text = variable_get('plus1_login_to_vote', 'Log in to vote')) { + $output_content .= l(t($login_text), 'user', array('html' => TRUE)); } - else if ($voted) { // User already voted. - $output .= check_plain(variable_get('plus1_you_voted', t('You voted'))); + } + else if ($voted) { // User already voted. + if ($voted_text = variable_get('plus1_you_voted', 'You voted')) { + $output_content .= check_plain(t($voted_text)); } - else if (user_access('vote on content')) { + } + else if (user_access('vote on content')) { + if ($vote_text = variable_get('plus1_vote', 'Vote')) { // User is eligible to vote. - // The class name provided by Drupal.settings.plus1.link_class what we will search for in our jQuery later. - $output .= '
' . l(t('Vote'), 'plus1/vote/' . $node->nid, - array('query' => 'token=' . drupal_get_token($node->nid), 'attributes' => array('class' => $link_class))) . '
'; + // The class name provided by Drupal.settings.plus1.vote_class what we will search for in our jQuery later. + $output_content .= '
'; + $output_content .= ''; + $output_content .= '
'; } - $output .= '
'; } - $output .= '
'; - $output .= $score; - $output .= '
'; + if ($output_content) { + $output_content = '
' . $output_content . '
'; + } + + $output = '
'; + $output .= '
'. $score .'
'; + $output .= $output_content; $output .= '
'; return $output; } \ No newline at end of file