Skip to content

Commit

Permalink
Fix cl-transformation support of ng-if (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
shedar committed Mar 12, 2019
1 parent 452747f commit 3dd6b62
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 20 additions & 1 deletion js/angular.cloudinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
transclude : false,
require: '^clImage',
link : function (scope, element, attrs, clImageCtrl) {
clImageCtrl.addTransformation(toCloudinaryAttributes(attrs, /^[^$]/));
var transformation = toCloudinaryAttributes(attrs, /^[^$]/);
clImageCtrl.addTransformation(transformation);
scope.$on('$destroy', function (event) {
clImageCtrl.removeTransformation(transformation);
})
}
}
}]);
Expand All @@ -100,6 +104,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'];
Expand All @@ -120,6 +133,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;
Expand Down
32 changes: 32 additions & 0 deletions spec/cloudinary_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,38 @@ describe("cloudinary", function () {
})
})
});
describe('child transformation with ng-if', function () {
it('should include transformation with ng-if="true"', function () {
var template = '<div><cl-image public_id="foobar">' +
'<cl-transformation ng-if="true" gravity="north" effect="sepia" radius="20" />' +
'</cl-image></div>';
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('should exclude transformation with ng-if="false"', function () {
var template = '<div><cl-image public_id="foobar">' +
'<cl-transformation ng-if="false" gravity="north" effect="sepia" radius="20" />' +
'</cl-image></div>';
var element = $compile(template)($rootScope);
$rootScope.$digest();
expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/foobar\"");
});
it('should update transformation on ng-if condition changed', function () {
var template = '<div><cl-image public_id="foobar">' +
'<cl-transformation ng-if="ngIfVisible" gravity="north" effect="sepia" radius="20" />' +
'</cl-image></div>';
var element = $compile(template)($rootScope);

$rootScope.ngIfVisible = true;
$rootScope.$digest();
expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/e_sepia,g_north,r_20/foobar\"");

$rootScope.ngIfVisible = false;
$rootScope.$digest();
expect(element.html()).toMatch("src=\"https?://res\.cloudinary\.com/" + CLOUD_NAME + "/image/upload/foobar\"");
});
});
describe("clSrc", function () {
it('populates the src attribute with the cloudinary URL for the public ID', function () {
var element = $compile("<div><img cl-src='foobar'/></div>")($rootScope);
Expand Down

0 comments on commit 3dd6b62

Please sign in to comment.