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

<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,
              form: { id: 'diff_form', class: 'button_to computation' },
              disabled: true %>
        <span id="diff_selection_label"></span>
      </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.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) %></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');
    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);
    }
  });
</script>