summaryrefslogtreecommitdiffstats
path: root/toolkit/components/formautofill/content/requestAutocomplete.js
blob: 47d50096469b71e40bb6eae1ee5a8d076c8a752a (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
/* 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/. */

/*
 * Implementation of "requestAutocomplete.xhtml".
 */

"use strict";

var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "Promise",
                                  "resource://gre/modules/Promise.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
                                  "resource://gre/modules/Task.jsm");

const RequestAutocompleteDialog = {
  resolveFn: null,
  autofillData: null,

  onLoad: function () {
    Task.spawn(function* () {
      let args = window.arguments[0].wrappedJSObject;
      this.resolveFn = args.resolveFn;
      this.autofillData = args.autofillData;

      window.sizeToContent();

      Services.obs.notifyObservers(window,
                                   "formautofill-window-initialized", "");
    }.bind(this)).catch(Cu.reportError);
  },

  onAccept: function () {
    // TODO: Replace with autofill storage module (bug 1018304).
    const dummyDB = {
      "": {
        "name": "Mozzy La",
        "street-address": "331 E Evelyn Ave",
        "address-level2": "Mountain View",
        "address-level1": "CA",
        "country": "US",
        "postal-code": "94041",
        "email": "email@example.org",
      }
    };

    let result = { fields: [] };
    for (let section of this.autofillData.sections) {
      for (let addressSection of section.addressSections) {
        let addressType = addressSection.addressType;
        if (!(addressType in dummyDB)) {
          continue;
        }

        for (let field of addressSection.fields) {
          let fieldName = field.fieldName;
          if (!(fieldName in dummyDB[addressType])) {
            continue;
          }

          result.fields.push({
            section: section.name,
            addressType: addressType,
            contactType: field.contactType,
            fieldName: field.fieldName,
            value: dummyDB[addressType][fieldName],
          });
        }
      }
    }

    window.close();
    this.resolveFn(result);
  },

  onCancel: function () {
    window.close();
    this.resolveFn({ canceled: true });
  },
};