From 624bcf4e3352971a77cf3e6a4266f1600f9da577 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Sun, 17 Aug 2014 20:09:05 +0200 Subject: add language blocks --- threads.js | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'threads.js') diff --git a/threads.js b/threads.js index 335b5d0..51d1c9f 100644 --- a/threads.js +++ b/threads.js @@ -2572,6 +2572,17 @@ Process.prototype.reportTimer = function () { return 0; }; +Process.prototype.reportLanguage = function () { + var ide = this.homeContext.receiver.parentThatIsA(IDE_Morph); + if (ide) { + var lang = ide.userLanguage; + if (lang) { + return lang; + } + } + return 'en'; +}; + // Process Dates and times in Snap // Map block options to built-in functions var dateMap = { -- cgit v1.3.1 From 667aa5a56564d07c05182eeae7bba7567710a844 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Mon, 18 Aug 2014 14:28:52 +0200 Subject: add geolocation block --- blocks.js | 19 +++++++++++++++++++ objects.js | 7 +++++++ threads.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) (limited to 'threads.js') diff --git a/blocks.js b/blocks.js index 5f9bb29..735d36b 100644 --- a/blocks.js +++ b/blocks.js @@ -844,6 +844,25 @@ SyntaxElementMorph.prototype.labelPart = function (spec) { ); part.setContents(['date']); break; + case '%locations': + part = new InputSlotMorph( + null, // text + false, // non-numeric + { + 'all' : ['all'], + 'country' : ['country'], + 'state' : ['state'], + 'state district' : ['state district'], + 'suburb' : ['suburb'], + 'city' : ['city'], + 'road' : ['road'], + 'house number' : ['house number'], + 'licence': ['licence'] + }, + true // read-only + ); + part.setContents(['all']); + break; case '%delim': part = new InputSlotMorph( null, // text diff --git a/objects.js b/objects.js index 4c0e862..5450a49 100644 --- a/objects.js +++ b/objects.js @@ -883,6 +883,11 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sensing', spec: 'language' }, + reportLocation: { + type: 'reporter', + category: 'sensing', + spec: 'location %locations' + }, // Operators reifyScript: { @@ -1891,6 +1896,7 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportDate')); blocks.push('-'); blocks.push(block('reportLanguage')); + blocks.push(block('reportLocation')); // for debugging: /////////////// @@ -5000,6 +5006,7 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportDate')); blocks.push('-'); blocks.push(block('reportLanguage')); + blocks.push(block('reportLocation')); // for debugging: /////////////// diff --git a/threads.js b/threads.js index 51d1c9f..754d502 100644 --- a/threads.js +++ b/threads.js @@ -2583,6 +2583,67 @@ Process.prototype.reportLanguage = function () { return 'en'; }; +Process.prototype.reportLocation = function (name) { + var myself = this; + + if (!myself.context.jsonpScript) { + if (!navigator.geolocation) { + myself.handleError({name: 'Location', + message: 'navigator.geolocation is not supported'}); + } + window.OSM_JSONP_Callback = function (data) { + myself.context.json = data; + window.OSM_JSONP_Callback = undefined; + }; + myself.context.jsonpScript = document.createElement('script'); + + navigator.geolocation.getCurrentPosition( + function (pos) { + var crd = pos.coords; + + myself.context.jsonpScript.src = + 'https://nominatim.openstreetmap.org/reverse' + + '?format=json&json_callback=OSM_JSONP_Callback' + + '&zoom=18&addressdetails=1' + + '&lat=' + + crd.latitude + + '&lon=' + + crd.longitude; + document.getElementsByTagName('HEAD')[0] + .appendChild(myself.context.jsonpScript); + }, + function (err) { + console.log(err); + myself.handleError({name: 'Location', message: err}); + }, + { enableHightAccuracy: true, timeout: 30000, maximumAge: 0} + ); + } else { + if (myself.context.json) { + switch (myself.inputOption(name)) { + case 'all': + return myself.context.json.display_name; + case 'state district': + return myself.context.json.address.state_district; + case 'house number': + return myself.context.json.address.house_number; + case 'licence': + return myself.context.json.licence; + case 'country': + case 'state': + case 'suburb': + case 'city': + case 'road': + return myself.context. + json.address[myself.inputOption(name)]; + } + } + } + + this.pushContext('doYield'); + this.pushContext(); +}; + // Process Dates and times in Snap // Map block options to built-in functions var dateMap = { -- cgit v1.3.1