From cd7ba253cf3e58b030295edeee203f95f7ea2416 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 29 Apr 2018 16:39:57 -0400 Subject: [PATCH] Added ability for JS plugins API to return values upon initialization, osm-quickedit mock --- .../app/js/classes/plugins/ApiFactory.js | 18 ++++++++++++------ app/static/app/js/classes/plugins/Map.js | 3 ++- app/static/app/js/components/Map.jsx | 16 ++++++++++++---- plugins/measure/public/MeasurePopup.jsx | 2 -- plugins/osm-quickedit/__init__.py | 1 + plugins/osm-quickedit/manifest.json | 13 +++++++++++++ plugins/osm-quickedit/plugin.py | 12 ++++++++++++ plugins/osm-quickedit/public/main.css | 0 plugins/osm-quickedit/public/main.js | 7 +++++++ 9 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 plugins/osm-quickedit/__init__.py create mode 100644 plugins/osm-quickedit/manifest.json create mode 100644 plugins/osm-quickedit/plugin.py create mode 100644 plugins/osm-quickedit/public/main.css create mode 100644 plugins/osm-quickedit/public/main.js diff --git a/app/static/app/js/classes/plugins/ApiFactory.js b/app/static/app/js/classes/plugins/ApiFactory.js index 239b49fe..e0092149 100644 --- a/app/static/app/js/classes/plugins/ApiFactory.js +++ b/app/static/app/js/classes/plugins/ApiFactory.js @@ -8,7 +8,6 @@ export default class ApiFactory{ // @param api {Object} create(api){ - // Adds two functions to obj // - eventName // - triggerEventName @@ -16,6 +15,10 @@ export default class ApiFactory{ // are more robust as we can detect more easily if // things break const addEndpoint = (obj, eventName, preTrigger = () => {}) => { + const askForResponse = (...args) => { + this.events.emit(`${api.namespace}::${eventName}::Response`, ...args); + }; + obj[eventName] = (callbackOrDeps, callbackOrUndef) => { if (Array.isArray(callbackOrDeps)){ // Deps @@ -24,20 +27,23 @@ export default class ApiFactory{ this.events.addListener(`${api.namespace}::${eventName}`, (...args) => { Promise.all(callbackOrDeps.map(dep => SystemJS.import(dep))) .then((...deps) => { - callbackOrUndef(...(Array.from(args).concat(...deps))); + askForResponse(callbackOrUndef(...(Array.from(args).concat(...deps)))); }); }); }else{ // Callback - this.events.addListener(`${api.namespace}::${eventName}`, callbackOrDeps); + this.events.addListener(`${api.namespace}::${eventName}`, (...args) => { + askForResponse(callbackOrDeps(...args)); + }); } } const triggerEventName = "trigger" + eventName[0].toUpperCase() + eventName.slice(1); - obj[triggerEventName] = (...args) => { - preTrigger(...args); - this.events.emit(`${api.namespace}::${eventName}`, ...args); + obj[triggerEventName] = (params, responseCb) => { + preTrigger(params, responseCb); + this.events.emit(`${api.namespace}::${eventName}`, params); + if (responseCb) this.events.addListener(`${api.namespace}::${eventName}::Response`, responseCb); }; } diff --git a/app/static/app/js/classes/plugins/Map.js b/app/static/app/js/classes/plugins/Map.js index bb32622d..63a6b287 100644 --- a/app/static/app/js/classes/plugins/Map.js +++ b/app/static/app/js/classes/plugins/Map.js @@ -12,7 +12,8 @@ export default { endpoints: [ ["willAddControls", leafletPreCheck], - ["didAddControls", leafletPreCheck] + ["didAddControls", leafletPreCheck], + ["addActionButtons", leafletPreCheck], ] }; diff --git a/app/static/app/js/components/Map.jsx b/app/static/app/js/components/Map.jsx index 738f34fd..ca0f564a 100644 --- a/app/static/app/js/components/Map.jsx +++ b/app/static/app/js/components/Map.jsx @@ -14,6 +14,7 @@ import ShareButton from './ShareButton'; import AssetDownloads from '../classes/AssetDownloads'; import PropTypes from 'prop-types'; import PluginsAPI from '../classes/plugins/API'; +import update from 'immutability-helper'; class Map extends React.Component { static defaultProps = { @@ -40,7 +41,8 @@ class Map extends React.Component { this.state = { error: "", - singleTask: null // When this is set to a task, show a switch mode button to view the 3d model + singleTask: null, // When this is set to a task, show a switch mode button to view the 3d model + pluginActionButtons: [] }; this.imageryLayers = []; @@ -236,12 +238,17 @@ class Map extends React.Component { }); }); - // PluginsAPI.events.addListener('Map::AddPanel', (e) => { - // console.log("Received response: " + e); - // }); PluginsAPI.Map.triggerDidAddControls({ map: this.map }); + + PluginsAPI.Map.triggerAddActionButtons({ + map: this.map + }, (button) => { + this.setState(update(this.state, { + pluginActionButtons: {$push: [button]} + })); + }); } componentDidUpdate(prevProps) { @@ -285,6 +292,7 @@ class Map extends React.Component {
+ {this.state.pluginActionButtons.map((button, i) =>
{button}
)} {(!this.props.public && this.state.singleTask !== null) ? { this.shareButton = ref; }} diff --git a/plugins/measure/public/MeasurePopup.jsx b/plugins/measure/public/MeasurePopup.jsx index a59dae38..4e12a433 100644 --- a/plugins/measure/public/MeasurePopup.jsx +++ b/plugins/measure/public/MeasurePopup.jsx @@ -36,8 +36,6 @@ module.exports = class MeasurePopup extends React.Component { lastCoord.dd.x )); - console.log(layers); - // Did we select a layer? if (layers.length > 0){ const layer = layers[layers.length - 1]; diff --git a/plugins/osm-quickedit/__init__.py b/plugins/osm-quickedit/__init__.py new file mode 100644 index 00000000..48aad58e --- /dev/null +++ b/plugins/osm-quickedit/__init__.py @@ -0,0 +1 @@ +from .plugin import * diff --git a/plugins/osm-quickedit/manifest.json b/plugins/osm-quickedit/manifest.json new file mode 100644 index 00000000..5a89dcc9 --- /dev/null +++ b/plugins/osm-quickedit/manifest.json @@ -0,0 +1,13 @@ +{ + "name": "OSM Quick Editor Button", + "webodmMinVersion": "0.5.2", + "description": "A plugin to add a button for quickly opening OpenStreetMap's iD editor and setup a TMS basemap.", + "version": "0.1.0", + "author": "Piero Toffanin", + "email": "pt@masseranolabs.com", + "repository": "https://github.com/OpenDroneMap/WebODM", + "tags": ["osm", "editor"], + "homepage": "https://github.com/OpenDroneMap/WebODM", + "experimental": true, + "deprecated": false +} \ No newline at end of file diff --git a/plugins/osm-quickedit/plugin.py b/plugins/osm-quickedit/plugin.py new file mode 100644 index 00000000..348fa707 --- /dev/null +++ b/plugins/osm-quickedit/plugin.py @@ -0,0 +1,12 @@ +from app.plugins import PluginBase, Menu, MountPoint +from django.shortcuts import render + +class Plugin(PluginBase): + def include_js_files(self): + return ['main.js'] + + # def include_css_files(self): + # return ['test.css'] + + + diff --git a/plugins/osm-quickedit/public/main.css b/plugins/osm-quickedit/public/main.css new file mode 100644 index 00000000..e69de29b diff --git a/plugins/osm-quickedit/public/main.js b/plugins/osm-quickedit/public/main.js new file mode 100644 index 00000000..62a5ee6a --- /dev/null +++ b/plugins/osm-quickedit/public/main.js @@ -0,0 +1,7 @@ +PluginsAPI.Map.addActionButtons([ + 'osm-quickedit/main.css' + ], function(options){ + + console.log("PLUGIN INIT!"); + return React.createElement("div", null, "HELLO"); +}); \ No newline at end of file