This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
forked from btford/angular-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modal.js
93 lines (82 loc) · 2.31 KB
/
modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* @license
* angular-modal v0.5.0
* (c) 2013 Brian Ford http://briantford.com
* License: MIT
*/
'use strict';
angular.module('btford.modal', []).
factory('btfModal', ['$animate', '$compile', '$rootScope', '$controller', '$q', '$http', '$templateCache', modalFactoryFactory]);
function modalFactoryFactory($animate, $compile, $rootScope, $controller, $q, $http, $templateCache) {
return function modalFactory (config) {
if (!(!config.template ^ !config.templateUrl)) {
throw new Error('Expected modal to have exacly one of either `template` or `templateUrl`');
}
var template = config.template,
controller = config.controller || null,
controllerAs = config.controllerAs,
container = angular.element(config.container || document.body),
element = null,
html,
scope;
if (config.template) {
html = $q.when(config.template);
} else {
html = $http.get(config.templateUrl, {
cache: $templateCache
}).
then(function (response) {
return response.data;
});
}
function activate (locals) {
return html.then(function (html) {
if (!element) {
attach(html, locals);
}
});
}
function attach (html, locals) {
element = angular.element(html);
if (element.length === 0) {
throw new Error('The template contains no elements; you need to wrap text nodes')
}
scope = $rootScope.$new();
if (controller) {
if (!locals) {
locals = {};
}
locals.$scope = scope;
var ctrl = $controller(controller, locals);
if (controllerAs) {
scope[controllerAs] = ctrl;
}
} else if (locals) {
for (var prop in locals) {
scope[prop] = locals[prop];
}
}
$compile(element)(scope);
return $animate.enter(element, container);
}
function deactivate () {
if (!element) {
return $q.when();
}
return $animate.leave(element).then(function () {
scope.$destroy();
scope = null;
element.remove();
element = null;
});
}
function active () {
return !!element;
}
return {
activate: activate,
deactivate: deactivate,
active: active
};
};
}