From 5a3fdada8fcc4e2a0344ec311340da362b345631 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 1 Sep 2019 01:31:55 +0100 Subject: [PATCH] fixes GMaps usage & Command creation --- .../Accounting/ManageController.cs | 4 +- src/Yavsc/Hubs/ChatHub.cs | 5 +- src/Yavsc/Services/ChatHubConnexionManager.cs | 2 +- src/Yavsc/Services/YavscMessageSender.cs | 4 +- src/Yavsc/Views/Blogspot/Create.cshtml | 7 + src/Yavsc/Views/Blogspot/Edit.cshtml | 12 +- src/Yavsc/Views/Command/Create.cshtml | 150 +++++++++--------- src/Yavsc/Views/Manage/SetActivity.cshtml | 20 ++- src/Yavsc/Views/Manage/SetAddress.cshtml | 52 +++--- src/Yavsc/Views/Shared/_Layout.cshtml | 8 - .../Views/Shared/_MapScriptsPartial.cshtml | 108 +------------ src/Yavsc/wwwroot/js/google-geoloc.js | 89 ++++++----- 12 files changed, 192 insertions(+), 269 deletions(-) diff --git a/src/Yavsc/Controllers/Accounting/ManageController.cs b/src/Yavsc/Controllers/Accounting/ManageController.cs index 2830131b..b17a08d4 100644 --- a/src/Yavsc/Controllers/Accounting/ManageController.cs +++ b/src/Yavsc/Controllers/Accounting/ManageController.cs @@ -705,7 +705,7 @@ namespace Yavsc.Controllers var uid = User.GetUserId(); var user = await _dbContext.Users.Include(u=>u.PostalAddress).SingleAsync(u=>u.Id==uid); ViewBag.GoogleSettings = _googleSettings; - return View (new Yavsc.ViewModels.Manage.SetAddressViewModel { Street1 = user.PostalAddress?.Address } ); + return View (user.PostalAddress ?? new Location()); } [HttpPost] @@ -727,7 +727,7 @@ namespace Yavsc.Controllers return RedirectToAction(nameof(Index), new { Message = ManageMessageId.SetAddressSuccess }); } ViewBag.GoogleSettings = _googleSettings; - return View(new Yavsc.ViewModels.Manage.SetAddressViewModel { Street1 = model.Address}); + return View(model); } public async Task PaymentInfo (string id) { diff --git a/src/Yavsc/Hubs/ChatHub.cs b/src/Yavsc/Hubs/ChatHub.cs index c0323cee..0355870f 100644 --- a/src/Yavsc/Hubs/ChatHub.cs +++ b/src/Yavsc/Hubs/ChatHub.cs @@ -303,8 +303,7 @@ namespace Yavsc if (!_cxManager.Kick(Context.ConnectionId, userName, roomName, reason)) return; } var ukeys = _cxManager.GetConnexionIds(userName); - - foreach(var ukey in ukeys) + if (ukeys!=null) foreach(var ukey in ukeys) Groups.Remove(ukey, roomGroupName); Clients.Group(roomGroupName).notifyRoom(NotificationTypes.Kick, roomName, $"{userName}: {reason}"); } @@ -403,7 +402,7 @@ namespace Yavsc } } var cxIds = _cxManager.GetConnexionIds(userName); - + if (cxIds!=null) foreach (var connectionId in cxIds) { var cli = Clients.Client(connectionId); diff --git a/src/Yavsc/Services/ChatHubConnexionManager.cs b/src/Yavsc/Services/ChatHubConnexionManager.cs index 2527961c..712b1f0d 100644 --- a/src/Yavsc/Services/ChatHubConnexionManager.cs +++ b/src/Yavsc/Services/ChatHubConnexionManager.cs @@ -263,7 +263,7 @@ namespace Yavsc.Services public IEnumerable GetConnexionIds(string userName) { - return ChatCxIds[userName]; + return ChatCxIds.ContainsKey(userName) ? ChatCxIds[userName] : null; } /// diff --git a/src/Yavsc/Services/YavscMessageSender.cs b/src/Yavsc/Services/YavscMessageSender.cs index 52061378..c1b9a9c0 100644 --- a/src/Yavsc/Services/YavscMessageSender.cs +++ b/src/Yavsc/Services/YavscMessageSender.cs @@ -111,8 +111,8 @@ namespace Yavsc.Services result.message_id = mailSent.MessageId; response.success++; } - var cxids = _cxManager.GetConnexionIds(user.UserName).ToArray(); - if (cxids.Length == 0) + var cxids = _cxManager.GetConnexionIds(user.UserName); + if (cxids == null) { _logger.LogDebug($"no cx to {user.UserName} <{user.Email}> "); } diff --git a/src/Yavsc/Views/Blogspot/Create.cshtml b/src/Yavsc/Views/Blogspot/Create.cshtml index adc581f3..adf6f77a 100644 --- a/src/Yavsc/Views/Blogspot/Create.cshtml +++ b/src/Yavsc/Views/Blogspot/Create.cshtml @@ -58,4 +58,11 @@ @{ await Html.RenderPartialAsync("_FSScriptsPartial"); } @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } + } diff --git a/src/Yavsc/Views/Blogspot/Edit.cshtml b/src/Yavsc/Views/Blogspot/Edit.cshtml index 1b12a4d5..56afe9c3 100644 --- a/src/Yavsc/Views/Blogspot/Edit.cshtml +++ b/src/Yavsc/Views/Blogspot/Edit.cshtml @@ -4,7 +4,7 @@ ViewData["Title"] = SR["Blog post edition"]; } -@section header{ +@section header { @@ -35,9 +35,18 @@ + + @{ await Html.RenderPartialAsync("_FSScriptsPartial"); } + +} + +@section scripts { @Html.Partial("_ValidationScriptsPartial") + }

@SR["Blog post edition"]

diff --git a/src/Yavsc/Views/Command/Create.cshtml b/src/Yavsc/Views/Command/Create.cshtml index 66a47255..8ef619e0 100644 --- a/src/Yavsc/Views/Command/Create.cshtml +++ b/src/Yavsc/Views/Command/Create.cshtml @@ -1,27 +1,18 @@ -@model RdvQuery -@{ ViewData["Title"] = $"Proposition de rendez-vous à {Model.PerformerProfile.Performer.UserName} [{ViewBag.Activity.Name}]"; } -@section scripts { - - - +@model RdvQuery +@section header { - + + - }); - - } +@{ ViewData["Title"] = $"Proposition de rendez-vous à {Model.PerformerProfile.Performer.UserName} [{ViewBag.Activity.Name}]"; }

@ViewData["Title"]

@@ -30,7 +21,7 @@
- Votre évennement + La date de votre évennement @@ -41,80 +32,83 @@
- +
- - + + +
+ + + - - - -
-
-
-
-
- - -
    -
-
+
+
+ +
+ + + +
    +
+
+
+
-
-
-
-
- +
+
+
- - - -
-
@SR["Coordonées GPS"] -
-
- - -
-
- - -
-
-
+ + +
+
+
+
+ + +
+
+ +
- - -
-
- - - -
-
-@Html.HiddenFor(model=>model.ClientId) -@Html.HiddenFor(model=>model.PerformerId) -@Html.HiddenFor(model=>model.ActivityCode) - - - + + @Html.HiddenFor(model=>model.ClientId) + @Html.HiddenFor(model=>model.PerformerId) + @Html.HiddenFor(model=>model.ActivityCode) + @Html.HiddenFor(model=>model.Location.Latitude) + @Html.Hidden("Location.Longitude")
- +@section scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } @{ await Html.RenderPartialAsync("_MapScriptsPartial"); } + +} \ No newline at end of file diff --git a/src/Yavsc/Views/Manage/SetActivity.cshtml b/src/Yavsc/Views/Manage/SetActivity.cshtml index 783acd06..c1c1fd66 100644 --- a/src/Yavsc/Views/Manage/SetActivity.cshtml +++ b/src/Yavsc/Views/Manage/SetActivity.cshtml @@ -99,7 +99,7 @@ -
    +
    @@ -118,8 +118,20 @@ @Html.Hidden("PerfomerId") - - +@section scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } @{ await Html.RenderPartialAsync("_MapScriptsPartial"); } - + +} diff --git a/src/Yavsc/Views/Manage/SetAddress.cshtml b/src/Yavsc/Views/Manage/SetAddress.cshtml index 000d8531..15a07f50 100644 --- a/src/Yavsc/Views/Manage/SetAddress.cshtml +++ b/src/Yavsc/Views/Manage/SetAddress.cshtml @@ -1,48 +1,48 @@ -@model SetAddressViewModel - +@model Location +@section header { - +}
    - +
    - + - +
    -
    +
    + + +
    - +@section scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } - -@section scripts { - - - - - +@{ await Html.RenderPartialAsync("_MapScriptsPartial"); } + } diff --git a/src/Yavsc/Views/Shared/_Layout.cshtml b/src/Yavsc/Views/Shared/_Layout.cshtml index f802641f..12a7bdf5 100644 --- a/src/Yavsc/Views/Shared/_Layout.cshtml +++ b/src/Yavsc/Views/Shared/_Layout.cshtml @@ -24,14 +24,6 @@ - - @RenderSection("header", required: false) diff --git a/src/Yavsc/Views/Shared/_MapScriptsPartial.cshtml b/src/Yavsc/Views/Shared/_MapScriptsPartial.cshtml index 21d4603e..88e089f9 100644 --- a/src/Yavsc/Views/Shared/_MapScriptsPartial.cshtml +++ b/src/Yavsc/Views/Shared/_MapScriptsPartial.cshtml @@ -1,108 +1,2 @@ - - - + diff --git a/src/Yavsc/wwwroot/js/google-geoloc.js b/src/Yavsc/wwwroot/js/google-geoloc.js index 5e25a50f..6498842c 100644 --- a/src/Yavsc/wwwroot/js/google-geoloc.js +++ b/src/Yavsc/wwwroot/js/google-geoloc.js @@ -1,71 +1,92 @@ if (typeof jQuery === 'undefined') { - throw new Error('Bootstrap\'s JavaScript requires jQuery') + throw new Error('This Google maps client script requires jQuery') +} +if (typeof google === 'undefined') { + throw new Error('This Google maps client script requires google') } -+ (function($, maps) { - $.widget("psc.googlegeocode", { + $.widget('psc.googlegeocode', { options: { + culture: 'fr', mapId: 'map', longId: 'Longitude', latId: 'Latitude', addrValidationId: 'AddressError', formValidId: 'ValidationSummary', - locComboId: 'LocationCombo' + locComboId: 'LocationCombo', + specifyPlaceMsg: 'Specify a place', + GoogleDidntGeoLocalizedMsg: 'Google didn\'t reconized this address' }, marker: null, gmap: null, - + onDragEnd: function(_this) { + // TODO reverse geo code + var npos = _this.marker.getPosition(); + var nlat = Number(npos.lat()); + var nlng = Number(npos.lng()); + $('#' + _this.options.latId).val(nlat.toLocaleString(_this.options.culture, { minimumFractionDigits: 8 })); + $('#' + _this.options.longId).val(nlng.toLocaleString(_this.options.culture, { minimumFractionDigits: 8 })); + }, _create: function() { - this.element.addClass("googlegeocode"); + var _this = this; + var scenter = { lat: parseFloat($('#' + _this.options.latId).val().replace(',', '.')), + lng: parseFloat($('#' + _this.options.longId).val().replace(',', '.')) }; + this.element.addClass('googlegeocode'); this.gmap = new maps.Map(document.getElementById(this.options.mapId), { zoom: 16, - center: { lat: 48.862854, lng: 2.2056466 } + center: scenter }); - var _this = this; - this.element.rules("add", { + this.marker = new maps.Marker({ + map: this.gmap, + draggable: true, + animation: maps.Animation.DROP, + position: scenter + }); + maps.event.addListener(this.marker, 'dragend', function() { _this.onDragEnd(_this) }); + this.element.data('val-required', this.options.specifyPlaceMsg); + this.element.data('val-remote', this.options.GoogleDidntGeoLocalizedMsg); + this.element.rules('add', { remote: { url: 'https://maps.googleapis.com/maps/api/geocode/json', type: 'get', data: { + key: _this.options.mapsApiKey, sensor: false, - address: function() {  return _this.element.val() } + address: function() { + return _this.element.val(); + } }, dataType: 'json', dataFilter: function(datastr) { - $('#' + _this.options.locComboId).html(""); + var ul = $('#' + _this.options.locComboId); + ul.html(''); var data = JSON.parse(datastr); data.results.forEach(function(item) { - if (item.formatted_address !== _this.element.val()) { - $('
  • ' + item.formatted_address + '
  • ') - .data("geoloc", item) - .click(function() { _this.chooseLoc('user', item) }) - .css('cursor', 'pointer') - .appendTo($('#' + _this.options.locComboId)); - } else {} + $('
  • ' + item.formatted_address + '
  • ') + .data('geoloc', item) + .click(function() { _this.chooseLoc('user', item) }) + .css('cursor', 'pointer') + .appendTo(ul); }); - if ((data.status === 'OK') && (data.results.length == 1)) { - // _this.chooseLoc('google',data.results[0]); + if ((data.status === 'OK') && (data.results.length >= 1)) { return true; } return false; - }, - error: function() { - // xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown); - return false; } } }) }, chooseLoc: function(sender, loc) { + var _this = this; if (sender === 'user') this.element.val(loc.formatted_address); var pos = loc.geometry.location; - var lat = new Number(pos.lat); - var lng = new Number(pos.lng); - $(document.getElementById(this.options.latId)).val(lat.toLocaleString('en')); - $(document.getElementById(this.options.longId)).val(lng.toLocaleString('en')); + var lat = Number(pos.lat); + var lng = Number(pos.lng); + $(document.getElementById(this.options.latId)).val(lat.toLocaleString(this.options.culture, { minimumFractionDigits: 8 })); + $(document.getElementById(this.options.longId)).val(lng.toLocaleString(this.options.culture, { minimumFractionDigits: 8 })); this.gmap.setCenter(pos); - if (this.marker) {  + if (this.marker) { this.marker.setMap(null); } this.marker = new maps.Marker({ @@ -74,18 +95,12 @@ if (typeof jQuery === 'undefined') { animation: maps.Animation.DROP, position: pos }); - maps.event.addListener(this.marker, 'dragend', function() { - // TODO reverse geo code - var pos = this.marker.getPosition(); - $('#' + this.options.latId).val(pos.lat); - $('#' + this.options.longId).val(pos.lng); - }); + maps.event.addListener(this.marker, 'dragend', function() { _this.onDragEnd(_this) }); this.element.valid(); $('#' + this.options.addrValidationId).empty(); $('#' + this.options.formValidId).empty(); $('#' + this.options.locComboId).empty(); - return this; } }) -})(jQuery, google.maps); \ No newline at end of file +})(jQuery, google.maps);