{{define "collection"}}
{{template "header" .}}

<style>
textarea.section.norm {
	font-family: Lora,'Palatino Linotype','Book Antiqua','New York','DejaVu serif',serif !important;
	min-height: 10em;
	max-height: 20em;
	resize: vertical;
}
@media (pointer: coarse) {
	.codable {
		font-size: 0.75em !important;
		height: 17em !important;
	}
}
</style>

<div class="content-container snug">
	<div id="overlay"></div>

	{{if .Silenced}}
		{{template "user-silenced"}}
	{{end}}

	{{template "collection-breadcrumbs" .}}

	<h1>Customize</h1>

	{{template "collection-nav" (dict "Alias" .Alias "Path" .Path "SingleUser" .SingleUser)}}

	{{if .Flashes}}<ul class="errors">
		{{range .Flashes}}<li class="urgent">{{.}}</li>{{end}}
	</ul>{{end}}

<form name="customize-form" action="/api/collections/{{.Alias}}" method="post" onsubmit="return disableSubmit()">
<div id="collection-options">
	<div style="text-align:center">
		<h1><input type="text" name="title" id="title" value="{{.DisplayTitle}}" placeholder="Title" maxlength="255" /></h1>
		<p><input type="text" name="description" id="description" value="{{.Description}}" placeholder="Description" maxlength="160" /></p>
	</div>

	<div class="option">
		<h2><a name="preferred-url"></a>URL</h2>
		<div class="section">
			{{if eq .Alias .Username}}<p style="font-size: 0.8em">This blog uses your username in its URL{{if .Federation}} and fediverse handle{{end}}. You can change it in your <a href="/me/settings">Account Settings</a>.</p>{{end}}
			<ul style="list-style:none">
				<li>
					{{.FriendlyHost}}/<strong>{{.Alias}}</strong>/
				</li>
				<li>
					<strong id="normal-handle-env" class="fedi-handle" {{if not .Federation}}style="display:none"{{end}}>@<span id="fedi-handle">{{.Alias}}</span>@<span id="fedi-domain">{{.FriendlyHost}}</span></strong>
				</li>
			</ul>
		</div>
	</div>

	<div class="option">
		<h2>Publicity</h2>
		<div class="section">
			<ul style="list-style:none">
				<li>
					<label><input type="radio" name="visibility" id="visibility-unlisted" value="0" {{if .IsUnlisted}}checked="checked"{{end}} />
						Unlisted
					</label>
					<p>This blog is visible to {{if .Private}}any registered user on this instance{{else}}anyone with its link{{end}}.</p>
				</li>
				<li>
				<label class="option-text"><input type="radio" name="visibility" id="visibility-private" value="2" {{if .IsPrivate}}checked="checked"{{end}} />
						Private
					</label>
					<p>Only you may read this blog (while you're logged in).</p>
				</li>
				<li>
					<label class="option-text"><input type="radio" name="visibility" id="visibility-protected" value="4" {{if .IsProtected}}checked="checked"{{end}} />
						Password-protected: <input type="password" class="low-profile" name="password" id="collection-pass" autocomplete="new-password" placeholder="{{if .IsProtected}}xxxxxxxxxxxxxxxx{{else}}a memorable password{{end}}" />
					</label>
					<p>A password is required to read this blog.</p>
				</li>
				{{if not .SingleUser}}
				<li>
					<label class="option-text{{if not .LocalTimeline}} disabled{{end}}"><input type="radio" name="visibility" id="visibility-public" value="1" {{if .IsPublic}}checked="checked"{{end}} {{if not .LocalTimeline}}disabled="disabled"{{end}} />
						Public
					</label>
					{{if .LocalTimeline}}<p>This blog is displayed on the public <a href="/read">reader</a>, and is visible to {{if .Private}}any registered user on this instance{{else}}anyone with its link{{end}}.</p>
					{{else}}<p>The public reader is currently turned off for this community.</p>{{end}}
				</li>
				{{end}}
			</ul>
		</div>
	</div>

	<div class="option">
		<h2 id="updates">Updates</h2>
		<div class="section">
			<p class="explain">Keep readers updated with your latest posts wherever they are.</p>
			<ul style="list-style:none">
				<li>
					<label class="option-text"><input type="checkbox" checked="checked" disabled />
						RSS feed
					</label>
					<p class="describe">Readers can subscribe to your blog's <a href="{{.CanonicalURL}}feed/" target="feed">RSS feed</a> with their favorite RSS reader.</p>
				</li>
				{{if .EmailCfg.Enabled}}
				<li>
					<label class="option-text" id="email-sub-label"><input type="checkbox" name="email_subs" id="email_subs" {{if .EmailSubsEnabled}}checked="checked"{{end}} />
						Email subscriptions
					</label>
					<p class="describe">
						Let readers subscribe to your blog via email, and optionally accept private replies.
					</p>
					<div id="custom-letter-reply" style="font-size: .8em; margin-top: -0.5em; margin-left: 1.8em; margin-bottom: 1em;" {{if not .EmailSubsEnabled}}style="display:none"{{end}}>
						Allow replies to this address:
						<input type="email" name="letter_reply" id="letter_reply" placeholder="me@example.com" value="{{.LetterReplyTo}}" {{if not .EmailSubsEnabled}}disabled{{end}} />
					</div>
				</li>
				{{end}}
                {{if .Federation}}
				<li>
					<label class="option-text" id="federate-label"><input type="checkbox" name="federate" id="federate" {{if .Federation}}checked="checked"{{end}} disabled />
						Federation
					</label>
					<strong id="normal-handle-env" class="fedi-handle">@<span id="fedi-handle">{{.Alias}}</span>@<span id="fedi-domain">{{.FriendlyHost}}</span></strong>
					<p class="describe">Allow others to follow your blog and interact with your posts in the fediverse. <a href="https://video.writeas.org/videos/watch/cc55e615-d204-417c-9575-7b57674cc6f3" target="video">See how it works</a>.</p>
				</li>
				{{end}}
			</ul>
		</div>
	</div>

	<div class="option">
		<h2>Display Format</h2>
		<div class="section">
			<p class="explain">Customize how your posts display on your page.
			</p>
			<ul style="list-style:none">
				<li>
					<label><input type="radio" name="format" id="format-blog" value="blog" {{if or (not .Format) (eq .Format "blog")}}checked="checked"{{end}} />
						Blog
					</label>
					<p>Dates are shown. Latest posts listed first.</p>
				</li>
				<li>
					<label class="option-text"><input type="radio" name="format" id="format-novel" value="novel" {{if eq .Format "novel"}}checked="checked"{{end}} />
						Novel
					</label>
					<p>No dates shown. Oldest posts first.</p>
				</li>
				<li>
					<label class="option-text"><input type="radio" name="format" id="format-notebook" value="notebook" {{if eq .Format "notebook"}}checked="checked"{{end}} />
						Notebook
					</label>
					<p>No dates shown. Latest posts first.</p>
				</li>
			</ul>
		</div>
	</div>

	<div class="option">
		<h2>Text Rendering</h2>
		<div class="section">
			<p class="explain">Customize how plain text renders on your blog.</p>
			<ul style="list-style:none">
				<li>
					<label class="option-text disabled"><input type="checkbox" name="markdown" checked="checked" disabled />
						Markdown
					</label>
				</li>
				<li>
					<label><input type="checkbox" name="mathjax" {{if .RenderMathJax}}checked="checked"{{end}} />
						MathJax
					</label>
				</li>
			</ul>
		</div>
	</div>

	<div class="option">
		<h2>Custom CSS</h2>
		<div class="section">
			<textarea id="css-editor" class="section codable" name="style_sheet">{{.StyleSheet}}</textarea>
			<p class="explain">See our guide on <a href="https://guides.write.as/customizing/#custom-css">customization</a>.</p>
		</div>
	</div>

	<div class="option">
		<h2>Post Signature</h2>
		<div class="section">
			<p class="explain">This content will be added to the end of every post on this blog, as if it were part of the post itself. Markdown, HTML, and shortcodes are allowed.</p>
			<textarea id="signature" class="section norm" name="signature">{{.Signature}}</textarea>
		</div>
	</div>

	<div class="option">
		<h2>Verification</h2>
		<div class="section">
			<p class="explain">Verify that you own another site on the open web, fediverse, etc. For example, enter your Mastodon profile address here, then on Mastodon add a link back to this blog &mdash; it will show up as <a href="https://joinmastodon.org/verification" target="mastoverified">verified</a> there.</p>
			<input type="text" name="verification_link" style="width:100%" value="{{.Verification}}" placeholder="https://writing.exchange/@writefreely" />
			<p class="explain">This adds a <code>rel="me"</code> code in your blog's <code>&lt;head&gt;</code>.</p>
		</div>
	</div>

	{{if .UserPage.StaticPage.AppCfg.Monetization}}
	<div class="option">
		<h2>Web Monetization</h2>
		<div class="section">
			<p class="explain">Web Monetization enables you to receive micropayments from readers via <a href="https://interledger.org">Interledger</a>. Add your payment pointer to enable Web Monetization on your blog.</p>
			<input type="text" name="monetization_pointer" style="width:100%" value="{{.Collection.Monetization}}" placeholder="$wallet.example.com/alice" />
		</div>
	</div>
	{{end}}

	<div class="option" style="text-align: center; margin-top: 4em;">
		<input type="submit" id="save-changes" value="Save changes" />
		<p><a href="{{if .SingleUser}}/{{else}}/{{.Alias}}/{{end}}">View Blog</a></p>
		{{if ne .Alias .Username}}<p><a class="danger" href="#modal-delete" onclick="promptDelete();">Delete Blog...</a></p>{{end}}
	</div>
</div>
</form>
</div>

		<div id="modal-delete" class="modal">
			<h2>Are you sure you want to delete this blog?</h2>
			<div class="body short">
				<p style="text-align:left">This will permanently erase <strong>{{.DisplayTitle}}</strong> ({{.FriendlyHost}}/{{.Alias}}) from the internet. Any posts on this blog will be saved and made into drafts (found on your <a href="/me/posts/">Drafts</a> page).</p>
				<p>If you're sure you want to delete this blog, enter its name in the box below and press <strong>Delete</strong>.</p>

				<ul id="delete-errors" class="errors"></ul>

				<input id="confirm-text" placeholder="{{.Alias}}" type="text" class="boxy" style="margin-top: 0.5em;" />
				<div style="text-align:right; margin-top: 1em;">
					<a id="cancel-delete" style="margin-right:2em" href="#">Cancel</a>
					<button id="btn-delete" class="danger" onclick="deleteBlog(); return false;">Delete</button>
				</div>
			</div>
		</div>

<script src="/js/h.js"></script>
<script src="/js/modals.js"></script>
<script src="/js/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
H.getEl('cancel-delete').on('click', closeModals);
var deleteBlog = function(e) {
	if (document.getElementById('confirm-text').value != '{{.Alias}}') {
		document.getElementById('delete-errors').innerHTML = '<li class="urgent">Enter <strong>{{.Alias}}</strong> in the box below.</li>';
		return;
	}
	// Clear errors
	document.getElementById('delete-errors').innerHTML = '';
	document.getElementById('btn-delete').innerHTML = 'Deleting...';

	var http = new XMLHttpRequest();
	var url = "/api/collections/{{.Alias}}?web=1";
	http.open("DELETE", url, true);
	http.setRequestHeader("Content-type", "application/json");
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if (http.status == 204) {
				window.location = '/me/c/';
			} else {
				var data = JSON.parse(http.responseText);
				document.getElementById('delete-errors').innerHTML = '<li class="urgent">'+data.error_msg+'</li>';
				document.getElementById('btn-delete').innerHTML = 'Delete';
			}
		}
	};
	http.send(null);
};

function createHidden(theForm, key, value) {
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = key;
    input.value = value;
    theForm.appendChild(input);
}
function disableSubmit() {
	var $form = document.forms['customize-form'];
	createHidden($form, 'style_sheet', cssEditor.getSession().getValue());
	var $btn = document.getElementById("save-changes");
	$btn.value = "Saving changes...";
	$btn.disabled = true;
	return true;
}
function promptDelete() {
	showModal("delete");
}

var $fediDomain = document.getElementById('fedi-domain');
var $fediCustomDomain = document.getElementById('fedi-custom-domain');
var $customDomain = document.getElementById('domain-alias');
var $customHandleEnv = document.getElementById('custom-handle-env');
var $normalHandleEnv = document.getElementById('normal-handle-env');

var $emailSubsCheck = document.getElementById('email_subs');
var $letterReply = document.getElementById('letter_reply');
H.getEl('email_subs').on('click', function() {
    let show = $emailSubsCheck.checked
    $letterReply.disabled = !show
})

if (matchMedia('(pointer:fine)').matches) {
	// Only initialize Ace editor on devices with a mouse
	var opt = {
		showLineNumbers: false,
		showPrintMargin: 0,
		minLines: 10,
		maxLines: 40,
	};
	var theme = "ace/theme/chrome";
	var cssEditor = ace.edit("css-editor");
	cssEditor.setTheme(theme);
	cssEditor.session.setMode("ace/mode/css");
	cssEditor.setOptions(opt);
	cssEditor.resize(true);
}
</script>

{{template "footer" .}}
{{end}}
