<!--!  Copyright (C) 2014 Edgewall Software

  This software is licensed as described in the file COPYING, which
  you should have received as part of this distribution. The terms
  are also available at http://trac.edgewall.org/wiki/TracLicense.

  This software consists of voluntary contributions made by many
  individuals. For the exact contribution history, see the revision
  history and logs, available at http://trac.edgewall.org/log/.
-->
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:i18n="http://genshi.edgewall.org/i18n">
  <xi:include href="prefs.html" />
  <head>
    <title>Notifications</title>
    <script type="text/javascript">//<![CDATA[
      jQuery(document).ready(function($) {
        var find_label = function(options, value) {
          var label;
          $.each(options, function(idx, option) {
            if (option.value === value) {
              label = option.text;
              return false;
            }
          });
          return label;
        };
        var highlight = function(nodes) {
          nodes.effect('highlight');
        };
        $("#content").find("h1,h2,h3,h4,h5,h6").addAnchor(_("Link to this section"));
        $("#content .subscription-format").change(function() {
          $.setWarningUnsavedChanges(true);
        });
        var rows = $("#content .subscription-rules .rules");
        rows.on("click", ".delete-rule", function() {
          $(this).closest("tr").remove();
          $.setWarningUnsavedChanges(true);
          return false;
        });
        rows.on("click", ".move-up-rule", function() {
          var row = $(this).closest("tr");
          row.prev("tr").before(row);
          highlight(row);
          $.setWarningUnsavedChanges(true);
          return false;
        });
        rows.on("click", ".move-down-rule", function() {
          var row = $(this).closest("tr");
          row.next("tr").after(row);
          highlight(row);
          $.setWarningUnsavedChanges(true);
          return false;
        });
        $("#content .add-rule").click(function() {
          var form = $(this.form);
          var distributor = $(this).val().replace(/^add-rule_/, "");
          var row = form.find(".new-rule-" + distributor).children("tr")
                        .clone();
          var adverb = form.find('[name="new-adverb-' + distributor + '"]');
          var adverb_val = adverb.val();
          var adverb_label = find_label(adverb[0].options, adverb_val);
          var class_ = form.find('[name="new-rule-' + distributor + '"]');
          var class_val = class_.val();
          var class_label = find_label(class_[0].options, class_val);
          if (!adverb_val || !class_val)
            return false;
          row.find("[name='adverb-" + distributor + "']").val(adverb_val);
          row.find("[name='class-" + distributor + "']").val(class_val);
          row.find(".new-rule-label").text(adverb_label + " " + class_label);
          row.find("[disabled]").removeAttr("disabled");
          row.find('.rule-label').attr('title', _("Drag rule to reorder"));
          form.find(".rules-" + distributor).append(row);
          highlight(row);
          $.setWarningUnsavedChanges(true);
          return false;
        });
        var sortable_options = {axis: 'y', tolerance: 'pointer',
                                placeholder: 'trac-placeholder',
                                handle: '.rule-label'};
        sortable_options.start = function(event, ui) {
          var placeholder = $(ui.placeholder);
          if (placeholder.children().length === 0) {
            var orig = $(ui.item).children();
            var clone = orig.clone();
            clone.find('input, select, button').removeAttr('name');
            placeholder.append(clone);
          }
          $.setWarningUnsavedChanges(true);
        };
        rows.sortable(sortable_options);
        rows.find(".rule-label").attr("title", _("Drag rule to reorder"));
        $("#content form").submit(function() {
          $.setWarningUnsavedChanges(false);
        });
      });
    //]]></script>
  </head>
  <body>
    <h2 id="subscriptions-section">Subscriptions</h2>
    <form action="" method="post">
      <div py:for="distributor, rules in data['rules'].iteritems()" class="ruleset">
        <h3>${distributor}</h3>
        <div class="field">
          <p>
            <label for="format-${distributor}">Format:</label>
            <select id="format-${distributor}" name="format-${distributor}"
                    class="subscription-format">
              <option value="">Default format</option>
              <option py:for="f in data['formatters'][distributor]"
                      value="${f}"
                      selected="${(f == data.selected_format[distributor]) or None}">${f}</option>
            </select>
            <noscript>
              &nbsp;
              <button class="trac-button" type="submit" name="action" title="Save format"
                      value="set-format_${distributor}">Save</button>
            </noscript>
          </p>
          <p class="hint" i18n:msg="distributor">
            Configure the format of your ${distributor} notifications.
          </p>
        </div>
        <div class="field" py:if="data['subscribers']">
          <label>Subscription rules:</label>
          <table class="subscription-rules" summary="Subscription rules">
            <tbody class="rules rules-$distributor">
              <tr py:for="rule in rules" class="rule">
                <td>
                  <div class="inlinebuttons">
                    <button class="trac-button move-up-rule"
                            type="submit" name="action" title="Move rule up"
                            value="move-rule_${rule.id}-${rule.priority - 1}">↑</button>
                  </div>
                </td>
                <td>
                  <div class="inlinebuttons">
                    <button class="trac-button move-down-rule"
                            type="submit" name="action" title="Move rule down"
                            value="move-rule_${rule.id}-${rule.priority + 1}">↓</button>
                  </div>
                </td>
                <td>
                  <div class="inlinebuttons">
                    <button class="trac-button delete-rule"
                            type="submit" name="action" title="Delete rule"
                            value="delete-rule_${rule.id}">–</button>
                  </div>
                </td>
                <td class="rule-label">
                  ${data.adverb_labels[rule.adverb]}: ${rule.description}
                  <div style="display:none">
                    <input type="hidden" name="class-$distributor" value="${rule['class']}" />
                    <input type="hidden" name="adverb-$distributor" value="$rule.adverb" />
                  </div>
                </td>
              </tr>
            </tbody>
            <tbody class="new-rule-$distributor" style="display:none">
              <tr class="rule">
                <td>
                  <div class="inlinebuttons">
                    <button class="trac-button move-up-rule"
                            type="submit" name="action" title="Move rule up"
                            value="">↑</button>
                  </div>
                </td>
                <td>
                  <div class="inlinebuttons">
                    <button class="trac-button move-down-rule"
                            type="submit" name="action" title="Move rule down"
                            value="">↓</button>
                  </div>
                </td>
                <td>
                  <div class="inlinebuttons">
                    <button class="trac-button delete-rule"
                            type="submit" name="action" title="Delete rule"
                            value="">–</button>
                  </div>
                </td>
                <td class="rule-label">
                  <span class="new-rule-label"></span>
                  <div style="display:none">
                    <input type="hidden" name="adverb-$distributor" value="" disabled="disabled" />
                    <input type="hidden" name="class-$distributor" value="" disabled="disabled" />
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
          <p>
            <select name="new-adverb-${distributor}">
              <option py:for="a in data['adverbs']" value="${a}">${data.adverb_labels[a]}:</option>
            </select>
            <select name="new-rule-${distributor}">
              <option py:for="s in data['subscribers']" value="${s['class']}">${s.description}</option>
            </select>
            <button class="trac-button add-rule"
                    type="submit" name="action" title="Add rule"
                    value="add-rule_${distributor}">Add</button>
          </p>
          <p class="hint" i18n:msg="distributor">
            Add, remove or reorder subscription rules to ${distributor} notifications. Only the first matching rule is applied.
          </p>
          <p class="hint" i18n:msg="">
            Example: The rule <strong>"Never notify: I update a ticket"</strong> should be above <strong>"Notify: Any ticket changes"</strong> to get notifications of any ticket changes except when you update a ticket.
          </p>
        </div>
        <div class="field" py:if="data.default_rules[distributor]">
          <label>Default rules:</label>
          <ul>
            <li py:for="rule in data['default_rules'][distributor]" class="default_rule">
              ${data.adverb_labels[rule.adverb]}: ${rule.description}
            </li>
          </ul>
          <p class="hint">
            These default rules have been configured by the site administrator. Configure subscription rules to override them.
          </p>
          <p class="hint" i18n:msg="">
            Example: The subscription rule <strong>"Notify: Any ticket changes"</strong> overrides the default rule <strong>"Never notify: I update a ticket"</strong>.
          </p>
        </div>
      </div>
      <p class="trac-noscript">
        <button class="trac-button" type="submit" name="action" value="replace_all">Save changes</button>
      </p>
    </form>
  </body>
</html>
