import dojo = require("dojo"); import declare = require("dojo/_base/declare"); declare global { namespace BGA { interface AjaxActions { "/player/profile/updateCity.html": { form_id: "profileinfos" }; "/table/table/updateCity.html": { form_id: "profileinfos" }; } } } class PlayerLocation_Template { page: InstanceType | null = null; board_div: HTMLElement | null = null; board_div_id: string | null = null; board_uid: string = "_"; template: string = '
${my_city}: ${position} [${LB_CHANGE}]
${POSITION_TEASING}
${MY_CITY}: ${LB_OK}
'; teasing: string = ""; googleApiLoaded: boolean = true; jtpl_citychoice: string = "${description}
"; locationDialog: any = null; cityChoiceResult: any = null; callback_url: string = ""; create(t: InstanceType, i: string, n: string, o: boolean, a: string) { this.page = t; this.board_div = dojo.byId(i); this.board_div_id = i; this.teasing = n; this.callback_url = a; var s = { my_city: _("My city"), my_position_visibility: "" != $("initial_position")!.innerHTML ? "block" : "none", position: $("initial_position")!.innerHTML, LB_CHANGE: _("LB_CHANGE"), POSITION_TEASING: this.teasing, MY_POSITION_TEASING: "" != $("initial_position")!.innerHTML ? "none" : "block", MY_CITY: _("My city"), INITIAL_CITY: $("initial_city")!.innerHTML, LB_OK: _("LB_OK"), }; var r = true; if ($("upperrightmenu_loggedin") ? "none" != dojo.style("upperrightmenu_loggedin", "display") && (r = true) // @ts-ignore - This looks like a typo. style should be requesting the display of the element. : $("disconnected_player_menu") && "none" != dojo.style("disconnected_player_menu") && (r = true), r ) { dojo.place(dojo.string.substitute(this.template, s), this.board_div!); dojo.connect($("savecity")!, "onclick", this as PlayerLocation_Template, "onSaveCity"); dojo.connect($("modifycity")!, "onclick", this as PlayerLocation_Template, "onModifyCity"); if (o) { var l = document.createElement("script"); l.type = "text/javascript"; var d = dojoConfig.locale.substr(0, 2); l.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyCrPx3aUElpdWAGTyxd-t9w92er94Nfxjk&sensor=false&callback=initGoogleApi&language=" + d; initGoogleApi = dojo.hitch(this, function () { this.googleApiLoaded = true; }); document.body.appendChild(l); } } } onModifyCity(t: Event) { dojo.stopEvent(t); dojo.style("current_localization", "display", "none"); dojo.style("current_localization_teasing", "display", "block"); } onSaveCity(t: Event) { dojo.stopEvent(t); var i = $("cityinput")!.value, n = $("country")!.value; if ("" != i) if ( "undefined" != typeof google && undefined !== google.maps && undefined !== google.maps.Geocoder ) { geocoder = new google.maps.Geocoder(); geocoder.geocode( { address: i, language: dojoConfig.locale.substr( 0, 2 ), region: n, }, dojo.hitch(this as PlayerLocation_Template, function (t, i) { if ( i == google.maps.GeocoderStatus.OK ) { this.cityChoiceResult = t; $("locationDialog_content") && dojo.destroy( "locationDialog_content" ); this.locationDialog = new ebg.popindialog(); this.locationDialog.create( "locationDialog" ); this.locationDialog.setTitle( _("Please confirm your city") ); this.locationDialog.setMaxWidth( 500 ); var n = "
"; for (var o in t) { var a = t[o], s = getLocationDescriptionFromResult( a ); n += dojo.string.substitute( this.jtpl_citychoice, { id: o, description: s, checked: 0 == toint(o) ? "checked='checked'" : "", } ); } n += "
"; n += " " + _( "Show my city to other players" ) + ' ("' + $("cityinput")!.value + '")'; n += "
"; n += "
"; n += "
"; this.locationDialog.setContent(n); this.locationDialog.show(); dojo.connect( $("validCityChoice")!, "onclick", this as PlayerLocation_Template, "onCityChoiceConfirm" ); } // @ts-ignore - Are these "showMessage" calls supposed to be "page.showMessage"? else i == google.maps.GeocoderStatus.ZERO_RESULTS ? this.showMessage("Sorry, we couldn't found your city", "error") : this.showMessage("Google Maps error: " + i, "error"); }) ); } else this.page!.showMessage( _("Failed to load Google Maps"), "error" ); } onCityChoiceConfirm(t: Event) { dojo.stopEvent(t); var i: string = String(0); dojo.query(".cityChoiceLink").forEach(function (e) { e.checked && (i = e.id.substr(15)); }); var n = this.cityChoiceResult[i], o = parseFloat(n.geometry.location.lat()), a = parseFloat(n.geometry.location.lng()); $("lon")!.value = String(a); $("lat")!.value = String(o); var s = analyseLocationDescriptionFromResult(n); $("loc_city")!.value = s.city; $("loc_area1")!.value = s.area1; $("loc_area2")!.value = s.area2; $("loc_country")!.value = s.country; $("city")!.value = $("cityinput")!.value; $("loc_cityprivacy")!.value = $("cityprivacy")!.checked ? String(0) : String(1); this.locationDialog.destroy(); "undefined" != typeof mainsite ? this.page!.ajaxcall( "/player/profile/updateCity.html", { form_id: "profileinfos" }, this, function () { this.page!.showMessage( __( "lang_mainsite", "Profile informations updated !" ), "info" ); mainsite.gotourl_forcereload( this.callback_url ); }, function (e) {}, "post" ) : this.page!.ajaxcall( "/table/table/updateCity.html", { form_id: "profileinfos" }, this, function () { this.page!.showMessage( __( "lang_mainsite", "Profile informations updated !" ), "info" ); location.reload(); }, function (e) {}, "post" ); } } let PlayerLocation = declare("ebg.playerlocation", PlayerLocation_Template); export = PlayerLocation; declare global { namespace BGA { type PlayerLocation = typeof PlayerLocation; interface EBG { playerlocation: PlayerLocation; } } var ebg: BGA.EBG; var initGoogleApi: () => void; var geocoder: { geocode: (t: { address: string; language: string; region: string; }, i: (t: any, i: any) => void) => void; }; }