forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.ios.ts
More file actions
438 lines (351 loc) · 17 KB
/
dialogs.ios.ts
File metadata and controls
438 lines (351 loc) · 17 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* iOS specific dialogs functions implementation.
*/
import dialogs = require("ui/dialogs");
import dialogs_common = require("ui/dialogs/dialogs-common");
import types = require("utils/types");
import utils = require("utils/utils");
import frame = require("ui/frame");
// merge the exports of the request file with the exports of this file
declare var exports;
require("utils/module-merge").merge(dialogs_common, exports);
class UIAlertViewDelegateImpl extends NSObject implements UIAlertViewDelegate {
public static ObjCProtocols = [UIAlertViewDelegate];
static new(): UIAlertViewDelegateImpl {
return <UIAlertViewDelegateImpl>super.new();
}
private _callback: (view: any, index: number) => void;
public initWithCallback(callback: (view: any, index: number) => void): UIAlertViewDelegateImpl {
this._callback = callback;
return this;
}
public alertViewClickedButtonAtIndex(view, index) {
this._callback(view, index);
}
}
class UIActionSheetDelegateImpl extends NSObject implements UIActionSheetDelegate {
public static ObjCProtocols = [UIActionSheetDelegate];
static new(): UIActionSheetDelegateImpl {
return <UIActionSheetDelegateImpl>super.new();
}
private _callback: (actionSheet: UIActionSheet, index: number) => void;
public initWithCallback(callback: (actionSheet: UIActionSheet, index: number) => void): UIActionSheetDelegateImpl {
this._callback = callback;
return this;
}
public actionSheetClickedButtonAtIndex(actionSheet, index) {
this._callback(actionSheet, index);
}
}
function createUIAlertView(options: dialogs.DialogOptions): UIAlertView {
var alert = new UIAlertView();
alert.title = options && options.title ? options.title : "";
alert.message = options && options.message ? options.message : "";;
return alert;
}
function addButtonsToAlertDialog(alert: UIAlertView, options: dialogs.ConfirmOptions): void {
if (!options) {
return;
}
if (options.cancelButtonText) {
alert.addButtonWithTitle(options.cancelButtonText);
}
if (options.neutralButtonText) {
alert.addButtonWithTitle(options.neutralButtonText);
}
if (options.okButtonText) {
alert.addButtonWithTitle(options.okButtonText);
}
}
function addButtonsToAlertController(alertController: UIAlertController, options: dialogs.ConfirmOptions,
okCallback?: Function, cancelCallback?: Function, neutralCallback?: Function): void {
if (!options) {
return;
}
if (types.isString(options.cancelButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.cancelButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
if (types.isFunction(cancelCallback)) {
cancelCallback();
}
}));
}
if (types.isString(options.neutralButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.neutralButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
if (types.isFunction(cancelCallback)) {
neutralCallback();
}
}));
}
if (types.isString(options.okButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.okButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
if (types.isFunction(okCallback)) {
okCallback();
}
}));
}
}
export function alert(arg: any): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
var options = types.isString(arg) ? { title: dialogs_common.ALERT, okButtonText: dialogs_common.OK, message: arg } : arg;
if (utils.ios.MajorVersion < 8) {
var alert = createUIAlertView(options);
if (options.okButtonText) {
alert.addButtonWithTitle(options.okButtonText);
}
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
var delegate = UIAlertViewDelegateImpl.new().initWithCallback(function (view, index) {
resolve();
// Remove the local variable for the delegate.
delegate = undefined;
});
alert.delegate = delegate;
alert.show();
} else {
var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.UIAlertControllerStyleAlert);
addButtonsToAlertController(alertController, options, () => { resolve(); });
showUIAlertController(alertController);
}
} catch (ex) {
reject(ex);
}
});
}
export function confirm(arg: any): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
var options = types.isString(arg) ? { title: dialogs_common.CONFIRM, okButtonText: dialogs_common.OK, cancelButtonText: dialogs_common.CANCEL, message: arg } : arg;
if (utils.ios.MajorVersion < 8) {
var alert = createUIAlertView(options);
addButtonsToAlertDialog(alert, options);
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
var delegate = UIAlertViewDelegateImpl.new().initWithCallback(function (view, index) {
resolve(index === 2 ? undefined : index === 0);
// Remove the local variable for the delegate.
delegate = undefined;
});
alert.delegate = delegate;
alert.show();
} else {
var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.UIAlertControllerStyleAlert);
addButtonsToAlertController(alertController, options, () => { resolve(true); }, () => { resolve(false) }, () => { resolve(undefined) });
showUIAlertController(alertController);
}
} catch (ex) {
reject(ex);
}
});
}
export function prompt(arg: any): Promise<dialogs.PromptResult> {
var options: dialogs.PromptOptions;
var defaultOptions = {
title: dialogs_common.PROMPT,
okButtonText: dialogs_common.OK,
cancelButtonText: dialogs_common.CANCEL,
inputType: dialogs.inputType.text,
};
if (arguments.length === 1) {
if (types.isString(arg)) {
options = defaultOptions;
options.message = arg;
} else {
options = arg;
}
} else if (arguments.length === 2) {
if (types.isString(arguments[0]) && types.isString(arguments[1])) {
options = defaultOptions;
options.message = arguments[0];
options.defaultText = arguments[1];
}
}
return new Promise<dialogs.PromptResult>((resolve, reject) => {
try {
var textField: UITextField;
if (utils.ios.MajorVersion < 8) {
var alert = createUIAlertView(options);
if (options.inputType === dialogs.inputType.password) {
alert.alertViewStyle = UIAlertViewStyle.UIAlertViewStyleSecureTextInput;
} else {
alert.alertViewStyle = UIAlertViewStyle.UIAlertViewStylePlainTextInput;
}
addButtonsToAlertDialog(alert, options);
textField = alert.textFieldAtIndex(0);
textField.text = types.isString(options.defaultText) ? options.defaultText : "";
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
var delegate = UIAlertViewDelegateImpl.new().initWithCallback(function (view, index) {
resolve({ result: index === 2 ? undefined : index === 0, text: textField.text });
// Remove the local variable for the delegate.
delegate = undefined;
});
alert.delegate = delegate;
alert.show();
} else {
var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.UIAlertControllerStyleAlert);
alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => {
arg.text = types.isString(options.defaultText) ? options.defaultText : "";
arg.secureTextEntry = options && options.inputType === dialogs.inputType.password;
});
textField = alertController.textFields.firstObject;
addButtonsToAlertController(alertController, options,
() => { resolve({ result: true, text: textField.text }); },
() => { resolve({ result: false, text: textField.text }) },
() => { resolve({ result: undefined, text: textField.text }) });
showUIAlertController(alertController);
}
} catch (ex) {
reject(ex);
}
});
}
export function login(arg: any): Promise<dialogs.LoginResult> {
var options: dialogs.LoginOptions;
var defaultOptions = { title: dialogs_common.LOGIN, okButtonText: dialogs_common.OK, cancelButtonText: dialogs_common.CANCEL };
if (arguments.length === 1) {
if (types.isString(arguments[0])) {
options = defaultOptions;
options.message = arguments[0];
} else {
options = arguments[0];
}
} else if (arguments.length === 2) {
if (types.isString(arguments[0]) && types.isString(arguments[1])) {
options = defaultOptions;
options.message = arguments[0];
options.userName = arguments[1];
}
} else if (arguments.length === 3) {
if (types.isString(arguments[0]) && types.isString(arguments[1]) && types.isString(arguments[2])) {
options = defaultOptions;
options.message = arguments[0];
options.userName = arguments[1];
options.password = arguments[2];
}
}
return new Promise<dialogs.LoginResult>((resolve, reject) => {
try {
var userNameTextField: UITextField;
var passwordTextField: UITextField;
if (utils.ios.MajorVersion < 8) {
var alert = createUIAlertView(options);
alert.alertViewStyle = UIAlertViewStyle.UIAlertViewStyleLoginAndPasswordInput;
addButtonsToAlertDialog(alert, options);
userNameTextField = alert.textFieldAtIndex(0);
userNameTextField.text = types.isString(options.userName) ? options.userName : "";
userNameTextField = alert.textFieldAtIndex(1);
userNameTextField.text = types.isString(options.password) ? options.password : "";
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
var delegate = UIAlertViewDelegateImpl.new().initWithCallback(function (view, index) {
resolve({ result: index === 2 ? undefined : index === 0, userName: userNameTextField.text, password: userNameTextField.text });
// Remove the local variable for the delegate.
delegate = undefined;
});
alert.delegate = delegate;
alert.show();
} else {
var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.UIAlertControllerStyleAlert);
alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => {
arg.placeholder = "Login";
arg.text = types.isString(options.userName) ? options.userName : "";
});
alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => {
arg.placeholder = "Password";
arg.secureTextEntry = true;
arg.text = types.isString(options.password) ? options.password : "";
});
userNameTextField = alertController.textFields.firstObject;
passwordTextField = alertController.textFields.lastObject;
addButtonsToAlertController(alertController, options,
() => { resolve({ result: true, userName: userNameTextField.text, password: passwordTextField.text }); },
() => { resolve({ result: false, userName: userNameTextField.text, password: passwordTextField.text }); },
() => { resolve({ result: undefined, userName: userNameTextField.text, password: passwordTextField.text }); });
showUIAlertController(alertController);
}
} catch (ex) {
reject(ex);
}
});
}
function showUIAlertController(alertController: UIAlertController) {
var topMostFrame = frame.topmost();
if (topMostFrame) {
var viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
if (viewController) {
viewController.presentModalViewControllerAnimated(alertController, true);
}
}
}
export function action(arg: any): Promise<string> {
var options: dialogs.ActionOptions;
var defaultOptions = { cancelButtonText: dialogs_common.CANCEL };
if (arguments.length === 1) {
if (types.isString(arguments[0])) {
options = defaultOptions;
options.message = arguments[0];
} else {
options = arguments[0];
}
} else if (arguments.length === 2) {
if (types.isString(arguments[0]) && types.isString(arguments[1])) {
options = defaultOptions;
options.message = arguments[0];
options.cancelButtonText = arguments[1];
}
} else if (arguments.length === 3) {
if (types.isString(arguments[0]) && types.isString(arguments[1]) && types.isDefined(arguments[2])) {
options = defaultOptions;
options.message = arguments[0];
options.cancelButtonText = arguments[1];
options.actions = arguments[2];
}
}
return new Promise<string>((resolve, reject) => {
try {
var i: number;
var action: string;
if (utils.ios.MajorVersion < 8) {
var actionSheet = new UIActionSheet();
if (types.isString(options.message)) {
actionSheet.title = options.message;
}
if (options.actions) {
for (i = 0; i < options.actions.length; i++) {
action = options.actions[i];
if (types.isString(action)) {
actionSheet.addButtonWithTitle(action);
}
}
}
if (types.isString(options.cancelButtonText)) {
actionSheet.addButtonWithTitle(options.cancelButtonText);
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
}
var delegate = UIActionSheetDelegateImpl.new().initWithCallback(function (sender: UIActionSheet, index: number) {
resolve(sender.buttonTitleAtIndex(index));
delegate = undefined;
});
actionSheet.delegate = delegate;
actionSheet.showInView(UIApplication.sharedApplication().keyWindow);
} else {
var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.message, "", UIAlertControllerStyle.UIAlertControllerStyleActionSheet);
if (options.actions) {
for (i = 0; i < options.actions.length; i++) {
action = options.actions[i];
if (types.isString(action)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(action, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
resolve(arg.title);
}));
}
}
}
if (types.isString(options.cancelButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.cancelButtonText, UIAlertActionStyle.UIAlertActionStyleCancel, (arg: UIAlertAction) => {
resolve(arg.title);
}));
}
showUIAlertController(alertController);
}
} catch (ex) {
reject(ex);
}
});
}