Skip to content

Instantly share code, notes, and snippets.

@mapcentia
Last active November 2, 2017 14:24
Show Gist options
  • Save mapcentia/841947d290cac8500a3eed065ca27911 to your computer and use it in GitHub Desktop.
Save mapcentia/841947d290cac8500a3eed065ca27911 to your computer and use it in GitHub Desktop.
'use strict';
/**
*
* @type {L.FeatureGroup}
*/
var drawnItems = new L.FeatureGroup();
/**
* @type {*|exports|module.exports}
*/
var drawControl;
var cloud;
var utils;
var exId = "loisReport";
var backboneEvents;
var reportRender;
var print;
var config = require('../../../config/config.js');
var printC = config.print.templates;
var scales = config.print.scales;
var endPrintEventName = "end:loisReportPrint";
/**
*
* @private
*/
var _clearDrawItems = function () {
drawnItems.clearLayers();
};
var xhr;
var search = function () {
// Dummy
var data = {foo: "bar"};
var url = "/api/extension/loisreport";
return new Promise(function (resolve, reject) {
xhr = $.ajax({
dataType: 'json',
url: url,
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: function (data) {
resolve(data);
//reportRender.render(data);
print.control(printC, scales, "_loisReport", "A4", "p", "inline");
print.print(endPrintEventName, data);
},
error: function () {
reject();
console.error(error);
},
complete: function () {
console.info("Done");
}
});
})
};
module.exports = {
set: function (o) {
cloud = o.cloud;
utils = o.utils;
backboneEvents = o.backboneEvents;
reportRender = o.extensions.test.reportRender;
print = o.print;
},
init: function () {
// When LOIS search is done, enable the print button
// Handle GUI when print is done. Using at custom event, so standard print is not triggered
backboneEvents.get().on(endPrintEventName, function (response) {
alert("PRINT FÆRDIGT")
});
var React = require('react');
var ReactDOM = require('react-dom');
utils.createMainTab(exId, __("LOIS rapport"), __("Søg LOIS...."), require('./../../../browser/modules/height')().max);
var activate = function () {
cloud.get().map.addLayer(drawnItems);
L.drawLocal = local;
drawControl = new L.Control.Draw({
position: 'topright',
draw: {
polygon: {
title: 'Draw a polygon!',
allowIntersection: true,
drawError: {
color: '#b00b00',
timeout: 1000
},
shapeOptions: {
color: '#662d91',
fillOpacity: 0
},
showArea: true
},
polyline: false,
circle: false,
rectangle: false,
marker: false
},
edit: {
featureGroup: drawnItems,
remove: false
}
});
cloud.get().map.addControl(drawControl);
// Unbind events
cloud.get().map.off('draw:created');
cloud.get().map.off('draw:drawstart');
cloud.get().map.off('draw:drawstop');
cloud.get().map.off('draw:editstart');
cloud.get().map.off('draw:deletestart');
cloud.get().map.off('draw:deletestop');
cloud.get().map.off('draw:deleted');
// Bind events
cloud.get().map.on('draw:created', function (e) {
drawnItems.addLayer(e.layer);
});
cloud.get().map.on('draw:drawstart', function (e) {
_clearDrawItems();
});
cloud.get().map.on('draw:drawstop', function (e) {
alert("SØG!");
search().then(function (e) {
console.log(e)
})
});
cloud.get().map.on('draw:editstop', function (e) {
console.log(e.layer);
search().then(function (e) {
console.log(e)
})
});
cloud.get().map.on('draw:editstart', function (e) {
console.info("draw:editstart");
});
}
class LoisReport extends React.Component {
constructor(props) {
super(props);
this.state = {
active: false
};
this.onActive = this.onActive.bind(this);
}
/**
*
* @param e
*/
onActive(e) {
this.setState({
active: e.target.checked
});
if (e.target.checked) {
// Turn info click off
//====================
backboneEvents.get().trigger("off:infoClick");
// Emit "on" event
//================
backboneEvents.get().trigger("on:" + exId);
activate();
} else {
// Turn info click on again
//=========================
backboneEvents.get().trigger("on:infoClick");
// Emit "off" event
//=================
backboneEvents.get().trigger("off:" + exId);
}
}
/**
*
*/
componentDidMount() {
}
/**
*
* @returns {XML}
*/
render() {
return (
<div role="tabpanel">
<div className="panel panel-default">
<div className="panel-body">
<div className="form-group">
<div className="togglebutton">
<label><input id="streetview-btn" type="checkbox"
defaultChecked={ this.state.active }
onChange={this.onActive}/>{__("Activate")}
</label>
</div>
</div>
</div>
</div>
</div>
);
}
}
// Append to DOM
//==============
try {
ReactDOM.render(
<LoisReport />,
document.getElementById(exId)
);
} catch (e) {
}
}
};
var local = {
draw: {
toolbar: {
actions: {
title: __('Cancel drawing'),
text: __('Cancel')
},
undo: {
title: __('Delete last point drawn'),
text: __('Delete last point')
},
finish: {
title: __('Finish drawing.'),
text: __('Finish')
},
buttons: {
polyline: __('Search with a line'),
polygon: __('Search with an area'),
rectangle: __('Search with a rectangle'),
circle: __('Search with a circle'),
marker: __('Search with a point')
}
},
handlers: {
circle: {
tooltip: {
start: __('Click and drag to draw circle.')
},
radius: __('Radius')
},
marker: {
tooltip: {
start: __('Click map to place marker.')
}
},
polygon: {
tooltip: {
start: __('Click to start drawing shape.'),
cont: __('Click to continue drawing shape.'),
end: __('Click first point to close this shape.')
}
},
polyline: {
error: __('<strong>Error:</strong> shape edges cannot cross!'),
tooltip: {
start: __('Click to start drawing line.'),
cont: __('Click to continue drawing line.'),
end: __('Click last point to finish line.')
}
},
rectangle: {
tooltip: {
start: __('Click and drag to draw rectangle.')
}
},
simpleshape: {
tooltip: {
end: __('Release mouse to finish drawing.')
}
}
}
},
edit: {
toolbar: {
actions: {
save: {
title: __('Save changes.'),
text: __('Save')
},
cancel: {
title: __('Cancel editing, discards all changes.'),
text: __('Cancel')
}
},
buttons: {
edit: __("Edit drawings."),
editDisabled: __('No drawings to edit.'),
remove: __('Delete drawings.'),
removeDisabled: __('No drawings to delete.')
}
},
handlers: {
edit: {
tooltip: {
text: __('Drag handles, or marker to edit drawing.'),
subtext: __('Click cancel to undo changes.')
}
},
remove: {
tooltip: {
text: __('Click on a drawing to remove')
}
}
}
}
}
/******************************/
'use strict';
var express = require('express');
var request = require("request");
var router = express.Router();
var http = require('http');
var fs = require('fs');
var config = require('../../../config/config.js');
router.post('/api/extension/loisreport', function (req, response) {
req.setTimeout(0); // no timeout
var body = req.body;
console.log(body);
'use strict';
var url = "http://bal0-lois:8080/api/datapakkeresultat?procedureID=3&procedureParametre=%7B%22UdStraekID%22%3A%221%22%2C%22LS2RapportID%22%3A%223%22%7D";
let options = {
method: 'POST',
uri: url,
json: body
};
request(options, function (err, res, body) {
response.header('content-type', 'application/json');
response.header('Cache-Control', 'no-cache, no-store, must-revalidate');
response.header('Expires', '0');
console.log(body);
var fileName = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
fs.writeFile(__dirname + "/../../../tmp/" + fileName, JSON.stringify(body, null, 4), function (err) {
if (err) {
console.log(err);
} else {
console.log("Repport saved");
}
});
response.send({data: body, key: fileName});
});
});
module.exports = router;
/******/
module.exports = {
set: function (o) {
return this;
},
init: function () {
},
render: function (e) {
console.log(e);
$("#report #without").append("<div>HEJ</div>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment