Skip to content

Commit

Permalink
Enable inline editing for LEaflet GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Dec 13, 2019
1 parent b7f3a60 commit 0975f29
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ different releases and which versions of PHP and MediaWiki they support, see the

## Mps 7.13.0

* The GeoJSON editor now shows in #display_maps and #ask for Leaflet maps using the geojson parameter.
* Removed the need to manually include `Maps_Settings.php` in `LocalSettings.php` when modifying maps settings.
* Improved compatibility with MediaWiki 1.35

## Maps 7.12.2

Expand Down
19 changes: 18 additions & 1 deletion resources/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@
return deferred.promise();
}

function getLatestRevision(pageName) {
let deferred = $.Deferred();

new mw.Api().post({
action: 'query',
prop: 'revisions',
rvlimit: 1,
rvprop: [ 'ids', 'content' ],
titles: pageName
}).done(function(response) {
deferred.resolve(response.query.pages[Object.keys(response.query.pages)[0]].revisions[0]);
});

return deferred.promise();
}

if (!window.maps) {window.maps = {};}

window.maps.api = {
canEditPage: canEditPage
canEditPage: canEditPage,
getLatestRevision: getLatestRevision
};
})(window.jQuery, window.mediaWiki);
52 changes: 33 additions & 19 deletions resources/leaflet/jquery.leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

$.fn.leafletmaps = function ( options ) {
let _this = this;
_this.options = options; // needed for LeafletAjax.js
_this.options = options; // needed by LeafletAjax.js

this.setup = function() {
this.map = L.map( this.get(0), getMapOptions(options) );
Expand All @@ -51,7 +51,7 @@
this.applyResizable();
this.bindAjaxEvents();

//this.maybeAddEditButton();
this.maybeAddEditButton();
};

this.maybeAddEditButton = function() {
Expand All @@ -75,28 +75,42 @@
this.addEditButton = function() {
this.editButton = L.easyButton(
'<img src="' + mw.config.get('egMapsScriptPath') + 'resources/leaflet/images/edit-solid.svg">',
function() {
_this.removeEditButton();
_this.mapContent.remove();

let editor = _this.getEditor();
editor.initialize(options.geojson);
this.startEditMode,
mw.msg('maps-editor-edit-geojson')
).addTo(this.map);
};

// TODO: edit conflict / old revision detection
this.startEditMode = function() {
_this.removeEditButton();
_this.mapContent.remove();

editor.onSaved(function() {
maps.api.getLatestRevision('GeoJson:' + options.GeoJsonSource).done(
function(revision) {
if (revision.revid === options.GeoJsonRevisionId) {
_this.initializeEditor(options.geojson);
}
else {
_this.purgePage();
_this.initializeEditor(JSON.parse(revision['*']));
}
}
);
};

editor.remove();
options.geojson = editor.getLayer().toGeoJSON();
_this.mapContent = maps.leaflet.FeatureBuilder.contentLayerFromOptions(options).addTo(_this.map);
this.initializeEditor = function(geoJson) {
let editor = _this.getEditor();
editor.initialize(geoJson);

alert(mw.msg('maps-json-editor-changes-saved'));
_this.addEditButton();
});
},
mw.msg('maps-editor-edit-geojson')
).addTo(this.map);
editor.onSaved(function() {
_this.purgePage();

editor.remove();
options.geojson = editor.getLayer().toGeoJSON();
_this.mapContent = maps.leaflet.FeatureBuilder.contentLayerFromOptions(options).addTo(_this.map);

alert(mw.msg('maps-json-editor-changes-saved'));
_this.addEditButton();
});
};

this.getEditor = function() {
Expand Down
3 changes: 3 additions & 0 deletions src/DataAccess/GeoJsonFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function fetch( string $fileLocation ) {
if ( $content instanceof \JsonContent ) {
return new GeoJsonFetcherResult(
$this->normalizeJson( $content->getNativeData() ),
$revision->getId(),
$title
);
}
Expand All @@ -58,6 +59,7 @@ public function fetch( string $fileLocation ) {
try {
return new GeoJsonFetcherResult(
$this->normalizeJson( $this->fileFetcher->fetchFile( $fileLocation ) ),
null,
null
);
}
Expand All @@ -69,6 +71,7 @@ public function fetch( string $fileLocation ) {
private function newEmptyResult(): GeoJsonFetcherResult {
return new GeoJsonFetcherResult(
[],
null,
null
);
}
Expand Down
8 changes: 7 additions & 1 deletion src/DataAccess/GeoJsonFetcherResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
class GeoJsonFetcherResult {

private $content;
private $revisionId;
private $source;

public function __construct( array $content, ?\TitleValue $source ) {
public function __construct( array $content, ?int $revisionId, ?\TitleValue $source ) {
$this->content = $content;
$this->revisionId = $revisionId;
$this->source = $source;
}

Expand All @@ -22,4 +24,8 @@ public function getTitleValue(): ?\TitleValue {
return $this->source;
}

public function getRevisionId(): ?int {
return $this->revisionId;
}

}
1 change: 1 addition & 0 deletions src/LeafletService.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function processingResultToMapParams( ProcessingResult $processingResult

$mapParams['geojson'] = $result->getContent();
$mapParams['GeoJsonSource'] = $result->getTitleValue() === null ? null : $result->getTitleValue()->getText();
$mapParams['GeoJsonRevisionId'] = $result->getRevisionId();
}

return $mapParams;
Expand Down

0 comments on commit 0975f29

Please sign in to comment.