in ui-modules/utils/bottom-sheet/bottom-sheet.js [248:397]
function open(options) {
options = angular.extend({}, defaultOptions, options);
options.resolve = options.resolve || {};
options.appendTo = options.appendTo || $document.find('body').eq(0);
// Perform some validations on options
if (MODES.indexOf(options.mode) === -1) {
throw new Error('"mode" not supported. Make sure that the mode is one of those: ' + MODES);
}
if (!options.appendTo.length) {
throw new Error('"appendTo" element not found. Make sure that the element passed is in DOM.');
}
if (!options.template && !options.templateUrl) {
throw new Error('One of "template" or "templateUrl" options is required.');
}
// Create promises
let bottomSheetResultDeferred = $q.defer();
let bottomSheetOpenedDeferred = $q.defer();
let bottomSheetClosedDeferred = $q.defer();
let bottomSheetRenderDeferred = $q.defer();
let promises = $q.all([
getTemplatePromise(options),
$uibResolve.resolve(options.resolve, {}, null, null)
]);
// Create bottom sheet instance
let bottomSheetInstance = {
result: bottomSheetResultDeferred.promise,
opened: bottomSheetOpenedDeferred.promise,
closed: bottomSheetClosedDeferred.promise,
rendered: bottomSheetRenderDeferred.promise,
close: (reason)=> {
close(reason, true);
},
dismiss: (reason)=> {
close(reason, false);
},
updateMode: (mode)=> {
updateMode(mode);
}
};
// Let's create our bottom sheet instance
promises.then((tplAndVars)=> {
let providedScope = options.scope || $rootScope;
let bottomSheetScope = providedScope.$new();
bottomSheetScope.$close = bottomSheetInstance.close;
bottomSheetScope.$dismiss = bottomSheetInstance.dismiss;
bottomSheetScope.$updateMode = bottomSheetInstance.updateMode;
bottomSheetScope.$on('$destroy', function() {
if (!bottomSheetScope.$$brDestructionScheduled) {
bottomSheetScope.$dismiss('$brUnscheduledDestruction');
}
});
bottomSheet = {
scope: bottomSheetScope,
deferred: bottomSheetResultDeferred,
renderDeferred: bottomSheetRenderDeferred,
closedDeferred: bottomSheetClosedDeferred,
animation: options.animation,
keyboard: options.keyboard,
mode: options.mode,
backdropClass: options.backdropClass,
backdropAnimationClass: options.backdropAnimationClass,
containerClass: options.containerClass,
containerAnimationClass: options.containerAnimationClass,
containerTemplateUrl: options.containerTemplateUrl,
openedClass: options.openedClass,
appendTo: options.appendTo,
content: tplAndVars[0],
ariaLabelledBy: options.ariaLabelledBy,
ariaDescribedBy: options.ariaDescribedBy,
};
// We create our own instance of controller, based on the given options
let ctrlInstance, ctrlInstantiate, ctrlLocals = {};
ctrlLocals.$scope = bottomSheetScope;
ctrlLocals.$scope.$resolve = {};
ctrlLocals.brBottomSheetInstance = bottomSheetInstance;
// If we passed a resolve block, all vars are injected into the local controller scope
let resolves = tplAndVars[1];
angular.forEach(resolves, function(value, key) {
ctrlLocals[key] = value;
ctrlLocals.$scope.$resolve[key] = value;
});
// the third param will make the controller instantiate later,private api
// @see https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L126
ctrlInstantiate = $controller(options.controller, ctrlLocals, true, options.controllerAs);
if (options.controllerAs && options.bindToController) {
ctrlInstance = ctrlInstantiate.instance;
ctrlInstance.$close = bottomSheetScope.$close;
ctrlInstance.$dismiss = bottomSheetScope.$dismiss;
angular.extend(ctrlInstance, {
$resolve: ctrlLocals.$scope.$resolve
}, providedScope);
}
ctrlInstance = ctrlInstantiate();
if (angular.isFunction(ctrlInstance.$onInit)) {
ctrlInstance.$onInit();
}
// Create the backdrop if the mode is set to 'modal'
if (options.mode === MODES[0]) {
createBackdrop();
}
bottomSheet.containerElm = angular.element('<div br-bottom-sheet-container></div>');
bottomSheet.containerElm.attr({
'class': bottomSheet.containerClass,
'animation-class': bottomSheet.containerAnimationClass || CLASS_ANIMATION_SLIDE_UP + ' ' + CLASS_ANIMATION_FADE,
'role': 'dialog',
'tabindex': -1,
}).append(bottomSheet.content);
if (bottomSheet.containerTemplateUrl) {
bottomSheet.containerElm.attr('template-url', bottomSheet.containerTemplateUrl);
}
if (bottomSheet.animation) {
bottomSheet.containerElm.attr('animation', 'true');
}
let bodyClass = bottomSheet.openedClass || CLASS_OPENED;
bottomSheet.appendTo.addClass(bodyClass);
if (bottomSheet.mode === MODES[0]) {
bottomSheet.appendTo.addClass(CLASS_MODE_MODAL);
}
if (bottomSheet.mode === MODES[1]) {
bottomSheet.appendTo.addClass(CLASS_MODE_INSET);
}
$animate.enter($compile(bottomSheet.containerElm)(bottomSheet.scope), bottomSheet.appendTo);
// Focus on the newly created bottom sheet
bottomSheet.containerElm[0].focus();
bottomSheetOpenedDeferred.resolve(true);
}).catch((reason)=> {
bottomSheetOpenedDeferred.reject(reason);
bottomSheetResultDeferred.reject(reason);
});
return bottomSheetInstance;
}