1// Handles form-submit by preparing to process response
2function handleSubmit()
3{
4	var form = window.openForm || document.getElementById('openForm');
5
6	if (window.parent.openNew && window.parent.baseUrl != null)
7	{
8		window.parent.openFile.setConsumer(null);
9		window.parent.open(window.parent.baseUrl);
10	}
11
12	// NOTE: File is loaded via JS injection into the iframe, which in turn sets the
13	// file contents in the parent window. The new window asks its opener if any file
14	// contents are available or waits for the contents to become available.
15	return true;
16};
17
18// Hides this dialog
19function hideWindow(cancel)
20{
21	window.parent.openFile.cancel(cancel);
22}
23
24function fileChanged()
25{
26	var supportedText = document.getElementById('openSupported');
27	var form = window.openForm || document.getElementById('openForm');
28	var openButton = document.getElementById('openButton');
29
30	if (form.upfile.value.length > 0)
31	{
32		openButton.removeAttribute('disabled');
33	}
34	else
35	{
36		openButton.setAttribute('disabled', 'disabled');
37	}
38}
39
40function main()
41{
42	if (window.parent != null && window.parent.Editor != null)
43	{
44		if (window.parent.Editor.useLocalStorage)
45		{
46			document.body.innerHTML = '';
47			var div = document.createElement('div');
48			div.style.fontFamily = 'Arial';
49
50			window.parent.listBrowserFiles(function(filesInfo)
51			{
52				if (window.parent != null)
53				{
54					if (filesInfo.length == 0)
55					{
56						window.parent.mxUtils.write(div, window.parent.mxResources.get('noFiles'));
57						div.style.color = (window.parent.uiTheme == 'dark') ? '#cccccc' : '';
58						window.parent.mxUtils.br(div);
59					}
60					else
61					{
62						// Sorts the array by filename (titles)
63						filesInfo.sort(function (a, b)
64						{
65							return a.title.toLowerCase().localeCompare(b.title.toLowerCase());
66						});
67
68						var table = document.createElement('table');
69						var hrow = document.createElement('tr');
70						hrow.style.backgroundColor = (window.parent.uiTheme == 'dark') ? '#000' : '#D6D6D6';
71						hrow.style.color = (window.parent.uiTheme == 'dark') ? '#cccccc' : '';
72						hrow.style.height = '25px';
73						hrow.style.textAlign = 'left';
74						table.appendChild(hrow);
75						var hName = document.createElement('th');
76						window.parent.mxUtils.write(hName, window.parent.mxResources.get('name'));
77						hrow.appendChild(hName);
78						var hModified = document.createElement('th');
79						hModified.style.width = '180px';
80						window.parent.mxUtils.write(hModified, window.parent.mxResources.get('lastModified'));
81						hrow.appendChild(hModified);
82						var hSize = document.createElement('th');
83						window.parent.mxUtils.write(hSize, window.parent.mxResources.get('size'));
84						hSize.style.width = '70px';
85						hrow.appendChild(hSize);
86						var hCtrl = document.createElement('th');
87						hCtrl.style.width = '23px';
88						hrow.appendChild(hCtrl);
89						table.style.fontSize = '12pt';
90						table.style.width = '100%';
91
92						for (var i = 0; i < filesInfo.length; i++)
93						{
94							var fileInfo = filesInfo[i];
95
96							if (fileInfo.title.length > 0)
97							{
98								var row = document.createElement('tr');
99								row.style.color = (window.parent.uiTheme == 'dark') ? '#cccccc' : '';
100								table.appendChild(row);
101
102								if (i & 1 == 1)
103								{
104									row.style.backgroundColor = (window.parent.uiTheme == 'dark') ? '#000' : '#E6E6E6';
105								}
106
107								var nameTd = document.createElement('td');
108								row.appendChild(nameTd);
109								var link = document.createElement('a');
110								link.style.fontDecoration = 'none';
111								window.parent.mxUtils.write(link, fileInfo.title);
112								link.style.cursor = 'pointer';
113								nameTd.appendChild(link);
114
115								var modifiedTd = document.createElement('td');
116								row.appendChild(modifiedTd);
117								var str = window.parent.EditorUi.prototype.timeSince(new Date(fileInfo.lastModified));
118
119								if (str == null)
120								{
121									str = window.parent.mxResources.get('lessThanAMinute');
122								}
123
124								window.parent.mxUtils.write(modifiedTd, window.parent.mxResources.get('timeAgo', [str]));
125
126								var sizeTd = document.createElement('td');
127								row.appendChild(sizeTd);
128								window.parent.mxUtils.write(sizeTd, window.parent.EditorUi.prototype.formatFileSize(fileInfo.size));
129
130								var ctrlTd = document.createElement('td');
131								row.appendChild(ctrlTd);
132								ctrlTd.style.textAlign = 'center';
133								var img = document.createElement('span');
134								img.className = 'geSprite geSprite-delete';
135								img.style.cursor = 'pointer';
136								img.style.display = 'inline-block';
137								ctrlTd.appendChild(img);
138
139								if (window.parent.uiTheme == 'dark')
140								{
141									img.style.filter = 'invert(100%)';
142								}
143
144								window.parent.mxEvent.addListener(img, 'click', (function(k)
145								{
146									return function()
147									{
148										if (window.parent.mxUtils.confirm(window.parent.mxResources.get('delete') + ' "' + k + '"?'))
149										{
150											window.parent.deleteBrowserFile(k, function()
151											{
152												window.location.reload();
153											});
154										}
155									};
156								})(fileInfo.title));
157
158								window.parent.mxEvent.addListener(link, 'click', (function(k)
159								{
160									return function()
161									{
162										if (window.parent.openNew && window.parent.baseUrl != null)
163										{
164											var of = window.parent.openFile;
165											window.parent.openBrowserFile(k, function(data)
166											{
167												window.parent.openWindow(window.parent.baseUrl + '#L' + encodeURIComponent(k), function()
168												{
169													of.cancel(false);
170												}, function()
171												{
172													of.setData(data, k);
173												});
174											}, function()
175											{
176												//TODO add error
177											});
178										}
179										else
180										{
181											window.parent.openBrowserFile(k, function(data)
182											{
183												window.parent.openFile.setData(data, k);
184											}, function()
185											{
186												//TODO add error
187											});
188										}
189									};
190								})(fileInfo.title));
191							}
192						}
193
194						div.appendChild(table);
195					}
196
197					var closeButton = window.parent.mxUtils.button(window.parent.mxResources.get('close'), function()
198					{
199						hideWindow(true);
200					});
201
202					closeButton.className = 'geBtn';
203					closeButton.style.position = 'fixed';
204					closeButton.style.bottom = '0px';
205					closeButton.style.right = '0px';
206					div.appendChild(closeButton);
207
208					document.body.appendChild(div);
209				}
210			});
211		}
212		else
213		{
214			var editLink = document.getElementById('editLink');
215			var openButton = document.getElementById('openButton');
216			openButton.value = window.parent.mxResources.get(window.parent.openKey || 'open');
217			var closeButton = document.getElementById('closeButton');
218			closeButton.value = window.parent.mxResources.get('close');
219			var supportedText = document.getElementById('openSupported');
220			supportedText.innerHTML = window.parent.mxResources.get('openSupported');
221			var form = window.openForm || document.getElementById('openForm');
222			form.setAttribute('action', window.parent.OPEN_URL);
223
224			form.onsubmit = function()
225			{
226				return handleSubmit();
227			};
228
229			form.upfile.onchange = fileChanged;
230
231			closeButton.onclick = function()
232			{
233				hideWindow(true);
234			};
235		}
236	}
237	else
238	{
239		document.body.innerHTML = 'Missing parent window';
240	}
241};
242
243window.addEventListener('load', main);
244