aboutsummaryrefslogtreecommitdiffstats
path: root/templates/manage.html.j2
blob: 551e9413344d9ffcb2a466937ab70dd1a3745240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{% extends "base.html.j2" %}

{% block head_meta %}
<title>Manage &quot;{{ poll.title }}&quot; - diddle 👉👈</title>
<meta name="description" content="{{ poll.description }}">
<meta property="og:title" content="Manage &quot;{{ poll.title }}&quot;">
<meta property="og:description" content="Manage diddle 👉👈 poll">
<meta property="og:url" content="{{ poll.manage_url() }}">
<meta property="og:site_name" content="diddle">
<meta property="og:type" content="website">
{% endblock %}

{% block content %}

<h2>Managing <span class="contrast">{{ poll.title }}</span> by <i>{{ poll.author_name }}</i></h2>
<p>{{ poll.pub_date_formatted_notz() }}.</p>

<div>
  Share this URL with your peers:<br>
  <a id="share-link" href="{{ poll.share_url() }}">{{ poll.share_url() }}</a> <div id="share-link-copy"></div>
</div>
<p></p>
<div>
  <strong>Note!</strong> You can only edit this diddle if you have this URL or use the same browser session:<br>
  <a id="manage-link" href="{{ poll.manage_url() }}">{{ poll.manage_url() }}</a>
</div>
<p></p>

<form class="poll-form" action="/manage/{{ poll.manage_code }}/update_info" method="post">
  {% include "poll_info_table.html.j2" %}
  <p></p>
  <input type="submit" value="Update">
</form>

<div>
  <h3>Options</h3>
</div>

<table class="manage-table">
  <thead>
    <tr>
      <th>{% if poll.is_whole_day %}Start date{% else %}Start time{% endif %}</th>
      <th>{% if poll.is_whole_day %}End date{% else %}End time{% endif %}</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    {% for choice in poll.choices %}
    <form action="/manage/{{ poll.manage_code }}/delete_choice/{{ choice.id }}" method="post">
      <tr>
        <td>
          <input type="{% if poll.is_whole_day %}date{% else %}datetime-local{% endif %}"
                 aria-label="{% if poll.is_whole_day %}Start date{% else %}Start datetime{% endif %}"
                 name="start_datetime_{{ choice.id }}"
                 value="{% if poll.is_whole_day %}{{ choice.start_date_notz() }}{% else %}{{ choice.start_datetime_notz() }}{% endif %}"
                 disabled
                 {% if last_choice_id == choice.id %}id="last-start-datetime"{% endif %}>
        </td>
        <td>
          <input type="{% if poll.is_whole_day %}date{% else %}datetime-local{% endif %}"
                 aria-label="{% if poll.is_whole_day %}End date{% else %}End datetime{% endif %}"
                 name="end_datetime_{{ choice.id }}"
                 value="{% if poll.is_whole_day %}{{ choice.end_date_notz() }}{% else %}{{ choice.end_datetime_notz() }}{% endif %}"
                 disabled
                 {% if last_choice_id == choice.id %}id="last-end-datetime"{% endif %}>
        </td>
        <td>
          <input class="red" type="submit" value="Delete">
        </td>
      </tr>
    </form>
    {% endfor %}
    <form action="/manage/{{ poll.manage_code }}/add_choice" method="post">
      <tr>
        <td>
          <input type="{% if poll.is_whole_day %}date{% else %}datetime-local{% endif %}"
                 aria-label="{% if poll.is_whole_day %}Start date{% else %}Start datetime{% endif %}"
                 name="start_datetime"
                 required>
        </td>
        <td>
          <input type="{% if poll.is_whole_day %}date{% else %}datetime-local{% endif %}"
                 aria-label="{% if poll.is_whole_day %}End date{% else %}End datetime{% endif %}"
                 name="end_datetime"
                 required>
        </td>
        <td>
          <input class="blue" type="submit" value="Add">
        </td>
      </tr>
    </form>
  </tbody>
</table>

<div class="danger-zone">
  <h3>Danger zone</h3>
  <form action="/manage/{{ poll.manage_code }}/delete" method="post">
    <!-- Empty forms make Lynx use HTTP method GET incorrectly, so we add a useless field -->
    <input type="hidden" name="poll_id" value="{{ poll.id }}">
    <input class="red" type="submit" value="Delete poll">
  </form>
</div>

<script>
const isWholeDay = {% if poll.is_whole_day %}true{% else %}false{% endif %};
const hour = 1000 * 60 * 60;
const day = hour * 24;

const startDatetimeInput = document.querySelector('input[name="start_datetime"]');
const endDatetimeInput = document.querySelector('input[name="end_datetime"]');

const lastStartDatetimeInput = document.getElementById('last-start-datetime');
const lastEndDatetimeInput = document.getElementById('last-end-datetime');

if (lastStartDatetimeInput !== null && startDatetimeInput.valueAsDate === null) {
  if (isWholeDay) {
    // set the start date to the last end date + 1 day if in whole day mode
    startDatetimeInput.valueAsDate = new Date(lastEndDatetimeInput.valueAsDate.getTime() + day);
    endDatetimeInput.valueAsDate = new Date(startDatetimeInput.valueAsDate.getTime());
  } else {
    // set the start date to the last end date + 1 hour if not in whole day mode
    startDatetimeInput.valueAsDate = new Date(lastEndDatetimeInput.valueAsDate.getTime());
    endDatetimeInput.valueAsDate = new Date(startDatetimeInput.valueAsDate.getTime() + hour);
  }
}

startDatetimeInput.addEventListener('change', onStartDatetimeChange);

function onStartDatetimeChange(event) {
  const endDatetimeInput = document.querySelector('input[name="end_datetime"]');
  if (endDatetimeInput.valueAsDate === null) {
    if (isWholeDay) {
      // set the end date to the start date if in whole day mode
      endDatetimeInput.valueAsDate = new Date(event.target.valueAsDate.getTime());
    } else {
      // set the end date to the start date + 1 hour if not in whole day mode
      endDatetimeInput.valueAsDate = new Date(event.target.valueAsDate.getTime() + hour);
    }
  }
}

const query = new URLSearchParams(window.location.search);
const focusNext = query.get('focus_next');
if (focusNext !== null) {
  const nextInput = document.querySelector(`input[name="start_datetime"]`);
  if (nextInput !== null) {
    nextInput.scrollIntoView();
    nextInput.focus();
  }
}

let shareLinkCopy = document.getElementById("share-link-copy");
if (shareLinkCopy !== null) {
  shareLinkCopy.outerHTML = "<button id='share-link-copy' aria-label='Copy share link' onclick='copyShareLink()'>📋</button>";
  shareLinkCopy = document.getElementById("share-link-copy"); // refresh the reference
}

function copyShareLink() {
  const shareLink = document.getElementById("share-link");
  const linkText = shareLink.getAttribute("href");
  navigator.clipboard.writeText(linkText);
  console.log("Copied to clipboard: " + linkText);
  shareLinkCopy.innerHTML = "✅";
  setTimeout(() => {
    shareLinkCopy.innerHTML = "📋";
  }, 2000);
}
</script>

{% endblock %}