1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5function Rule(data) {
6  var rules = document.getElementById('rules');
7  this.node = document.getElementById('rule-template').cloneNode(true);
8  this.node.id = 'rule' + (Rule.next_id++);
9  this.node.rule = this;
10  rules.appendChild(this.node);
11  this.node.hidden = false;
12
13  if (data) {
14    this.getElement('matcher').value = data.matcher;
15    this.getElement('match-param').value = data.match_param;
16    this.getElement('action').value = data.action;
17    this.getElement('action-js').value = data.action_js;
18    this.getElement('enabled').checked = data.enabled;
19  }
20
21  this.getElement('enabled-label').htmlFor = this.getElement('enabled').id =
22    this.node.id + '-enabled';
23
24  this.render();
25
26  this.getElement('matcher').onchange = storeRules;
27  this.getElement('match-param').onkeyup = storeRules;
28  this.getElement('action').onchange = storeRules;
29  this.getElement('action-js').onkeyup = storeRules;
30  this.getElement('enabled').onchange = storeRules;
31
32  var rule = this;
33  this.getElement('move-up').onclick = function() {
34    var sib = rule.node.previousSibling;
35    rule.node.parentNode.removeChild(rule.node);
36    sib.parentNode.insertBefore(rule.node, sib);
37    storeRules();
38  };
39  this.getElement('move-down').onclick = function() {
40    var parentNode = rule.node.parentNode;
41    var sib = rule.node.nextSibling.nextSibling;
42    parentNode.removeChild(rule.node);
43    if (sib) {
44      parentNode.insertBefore(rule.node, sib);
45    } else {
46      parentNode.appendChild(rule.node);
47    }
48    storeRules();
49  };
50  this.getElement('remove').onclick = function() {
51    rule.node.parentNode.removeChild(rule.node);
52    storeRules();
53  };
54  storeRules();
55}
56
57Rule.prototype.getElement = function(name) {
58  return document.querySelector('#' + this.node.id + ' .' + name);
59}
60
61Rule.prototype.render = function() {
62  this.getElement('move-up').disabled = !this.node.previousSibling;
63  this.getElement('move-down').disabled = !this.node.nextSibling;
64  this.getElement('action-js').style.display =
65    (this.getElement('action').value == 'js') ? 'block' : 'none';
66}
67
68Rule.next_id = 0;
69
70function loadRules() {
71  var rules = localStorage.rules;
72  try {
73    JSON.parse(rules).forEach(function(rule) {new Rule(rule);});
74  } catch (e) {
75    localStorage.rules = JSON.stringify([]);
76  }
77}
78
79function storeRules() {
80  localStorage.rules = JSON.stringify(Array.prototype.slice.apply(
81      document.getElementById('rules').childNodes).map(function(node) {
82    node.rule.render();
83    return {matcher: node.rule.getElement('matcher').value,
84            match_param: node.rule.getElement('match-param').value,
85            action: node.rule.getElement('action').value,
86            action_js: node.rule.getElement('action-js').value,
87            enabled: node.rule.getElement('enabled').checked};
88  }));
89}
90
91window.onload = function() {
92  loadRules();
93  document.getElementById('new').onclick = function() {
94    new Rule();
95  };
96}
97