# HG changeset patch # User Matti Hamalainen # Date 1509247010 -7200 # Node ID ccf488dac4c23a7d4e52f05231931f290cfe39f8 # Parent eb98e57d396933ef0ee57fd353a01b0599ea8918 Some prototyping/initial code for the map search client-side stuff, JavaScript part. diff -r eb98e57d3969 -r ccf488dac4c2 www/search.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/search.js Sun Oct 29 05:16:50 2017 +0200 @@ -0,0 +1,158 @@ +// +// Frontend map/location search JavaScript functionality +// Written by Matti 'ccr' Hamalainen +// (C) Copyright 2017 Tecnic Software productions (TNSP) +// + +var fieldPattern, fieldRes, fieldLog; +var msgLog, mapWS, mapList; + + +function mapAddEventOb(evobj, evtype, evcallback) +{ + if (evobj == null || typeof(evobj) == 'undefined') + return; + + if (evobj.addEventListener) + evobj.addEventListener(evtype, evcallback, false); + else + if (evobj.attachEvent) + evobj.attachEvent("on" + evtype, evcallback); + else + evobj["on"+evtype] = evcallback; +} + + +function mapAddEvent(obname, evtype, evcallback) +{ + mapAddEventOb(document.getElementById(obname), evtype, evcallback); +} + + +function mapLog(msg) +{ + if (msgLog.length >= 10) + msgLog = msgLog.slice(1, 10); + + msgLog.push(msg); + + if (fieldLog) + { + fieldLog.innerHTML = msgLog.join("
"); + } +} + + +function mapPerformSearch() +{ + if (fieldPattern.value.trim() == "") + { + mapLog("Nothing to search for."); + return; + } + + if (mapWS) + { + mapLog("Old query not finished."); + return; + } + + mapWS = new WebSocket("ws://localhost:9999/mapSearch"); + if (!mapWS) + { + mapLog("Could not create WebSocket connection?"); + return; + } + + mapLog("WebSocket connection to server established."); + btnSearch.disabled = true; + + mapWS.onopen = function() + { + // Web Socket is connected, send data using send() + mapLog("Sending query to server."); + mapWS.send("MAPSEARCH\n" + fieldPattern.value); + }; + + mapWS.onmessage = function(evt) + { + if (evt.data.substr(0, 6) == "ERROR:") + { + mapLog("ERROR! "+ evt.data.substr(6)); + } + else + if (evt.data.substr(0, 7) == "RESULT:" && evt.data.length > 10) + { + var results = JSON.parse(evt.data.substr(7)); + mapLog("Receiving results."); + if (results) + { + var str = ""; + for (var i = 0; i < results.length; i++) + { + var res = results[i]; + str += "
"+ + ""+ + res[3] +", "+ res[4] +" at "+ res[2] +""+ + " ["+ res[5] +", "+ res[6] +" global]"+ + " ("+ res[0] +"% match)"+ + (res[1] ? " centered" : "")+ + "
"; + } + fieldRes.innerHTML = str; + } + else + fieldRes.innerHTML = "ERROR!"; + } + else + if (evt.data.substr(0, 9) == "PROGRESS:") + { + mapLog("Search progress "+ evt.data.substr(9) +"% .."); + } + else + if (evt.data == "END") + { + mapLog("Server ending communication."); + mapWS.close(); + } + else + { + mapLog("Sir! A message for you! "+ evt.data); + } + }; + + mapWS.onclose = function() + { + mapLog("WebSocket connection closed."); + mapWS = null; + btnSearch.disabled = false; + }; + + mapWS.onerror = function() + { + mapLog("Error!"); + mapWS = null; + btnSearch.disabled = false; + }; +} + + +function mapInitSearch() +{ + msgLog = []; + + fieldPattern = document.getElementById("pattern"); + fieldRes = document.getElementById("results"); + fieldLog = document.getElementById("log"); + btnSearch = document.getElementById("btnSearch"); + + mapAddEventOb(btnSearch, "click", mapPerformSearch); + mapAddEvent("btnClear", "click", + function () + { + mapLog("Cleared pattern and results."); + fieldRes.innerHTML = ""; + fieldPattern.value = ""; + }); +}