summaryrefslogtreecommitdiff
path: root/mailnews/import/content/fieldMapImport.js
blob: cd0f2d5dafa0c2fe2173e93c0dee56957778295e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* 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 importService;
var fieldMap = null;
var recordNum = 0;
var addInterface = null;
var dialogResult = null;
var gPreviousButton;
var gNextButton;
var gMoveUpButton;
var gMoveDownButton;
var gListbox;
var gSkipFirstRecordButton;

function OnLoadFieldMapImport()
{
  top.importService = Components.classes["@mozilla.org/import/import-service;1"]
                                .getService(Components.interfaces.nsIImportService);

  // We need a field map object...
  // assume we have one passed in? or just make one?
  if (window.arguments && window.arguments[0]) {
    top.fieldMap = window.arguments[0].fieldMap;
    top.addInterface = window.arguments[0].addInterface;
    top.dialogResult = window.arguments[0].result;
  }
  if (top.fieldMap == null) {
    top.fieldMap = top.importService.CreateNewFieldMap();
    top.fieldMap.DefaultFieldMap( top.fieldMap.numMozFields);
  }

  gMoveUpButton = document.getElementById("upButton");
  gMoveDownButton = document.getElementById("downButton");
  gPreviousButton = document.getElementById("previous");
  gNextButton = document.getElementById("next");
  gListbox = document.getElementById("fieldList");
  gSkipFirstRecordButton = document.getElementById("skipFirstRecord");

  // Set the state of the skip first record button
  gSkipFirstRecordButton.checked = top.fieldMap.skipFirstRecord;

  ListFields();
  Browse(1);
  gListbox.selectedItem = gListbox.getItemAtIndex(0);
  disableMoveButtons();
}

function IndexInMap( index)
{
  var count = top.fieldMap.mapSize;
  for (var i = 0; i < count; i++) {
    if (top.fieldMap.GetFieldMap( i) == index)
      return( true);
  }

  return( false);
}

function ListFields() {
  if (top.fieldMap == null)
    return;

  var count = top.fieldMap.mapSize;
  var index;
  var i;
  for (i = 0; i < count; i++) {
    index = top.fieldMap.GetFieldMap( i);
    AddFieldToList(top.fieldMap.GetFieldDescription( index), index, top.fieldMap.GetFieldActive( i));
  }

  count = top.fieldMap.numMozFields;
  for (i = 0; i < count; i++) {
    if (!IndexInMap( i))
      AddFieldToList(top.fieldMap.GetFieldDescription( i), i, false);
  }
}

function CreateField( name, index, on)
{
  var item = document.createElement('listitem');
  item.setAttribute('field-index', index);
  item.setAttribute('type', "checkbox");
  var cell = document.createElement('listcell');
  var cCell = document.createElement( 'listcell');  
  cCell.setAttribute('type', "checkbox");
  cCell.setAttribute( 'label', name);
  if (on == true)
    cCell.setAttribute( 'checked', "true");
  item.appendChild( cCell);
  cell.setAttribute( "class", "importsampledata");
  cell.setAttribute( 'label', "");
  item.appendChild( cell);
  return( item);
}

function AddFieldToList(name, index, on)
{
  var item = CreateField(name, index, on);
  gListbox.appendChild(item);
}

function itemClicked(event)
{
  if (event.button == 0) {
    var on = gListbox.selectedItem.firstChild.getAttribute('checked');
    gListbox.selectedItem.firstChild.setAttribute('checked', (on != "true"));
  }
}

// The "Move Up/Move Down" buttons should move the items in the left column
// up/down but the values in the right column should not change.
function moveItem(up)
{
  var selectedItem = gListbox.selectedItem;
  var swapPartner = (up ? gListbox.getPreviousItem(selectedItem, 1)
                        : gListbox.getNextItem(selectedItem, 1));

  var tmpLabel = swapPartner.lastChild.getAttribute('label');
  swapPartner.lastChild.setAttribute('label', selectedItem.lastChild.getAttribute('label'));
  selectedItem.lastChild.setAttribute('label', tmpLabel);

  var newItemPosition = (up ? selectedItem.nextSibling : selectedItem);
  gListbox.insertBefore(swapPartner, newItemPosition);
  gListbox.ensureElementIsVisible(selectedItem);
  disableMoveButtons();
}

function disableMoveButtons()
{
  var selectedIndex = gListbox.selectedIndex;
  gMoveUpButton.disabled = (selectedIndex == 0);
  gMoveDownButton.disabled = (selectedIndex == (gListbox.getRowCount() - 1));
}

function ShowSampleData(data)
{
  var fields = data.split("\n");
  for (var i = 0; i < gListbox.getRowCount(); i++)
    gListbox.getItemAtIndex(i).lastChild.setAttribute('label', (i < fields.length) ? fields[i] : '');
}

function FetchSampleData(num)
{
  if (!top.addInterface)
    return false;

  var data = top.addInterface.GetData( "sampleData-" + num);
  if (!(data instanceof Components.interfaces.nsISupportsString))
    return false;
  ShowSampleData( data.data);
  return true;  
}

function Browse(step)
{
  recordNum += step;
  if (FetchSampleData(recordNum - 1))
    document.getElementById('recordNumber').setAttribute('value', ("" + recordNum));

  gPreviousButton.disabled = (recordNum == 1);
  gNextButton.disabled = (addInterface.GetData("sampleData-" + recordNum) == null);
}

function FieldImportOKButton()
{
  var max = gListbox.getRowCount();
  var fIndex;
  var on;
  // Ensure field map is the right size
  top.fieldMap.SetFieldMapSize(max);

  for (var i = 0; i < max; i++) {
    fIndex = gListbox.getItemAtIndex(i).getAttribute( 'field-index');
    on = gListbox.getItemAtIndex(i).firstChild.getAttribute('checked');
    top.fieldMap.SetFieldMap( i, fIndex);
    top.fieldMap.SetFieldActive( i, (on == "true"));
  }

  top.fieldMap.skipFirstRecord = gSkipFirstRecordButton.checked;

  top.dialogResult.ok = true;

  return true;
}