{% extends "default.html" %}
{%- import 'component/sorting_time.html' as sorting_time -%}
{% block desktopBanner %}

{% if v and FINGERPRINT_TOKEN %}
	{% if not v.fp %}
		<script>
			function fp(fp) {
				const xhr = new XMLHttpRequest();
				xhr.open("POST", '/fp/'+fp);
				xhr.setRequestHeader('xhr', 'xhr');
				var form = new FormData()
				form.append("formkey", formkey());
				xhr.send(form);
			};

			const fpPromise = new Promise((resolve, reject) => {
				const script = document.createElement('script');
				script.onload = resolve;
				script.onerror = reject;
				script.async = true;
				script.src = "{{ 'js/fp.js' | asset }}";
				document.head.appendChild(script);
			})
				.then(() => FingerprintJS.load({token: '{{FINGERPRINT_TOKEN}}'}));

			fpPromise
				.then(fp => fp.get())
				.then(result => {if (result.visitorId != '{{v.fp}}') fp(result.visitorId);})
		</script>
	{% endif %}
{% endif %}
  
<div class="row" style="overflow: visible;padding-top:5px;">
	<div class="col">
		<div class="d-flex justify-content-between align-items-center pt-3 pb-2 sorting" style="float:right">

			{% block navbar %}
			<div class="d-flex align-items-center">
				<a href="/submit" class="btn btn-secondary ml-2 mr-2 d-nowrap">
					<i class="fas fa-edit mr-2 "></i>
					Post
				</a>

				<div class="dropdown dropdown-actions mx-2">
					<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
						{% if t=="hour" %}<i class="fas fa-clock mr-2 "></i>
						{% elif t=="day" %}<i class="fas fa-calendar-day mr-2 "></i>
						{% elif t=="week" %}<i class="fas fa-calendar-week mr-2 "></i>
						{% elif t=="month" %}<i class="fas fa-calendar-alt mr-2 "></i>
						{% elif t=="year" %}<i class="fas fa-calendar mr-2 "></i>
						{% elif t=="all" %}<i class="fas fa-infinity mr-2 "></i>
						{% endif %}
						{{t | capitalize}}
					</button>
					<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 31px, 0px);">
						{% if t != "hour" %}<a class="dropdown-item" href="?sort={{sort}}&t=hour"><i class="fas fa-clock mr-2 "></i>Hour</a>{% endif %}
						{% if t != "day" %}<a class="dropdown-item" href="?sort={{sort}}&t=day"><i class="fas fa-calendar-day mr-2 "></i>Day</a>{% endif %}
						{% if t != "week" %}<a class="dropdown-item" href="?sort={{sort}}&t=week"><i class="fas fa-calendar-week mr-2 "></i>Week</a>{% endif %}
						{% if t != "month" %}<a class="dropdown-item" href="?sort={{sort}}&t=month"><i class="fas fa-calendar-alt mr-2 "></i>Month</a>{% endif %}
						{% if t != "year" %}<a class="dropdown-item" href="?sort={{sort}}&t=year"><i class="fas fa-calendar mr-2 "></i>Year</a>{% endif %}
						{% if t != "all" %}<a class="dropdown-item" href="?sort={{sort}}&t=all"><i class="fas fa-infinity mr-2 "></i>All</a>{% endif %}
					</div>
				</div>
				{{sorting_time.sort_dropdown(sort, t, SORTS_POSTS)}}
			</div>
			{% endblock %}
		</div>
	</div>
</div>

{% endblock %}
{% block content %}
<div class="row no-gutters {% if listing %}mt-md-3{% elif not listing %}my-md-3{% endif %}">
	<div class="col-12">
		<div class="posts" id="posts">
			{% include "submission_listing.html" %}
		</div>
	</div>
</div>
{% endblock %}

{% block pagenav %}
{% if listing %}
<nav aria-label="Page navigation">
	<ul class="pagination pagination-sm mb-0">
		{% if page>1 %}
		<li class="page-item">
			<small><a class="page-link" href="?sort={{sort}}&page={{page-1}}&t={{t}}" tabindex="-1">Prev</a></small>
		</li>
		{% else %}
		<li class="page-item disabled"><span class="page-link">Prev</span></li>
		{% endif %}
		{% if next_exists %}
		<li class="page-item">
			<small><a class="page-link" href="?sort={{sort}}&page={{page+1}}&t={{t}}">Next</a></small>
		</li>
		{% else %}
		<li class="page-item disabled"><span class="page-link">Next</span></li>
		{% endif %}
	</ul>
</nav>
{% endif %}

{% if PUSHER_ID != 'blahblahblah' and v %}
	<div class="d-none" id="strid">{{request.host}}{{v.id}}</div>
	<div class="d-none" id="pusherid">{{PUSHER_ID}}</div>
	<script src="{{ 'js/pusher.js' | asset }}"></script>
	<script>
		if (typeof Android != 'undefined') {
			Android.Subscribe('{{request.host}}{{v.id}}');
		}
	</script>
{% endif %}

{% if request.path in ('/','/logged_out') and g.timestamp > session.get('tooltip_last_dismissed',0)+60*60*24*30 and not g.webview %}
	<style>
		.beg-icon {
			color: #919191;
			float: left;
			font-size: 10px;
			margin-top: 0.25rem;
			margin-right: 0.25rem;
		}
	</style>

	<div id="mobile-prompt-container" class="fixed-top">
		<div id="mobile-prompt" href="javascript:void(0)" data-bs-toggle="tooltip" data-bs-container="#mobile-prompt-container" data-bs-placement="top" data-bs-trigger="click" data-bs-html="true" data-bs-original-title="<i class='beg-icon fas fa-x'></i>Install the {{SITE_TITLE}} webapp by saving this page to your home screen!"></div>
	</div>

	<script>
		if (!("standalone" in window.navigator) && !(window.navigator.standalone)) {
			if (window.innerWidth <= 737) {
				const tt = bootstrap.Tooltip.getOrCreateInstance(document.getElementById('mobile-prompt'))
				tt.show()
				document.getElementsByClassName('tooltip')[0].onclick = function(event){
					tt.hide()
					var xhr = new XMLHttpRequest();
					xhr.withCredentials=true;
					xhr.open("POST", '/dismiss_mobile_tip', true);
					xhr.send();
				}
			}
		} 
	</script>

	<style>
		#mobile-prompt + .bs-tooltip-bottom {
			transform: None !important;
			inset: 0px 0px auto auto !important;
		}
	</style>
{% endif %}

{% endblock %}
