Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(interimElement): allow options.parent to be a selector
Browse files Browse the repository at this point in the history
Closes #640.
  • Loading branch information
ajoslin committed Nov 15, 2014
1 parent 12b8cbc commit 342051e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/core/services/interimElement/interimElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function InterimElementProvider() {
*/
provider.addPreset('build', {
methods: ['controller', 'controllerAs', 'onRemove', 'onShow', 'resolve',
'template', 'templateUrl', 'themable', 'transformTemplate']
'template', 'templateUrl', 'themable', 'transformTemplate', 'parent']
});

return provider;
Expand Down Expand Up @@ -178,7 +178,7 @@ function InterimElementProvider() {
}

/* @ngInject */
function InterimElementFactory($q, $rootScope, $timeout, $rootElement, $animate, $mdCompiler, $mdTheming) {
function InterimElementFactory($document, $q, $rootScope, $timeout, $rootElement, $animate, $mdCompiler, $mdTheming) {

return function createInterimElementService() {
/*
Expand Down Expand Up @@ -280,10 +280,13 @@ function InterimElementProvider() {
angular.extend(compileData.locals, self.options);

// Search for parent at insertion time, if not specified
if (!options.parent) {
if (angular.isString(options.parent)) {
options.parent = angular.element($document[0].querySelector(options.parent));
} else if (!options.parent) {
options.parent = $rootElement.find('body');
if (!options.parent.length) options.parent = $rootElement;
}

element = compileData.link(options.scope);
if (options.themable) $mdTheming(element);
var ret = options.onShow(options.scope, element, options);
Expand Down
44 changes: 44 additions & 0 deletions src/core/services/interimElement/interimElement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,50 @@ describe('$$interimElement service', function() {
it('returns a promise', inject(function($$interimElement) {
expect(typeof Service.show().then).toBe('function');
}));

it('defaults parent to $rootElement', inject(function($rootElement, $rootScope) {
var shown = false;
Service.show({
onShow: function(scope, element, options) {
expect(options.parent[0]).toBe($rootElement[0]);
shown = true;
}
});
$rootScope.$digest();
expect(shown).toBe(true);
}));

it('allows parent reference', inject(function($rootScope) {
var parent = angular.element('<div>');

var shown = false;
Service.show({
parent: parent,
onShow: function(scope, element, options) {
expect(options.parent[0]).toBe(parent[0]);
shown = true;
}
});
$rootScope.$digest();
expect(shown).toBe(true);
}));

it('allows string parent selector', inject(function($rootScope, $document) {
var parent = angular.element('<div id="super-parent">');
spyOn($document[0], 'querySelector').andReturn(parent[0]);

var shown = false;
Service.show({
parent: '#super-parent',
onShow: function(scope, element, options) {
expect($document[0].querySelector).toHaveBeenCalledWith('#super-parent');
expect(options.parent[0]).toBe(parent[0]);
shown = true;
}
});
$rootScope.$digest();
expect(shown).toBe(true);
}));
});


Expand Down

0 comments on commit 342051e

Please sign in to comment.