From be3b085f81fcc4fc3cc122849b50ff29ac39feaa Mon Sep 17 00:00:00 2001 From: TonyFord <tonyford@strategy-investor.de> Date: Mon, 3 Feb 2020 17:53:04 +0100 Subject: [PATCH] add distance calculation --- _data/repositories.yml | 2 +- _includes/overview.html | 11 ++++----- _layouts/default.html | 1 + assets/js/script.js | 49 +++++++++++++++++++++++++++++++++++++++++ css/main.scss | 5 +++++ script/get_orders.py | 10 +++++++++ 6 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 assets/js/script.js diff --git a/_data/repositories.yml b/_data/repositories.yml index 5b1c10b..308720a 100644 --- a/_data/repositories.yml +++ b/_data/repositories.yml @@ -1,2 +1,2 @@ sources: - - https://git.fairkom.net/tonyford/myp2p-orders + - https://git.fairkom.net/tonyford/myp2p diff --git a/_includes/overview.html b/_includes/overview.html index 0381927..39aa47f 100644 --- a/_includes/overview.html +++ b/_includes/overview.html @@ -9,7 +9,7 @@ {% for c in cat %} {% capture ID %}{{ site.data.categories[c].ID }}{% endcapture %} - <li onclick="$('.orderlist-div').toggleClass('d-none',true); $('.cat-{{ ID }}').toggleClass('d-none',false); $('.categories li').toggleClass('active', false ); $(this).toggleClass('active',true);">{{ site.data.categories[c].title | map: include.ln }} [{{ site.categories[ID] | size }}]</li> + <li onclick="get_by_cat(this,'{{ID}}')">{{ site.data.categories[c].title | map: include.ln }} [{{ site.categories[ID] | size }}]</li> {% endfor %} </ul> </div> @@ -21,17 +21,18 @@ <div class="orderlist-div cat-{{ ID }}"> <div class="orderlist-date"> - {{ o.date | slice: 0,10 }} <div class="image">{% if o.image-profile == 'img' %}<img src="{{ o.image }}">{% else if o.image-profile == 'imgbb'%}{{ o.image }}{% endif %}</div> </div> <div class="orderlist-title"> - <u>{{ o.headline | truncate: 32 }}</u><br> - <small><lang>#{{ o.ln }}</lang> <used>#{{ o.condition }}</used> <tags>#{{ o.tags }}</tags></small><br> + <a href="{{ o.git-repo }}/blob/master/{{ o.order-title }}" target="git-order"><u>{{ o.headline | truncate: 32 }}</u></a> <a href="{{ o.git-profile }}" target="git-profile"><span class="feather-icon icon-user"></span></a> <a href="{{ o.git-repo }}/issues/new?issue[title]={{ o.order-title }}" target="git-contact"><span class="feather-icon icon-mail"></span></a><br> + <small><lang><span class="feather-icon icon-tag"></span>{{ o.ln }}</lang> <used><span class="feather-icon icon-tag"></span>{{ o.condition }}</used> <tags><span class="feather-icon icon-tag"></span>{{ o.tags }}</tags> <span class="d-none distance-data">{{ o.latitude }},{{ o.longitude }}</span> <span class="distance"></span></small><br> <div><small>{{ o.description }}</small></div> </div> - <div class="orderlist-price">{% if o.min-size > 0 %}{{ o.min-size }}..{% endif %}{{ o.max-size }} {{ o.unit }}<br>( {{ o.price-per-unit | decimals }} {{ o.price-currency }} / {{ o.unit }} )<br>( {% capture price_ex %}{{ site.data.price-sources[o.price-source].price[o.price-currency] }}{% endcapture %}{% if price_ex != "" %}{{ price_ex }}{% else %}{{ o.price-source }}{% endif %} {{ o.price-currency }} / FAIR )<br><i>{% if price_ex != "" %}<a href="{{ site.data.price-sources[o.price-source].www }}">{{ o.price-source }}</a>{% else %}fixed price{% endif %}</i></div> + <div class="orderlist-price">{% if o.min-size > 0 %}{{ o.min-size }}..{% endif %}{{ o.max-size }} {{ o.unit }}<br>( {{ o.price-per-unit | decimals }} {{ o.price-currency }} / {{ o.unit }} )<br>( {% capture price_ex %}{{ site.data.price-sources[o.price-source].price[o.price-currency] }}{% endcapture %}{% capture price %}{% if price_ex != "" %}{{price_ex}}{% else %}{{ o.price-source }}{% endif %}{% endcapture %}{{ price }} {{ o.price-currency }} / FAIR )<br><i>{% if price_ex != "" %}<a href="{{ site.data.price-sources[o.price-source].www }}">{{ o.price-source }}</a>{% else %}fixed price{% endif %}</i> + <br><br><font class="faircoin-price">{{ o.price-per-unit | divided_by: price | decimals }} FAIR</font> + </div> </div> {% endfor %} {% endfor %} diff --git a/_layouts/default.html b/_layouts/default.html index 6537079..aba7276 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -11,6 +11,7 @@ <script src="https://cdn.rawgit.com/michalsnik/aos/2.1.1/dist/aos.js"></script> <script src="{{ site.baseurl }}/assets/js/simple-jekyll-search.js"></script> <script src="{{ site.baseurl }}/assets/js/jquery.min.js"></script> + <script src="{{ site.baseurl }}/assets/js/script.js"></script> <script> AOS.init({ once: true diff --git a/assets/js/script.js b/assets/js/script.js new file mode 100644 index 0000000..404f4f5 --- /dev/null +++ b/assets/js/script.js @@ -0,0 +1,49 @@ +$( document ).ready(function() { + get_distances(); +}); + +function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { + var R = 6371; // Radius of the earth in km + var dLat = deg2rad(lat2-lat1); // deg2rad below + var dLon = deg2rad(lon2-lon1); + var a = + Math.sin(dLat/2) * Math.sin(dLat/2) + + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * + Math.sin(dLon/2) * Math.sin(dLon/2) + ; + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + var d = R * c; // Distance in km + return d; +} + +function deg2rad(deg) { + return deg * (Math.PI/180) +} + +function get_by_cat(obj, ID){ + $('.orderlist-div').toggleClass('d-none',true); + $('.cat-'+ID).toggleClass('d-none',false); + $('.categories li').toggleClass('active', false ); + $(obj).toggleClass('active',true); +} + +function get_distances(){ + var url_string = window.location.href; + var url = new URL(url_string); + var lat = url.searchParams.get('lat'); + var long = url.searchParams.get('long'); + if( lat == null || long == null ){ + lat=-100; + } + if( lat >= -90 && lat <= 90 && long >=-180 && long <= 180 ){ + } else { + return; + } + + $.each( $('.distance'), function(i,v){ + var geo=$( $(v).context.previousElementSibling ).text(); + var llat=geo.split(/,/g)[0]; + var llong=geo.split(/,/g)[1]; + $(v).html('<span class="feather-icon icon-map-pin"></span> ' + Math.round( getDistanceFromLatLonInKm (lat, long, llat, llong ) )+'km' ); + } ); +} diff --git a/css/main.scss b/css/main.scss index 399ae0f..8b861b1 100644 --- a/css/main.scss +++ b/css/main.scss @@ -187,3 +187,8 @@ lang { tags { color: darkgoldenrod; } + +.faircoin-price { + font-size:1rem; + color: goldenrod; +} diff --git a/script/get_orders.py b/script/get_orders.py index 7e96e81..44279c8 100644 --- a/script/get_orders.py +++ b/script/get_orders.py @@ -32,6 +32,11 @@ Sources=load( s, Loader=Loader )['sources'] for source in Sources: + G=source.split('/') + gituser=G[-2] + gitrepo=G[-1] + giturl='/'.join(G[0:-2]) + contents = urlopen( source + '/raw/master/index.yml' ).read() O=load( contents, Loader=Loader ) @@ -42,6 +47,11 @@ for source in Sources: file = open('../_posts/'+Date + '-' +furl+'.md','w') file.write('---\n') file.write('layout: none\n') + file.write('git-repo: '+source+'\n') + file.write('git-profile: '+giturl + '/'+gituser+'\n' ) + file.write('order-title: '+furl+'\n' ) + file.write('latitude: '+str( O['latitude'] )+'\n' ) + file.write('longitude: '+str( O['longitude'] )+'\n' ) file.write(contents.decode("utf-8")) file.write('\n---') file.close() -- GitLab