-
Notifications
You must be signed in to change notification settings - Fork 31
/
dialogs.ts
72 lines (63 loc) · 2.41 KB
/
dialogs.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
import { Channel } from './channel'
import {
DialogsAPI,
OpenCustomWidgetOptions,
OpenAlertOptions,
OpenConfirmOptions,
IdsAPI,
} from './types'
const isObject = (o: any) => typeof o === 'object' && o !== null && !Array.isArray(o)
const prepareOptions = (options: any) => (isObject(options) ? options : {})
export default function createDialogs(channel: Channel, ids: IdsAPI): DialogsAPI {
return {
openAlert: openSimpleDialog.bind(null, 'alert'),
openConfirm: openSimpleDialog.bind(null, 'confirm'),
openPrompt: openSimpleDialog.bind(null, 'prompt'),
openExtension: openExtensionDialog,
openCurrentApp: openCurrentAppDialog,
openCurrent: openCurrentDialog,
selectSingleEntry: openEntitySelector.bind(null, 'Entry', false),
selectSingleAsset: openEntitySelector.bind(null, 'Asset', false),
selectMultipleEntries: openEntitySelector.bind(null, 'Entry', true),
selectMultipleAssets: openEntitySelector.bind(null, 'Asset', true),
}
function openSimpleDialog(type: string, options?: OpenAlertOptions | OpenConfirmOptions) {
return channel.call('openDialog', type, prepareOptions(options)) as Promise<any>
}
function openExtensionDialog(optionsInput?: OpenCustomWidgetOptions) {
let options = prepareOptions(optionsInput)
// Use provided ID, default to the current extension.
options = { ...options, id: options.id || ids.extension }
if (options.id) {
return channel.call('openDialog', 'extension', options)
} else {
throw new Error('Extension ID not provided.')
}
}
function openCurrentDialog(options?: Omit<OpenCustomWidgetOptions, 'id'>) {
if (ids.app) {
return openCurrentAppDialog(options)
} else {
return openExtensionDialog({
...options,
id: ids.extension,
})
}
}
function openCurrentAppDialog(options?: Omit<OpenCustomWidgetOptions, 'id'>) {
options = prepareOptions(options)
if (ids.app) {
// Force ID of the current app.
const withForcedId = { ...options, id: ids.app }
return channel.call('openDialog', 'app', withForcedId)
} else {
throw new Error('Not in the app context.')
}
}
function openEntitySelector(entityType: string, multiple: boolean, options?: any) {
options = prepareOptions(options)
options.entityType = entityType
options.multiple = multiple
return channel.call('openDialog', 'entitySelector', options) as Promise<any>
}
}