Skip to content

Commit eedb44f

Browse files
committed
Replace GeoJSON upload prompt with modal widget
Signed-off-by: Daniel Kastl <[email protected]>
1 parent 4df2544 commit eedb44f

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% if Setting.plugin_redmine_gtt['enable_geojson_upload_on_issue_map'] == "true" %>
2+
<div id="dialog-geojson-upload" title="<%= l(:title_geojson_upload) %>">
3+
<textarea placeholder="<%= l(:placeholder_geojson_upload) %>" class="ui-widget ui-state-default ui-corner-all"></textarea>
4+
<input type="file" id="file-selector" accept=".json,.geojson">
5+
</div>
6+
<% end %>

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ en:
8686

8787
project_module_gtt: "GTT"
8888
permission_manage_gtt_settings: "Manage GTT settings"
89+
90+
title_geojson_upload: "Import GeoJSON"
91+
placeholder_geojson_upload: "Please paste a GeoJSON geometry here, or import the GeoJSON data from a file."

lib/redmine_gtt/view_hooks.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ class ViewHooks < Redmine::Hook::ViewListener
1313

1414
render_on :view_issues_form_details_top,
1515
partial: 'redmine_gtt/hooks/view_issues_form_details_top'
16+
17+
render_on :view_layouts_base_body_bottom,
18+
partial: 'redmine_gtt/hooks/view_layouts_base_body_bottom'
19+
1620
end
1721
end

src/components/gtt-client.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -486,24 +486,61 @@ export class GttClient {
486486
editbar.addControl(control)
487487
})
488488

489-
// Upload button
490-
if (this.contents.upload === "true") {
491-
editbar.addControl(new Button({
492-
html: '<i class="material-icons">file_upload</i>',
493-
title: 'Upload GeoJSON',
494-
handleClick: () => {
495-
const data = prompt("Please paste a GeoJSON geometry here")
489+
// Uses jQuery UI for GeoJSON Upload modal window
490+
const mapObj = this
491+
const dialog = $("#dialog-geojson-upload").dialog({
492+
autoOpen: false,
493+
resizable: true,
494+
height: 'auto',
495+
width: 380,
496+
modal: true,
497+
buttons: {
498+
'Load': function() {
499+
const geojson_input = document.querySelector('#dialog-geojson-upload textarea') as HTMLInputElement
500+
const data = geojson_input.value
496501
if (data !== null) {
497502
const features = new GeoJSON().readFeatures(
498503
JSON.parse(data), {
499504
featureProjection: 'EPSG:3857'
500505
}
501506
)
502-
this.vector.getSource().clear()
503-
this.vector.getSource().addFeatures(features)
504-
this.updateForm(features)
505-
this.zoomToExtent()
507+
mapObj.vector.getSource().clear()
508+
mapObj.vector.getSource().addFeatures(features)
509+
mapObj.updateForm(features)
510+
mapObj.zoomToExtent()
506511
}
512+
$(this).dialog('close')
513+
},
514+
'Cancel': function() {
515+
$(this).dialog('close')
516+
}
517+
}
518+
});
519+
520+
// Upload button
521+
if (this.contents.upload === "true") {
522+
523+
const fileSelector = document.getElementById('file-selector')
524+
fileSelector.addEventListener('change', (event: any) => {
525+
const file = event.target.files[0]
526+
// Check if the file is GeoJSON.
527+
if (file.type && !file.type.startsWith('application/geo')) {
528+
console.log('File is not a GeoJSON document.', file.type, file);
529+
return;
530+
}
531+
const fileReader = new FileReader();
532+
fileReader.addEventListener('load', (event: any) => {
533+
const geojson_input = document.querySelector('#dialog-geojson-upload textarea') as HTMLInputElement
534+
geojson_input.value = JSON.stringify(event.target.result, null, 2)
535+
});
536+
fileReader.readAsText(file);
537+
});
538+
539+
editbar.addControl(new Button({
540+
html: '<i class="material-icons">file_upload</i>',
541+
title: 'Upload GeoJSON',
542+
handleClick: () => {
543+
dialog.dialog('open')
507544
}
508545
}))
509546
}

src/stylesheets/app.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,16 @@ i[id^='icon_settings_tracker_'] {
106106
max-width: 510px;
107107
padding: 0.5em;
108108
}
109+
110+
.ui-dialog .ui-dialog-buttonpane button {
111+
height: inherit;
112+
}
113+
114+
.ui-dialog .ui-dialog-content {
115+
padding: 0.5em 0 0 0;
116+
}
117+
118+
.ui-dialog .ui-dialog-content textarea {
119+
width: 100%;
120+
height: 300px;
121+
}

0 commit comments

Comments
 (0)