summaryrefslogtreecommitdiffstats
path: root/browser/src/control/IFrameDialog.js
blob: a573a809b623a00970cbc96501b44a811f4c0cc1 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- js-indent-level: 8 -*- */
/*
 * L.IFrameDialog
 */

L.IFrameDialog = L.Class.extend({

	options: {
		prefix: 'iframe-none',
		method: 'get'
	},

	initialize: function (url, params, element, options) {
		var content, form;

		this._loading = false;
		L.setOptions(this, options);

		if (this.options.prefix==='div-infobar') {
			this._container = L.DomUtil.create('div', this.options.prefix + '-wrap snackbar jsdialog-container');
			content = L.DomUtil.create('div', this.options.prefix + '-content lokdialog ui-dialog-content', this._container);
		}
		else {
			this._container = L.DomUtil.create('div', this.options.prefix + '-wrap');
			content = L.DomUtil.create('div', this.options.prefix + '-content', this._container);
		}
		this._container.style.display = 'none';

		form = L.DomUtil.create('form', '', content);

		this.fillParams(url, params, form);

		this._iframe = L.DomUtil.create('iframe', this.options.prefix + '-modal', content);
		this._iframe.name = form.target;

		if (this.options.id) {
			this._iframe.id = this.options.id;
		}

		if (element) {
			document.body.insertBefore(this._container, element);
		} else {
			document.body.appendChild(this._container);
		}

		form.submit();
		this._iframe.addEventListener('load', L.bind(this.onLoad, this));
	},

	fillParams: function (url, params, form) {
		var input, keys;
		form.action = url;
		form.target = this.options.prefix + '-form';
		form.method = this.options.method;

		for (var item in params) {
			keys = Object.keys(params[item]);
			if (keys.length > 0) {
				input = L.DomUtil.create('input', '', form);
				input.type = 'hidden';
				input.name = String(keys[0]);
				input.value = String(params[item][keys[0]]);
			}
		}
	},

	onLoad: function () {
		var msg = this.options.prefix + '-load';
		var that = this;
		this._loading = true;
		this._errorTimer = setTimeout(function () {
			if (!that.isVisible()) {
				window.postMessage('{"MessageId":"' + msg + '"}');
			}
		}, 1000);
	},

	clearTimeout: function ()
	{
		clearTimeout(this._errorTimer);
	},

	remove: function () {
		L.DomEvent.off(this._iframe, 'load', this.onLoad, this);
		L.DomUtil.remove(this._container);
		this._container = this._iframe = null;
	},

	hasLoaded: function () {
		return this.queryContainer() && this._loading;
	},

	queryContainer: function () {
		return document.body.querySelector('.' + this.options.prefix + '-wrap');
	},

	postMessage: function (msg) {
		this._iframe.contentWindow.postMessage(JSON.stringify(msg), '*');
	},

	isVisible: function () {
		var elem = this.queryContainer();
		return elem && elem.style.display !== 'none';
	},

	show: function () {
		this._container.style.display = '';
	}
});

// Close when pressing Escape
window.addEventListener('keyup', function iframeKeyupListener (e) {
	if (e.keyCode === 27 || e.key === 'Escape') {
		window.postMessage('{"MessageId":"welcome-close"}', '*');
	}
});

L.iframeDialog = function (url, params, element, options) {
	return new L.IFrameDialog(url, params, element, options);
};