summaryrefslogtreecommitdiff
path: root/app/views/revisions/index.html.erb
blob: 7ea74a6c6fffee59444800c6bf6194937ec3f2a2 (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
<h2>Revisions for Node: <%= @node.unique_name %></h2>

<p class="node_action_bar standalone_action_bar">
  <%= link_to 'Back to node', node_path(@node) %>
</p>

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

<table id="revisions" class="revisions_table">
  <thead>
    <tr class="header">
      <th>First</th>
      <th>Last</th>
      <th>Rev.</th>
      <th>Title</th>
      <th>Editor</th>
      <th>Date</th>
      <th></th>
      <th></th>
    </tr>
    <tr class="no_hover diff_sticky_bar">
      <td colspan="8">
        <%= button_to 'Diff revisions', diff_node_revisions_path(@node),
              method: :post,
              params: { locale: @locale },
              form: { id: 'diff_form', class: 'button_to computation' },
              disabled: true %>
        <span id="diff_selection_label"></span>
        <label><%= radio_button_tag :view, 'inline', true %> Inline</label>
        <label><%= radio_button_tag :view, 'side_by_side', false %> Side by side</label>
      </td>
    </tr>
  </thead>
  <tbody>
  <% pages = (@pages || @node.pages.all).reverse %>
  <% pages.each_with_index do |page, index| %>
    <tr>
      <td><%= radio_button_tag :start_revision, page.revision, index == 1 %></td>
      <td><%= radio_button_tag :end_revision, page.revision, index == 0 %></td>
      <td class="revision"><%= page.revision %></td>
      <td class="title"><%= page.translations.find_by(:locale => @locale)&.title || "—" %></td>
      <td class="user"><%= page.editor.try(:login) %></td>
      <td class="date"><%= page.updated_at %></td>
      <td><%= link_to 'show', node_revision_path(@node, page, :locale => @locale) %></td>
      <td>
        <%= button_to 'restore', restore_node_revision_path(@node, page),
              method: :put,
              form: { data: { confirm: "Restore this revision?" }, class: 'button_to state_changing' } %>
      </td>
    </tr>
  <% end %>
  </tbody>
</table>

<script>
  function update_diff_button_state() {
    var start = document.querySelector('input[name="start_revision"]:checked');
    var end = document.querySelector('input[name="end_revision"]:checked');
    var valid = start && end && start.value !== end.value;
    document.querySelector('#diff_form button[type="submit"]').disabled = !valid;

    var label = document.getElementById('diff_selection_label');
    if (start && end) {
      label.textContent = start.value + ' against ' + end.value;
    } else if (start || end) {
      label.textContent = (start || end).value + ' selected';
    } else {
      label.textContent = '';
    }
  }

  document.querySelectorAll('input[name="start_revision"], input[name="end_revision"]')
    .forEach(function(radio) { radio.addEventListener('change', update_diff_button_state); });

  update_diff_button_state();

  document.getElementById('diff_form').addEventListener('submit', function(e) {
    var start = document.querySelector('input[name="start_revision"]:checked');
    var end = document.querySelector('input[name="end_revision"]:checked');
    var view = document.querySelector('input[name="view"]:checked');
    if (start) {
      var s = document.createElement('input');
      s.type = 'hidden'; s.name = 'start_revision'; s.value = start.value;
      this.appendChild(s);
    }
    if (end) {
      var en = document.createElement('input');
      en.type = 'hidden'; en.name = 'end_revision'; en.value = end.value;
      this.appendChild(en);
    }
    if (view) {
      var v = document.createElement('input');
      v.type = 'hidden'; v.name = 'view'; v.value = view.value;
      this.appendChild(v);
    }
  });
</script>