UNPKG

node-red-contrib-snap4city-user

Version:

Nodes for Snap4city project, targeted to standard user (no developer)

372 lines (335 loc) 14.8 kB
<!--/* NODE-RED-CONTRIB-SNAP4CITY-USER Copyright (C) 2018 DISIT Lab http://www.disit.org - University of Florence This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */--> <script type="text/x-red" data-template-name="service-search-along-path"> <div class="form-row" id="rowAuthentication"> <label for="node-input-authentication">Authentication</label> <input type="text" id="node-input-authentication"> </div> <div class="form-tips" id="tipAuthentication" style="margin-bottom: 10px;"> If you have private data and you want to access them, you must have to insert you account data. You can register for one account <a href="https://www.snap4city.org" target="_blank">here</a>. </div> <div class="form-row"> <label for="node-input-name">Name</label> <input type="text" autocomplete="off" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-maxresults">Max Results</label> <select id="node-input-maxresults"> <option value="100">100</option> <option value="200">200</option> <option value="500">500</option> <option value="1000">1000</option> <option value="2000">2000</option> <option value="0">All</option> </select> </div> <div class="form-row"> <label for="node-input-lang">Language</label> <select id="node-input-lang"> <option value="en">English</option> <option value="fr">French</option> <option value="de">German</option> <option value="it">Italian</option> <option value="es">Spanish</option> </select> </div> <div class="form-row"> <label for="node-input-model">Model</label> <input type="text" autocomplete="off" id="node-input-model" placeholder="Model"> </div> <div class="form-row"> <label for="node-input-path">Path</label> <input type="text" autocomplete="off" id="node-input-path" placeholder="Path" disabled> </div> <div class="form-row"> <link rel="stylesheet" href="s4c/css/leaflet.css" /> <link rel="stylesheet" href="s4c/css/leaflet.draw.css" /> <div id="node-input-map" style="width: 80%; height: 300px"></div> </div> <div class="form-row"> <label for="node-input-categories">Categories</label> <input type="text" autocomplete="off" id="node-input-categories" placeholder="Categories" value="Services" disabled> </div> <div class="form-row"> <link rel="stylesheet" href="s4c/css/ui.fancytree.min.css" /> <div> <button onclick="$('#filterCategories').val('');filterMenu();selectCategories();"> <i class="fa fa-trash"></i> </button> <input id="filterCategories" name="search" onkeyup="filterMenu();" style="width:80%;" placeholder="Filter Categories..."> </div> <div id="node-input-fancytree" style="width: 80%; height: 400px"></div> </div> </script> <script type="text/x-red" data-help-name="service-search-along-path"> <p>It allows to retrieve the set of services that are near a given GPS position. The services can be filtered as belonging to specific categories (e.g. Accommodation, Hotel, Restaurant etc). It can also be used to find services that have a WKT spatial description that contains a specific GPS position.</p> <h3>Inputs</h3> A JSON with these parameters: <dl class="message-properties"> <dt>path <span class="property-type">string</span> </dt> <dd>&lt;WKT_string>” for a geographic path described as Well Known Text</dd> <dt>categories <span class="property-type">string</span> </dt> <dd> the list of categories of the services to be retrieved separated with semicolon, if omitted all kinds of services are returned. It can contain macro categories or categories, if a macro category is specified all categories in the macro category are used. The complete list of categories and macro categories can be retrieved on servicemap.disit.org</dd> <dt>maxresults <span class="property-type">number</span> </dt> <dd> maximum number of results to be returned (if parameter is missing 100 is assumed), if it is 0 all results are returned</dd> <dt>lang <span class="property-type">string</span> </dt> <dd> ISO 2 chars language code (e.g. “it”, “en”, “fr”, “de”, “es”) to be used for returned descriptions if available in multiple languages. Currently for languages other than “it” and “en” it returns “en” descriptions. (if parameter is missing “en” is assumed)</dd> <dt>model <span class="property-type">string</span> </dt> <dd> search for iot devices created with a specific model name.</dd> </dl> <h3>Outputs</h3> <ol class="node-ports"> <li>ServiceUri Array <dl class="message-properties"> <dd> Returns an array containing the servicesUri of each service found</dd> </dl> </li> <li> All services together <dl class="message-properties"> <dd> It returns the services merge in Results section</dd> </dl> </li> </ol> <h3>Details</h3> <p>The node can receive a JSON with the parameters described in the Inputs section and with them generate the output JSON. If the values are not present in the input JSON, these are read by those in the configuration. If they are not present in either part, an error is generated for the necessary parameters.</p> </script> <script type="text/javascript"> $.ajax({ url: "s4c/js/jquery.fancytree-all.min.js", async: false, dataType: "script" }); function selectCategories() { selectedKeys = []; if ($("#node-input-fancytree").fancytree("getTree").isFilterActive()) { selectedKeys = $.map($("#node-input-fancytree").fancytree("getTree").getSelectedNodes(), function (node) { if (node.isMatched() && !node.hasChildren()) { return node.key; } }); } else { selectedKeys = $.map($("#node-input-fancytree").fancytree("getTree").getSelectedNodes(true), function (node) { return node.key; }); } if (selectedKeys.length == 0) { $("#node-input-categories").val("Service"); } else { $("#node-input-categories").val(selectedKeys.join(";")); } } function filterMenu() { var match = $("#filterCategories").val(); var opts = $("#node-input-fancytree").fancytree("getTree").filterNodes(match, { autoExpand: true, leavesOnly: true }); } RED.nodes.registerType('service-search-along-path', { category: 'S4CSearch', color: '#E9967A', defaults: { name: { value: "" }, authentication: { type: "snap4city-authentication", required: false }, path: { value: "", required: false }, categories: { value: "Service", required: false }, model: { value: "", required: false }, maxresults: { value: 100, required: false, validate: RED.validators.number() }, lang: { value: "", required: false } }, outputs: 2, inputs: 1, outputLabels: ["service uri array", "geojson result"], icon: "marker.png", label: function () { return this.name || "service-search-along-path"; }, oneditprepare: function () { $.ajax({ url: "s4c/js/leaflet.js", async: false, dataType: "script" }); $.ajax({ url: "s4c/js/leaflet.draw.js", async: false, dataType: "script" }); node = this; map = L.map('node-input-map').setView([43.78, 11.23], 9); L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' }).addTo(map); window.node_input_map = map; var mapLayers = {}; drawnItems = new L.FeatureGroup(); map.addLayer(drawnItems); var editControl = new L.Control.Draw({ draw: false, edit: { featureGroup: drawnItems } }); map.addControl(editControl); drawControl = new L.Control.Draw({ draw: { position: 'topleft', polygon: false, rectangle: false, marker: false, circlemarker: false, circle: false } }); map.addControl(drawControl); L.control.layers(mapLayers, { 'drawlayer': drawnItems }, { collapsed: true }).addTo(map); map.on(L.Draw.Event.CREATED, function (e) { var fence = e.layer; fence.nodeID = node.id; if (drawnItems.hasLayer(fence) == false) { drawnItems.addLayer(fence); } drawControl.remove(); polygons = {}; drawnItems.eachLayer(function (layer) { polygons[layer.nodeID] = layer.toGeoJSON(); }); $("#node-input-path").val(polygons[node.id].geometry.type.toUpperCase() + JSON.stringify( polygons[node.id].geometry.coordinates).replace(/\],\[/g, "&").replace( /,/g, " ").replace(/\[\[/g, "(").replace(/\]\]/g, ")").replace(/&/g, ",")); }); map.on('draw:edited', function (e) { var fences = e.layers; fences.eachLayer(function (fence) { fence.shape = "geofence"; if (drawnItems.hasLayer(fence) == false) { drawnItems.addLayer(fence); } }); polygons = {}; drawnItems.eachLayer(function (layer) { polygons[layer.nodeID] = layer.toGeoJSON(); }); $("#node-input-path").val(polygons[node.id].geometry.type.toUpperCase() + JSON.stringify( polygons[node.id].geometry.coordinates).replace(/\],\[/g, "&").replace( /,/g, " ").replace(/\[\[/g, "(").replace(/\]\]/g, ")").replace(/&/g, ",")); }); map.on('draw:deleted', function (e) { drawControl.addTo(map); $("#node-input-path").val(""); }); $.ajax({ url: "s4c/json/categories.json", async: false, cache: false, timeout: 2000, dataType: "json", success: function (data) { $("#node-input-fancytree").fancytree({ source: data, extensions: ["glyph", "filter"], checkbox: true, imagePath: "s4c/img/", selectMode: 3, clickFolderMode: 2, click: function (event, data) { if (!data.node.folder) { data.node.toggleSelected(); selectCategories(); return false; } }, glyph: { map: { checkbox: "fa fa-square", checkboxSelected: "fa fa-check-square", checkboxUnknown: "fa fa-share-square" } }, filter: { autoApply: false, // Re-apply last filter if lazy data is loaded counter: false, // Show a badge with number of matching child nodes near parent icons fuzzy: false, // Match single characters in order, e.g. 'fb' will match 'FooBar' leavesOnly: true, highlight: false, // Highlight matches by wrapping inside <mark> tags nodata: "No services", mode: "hide" // Grayout unmatched nodes (pass "hide" to remove unmatched node instead) } }); $("span.fancytree-checkbox").css("font-size", "30px").css("margin-top", "5px").css("margin-right", "5px"); $("img.fancytree-icon").css("font-size", "35px"); $("#node-input-fancytree").on("click", function (event, data) { selectCategories(); }); }, error: function (data) { console.log(data); } }); }, oneditresize: function () { if (window.node_input_map) { window.node_input_map.invalidateSize(true); } } }); </script>