-
Notifications
You must be signed in to change notification settings - Fork 38
/
dialog.ts
144 lines (119 loc) · 3.73 KB
/
dialog.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {assign, defaults} from 'lodash';
import angular from 'angular';
import {registerDialog} from 'dialog-polyfill';
import {inject} from '../../core';
let instance = {scope: null, dialog: null};
export interface IDialog extends ng.IPromise<any> {
element(): Object;
scope(): Object;
}
export interface IDialogOptions {
html: string;
title?: string;
subTitle?: string;
icon?: string;
showCloseAction?: boolean;
onConfirm?(scope): any;
}
function createScope(deferred, scope?, locals?) {
scope = scope ? scope.$new() : inject('$rootScope').$new(true);
if (locals) {
assign(scope, locals);
}
scope.dialogEvents = {
reject() {
deferred.reject();
},
resolve() {
const res = this.onConfirm(scope);
if (res && res.then) {
scope.dialogError = null;
scope.dialogResolving = true;
res
.then(r => deferred.resolve(r, scope), e => scope.dialogError = e)
.finally(() => scope.dialogResolving = false);
} else {
deferred.resolve(scope);
}
}
};
return scope;
}
function showDialog(scope, htmlOrOptions: string | IDialogOptions) {
let options: IDialogOptions;
if (typeof htmlOrOptions === 'string') {
options = {
html: htmlOrOptions
}
} else {
options = {
...htmlOrOptions,
html: `
<dialog class="bi-theme--lighter">
<dialog-title class="bi-align bi-s-h">
${htmlOrOptions.icon ? `
<i
class="bi-dialog-icon bi-icon"
ng-class="dialogOptions.iconClass"
>${htmlOrOptions.icon}</i>
` : ''}
<span>${htmlOrOptions.title}</span>
</dialog-title>
${htmlOrOptions.subTitle ? `<dialog-subtitle class="bi-muted">${htmlOrOptions.subTitle}</dialog-subtitle>` : ''}
<dialog-content>
<div class="bi-align bi-s-h">
<div>${htmlOrOptions.html}</div>
</div>
</dialog-content>
</dialog>
`
};
}
options = defaults({}, options, {
showCloseAction: true
});
const element = angular.element(options.html)
.removeAttr('ng-non-bindable')
.addClass('bi-dialog')
.prepend(`
<span class="bi-dialog-close" ng-if="dialogOptions.showCloseAction">
<i class="bi-action bi-icon" ng-click="dialogEvents.reject()">close</i>
</span>
<span class="bi-dialog-loader bi-spinner" ng-if="dialogResolving"><span>
`)
.append(`
<dialog-error
class="bi-danger"
ng-if="error"
>{{dialogError.data && dialogError.data.errorDescription || 'Unknown error'}}</dialog-error>
`);
scope.dialogEvents.onConfirm = options.onConfirm || (() => inject('$parse')(element.attr('on-confirm'))(scope));
const dialog = inject('$compile')(element)(assign(scope, {dialogOptions: options}))
.on('close', () => scope.dialogEvents.reject())
.appendTo(window.document.body)
.get(0);
registerDialog(dialog);
inject('$timeout')(() => dialog.showModal());
return (instance = {scope, dialog});
}
function destroy({scope, dialog}) {
if (scope) {
scope.$destroy();
}
if (dialog) {
if (dialog.getAttribute('open') !== null) {
dialog.close();
}
dialog.remove();
}
instance = {scope: null, dialog: null};
}
export default function(htmlOrOptions: string | IDialogOptions, parentScope?, locals?): IDialog {
destroy(instance);
const deferred = ((inject('$q') as ng.IQService)).defer();
const {scope, dialog} = showDialog(createScope(deferred, parentScope, locals), htmlOrOptions);
const promise = deferred.promise.finally(() => destroy({scope, dialog})) as IDialog;
promise.element = () => angular.element(dialog);
promise.scope = () => scope;
return promise;
}