summaryrefslogtreecommitdiffstats
path: root/loleaflet/src/control/Control.AlertDialog.js
blob: 630e5a63ced89ddb9840b31245d98b89ed0865dd (plain)
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
/* -*- js-indent-level: 8 -*- */
/*
 * L.Control.Dialog used for displaying alerts
 */

/* global _ vex sanitizeUrl */
L.Control.AlertDialog = L.Control.extend({
	onAdd: function (map) {
		// TODO: Better distinction between warnings and errors
		map.on('error', this._onError, this);
		map.on('warn', this._onError, this);
	},

	_onError: function(e) {
		if (!this._map._fatal) {
			// TODO. queue message errors and pop-up dialogs
			// Close other dialogs before presenting a new one.
			vex.closeAll();
		}

		if (e.msg) {
			if (window.ThisIsAMobileApp && this._map._fatal) {
				var buttonsList = [];
				buttonsList.push({
					text: _('Close'),
					type: 'button',
					className: 'vex-dialog-button-primary',
					click: function openClick () {
						window.postMobileMessage('BYE');
						vex.closeAll();
					}
				});

				vex.dialog.alert({
					message: e.msg,
					buttons: buttonsList,
					callback: function() {},
				});
			}
			else
				vex.dialog.alert(e.msg);
		}
		else if (e.cmd == 'load' && e.kind == 'docunloading') {
			// Handled by transparently retrying.
			return;
		} else if (e.cmd == 'openlink') {
			var url = e.url;
			var messageText = window.errorMessages.leaving;

			var isLinkValid = sanitizeUrl.sanitizeUrl(url) !== 'about:blank';

			if (!isLinkValid) {
				messageText = window.errorMessages.invalidLink;
			}

			var containerWithLink = document.createElement('div');
			containerWithLink.innerHTML = messageText;
			var externalUrl = document.createElement('p');
			externalUrl.classList.add('vex-dialog-external-url');
			externalUrl.innerHTML = url;
			containerWithLink.appendChild(externalUrl);
			buttonsList = [];

			if (isLinkValid) {
				buttonsList.push({
					text: _('Open link'),
					type: 'button',
					className: 'vex-dialog-button-primary',
					click: function openClick () {
						window.open(url, '_blank');
						vex.closeAll();
					}
				});
			}

			vex.dialog.open({
				unsafeMessage: containerWithLink.outerHTML,
				showCloseButton: true,
				contentClassName: 'word-wrap-for-vex-dialog',
				buttons: buttonsList,
				callback: function() {},
				afterClose: function () {
					vex.dialogID = -1;
					e.map.focus();
				}
			});
		} else if (e.cmd && e.kind) {
			this._map.fire('hidebusy');

			var msg = _('The server encountered a %0 error while parsing the %1 command.');
			msg = msg.replace('%0', e.kind);
			msg = msg.replace('%1', e.cmd);
			vex.dialog.alert(msg);
		}
	}
});

L.control.alertDialog = function (options) {
	return new L.Control.AlertDialog(options);
};