Skip to content

Commit

Permalink
move startExport to DistortableCollection.Edit (#384)
Browse files Browse the repository at this point in the history
* move startexport TO DistortableCollection.Edit

* bump to 0.7.2
  • Loading branch information
sashadev-sky committed Sep 15, 2019
1 parent e6f9001 commit ef89b16
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 142 deletions.
136 changes: 66 additions & 70 deletions dist/leaflet.distortableimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,66 +853,6 @@ L.DistortableCollection = L.FeatureGroup.extend({
json.avg_cm_per_pixel = this._getAvgCmPerPixel(json.images);

return json;
},

startExport: function(opts) {
opts = opts || {};
opts.collection = opts.collection || this.generateExportJson();
opts.frequency = opts.frequency || 3000;
opts.scale = opts.scale || 100; // switch it to _getAvgCmPerPixel !
var statusUrl, updateInterval;

// this may be overridden to update the UI to show export progress or completion
function _defaultUpdater(data) {
data = JSON.parse(data);
// optimization: fetch status directly from google storage:
if (statusUrl !== data.status_url && data.status_url.match('.json')) { statusUrl = data.status_url; }
if (data.status === "complete") {
clearInterval(updateInterval);
}
if (data.status === 'complete' && data.jpg !== null) {
alert("Export succeeded. http://export.mapknitter.org/" + data.jpg);
}
// TODO: update to clearInterval when status == "failed" if we update that in this file:
// https://github.com/publiclab/mapknitter-exporter/blob/main/lib/mapknitterExporter.rb
console.log(data);
}

// receives the URL of status.json, and starts running the updater to repeatedly fetch from status.json;
// this may be overridden to integrate with any UI
function _defaultHandleStatusUrl(data) {
console.log(data);
statusUrl = "//export.mapknitter.org" + data;
opts.updater = opts.updater || _defaultUpdater;

// repeatedly fetch the status.json
updateInterval = setInterval(function intervalUpdater() {
$.ajax(statusUrl + "?" + Date.now(), { // bust cache with timestamp
type: "GET",
crossDomain: true
}).done(function(data) {
opts.updater(data);
});
}, opts.frequency);
}

function _fetchStatusUrl(collection, scale) {
opts.handleStatusUrl = opts.handleStatusUrl || _defaultHandleStatusUrl;

$.ajax({
url: "//export.mapknitter.org/export",
crossDomain: true,
type: "POST",
data: {
collection: JSON.stringify(collection.images),
scale: scale
},
success: opts.handleStatusUrl // this handles the initial response
});
}

_fetchStatusUrl(opts.collection, opts.scale);

}
});

Expand Down Expand Up @@ -1761,8 +1701,7 @@ L.distortableImage = L.DistortableImage;
},

addHooks: function () {
var edit = this._overlay;

var edit = this._overlay.editing;
edit.startExport();
}
});
Expand All @@ -1782,8 +1721,7 @@ L.distortableImage = L.DistortableImage;
},

addHooks: function() {
var edit = this._overlay;

var edit = this._overlay.editing;
edit._removeGroup();
}
});
Expand All @@ -1803,8 +1741,7 @@ var Locks = L.EditAction.extend({
},

addHooks: function () {
var edit = this._overlay;

var edit = this._overlay.editing;
edit._lockGroup();
}
});
Expand All @@ -1824,8 +1761,7 @@ var Unlocks = L.EditAction.extend({
},

addHooks: function () {
var edit = this._overlay;

var edit = this._overlay.editing;
edit._unlockGroup();
}
});
Expand Down Expand Up @@ -2646,15 +2582,75 @@ L.DistortableCollection.Edit = L.Handler.extend({
if (e) { L.DomEvent.stopPropagation(e); }
},

startExport: function(opts) {
opts = opts || {};
opts.collection = opts.collection || this._group.generateExportJson();
opts.frequency = opts.frequency || 3000;
opts.scale = opts.scale || 100; // switch it to _getAvgCmPerPixel !
var statusUrl, updateInterval;

// this may be overridden to update the UI to show export progress or completion
function _defaultUpdater(data) {
data = JSON.parse(data);
// optimization: fetch status directly from google storage:
if (statusUrl !== data.status_url && data.status_url.match('.json')) { statusUrl = data.status_url; }
if (data.status === "complete") {
clearInterval(updateInterval);
}
if (data.status === 'complete' && data.jpg !== null) {
alert("Export succeeded. http://export.mapknitter.org/" + data.jpg);
}
// TODO: update to clearInterval when status == "failed" if we update that in this file:
// https://github.com/publiclab/mapknitter-exporter/blob/main/lib/mapknitterExporter.rb
console.log(data);
}

// receives the URL of status.json, and starts running the updater to repeatedly fetch from status.json;
// this may be overridden to integrate with any UI
function _defaultHandleStatusUrl(data) {
console.log(data);
statusUrl = "//export.mapknitter.org" + data;
opts.updater = opts.updater || _defaultUpdater;

// repeatedly fetch the status.json
updateInterval = setInterval(function intervalUpdater() {
$.ajax(statusUrl + "?" + Date.now(), { // bust cache with timestamp
type: "GET",
crossDomain: true,
}).done(function(data) {
opts.updater(data);
});
}, opts.frequency);
}

function _fetchStatusUrl(collection, scale) {
opts.handleStatusUrl = opts.handleStatusUrl || _defaultHandleStatusUrl;

$.ajax({
url: "//export.mapknitter.org/export",
crossDomain: true,
type: "POST",
data: {
collection: JSON.stringify(collection.images),
scale: scale
},
success: opts.handleStatusUrl // this handles the initial response
});
}

_fetchStatusUrl(opts.collection, opts.scale);
},

_addToolbar: function() {
var map = this._group._map;
var group = this._group,
map = group._map;

try {
if (!this.toolbar) {
this.toolbar = L.distortableImage.controlBar({
actions: this.editActions,
position: 'topleft'
}).addTo(map, this);
}).addTo(map, group);
this.fire('toolbar:created');
}
} catch (e) { }
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leaflet-distortableimage",
"version": "0.7.1",
"version": "0.7.2",
"description": "Leaflet plugin enabling image overlays to be distorted, stretched, and warped (built for Public Lab's MapKnitter: http://publiclab.org).",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
60 changes: 0 additions & 60 deletions src/DistortableCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,66 +235,6 @@ L.DistortableCollection = L.FeatureGroup.extend({
json.avg_cm_per_pixel = this._getAvgCmPerPixel(json.images);

return json;
},

startExport: function(opts) {
opts = opts || {};
opts.collection = opts.collection || this.generateExportJson();
opts.frequency = opts.frequency || 3000;
opts.scale = opts.scale || 100; // switch it to _getAvgCmPerPixel !
var statusUrl, updateInterval;

// this may be overridden to update the UI to show export progress or completion
function _defaultUpdater(data) {
data = JSON.parse(data);
// optimization: fetch status directly from google storage:
if (statusUrl !== data.status_url && data.status_url.match('.json')) { statusUrl = data.status_url; }
if (data.status === "complete") {
clearInterval(updateInterval);
}
if (data.status === 'complete' && data.jpg !== null) {
alert("Export succeeded. http://export.mapknitter.org/" + data.jpg);
}
// TODO: update to clearInterval when status == "failed" if we update that in this file:
// https://github.com/publiclab/mapknitter-exporter/blob/main/lib/mapknitterExporter.rb
console.log(data);
}

// receives the URL of status.json, and starts running the updater to repeatedly fetch from status.json;
// this may be overridden to integrate with any UI
function _defaultHandleStatusUrl(data) {
console.log(data);
statusUrl = "//export.mapknitter.org" + data;
opts.updater = opts.updater || _defaultUpdater;

// repeatedly fetch the status.json
updateInterval = setInterval(function intervalUpdater() {
$.ajax(statusUrl + "?" + Date.now(), { // bust cache with timestamp
type: "GET",
crossDomain: true
}).done(function(data) {
opts.updater(data);
});
}, opts.frequency);
}

function _fetchStatusUrl(collection, scale) {
opts.handleStatusUrl = opts.handleStatusUrl || _defaultHandleStatusUrl;

$.ajax({
url: "//export.mapknitter.org/export",
crossDomain: true,
type: "POST",
data: {
collection: JSON.stringify(collection.images),
scale: scale
},
success: opts.handleStatusUrl // this handles the initial response
});
}

_fetchStatusUrl(opts.collection, opts.scale);

}
});

Expand Down
64 changes: 62 additions & 2 deletions src/edit/DistortableCollection.Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,75 @@ L.DistortableCollection.Edit = L.Handler.extend({
if (e) { L.DomEvent.stopPropagation(e); }
},

startExport: function(opts) {
opts = opts || {};
opts.collection = opts.collection || this._group.generateExportJson();
opts.frequency = opts.frequency || 3000;
opts.scale = opts.scale || 100; // switch it to _getAvgCmPerPixel !
var statusUrl, updateInterval;

// this may be overridden to update the UI to show export progress or completion
function _defaultUpdater(data) {
data = JSON.parse(data);
// optimization: fetch status directly from google storage:
if (statusUrl !== data.status_url && data.status_url.match('.json')) { statusUrl = data.status_url; }
if (data.status === "complete") {
clearInterval(updateInterval);
}
if (data.status === 'complete' && data.jpg !== null) {
alert("Export succeeded. http://export.mapknitter.org/" + data.jpg);
}
// TODO: update to clearInterval when status == "failed" if we update that in this file:
// https://github.com/publiclab/mapknitter-exporter/blob/main/lib/mapknitterExporter.rb
console.log(data);
}

// receives the URL of status.json, and starts running the updater to repeatedly fetch from status.json;
// this may be overridden to integrate with any UI
function _defaultHandleStatusUrl(data) {
console.log(data);
statusUrl = "//export.mapknitter.org" + data;
opts.updater = opts.updater || _defaultUpdater;

// repeatedly fetch the status.json
updateInterval = setInterval(function intervalUpdater() {
$.ajax(statusUrl + "?" + Date.now(), { // bust cache with timestamp
type: "GET",
crossDomain: true,
}).done(function(data) {
opts.updater(data);
});
}, opts.frequency);
}

function _fetchStatusUrl(collection, scale) {
opts.handleStatusUrl = opts.handleStatusUrl || _defaultHandleStatusUrl;

$.ajax({
url: "//export.mapknitter.org/export",
crossDomain: true,
type: "POST",
data: {
collection: JSON.stringify(collection.images),
scale: scale
},
success: opts.handleStatusUrl // this handles the initial response
});
}

_fetchStatusUrl(opts.collection, opts.scale);
},

_addToolbar: function() {
var map = this._group._map;
var group = this._group,
map = group._map;

try {
if (!this.toolbar) {
this.toolbar = L.distortableImage.controlBar({
actions: this.editActions,
position: 'topleft'
}).addTo(map, this);
}).addTo(map, group);
this.fire('toolbar:created');
}
} catch (e) { }
Expand Down
12 changes: 4 additions & 8 deletions src/edit/tools/DistortableImage.ControlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ L.distortableImage = L.DistortableImage;
},

addHooks: function () {
var edit = this._overlay;

var edit = this._overlay.editing;
edit.startExport();
}
});
Expand All @@ -37,8 +36,7 @@ L.distortableImage = L.DistortableImage;
},

addHooks: function() {
var edit = this._overlay;

var edit = this._overlay.editing;
edit._removeGroup();
}
});
Expand All @@ -58,8 +56,7 @@ var Locks = L.EditAction.extend({
},

addHooks: function () {
var edit = this._overlay;

var edit = this._overlay.editing;
edit._lockGroup();
}
});
Expand All @@ -79,8 +76,7 @@ var Unlocks = L.EditAction.extend({
},

addHooks: function () {
var edit = this._overlay;

var edit = this._overlay.editing;
edit._unlockGroup();
}
});
Expand Down

0 comments on commit ef89b16

Please sign in to comment.