From bbc80029bba3f0682646d04c323fe3b3750e56af Mon Sep 17 00:00:00 2001 From: Oleh Romanovskyi Date: Tue, 10 Apr 2018 04:59:23 +0300 Subject: [PATCH] Fix `cl-transformation` support of `ng-if` (#105) --- js/angular.cloudinary.js | 32 +++++++- spec/cloudinary_spec.js | 162 +++++++++++++++++++++++---------------- 2 files changed, 127 insertions(+), 67 deletions(-) diff --git a/js/angular.cloudinary.js b/js/angular.cloudinary.js index 63d20dd1..e393ddfc 100644 --- a/js/angular.cloudinary.js +++ b/js/angular.cloudinary.js @@ -92,7 +92,11 @@ link : function (scope, element, attrs, ctrl) { var tagController = ctrl.clImage || ctrl.clVideo; if (tagController) { - tagController.addTransformation(toCloudinaryAttributes(attrs, /^[^$]/)); + var transformation = toCloudinaryAttributes(attrs, /^[^$]/); + tagController.addTransformation(transformation); + scope.$on('$destroy', function (event) { + tagController.removeTransformation(transformation); + }) } else { console.warn("cl-transformation should be a child of cl-image or cl-video") } @@ -105,6 +109,15 @@ this.addTransformation = function(ts) { $scope.transformations = $scope.transformations || []; $scope.transformations.push(ts); + $scope.transformations = $scope.transformations.slice() + }; + this.removeTransformation = function (ts) { + $scope.transformations = $scope.transformations || []; + var index = $scope.transformations.indexOf(ts); + if (index >= 0) { + $scope.transformations.splice(index, 1); + $scope.transformations = $scope.transformations.slice() + } } }; Controller.$inject = ['$scope']; @@ -125,6 +138,12 @@ options.transformation = scope.transformations; } + scope.$watch('transformations', function(value){ + if (!value) return; + options.transformation = value; + loadImage(); + }); + // store public id and load image attrs.$observe('publicId', function(value){ if (!value) return; @@ -173,6 +192,15 @@ this.addTransformation = function(ts) { $scope.transformations = $scope.transformations || []; $scope.transformations.push(ts); + $scope.transformations = $scope.transformations.slice() + }; + this.removeTransformation = function (ts) { + $scope.transformations = $scope.transformations || []; + var index = $scope.transformations.indexOf(ts); + if (index >= 0) { + $scope.transformations.splice(index, 1); + $scope.transformations = $scope.transformations.slice() + } } }; Controller.$inject = ['$scope']; @@ -251,4 +279,4 @@ return instance; }]; }); -}); \ No newline at end of file +}); diff --git a/spec/cloudinary_spec.js b/spec/cloudinary_spec.js index 196d10be..660b36d0 100644 --- a/spec/cloudinary_spec.js +++ b/spec/cloudinary_spec.js @@ -106,7 +106,7 @@ describe("cloudinary", function () { // Check that the compiled element contains the templated content expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/if_w_gt_200,c_scale,w_100/foobar\""); }); - + it ('should add if (condition) to the result URL', function() { // Compile a piece of HTML containing the directive var element = $compile("
")($rootScope); @@ -114,34 +114,34 @@ describe("cloudinary", function () { // Check that the compiled element contains the templated content expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/if_w_gt_200,c_scale,w_100/foobar\""); - }); - + }); + it ('should add if (condition) to the result URL', function() { // Compile a piece of HTML containing the directive - var element = $compile("
" + - "" + + var element = $compile("
" + + "" + ""+ - "" + + "" + ""+ - "" + + "" + "
")($rootScope); $rootScope.$digest(); // Check that the compiled element contains the templated content expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/if_w_gt_200/c_scale,w_100/c_fill,h_100/if_end/foobar\""); }); - + it ('should add if (condition) to the result URL', function() { // Compile a piece of HTML containing the directive - var element = $compile("
" + - "" + + var element = $compile("
" + + "" + ""+ - "" + + "" + ""+ "" + - "" + + "" + ""+ - "" + + "" + "
")($rootScope); $rootScope.$digest(); @@ -149,7 +149,7 @@ describe("cloudinary", function () { expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/if_w_gt_200/c_scale,w_100/c_fill,h_100/if_else/c_fill,w_200/c_fit,h_200/if_end/foobar\""); }); }); - + describe("responsive", function () { var testWindow, tabImage2, image1; beforeAll(function (done) { @@ -188,63 +188,95 @@ describe("cloudinary", function () { }) }) }); - describe('clVideo', function () { - it('throws an exception with missing publicId', function () { - expect(function() { - $compile('
')($rootScope); - $rootScope.$digest(); - }).toThrow(new Error( - 'You must set the public id of the video to load, e.g. ' - )); + describe('child transformation with ng-if', function () { + it('should include transformation with ng-if="true"', function () { + var template = '
' + + '' + + '
'; + var element = $compile(template)($rootScope); + $rootScope.$digest(); + expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/e_sepia,g_north,r_20/foobar\""); }); - it('does not throw an error if transformation is not in video or image tag', function () { - $compile('')($rootScope); + it('should exclude transformation with ng-if="false"', function () { + var template = '
' + + '' + + '
'; + var element = $compile(template)($rootScope); $rootScope.$digest(); + expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/foobar\""); }); - describe('videos with nested transformations', function () { - it('creates a