/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

var BASE = 'extensions.auto-confirm@myokoym.net.';
var { prefs } = Components.utils.import('resource://auto-confirm-resources/modules/lib/prefs.js', {});

var gMessages;
var gRules;

function initGeneral() {
  gMessages = document.getElementById('messages');
  gRules = document.getElementById('rules');
  buildRulesList();
}

function buildRulesList() {
  var commonDialogRules = prefs.getChildren(BASE + 'common');
  var generalRules = prefs.getChildren(BASE + 'general');

  var allRules = commonDialogRules.concat(generalRules);
  var basePartMatcher = new RegExp('^' + BASE + '(common|general)\\.');
  allRules.sort(function(aA, aB) {
    aA = aA.replace(basePartMatcher, '');
    aB = aB.replace(basePartMatcher, '');
    return aA < aB ?  -1 :
           aA == aB ? 0 :
                      1;
  });

  var fragment = document.createDocumentFragment();
  for (let base of allRules) {
    let name = decodeURIComponent(base.replace(basePartMatcher, ''));
    let group = RegExp.$1;

    let item = document.createElement('richlistitem');
    item.setAttribute('orient', 'horizontal');
    item.setAttribute('align', 'center');

    // common properties
    item.setAttribute('data-name', name);
    item.setAttribute('data-group', group);
    item.setAttribute('data-title', prefs.getPref(base + '.title') || '');
    item.setAttribute('data-text', prefs.getPref(base + '.text') || '');
    item.setAttribute('data-action', prefs.getPref(base + '.action') || '');
    item.setAttribute('data-actions', prefs.getPref(base + '.actions') || '');

    // commonDialog rule specific properties
    item.setAttribute('data-type', prefs.getPref(base + '.type') || '');

    // general rule specific properties
    item.setAttribute('data-url', prefs.getPref(base + '.url') || '');

    let label = document.createElement('label');
    label.setAttribute('value', name);
    label.setAttribute('tooltiptext', name);
    label.setAttribute('flex', 1);
    item.appendChild(label);

    let editButton = document.createElement('button');
    editButton.setAttribute('label', gMessages.getString('config.rule.item.controls.edit.label'));
    editButton.setAttribute('tooltiptext', gMessages.getString('config.rule.item.controls.edit.tooltip'));
    editButton.setAttribute('style', 'min-width:0');

    let deleteButton = document.createElement('button');
    deleteButton.setAttribute('label', gMessages.getString('config.rule.item.controls.delete.label'));
    deleteButton.setAttribute('tooltiptext', gMessages.getString('config.rule.item.controls.delete.tooltip'));
    deleteButton.setAttribute('style', 'min-width:0');

    item.appendChild(editButton);
    item.appendChild(deleteButton);

    fragment.appendChild(item);
  }
  gRules.appendChild(fragment);
}

function shutdown() {
  Components.utils.unload('resource://auto-confirm-resources/modules/lib/prefs.js');
}

@@ -354,16 +354,29 @@
 , %7B%7D);%0A%0A
+var gRules;%0A%0A
 var gMes
@@ -392,16 +392,20 @@
 r gRules
+List
 ;%0A%0Afunct
@@ -483,16 +483,20 @@
   gRules
+List
  = docum
@@ -524,16 +524,40 @@
 ules');%0A
+  gRules = loadRules();%0A
   buildR
@@ -580,30 +580,25 @@
 unction 
-buil
+loa
 dRules
-List
 () %7B%0A  v
@@ -1054,85 +1054,42 @@
 %0A%0A  
-var fragment = document.createDocumentFragment();%0A  for (let base of allRules
+return allRules.map(function(aBase
 ) %7B%0A
@@ -1189,300 +1189,105 @@
 $1;%0A
-%0A
     
-let item = document.createElement('richlistitem');%0A    item.setAttribute('orient', 'horizontal');%0A    item.setAttribute('align', 'center');%0A%0A    // common properties%0A    item.setAttribute('data-name',
+return %7B%0A      // common properties%0A      name:   
  name
-);
+,
 %0A    
-item.setAttribute('data-
+  
 group
-',
+:  
  group
-);
+,
 %0A    
-item.setAttribute('data-
+  
 title
-',
+:  
  pre
@@ -1323,45 +1323,24 @@
 %7C ''
-);
+,
 %0A    
-item.setAttribute('data-text',
+  text:   
  pre
@@ -1375,47 +1375,24 @@
 %7C ''
-);
+,
 %0A    
-item.setAttribute('data-
+  
 action
-',
+: 
  pre
@@ -1429,48 +1429,24 @@
 %7C ''
-);
+,
 %0A    
-item.setAttribute('data-
+  
 actions
-',
+:
  pre
@@ -1476,28 +1476,28 @@
 ions') %7C%7C ''
-);%0A%0A
+,%0A  
     // commo
@@ -1537,38 +1537,18 @@
     
-item.setAttribute('data-type',
+  type:   
  pre
@@ -1575,28 +1575,28 @@
 type') %7C%7C ''
-);%0A%0A
+,%0A  
     // gener
@@ -1631,72 +1631,452 @@
     
-item.setAttribute('data-url', prefs.getPref(base + '.url') %7C%7C ''
+  url:     prefs.getPref(base + '.url') %7C%7C ''%0A    %7D;%0A  %7D);%0A%7D%0A%0Afunction buildRulesList() %7B%0A%0A  var fragment = document.createDocumentFragment();%0A  for (let rule of gRules) %7B%0A    let item = document.createElement('richlistitem');%0A    item.setAttribute('orient', 'horizontal');%0A    item.setAttribute('align', 'center');%0A    item.rule = rule;%0A%0A    Object.keys(rule).forEach(function(aKey) %7B%0A      item.setAttribute('data-' + aKey, rule%5BaKey%5D);%0A    %7D
 );%0A%0A
@@ -2156,16 +2156,21 @@
 value', 
+rule.
 name);%0A 
@@ -2206,16 +2206,21 @@
 ptext', 
+rule.
 name);%0A 
@@ -3040,16 +3040,20 @@
   gRules
+List
 .appendC

Store all rules to a glbal variable