summaryrefslogtreecommitdiff
path: root/app/views/revisions/index.html.erb
blob: e951016f9883794a69480662c57fd565a2ce8aec (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
<h2><%= t(".title", :path => @node.unique_name) %></h2>

<p class="node_action_bar">
  <%= link_to t(".back_to_node"), node_path(@node) %>
</p>

<% if Page.non_default_locales.any? %>
  <p class="diff_locale_toggle">
    <%= t("revisions.locale_label") %>
    <% ([I18n.default_locale] + Page.non_default_locales).each_with_index do |locale, i| %>
      <%= " &middot; ".html_safe if i > 0 %>
      <% if locale == @translation_locale %>
        <strong><%= locale.to_s.upcase %></strong>
      <% else %>
        <%= link_to locale.to_s.upcase, node_revisions_path(@node, :translation_locale => locale) %>
      <% end %>
    <% end %>
  </p>
<% end %>

<% pages = (@pages || @node.pages.all).reverse %>

<table id="revisions" class="admin_table revisions_table">
  <thead>
    <tr class="header">
      <th><%= t("admin.columns.rev") %></th>
      <th><%= t("admin.columns.title") %></th>
      <th><%= t("admin.columns.editor") %></th>
      <th><%= t("admin.columns.date") %></th>
      <th></th>
      <th></th>
    </tr>
    <tr class="diff_sticky_bar">
      <td colspan="6">
        <%= form_tag diff_node_revisions_path(@node), :method => :post,
              :id => "diff_form", :class => "button_to computation" do %>
          <%= hidden_field_tag :translation_locale, @translation_locale %>
          <%= hidden_field_tag :start_revision, pages[1]&.revision %>
          <%= hidden_field_tag :end_revision,   pages[0]&.revision %>
          <%= submit_tag t(".diff_revisions") %>
          <span id="diff_selection_label" aria-live="polite">
            <%= t(".diff_selection_html",
                  :from => tag.span(:id => "diff_from"),
                  :to   => tag.span(:id => "diff_to")) %>
          </span>
          <label><%= radio_button_tag :view, 'inline', true %> <%= t("revisions.inline") %></label>
          <label><%= radio_button_tag :view, 'side_by_side', false %> <%= t("revisions.side_by_side") %></label>
        <% end %>
      </td>
    </tr>
  </thead>
  <tbody>
  <% pages.each do |page| %>
    <tr data-revision="<%= page.revision %>" tabindex="0" aria-pressed="false">
      <td class="revision"><%= page.revision %></td>
      <td class="title"><%= page.translations.find_by(:locale => @translation_locale)&.title || "—" %></td>
      <td class="user"><%= page.editor.try(:login) %></td>
      <td class="date"><%= admin_datetime(page.updated_at) %></td>
      <td><%= link_to t(".show_link"), node_revision_path(@node, page, :translation_locale => @translation_locale) %></td>
      <td>
        <%= button_to t(".restore_link"), restore_node_revision_path(@node, page),
              method: :put,
              form: { data: { confirm: t(".confirm_restore") }, class: 'button_to state_changing' } %>
      </td>
    </tr>
  <% end %>
  </tbody>
</table>

<%= javascript_tag nonce: true do %>
  (function () {
    var table = document.getElementById('revisions');
    var form  = document.getElementById('diff_form');
    if (!table || !form) { return; }

    var from_field = form.querySelector('input[name="start_revision"]');
    var to_field   = form.querySelector('input[name="end_revision"]');
    var from_label = document.getElementById('diff_from');
    var to_label   = document.getElementById('diff_to');
    var readout    = document.getElementById('diff_selection_label');
    var submit     = form.querySelector('input[type="submit"]');
    var rows       = table.querySelectorAll('tbody tr');

    // Two-slot FIFO: the newest pick becomes the target and the previous
    // target becomes the source, so two taps anywhere give any pair.
    // Picking the current target is a no-op; picking the current source
    // swaps them.
    var stack = [from_field.value, to_field.value].filter(function (v) { return v !== ''; });

    function render() {
      var from = stack[0] || '';
      var to   = stack[1] || '';
      from_field.value = from;
      to_field.value   = to;
      from_label.textContent = from;
      to_label.textContent   = to;
      readout.hidden  = !(from && to);
      submit.disabled = !(from && to && from !== to);

      rows.forEach(function (row) {
        var rev = row.dataset.revision;
        row.classList.toggle('diff_source', rev === from);
        row.classList.toggle('diff_target', rev === to);
        row.setAttribute('aria-pressed', (rev === from || rev === to) ? 'true' : 'false');
      });
    }

    function pick(rev) {
      if (rev === stack[1]) { return; }
      stack.push(rev);
      if (stack.length > 2) { stack.shift(); }
      render();
    }

    rows.forEach(function (row) {
      row.addEventListener('click', function (e) {
        if (e.target.closest('a, button, input, label')) { return; }
        pick(row.dataset.revision);
      });
      row.addEventListener('keydown', function (e) {
        if (e.key === 'Enter' || e.key === ' ') {
          e.preventDefault();
          pick(row.dataset.revision);
        }
      });
    });

    render();
  })();
<% end %>