Skip to content

Commit b6a3e8f

Browse files
Merge pull request #18654 from MoonE/gis-reduce-exported-functions
Reduce amount of exported function from gis_data_editor.ts
2 parents 4737adb + fdd666a commit b6a3e8f

3 files changed

Lines changed: 35 additions & 48 deletions

File tree

‎js/src/gis_data_editor.ts‎

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ajaxShowMessage } from './modules/ajax-message.ts';
1010
*
1111
*/
1212

13-
window.gisEditorLoaded = false;
13+
let gisEditorLoaded = false;
1414

1515
/**
1616
* Closes the GIS data editor and perform necessary clean up work.
@@ -86,42 +86,30 @@ function initGISEditorVisualization () {
8686
/**
8787
* Loads JavaScript files and the GIS editor.
8888
*
89-
* @param value current value of the geometry field
90-
* @param field field name
91-
* @param type geometry type
92-
* @param inputName name of the input field
93-
* @param token token
89+
* @param {function} resolve
9490
*/
95-
function loadJSAndGISEditor (value, field, type, inputName) {
96-
var head = document.getElementsByTagName('head')[0];
97-
var script;
91+
function loadJSAndGISEditor (resolve) {
92+
let script;
9893

9994
script = document.createElement('script');
10095
script.src = 'js/dist/table/gis_visualization.js';
101-
head.appendChild(script);
96+
document.head.appendChild(script);
10297

10398
// OpenLayers.js is BIG and takes time. So asynchronous loading would not work.
10499
// Load the JS and do a callback to load the content for the GIS Editor.
105100
script = document.createElement('script');
101+
script.src = 'js/vendor/openlayers/OpenLayers.js';
102+
script.addEventListener('load', function () {
103+
resolve();
104+
});
106105

107-
script.onreadystatechange = function () {
108-
if (this.readyState === 'complete') {
109-
loadGISEditor(value, field, type, inputName);
110-
}
111-
};
112-
113-
script.onload = function () {
114-
loadGISEditor(value, field, type, inputName);
115-
};
116-
117-
script.onerror = function () {
118-
loadGISEditor(value, field, type, inputName);
119-
};
106+
script.addEventListener('error', function () {
107+
resolve();
108+
});
120109

121-
script.src = 'js/vendor/openlayers/OpenLayers.js';
122-
head.appendChild(script);
110+
document.head.appendChild(script);
123111

124-
window.gisEditorLoaded = true;
112+
gisEditorLoaded = true;
125113
}
126114

127115
/**
@@ -153,10 +141,7 @@ function loadGISEditor (value, field, type, inputName) {
153141
}, 'json');
154142
}
155143

156-
/**
157-
* Opens up the dialog for the GIS data editor.
158-
*/
159-
function openGISEditor () {
144+
function openGISEditorInternal () {
160145
// Center the popup
161146
var windowWidth = document.documentElement.clientWidth;
162147
var windowHeight = document.documentElement.clientHeight;
@@ -183,6 +168,24 @@ function openGISEditor () {
183168
$gisEditor.fadeIn('fast');
184169
}
185170

171+
/**
172+
* Opens up the dialog for the GIS data editor.
173+
*
174+
* @param value current value of the geometry field
175+
* @param field field name
176+
* @param type geometry type
177+
* @param inputName name of the input field
178+
*/
179+
function openGISEditor (value, field, type, inputName) {
180+
openGISEditorInternal();
181+
182+
if (gisEditorLoaded) {
183+
loadGISEditor(value, field, type, inputName);
184+
} else {
185+
loadJSAndGISEditor(loadGISEditor.bind(this, value, field, type, inputName));
186+
}
187+
}
188+
186189
/**
187190
* Prepare and insert the GIS data in Well Known Text format
188191
* to the input field.
@@ -397,13 +400,8 @@ AJAX.registerOnload('gis_data_editor.js', function () {
397400

398401
declare global {
399402
interface Window {
400-
gisEditorLoaded: boolean;
401-
loadJSAndGISEditor: typeof loadJSAndGISEditor;
402-
loadGISEditor: typeof loadGISEditor;
403403
openGISEditor: typeof openGISEditor;
404404
}
405405
}
406406

407-
window.loadJSAndGISEditor = loadJSAndGISEditor;
408-
window.loadGISEditor = loadGISEditor;
409407
window.openGISEditor = openGISEditor;

‎js/src/table/change.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,7 @@ AJAX.registerOnload('table/change.js', function () {
589589
// Names of input field and null checkbox
590590
var inputName = $span.parent('td').children('input[type=\'text\']').attr('name');
591591

592-
window.openGISEditor();
593-
if (! window.gisEditorLoaded) {
594-
window.loadJSAndGISEditor(value, field, type, inputName);
595-
} else {
596-
window.loadGISEditor(value, field, type, inputName);
597-
}
592+
window.openGISEditor(value, field, type, inputName);
598593
});
599594

600595
/**

‎js/src/table/select.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,8 @@ AJAX.registerOnload('table/select.js', function () {
273273

274274
// Names of input field and null checkbox
275275
var inputName = $span.parent('td').children('input[type=\'text\']').attr('name');
276-
// Token
277276

278-
window.openGISEditor();
279-
if (! window.gisEditorLoaded) {
280-
window.loadJSAndGISEditor(value, field, type, inputName);
281-
} else {
282-
window.loadGISEditor(value, field, type, inputName);
283-
}
277+
window.openGISEditor(value, field, type, inputName);
284278
});
285279

286280
/**

0 commit comments

Comments
 (0)