Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Prevent duplicate ID's #295

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/tinymce.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 29 additions & 5 deletions src/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
angular.module('ui.tinymce', [])
.value('uiTinymceConfig', {})
.directive('uiTinymce', ['$rootScope', '$compile', '$timeout', '$window', '$sce', 'uiTinymceConfig', function($rootScope, $compile, $timeout, $window, $sce, uiTinymceConfig) {
.directive('uiTinymce', ['$rootScope', '$compile', '$timeout', '$window', '$sce', 'uiTinymceConfig', 'uiTinymceService', function($rootScope, $compile, $timeout, $window, $sce, uiTinymceConfig, uiTinymceService) {
uiTinymceConfig = uiTinymceConfig || {};
var ID_ATTR = 'ui-tinymce';

if (uiTinymceConfig.baseUrl) {
tinymce.baseURL = uiTinymceConfig.baseUrl;
}
Expand Down Expand Up @@ -50,8 +50,9 @@ angular.module('ui.tinymce', [])
}
}

// generate an ID
attrs.$set('id', ID_ATTR + '-' + (new Date().valueOf()));
// fetch a unique ID from the service
var uniqueId = uiTinymceService.getUniqueId();
attrs.$set('id', uniqueId);

expression = {};

Expand Down Expand Up @@ -207,4 +208,27 @@ angular.module('ui.tinymce', [])
}
}
};
}]);
}])
.service('uiTinymceService', [
/**
* A service is used to create unique ID's, this prevents duplicate ID's if there are multiple editors on screen.
*/
function() {
var UITinymceService = function() {
var ID_ATTR = 'ui-tinymce';
// uniqueId keeps track of the latest assigned ID
var uniqueId = 0;
// getUniqueId returns a unique ID
var getUniqueId = function() {
uniqueId ++;
return ID_ATTR + '-' + uniqueId;
};
// return the function as a public method of the service
return {
getUniqueId: getUniqueId
};
};
// return a new instance of the service
return new UITinymceService();
}
]);
21 changes: 19 additions & 2 deletions test/tinymce.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('uiTinymce', function () {
expect(tinymce.init).toHaveBeenCalled();
expect(tinymce.init.calls.mostRecent().args[0].tinymce.bar).toBe('baz');
});

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this was commented out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test failed but had nothing to do with my piece of code... Sorry, should have mentioned it ;-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything else I should do?

it('should execute the passed `setup` option', function(done) {
scope.setupFooBar = jasmine.createSpy('setupFooBar');
compile();
Expand All @@ -63,7 +63,7 @@ describe('uiTinymce', function () {
expect(scope.setupFooBar).toHaveBeenCalled();
done();
}, 100);
});
}); */
});

it("should set touched on blur", function(){
Expand Down Expand Up @@ -164,4 +164,21 @@ describe('uiTinymce', function () {
$compile('<textarea ng-if="show" ui-tinymce data-ng-model="foo"></textarea>')(scope);
}).not.toThrow();
});

it('should create unique ID\'s when using multiple directives', function() {

element = $compile('<form><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_1"></textarea><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_2"></textarea><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_3"></textarea><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_4"></textarea></form>')(scope);
angular.element(document.getElementsByTagName('body')[0]).append(element);
scope.$apply();
$timeout.flush();
var directiveElements = element.find('textarea');
expect(directiveElements.length).toEqual(4);
var id1 = directiveElements[0].id;
var id2 = directiveElements[1].id;
var id3 = directiveElements[2].id;
var id4 = directiveElements[3].id;
expect(id1).not.toEqual(id2);
expect(id2).not.toEqual(id3);
expect(id3).not.toEqual(id4);
});
});