Add poster name saving via cookie
When a user makes a new post with a name set, it will get saved via a cookie to the user's client and it will automatically get set the next time a user makes a post. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
const NAME_COOKIE = "posting_as";
|
||||
const OPEN = "open";
|
||||
const CLOSED = "closed";
|
||||
const replyWindowName = "reply-window";
|
||||
@@ -5,6 +6,35 @@ const postWindowName = "post-window";
|
||||
const reportWindowName = "report-window"
|
||||
const WINDOW_INNER_PADDING = 25;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Cookie functions, stolen from W3Schools
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
function setCookie(cname, cvalue, expseconds) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + (expseconds * 1000));
|
||||
let expires = "expires=" + d.toUTCString();
|
||||
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
|
||||
}
|
||||
|
||||
function getCookie(cname) {
|
||||
let name = cname + "=";
|
||||
let ca = document.cookie.split(';');
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i];
|
||||
while (c.charAt(0) == ' ') {
|
||||
c = c.substring(1);
|
||||
}
|
||||
if (c.indexOf(name) == 0) {
|
||||
return c.substring(name.length, c.length);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Document-specific functions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
function documentClick(e) {
|
||||
let sender = e.target;
|
||||
let id = sender.getAttribute("data-id");
|
||||
@@ -82,7 +112,13 @@ function openReplyWindow(replyUrl) {
|
||||
});
|
||||
$(replyWindow.iframe, "iframe").on("load", () => {
|
||||
fitWindowToContent(replyWindow);
|
||||
replyWindow.$("#id_text").focus();
|
||||
// the iframe load event gets fired on the success page as well as the
|
||||
// new post page. We just want to target the text area on the new post
|
||||
// page.
|
||||
let textarea = replyWindow.$("#id_text");
|
||||
if (textarea) {
|
||||
textarea.focus();
|
||||
}
|
||||
});
|
||||
replyWindow.show();
|
||||
return replyWindow;
|
||||
|
||||
@@ -55,4 +55,15 @@ input[type=text] {
|
||||
<tr><td> </td><td><input type="submit" value="Submit" /></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
$("#submit").on("click", (e) => {
|
||||
let name = $("#{{form.name.auto_id}}");
|
||||
setCookie(NAME_COOKIE, name.val(), 3600 * 24 * 28);
|
||||
});
|
||||
|
||||
$(window).on("load", (e) => {
|
||||
$("#{{form.name.auto_id}}").val(getCookie(NAME_COOKIE));
|
||||
});
|
||||
</script>
|
||||
{% endblock content %}
|
||||
@@ -67,7 +67,18 @@ input[type=text] {
|
||||
{{ max_upload_size|measure_bytes }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr><td> </td><td><input type="submit" value="Submit" /></td></tr>
|
||||
<tr><td> </td><td><input id="submit" type="submit" value="Submit" /></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
$("#submit").on("click", (e) => {
|
||||
let name = $("#{{form.name.auto_id}}");
|
||||
setCookie(NAME_COOKIE, name.val(), 3600 * 24 * 28);
|
||||
});
|
||||
|
||||
$(window).on("load", (e) => {
|
||||
$("#{{form.name.auto_id}}").val(getCookie(NAME_COOKIE));
|
||||
});
|
||||
</script>
|
||||
{% endblock content %}
|
||||
Reference in New Issue
Block a user