From 8b28252ccf106790833d17cc38535cabe2f987fb Mon Sep 17 00:00:00 2001
From: Rafael Ferreira Silva <rafael@rafaelsilva.net>
Date: Thu, 3 May 2012 17:56:42 -0300
Subject: [PATCH] Issue #1419210 by mauritsl, rfsbsb Added the Rate Slider module

---
 rate_slider/rate_slider.info                |    6 ++
 rate_slider/rate_slider.module              |   82 +++++++++++++++++++++++++++
 rate_slider/templates/slider/slider.css     |   65 +++++++++++++++++++++
 rate_slider/templates/slider/slider.js      |   56 ++++++++++++++++++
 rate_slider/templates/slider/slider.tpl.php |   17 ++++++
 5 files changed, 226 insertions(+), 0 deletions(-)
 create mode 100644 rate_slider/rate_slider.info
 create mode 100644 rate_slider/rate_slider.module
 create mode 100644 rate_slider/templates/slider/slider.css
 create mode 100644 rate_slider/templates/slider/slider.js
 create mode 100644 rate_slider/templates/slider/slider.tpl.php

diff --git a/rate_slider/rate_slider.info b/rate_slider/rate_slider.info
new file mode 100644
index 0000000..9c1a831
--- /dev/null
+++ b/rate_slider/rate_slider.info
@@ -0,0 +1,6 @@
+name = Rate Slider
+description = Add a slider widget type
+core = 7.x
+package = Voting
+dependencies[] = rate
+dependencies[] = jquery_ui
diff --git a/rate_slider/rate_slider.module b/rate_slider/rate_slider.module
new file mode 100644
index 0000000..5be6f95
--- /dev/null
+++ b/rate_slider/rate_slider.module
@@ -0,0 +1,82 @@
+<?php
+
+/**
+ * Implements hook_rate_templates().
+ */
+function rate_slider_rate_templates() {
+  $templates = array();
+
+  $templates['slider'] = new stdClass();
+  $templates['slider']->value_type = 'percent';
+  $templates['slider']->options = array(
+    array(10, '1'),
+    array(20, '2'),
+    array(30, '3'),
+    array(40, '4'),
+    array(50, '5'),
+    array(60, '6'),
+    array(70, '7'),
+    array(80, '8'),
+    array(90, '9'),
+    array(100, '10'),
+  );
+  $templates['slider']->theme = 'rate_template_slider';
+  $templates['slider']->css = drupal_get_path('module', 'rate_slider') . '/templates/slider/slider.css';
+  $templates['slider']->js = drupal_get_path('module', 'rate_slider') . '/templates/slider/slider.js';
+  $templates['slider']->customizable = FALSE;
+  $templates['slider']->translate = FALSE;
+  $templates['slider']->template_title = t('Slider');
+
+  return $templates;
+}
+
+/**
+ * Implements hook_theme().
+ */
+function rate_slider_theme() {
+  return array(
+    'rate_template_slider' => array(
+      'arguments' => array('links' => NULL, 'results' => NULL, 'mode' => NULL, 'just_voted' => FALSE, 'content_type' => NULL, 'content_id' => NULL, 'display_options' => NULL),
+      'template' => 'slider',
+      'path' => drupal_get_path('module', 'rate_slider') . '/templates/slider',
+    ),
+  );
+}
+
+/**
+ * Preprocess function for the slider template.
+ */
+function rate_slider_preprocess_rate_template_slider(&$variables) {
+  extract($variables);
+
+  jquery_ui_add(array('ui.slider'));
+
+  // Calculate start value for slider.
+  if ($variables['results']['count'] == 0) {
+    $variables['value'] = 50;
+  }
+  else {
+    $variables['value'] = (int) $variables['results']['rating'];
+  }
+
+  $buttons = array();
+  foreach ($links as $link) {
+    $button = theme('rate_button', array('text' => $link['text'], 'href' => $link['href'], 'class' => ''));
+    $buttons[] = $button;
+  }
+  $variables['buttons'] = $buttons;
+
+  $info = array();
+  if ($mode == RATE_CLOSED) {
+    $info[] = t('Voting is closed.');
+  }
+
+  if ($mode != RATE_COMPACT && $mode != RATE_COMPACT_DISABLED) {
+    if (isset($results['user_vote'])) {
+      $vote = $results['user_vote'];
+      $info[] = t('You voted !vote.', array('!vote' => number_format($vote / 10, 2)));
+    }
+    $info[] = t('Total votes: !count', array('!count' => $results['count']));
+  }
+  $variables['info'] = implode(' ', $info);
+}
diff --git a/rate_slider/templates/slider/slider.css b/rate_slider/templates/slider/slider.css
new file mode 100644
index 0000000..09dee86
--- /dev/null
+++ b/rate_slider/templates/slider/slider.css
@@ -0,0 +1,65 @@
+.rate-widget-slider ul {
+  list-style: none;
+  overflow: hidden;
+}
+.rate-widget-slider .item-list ul li {
+  background-image: none;
+  padding: 0;
+  margin: 0 5px 0 0;
+}
+.rate-widget-slider ul li {
+  float: left;
+  list-style: none;
+}
+.rate-widget-slider ul li a {
+  display: block;
+  float: left;
+}
+
+.rate-widget-slider .rate-slider-value {
+  height: .8em;
+  background-color: #ccc;
+}
+
+.rate-widget-slider.rate-user .rate-slider-value {
+  background-color: #f99;
+}
+
+.rate-widget-slider .ui-slider {
+	/*resets*/margin: 0; padding: 0; border: 0; outline: 0; line-height: 1.3; text-decoration: none; font-size: 100%; list-style: none;
+	font-family: Verdana, Arial, sans-serif;
+	background: #eee;
+	border: 1px solid #ccc;
+	height: .8em;
+  width: 200px;
+	position: relative;
+}
+.rate-widget-slider .ui-slider-handle {
+	/*resets*/margin: 0; padding: 0; border: 0; outline: 0; line-height: 1.3; text-decoration: none; font-size: 100%; list-style: none;
+	position: absolute;
+	z-index: 2;
+	top: -3px;
+	width: 1.2em;
+	height: 1.2em;
+	background-color: #ccc;
+	border: 1px solid #999;
+}
+.rate-widget-slider .ui-slider-handle:hover {
+	background-color: #999;
+	border: 1px solid #999;
+}
+.rate-widget-slider .ui-slider-handle-active, .ui-slider-handle-active:hover {
+	background-color: #999;
+	border: 1px solid #999;
+}
+.rate-widget-slider .ui-slider-range {
+	/*resets*/margin: 0; padding: 0; border: 0; outline: 0; line-height: 1.3; text-decoration: none; font-size: 100%; list-style: none;
+	height: .8em;
+	background-color: #ccc;
+	position: absolute;
+	border: 1px solid #999;
+	border-left: 0;
+	border-right: 0;
+	top: -1px;
+	z-index: 1;
+}
diff --git a/rate_slider/templates/slider/slider.js b/rate_slider/templates/slider/slider.js
new file mode 100644
index 0000000..5d2459a
--- /dev/null
+++ b/rate_slider/templates/slider/slider.js
@@ -0,0 +1,56 @@
+(function ($) {
+  Drupal.behaviors.RateSlider = {
+    attach: function (context) {
+      $('.rate-widget-slider:not(.rate-slider-processed)',context).addClass('rate-slider-processed').each(function() {
+        var widget = $(this);
+        var ids = widget.attr('id').match(/^rate\-([a-z]+)\-([0-9]+)\-([0-9]+)\-([0-9])$/);
+        var data = {
+          content_type: ids[1],
+          content_id: ids[2],
+          widget_id: ids[3],
+          widget_mode: ids[4]
+        };
+
+        var s = $(".rate-slider", widget);
+        var v = $(s).attr("class").match(/rate\-value\-([0-9]+)/)[1];
+
+        widget.prepend(s);
+
+        // Check if this widget is active (disabled widgets have <span>'s instead of <a>'s).'
+        if ($("ul a", widget).length > 0) {
+          // Add the slider.
+          s.slider({
+            min: 10,
+            max: 100,
+            steps: 90,
+            startValue: v, // jQuery UI 1.2
+            value: v, // jQuery UI 1.3
+            slide: function(event,ui) {
+              //ui.value;
+              var vote = ui.value / 10;
+              $(".rate-slider-value", s).width(((ui.value - 10) * (10 / 9)) + '%');
+              $(".rate-info", widget).text(Drupal.t("Your vote: !vote", {"!vote": vote.toFixed(1)}));
+            },
+            stop: function(event,ui) {
+              data.value = ui.value;
+              var itemid = "#rate-button-" + Math.round(data.value / 10);
+              var token = $(itemid).attr('href').match(/rate\=([a-zA-Z0-9\-_]{32,64})/)[1];
+              return Drupal.rateVote(widget, data, token);
+            }
+          });
+        }
+        else {
+          // Widget is disabled. Only add the slider styling.
+          $(s).width('200px');
+          $(s).addClass('ui-slider');
+        }
+
+        // Add the rating bar.
+        s.prepend('<div class="rate-slider-value" style="width: ' + ((v - 10) * (10 / 9)) + '%" />');
+
+        // Hide the links for the non-js variant.
+        $("ul", widget).hide();
+      });
+    }
+  }
+})(jQuery);
diff --git a/rate_slider/templates/slider/slider.tpl.php b/rate_slider/templates/slider/slider.tpl.php
new file mode 100644
index 0000000..dc49cb8
--- /dev/null
+++ b/rate_slider/templates/slider/slider.tpl.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * @file
+ * Rate widget theme
+ */
+
+print '<div class="rate-slider rate-value-' . $value . '">';
+print theme('item_list', array('items' => $buttons));
+print '</div>';
+
+if ($info) {
+  print '<div class="rate-info">' . $info . '</div>';
+}
+
+if ($display_options['description']) {
+  print '<div class="rate-description">' . $display_options['description'] . '</div>';
+}
-- 
1.7.0.4

