summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/content/fieldMapImport.js
blob: 3f777c0cb6cb068f839bdbf36fd92164032db948 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* 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 gRecordNum = 0;
var addInterface = null;
var dialogResult = null;
var gPreviousButton;
var gNextButton;
var gMoveUpButton;
var gMoveDownButton;
var gListbox;
var gSkipFirstRecordButton;

window.addEventListener("DOMContentLoaded", event => {
  OnLoadFieldMapImport();
});
window.addEventListener("load", resizeColumns, { once: true });
window.addEventListener("resize", resizeColumns);

document.addEventListener("dialogaccept", FieldImportOKButton);

function resizeColumns() {
  let list = document.getElementById("fieldList");
  let cols = list.getElementsByTagName("treecol");
  list.style.setProperty(
    "--column1width",
    cols[0].getBoundingClientRect().width + "px"
  );
  list.style.setProperty(
    "--column2width",
    cols[1].getBoundingClientRect().width + "px"
  );
}

function OnLoadFieldMapImport() {
  top.importService = Cc["@mozilla.org/import/import-service;1"].getService(
    Ci.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();
  browseDataPreview(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;
  }

  // Add rows for every mapped field.
  let count = top.fieldMap.mapSize;
  for (let i = 0; i < count; i++) {
    let index = top.fieldMap.GetFieldMap(i);
    if (index == -1) {
      continue;
    }
    AddFieldToList(
      top.fieldMap.GetFieldDescription(index),
      index,
      top.fieldMap.GetFieldActive(i)
    );
  }

  // Add rows every possible field we don't already have a row for.
  count = top.fieldMap.numMozFields;
  for (let i = 0; i < count; i++) {
    if (!IndexInMap(i)) {
      AddFieldToList(top.fieldMap.GetFieldDescription(i), i, false);
    }
  }

  // Add dummy rows if the data has more fields than Thunderbird does.
  let data = top.addInterface.GetData("sampleData-0");
  if (!(data instanceof Ci.nsISupportsString)) {
    return;
  }
  count = data.data.split("\n").length;
  for (let i = gListbox.itemCount; i < count; i++) {
    AddFieldToList(null, -1, false);
  }
}

function CreateField(name, index, on) {
  var item = document.createXULElement("richlistitem");
  item.setAttribute("align", "center");
  item.setAttribute("field-index", index);
  item.setAttribute("allowevents", "true");

  var checkboxCell = document.createXULElement("hbox");
  checkboxCell.setAttribute("style", "width: var(--column1width)");
  let checkbox = document.createXULElement("checkbox");
  if (!name) {
    checkbox.disabled = true;
  } else if (on) {
    checkbox.setAttribute("checked", "true");
  }
  checkboxCell.appendChild(checkbox);

  var firstCell = document.createXULElement("label");
  firstCell.setAttribute("style", "width: var(--column2width)");
  firstCell.setAttribute("value", name || "");

  var secondCell = document.createXULElement("label");
  secondCell.setAttribute("class", "importsampledata");
  secondCell.setAttribute("flex", "1");

  item.appendChild(checkboxCell);
  item.appendChild(firstCell);
  item.appendChild(secondCell);
  return item;
}

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

// 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.lastElementChild.getAttribute("value");
  swapPartner.lastElementChild.setAttribute(
    "value",
    selectedItem.lastElementChild.getAttribute("value")
  );
  selectedItem.lastElementChild.setAttribute("value", tmpLabel);

  var newItemPosition = up ? selectedItem.nextElementSibling : 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)
      .lastElementChild.setAttribute(
        "value",
        i < fields.length ? fields[i] : ""
      );
  }
}

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

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

/**
 * Handle the command event of #next and #previous buttons.
 *
 * @param {Event} event - The command event of #next or #previous button.
 */
function nextPreviousOnCommand(event) {
  browseDataPreview(event.target.id == "next" ? 1 : -1);
}

/**
 * Browse the import data preview by moving to the next or previous record.
 * Also handle the disabled status of the #next and #previous buttons at the
 * first or last record.
 *
 * @param {integer} step - How many records to move forwards or backwards.
 *   Used by the #next and #previous buttons with step of 1 or -1.
 */
function browseDataPreview(step) {
  gRecordNum += step;
  if (FetchSampleData(gRecordNum - 1)) {
    document.l10n.setAttributes(
      document.getElementById("labelRecordNumber"),
      "import-ab-csv-preview-record-number",
      {
        recordNumber: gRecordNum,
      }
    );
  }

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

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

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

  top.fieldMap.skipFirstRecord = gSkipFirstRecordButton.checked;

  top.dialogResult.ok = true;
}