From f98f9d64ae1ace6ff22b60c5be84429f057b9749 Mon Sep 17 00:00:00 2001
From: Marc Endres <maen007@gmail.com>
Date: 09.05.2017 04:08:35

fixes that the second click on the same image breaks the window size for big images


diff --git a/picture.js b/picture.js
index 0de7470..b90d714 100644
--- a/picture.js
+++ b/picture.js
@@ -28,11 +28,11 @@
               // but that's not supported by <IE9 and Opera.
               this.style.maxHeight = $(window).height() + 'px';
               this.style.maxWidth = $(window).width() + 'px';
-              $.colorbox.resize({innerHeight: this.height, innerWidth: this.width});
+              $.colorbox.resize({height: this.Height, width: this.Width});
               // Remove overwrite of this values again to ensure we respect the
               // stylesheet.
-              this.style.maxHeight = null;
-              this.style.maxWidth = null;
+            //  this.style.maxHeight = null;
+            //  this.style.maxWidth = null;
             });
           });
         }
diff --git a/picture.module b/picture.module
index aba547a..f4c1e73 100644
--- a/picture.module
+++ b/picture.module
@@ -163,7 +163,7 @@
         'title' => t('Ajax support for picture'),
         'version' => VERSION,
         'js' => array(
-          drupal_get_path('module', 'picture') . '/picture.min.js' => array(
+          drupal_get_path('module', 'picture') . '/picture.js' => array(
             'type' => 'file',
             'weight' => -10,
             'group' => JS_DEFAULT,
diff --git a/picture_colorbox.js b/picture_colorbox.js
index 219b2a1..957cff1 100644
--- a/picture_colorbox.js
+++ b/picture_colorbox.js
@@ -1,36 +1,56 @@
 (function ($) {
   Drupal.behaviors.pictureLazyloadPictures = {
     attach: function (context, settings) {
-      $(context).bind('cbox_load', function () {
+        var getMaxDimensions = function(image, container) {
+        var scaling, aspect;
+
+        // get drupal colorbox max width and height
+        scaling = $.map([
+          { w: settings.colorbox.maxWidth || '100%', r: $(container).height() },
+          { w: settings.colorbox.maxHeight || '100%', r: $(container).width() }
+        ], function(val) {
+          if (val.w.indexOf('%') < 0) {
+            return parseInt(val.w) / val.r;
+          } else {
+            return parseInt(val.w)/100.0;
+          }
+        });
+
+        // limit width and height to container size
+        aspect = (image.width / image.height);
+        var maxHeight = Math.min(image.height, $(container).height());
+        var maxWidth = Math.min(image.width, $(container).width(), (aspect < 1) ? maxHeight / aspect : maxHeight * aspect);
+        //alert(maxHeight + ' ' + maxWidth);
+        return {
+          width: maxWidth * scaling[0],
+          height: maxHeight * scaling[1]
+        };
+      };
+      
+      $(context).on('cbox_load', function () {
         var href = $.colorbox.element()[0].hash;
 
-        if (href.search('#picture-colorbox-') === 0) {
+        if (href.search('#picture-colorbox-') === 0) {            
           href = '.' + href.substr(1, href.length) + ', #' + href.substr(1, href.length);
         }
         var $target = $(href);
-        $('span[lazyload]', $target).replaceWith(function () {
-          var picture = $('<div>').append($(this).clone()).html();
+            $('span[lazyload]', $target).replaceWith(function () {
+              var picture = $('<div>').append($(this).clone()).html();
 
-          picture = picture.replace(/<span/ig, '<picture');
-          picture = picture.replace(/<\/span>/ig, '</picture>');
-          picture = picture.replace(/ data-srcset="/ig, ' srcset="');
-          $('img', picture).load(function() {
-            // Ensure there's no max-width / max-height otherwise we won't get
-            // the proper values. We could use naturalWeight / naturalHeight
-            // but that's not supported by <IE9 and Opera.
-            this.style.maxHeight = $(window).height() + 'px';
-            this.style.maxWidth = $(window).width() + 'px';
-            $.colorbox.resize({innerHeight: this.height, innerWidth: this.width});
-            // Remove overwrite of this values again to ensure we respect the
-            // stylesheet.
-            this.style.maxHeight = null;
-            this.style.maxWidth = null;
+              picture = picture.replace(/<span/ig, '<picture');
+              picture = picture.replace(/<\/span>/ig, '</picture>');
+              picture = picture.replace(/ data-srcset="/ig, ' srcset="');
+              $('img', picture).load(function() {
+                // Ensure there's no max-width / max-height otherwise we won't get
+                // the proper values. We could use naturalWeight / naturalHeight
+                // but that's not supported by <IE9 and Opera.
+                this.style.maxHeight = $(window).height() + 'px';
+                this.style.maxWidth = $(window).width() + 'px';                
+                $.colorbox.resize(getMaxDimensions(this, window));
+              });
 
-            $.colorbox.resize();
-          });
-
-          return $(picture).attr('lazyload', null);
-        });
+              return $(picture).attr('lazyload', null);
+            });
       }).bind('cbox_complete', function () {
         $.colorbox.resize();
       });
