@@ -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
398401declare 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 ;
409407window . openGISEditor = openGISEditor ;
0 commit comments