From edd8a8ab633ef62f37ccec649d5fbf55caa0d2b8 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Je=CC=81ro=CC=82me=20Danthinne?= <jerome@grincheux.be>
Date: Thu, 24 Mar 2011 08:59:32 +0100
Subject: [PATCH] fixed : added 'processed' class to the slideshow so the scripts are not applied more than once.
 Re-moved all markup creation in the .module file, instead of .js.

---
 field_slideshow.module |   48 ++++++++++++++++------------
 js/field_slideshow.js  |   81 +++++++++++++++++++++++------------------------
 2 files changed, 68 insertions(+), 61 deletions(-)

diff --git a/field_slideshow.module b/field_slideshow.module
index c83377d..bc80889 100644
--- a/field_slideshow.module
+++ b/field_slideshow.module
@@ -435,33 +435,41 @@ function theme_field_slideshow($variables) {
   if ($variables['controls']) $classes[] = 'with-controls';
   $output = '<div id="field-slideshow-' . $field_slideshow_id . '-wrapper" class="field-slideshow-wrapper">
     <div class="' . implode(' ', $classes) . '" style="width:' . $slides_max_width . 'px; height:' . $slides_max_height . 'px">';
+
   $output .= $slides_output; // adds the slides
-  $output .= "</div>"; // .field-slideshow & .field-slideshow-wrapper div closed
+  $output .= '</div>'; // .field-slideshow & .field-slideshow-wrapper div closed
+
+  // Add controls if needed
+  if ($variables['controls']) {
+    $output .= '<div id="field-slideshow-' . $field_slideshow_id . '-controls" class="field-slideshow-controls"><a href="#" class="prev">' . t('Prev') . '</a> <a href="#" class="next">' . t('Next') . '</a></div>';
+  }
 
   // Add thumbnails if needed
-  if ($variables['pager'] == 'image') {
-    $output .= '<ul id="field-slideshow-' . $field_slideshow_id . '-pager" class="field-slideshow-pager">';
-    foreach ($variables['items'] as $num => $item) {
-      $image = array(
-        'path' => $item['uri'],
-        'alt' => $item['alt'],
-      );
-      if (drupal_strlen($item['title']) > 0) {
-        $image['title'] = $item['title'];
-      }
-      if ($variables['pager_image_style']) {
-        $image['style_name'] = $variables['pager_image_style'];
-        $image_output = theme('image_style', $image);
-      }
-      else {
-        $image_output = theme('image', $image);
+  if ($variables['pager'] != '') {
+    $output .= '<' . ($variables['pager'] == 'image' ? 'ul' : 'div') . ' id="field-slideshow-' . $field_slideshow_id . '-pager" class="field-slideshow-pager">';
+    if ($variables['pager'] == 'image') {
+      foreach ($variables['items'] as $num => $item) {
+        $image = array(
+          'path' => $item['uri'],
+          'alt' => $item['alt'],
+        );
+        if (drupal_strlen($item['title']) > 0) {
+          $image['title'] = $item['title'];
+        }
+        if ($variables['pager_image_style']) {
+          $image['style_name'] = $variables['pager_image_style'];
+          $image_output = theme('image_style', $image);
+        }
+        else {
+          $image_output = theme('image', $image);
+        }
+        $output .= '<li><a href="#">' . theme('image_style', $image) . '</a></li>';
       }
-      $output .= '<li><a href="#">' . theme('image_style', $image) . '</a></li>';
     }
-    $output .= '</ul>';
+    $output .= '</' . ($variables['pager'] == 'image' ? 'ul' : 'div') . '>';
   }
 
-  $output .= "</div>"; // .field-slideshow-wrapper div closed
+  $output .= '</div>'; // .field-slideshow-wrapper div closed
 
   // Add the Cycle plugin and the Js code
   drupal_add_js(drupal_get_path('module', 'field_slideshow') . '/js/jquery.cycle.all.min.js');
diff --git a/js/field_slideshow.js b/js/field_slideshow.js
index d0b65bf..31caa2f 100644
--- a/js/field_slideshow.js
+++ b/js/field_slideshow.js
@@ -6,50 +6,49 @@
         var settings = Drupal.settings.field_slideshow[i];
         var slideshow = $('.' + i);
 
-        // Add padding if needed
-        var max_outerWidth = 0;
-        var max_outerHeight = 0;
-        $('.field-slideshow-slide', slideshow).each(function() {
-          $this = $(this);
-          max_outerWidth = Math.max(max_outerWidth, $this.outerWidth(true));
-          max_outerHeight = Math.max(max_outerHeight, $this.outerHeight(true));
-        });
-        slideshow.css({
-          'padding-right': (max_outerWidth - parseInt(slideshow.css('width'))) + 'px',
-          'padding-bottom': (max_outerHeight - parseInt(slideshow.css('height'))) + 'px'
-        });
-
-        // Create Pager wrapper (if not image)
-        if (settings.pager != '' && settings.pager != 'image') slideshow.after("<div id='" + i + "-pager' class='field-slideshow-pager'/>");
-
-        // Create Controls
-        if (settings.controls) slideshow.after("<div id='" + i + "-controls' class='field-slideshow-controls'><a href='#' class='prev'>" + Drupal.t('Prev') + "</a> <a href='#' class='next'>" + Drupal.t('Next') + "</a></div>");
-
-        // Add options
-        var options = {
-          resizing: 0,
-          fx: settings.fx,
-          speed: settings.speed,
-          timeout: parseInt(settings.timeout)
-        }
-
-        if (settings.speed == "0" && settings.timeout == "0") options.fastOnEvent = true;
-        if (settings.controls != "0") {
-          options.prev = "#" + i + "-controls .prev";
-          options.next = "#" + i + "-controls .next";
-        }
-        if (settings.pause != "0") options.pause = true;
+        if (!slideshow.hasClass('field-slideshow-processed')) {
+          slideshow.addClass('field-slideshow-processed');
+
+          // Add padding if needed
+          var max_outerWidth = 0;
+          var max_outerHeight = 0;
+          $('.field-slideshow-slide', slideshow).each(function() {
+            $this = $(this);
+            max_outerWidth = Math.max(max_outerWidth, $this.outerWidth(true));
+            max_outerHeight = Math.max(max_outerHeight, $this.outerHeight(true));
+          });
+          slideshow.css({
+            'padding-right': (max_outerWidth - parseInt(slideshow.css('width'))) + 'px',
+            'padding-bottom': (max_outerHeight - parseInt(slideshow.css('height'))) + 'px'
+          });
+
+          // Add options
+          var options = {
+            resizing: 0,
+            fx: settings.fx,
+            speed: settings.speed,
+            timeout: parseInt(settings.timeout)
+          }
+
+          if (settings.speed == "0" && settings.timeout == "0") options.fastOnEvent = true;
+          if (settings.controls != "0") {
+            options.prev = "#" + i + "-controls .prev";
+            options.next = "#" + i + "-controls .next";
+          }
+          if (settings.pause != "0") options.pause = true;
+
+          if (settings.pager != '') {
+            options.pager = "#" + i + "-pager";
+            if (settings.pager == 'image') options.pagerAnchorBuilder = function(idx, slide) {
+              return '#' + i + '-pager li:eq(' + idx + ') a';
+            };
+          }
+
+          // Cycle!
+          slideshow.cycle(options); 
 
-        if (settings.pager != '') {
-          options.pager = "#" + i + "-pager";
-          if (settings.pager == 'image') options.pagerAnchorBuilder = function(idx, slide) {
-            return '#' + i + '-pager li:eq(' + idx + ') a';
-          };
         }
 
-        // Cycle!
-        slideshow.cycle(options);
-
       }
 
     }
-- 
1.7.2.1