~jonas-drange/online-services-common-js/navbar-autocomplete

« back to all changes in this revision

Viewing changes to src/contacts-merge/js/contacts-merge.js

  • Committer: Stephen Stewart
  • Date: 2014-02-22 23:57:25 UTC
  • mfrom: (18.1.2 trunk)
  • Revision ID: stephen.stewart@canonical.com-20140222235725-iw6f15t9umws19xd
mergeĀ lp:~stephen-stewart/online-services-common-js/remove-u1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
var CLICK = 'click',
2
 
    SmartMergeDialog;
3
 
 
4
 
SmartMergeDialog = Y.Base.create('smartMergeDialog', Y.Overlay, [], {
5
 
    initializer: function() {
6
 
        this._getMergeCandidates();
7
 
    },
8
 
    bindUI: function() {
9
 
        /* Listen for escape event. */
10
 
        Y.on('keydown', function(e) {
11
 
            if (e.keyCode === 27) {
12
 
                this.destroy();
13
 
                Y.Global.fire('one:idle');
14
 
            }
15
 
        }, window, this);
16
 
    },
17
 
    _getMergeCandidates: function() {
18
 
        Y.io('/contacts/_/smart_merge_contact_list', {
19
 
            timeout: 2*60*1000, // 2 minutes
20
 
            on: {
21
 
                success: Y.bind(this._populateContent, this),
22
 
                failure: Y.bind(this._handleFailure, this)
23
 
            }
24
 
        });
25
 
    },
26
 
    _handleCancel: function(e) {
27
 
        e.preventDefault();
28
 
        this.destroy();
29
 
    },
30
 
    _handleFailure: function() {
31
 
        Y.Global.fire('one:idle');
32
 
        Y.Global.fire('one:error');
33
 
        this.destroy();
34
 
    },
35
 
    _handleSave: function(e) {
36
 
        e.preventDefault();
37
 
        Y.Global.fire('one:busy');
38
 
        Y.io('/contacts/merge', {
39
 
            method: 'POST',
40
 
            data: {'csrfmiddlewaretoken': YUI.Env.CSRF_TOKEN},
41
 
            timeout: 2*60*1000, // 2 minutes
42
 
            form: {
43
 
                id: Y.one('#smart-merge-form')
44
 
            },
45
 
            on: {
46
 
                success: Y.bind(this._saveSuccess, this),
47
 
                failure: function() {
48
 
                }
49
 
            }
50
 
        });
51
 
    },
52
 
    _populateContent: function(id, response) {
53
 
        if (!this.get('cancelled')) {
54
 
            var content = Y.Node.create('<div class="contact-box">' +
55
 
                                        response.responseText + '</div>'),
56
 
            wrapperDiv,
57
 
            footer = Y.Node.create('<div class="control-footer"></div>'),
58
 
            submitButton = content.one('input[type="submit"]'),
59
 
            cancel,
60
 
            save,
61
 
            description,
62
 
            close;
63
 
 
64
 
            if (submitButton) {
65
 
                submitButton.remove();
66
 
                wrapperDiv = Y.Node.create(
67
 
                    '<div></div>'),
68
 
                    description = Y.Node.create(
69
 
                        '<p>' + this.get('strings').description + '</p>'),
70
 
                        save = Y.Node.create('<a class="clickable save-button">Merge</a>'),
71
 
                        cancel = Y.Node.create('<a class="clickable cancel-button">Cancel</a>');
72
 
 
73
 
                        wrapperDiv.appendChild(description);
74
 
                        wrapperDiv.appendChild(content);
75
 
                        this.set('bodyContent', wrapperDiv);
76
 
 
77
 
                        save.on(CLICK, this._handleSave, this);
78
 
                        cancel.on(CLICK, this._handleCancel, this);
79
 
 
80
 
                        footer.appendChild(save);
81
 
                        footer.appendChild(cancel);
82
 
                        this.set('footerContent', footer);
83
 
 
84
 
            } else {
85
 
                close = Y.Node.create(
86
 
                    '<a class="clickable save-button">OK</a>');
87
 
                    close.on(CLICK, this._handleCancel, this);
88
 
 
89
 
                    footer.appendChild(close);
90
 
                    this.set('bodyContent', content);
91
 
                    this.set('footerContent', footer);
92
 
            }
93
 
            Y.Global.fire('one:idle');
94
 
            this.render();
95
 
        }
96
 
    },
97
 
    _saveFailure: function() {
98
 
        Y.Global.fire('one:idle');
99
 
        this.destroy();
100
 
        Y.Global.fire(
101
 
            'one:error',
102
 
            this.get('strings').error_msg);
103
 
    },
104
 
    _saveSuccess: function() {
105
 
        Y.Global.fire('one:stale');
106
 
        Y.Global.fire('one:idle');
107
 
        this.destroy();
108
 
    }
109
 
}, {
110
 
    ATTRS: {
111
 
        headerContent: { value: '<h2>Merge contacts</h2>' },
112
 
        visible: { value: true },
113
 
        centered: { value: true },
114
 
        width: { value: '400px' },
115
 
        zIndex: { value: 1000 },
116
 
 
117
 
        saveButton: {},
118
 
        cancelButton: {},
119
 
        cancelled: {
120
 
            value: false
121
 
        },
122
 
        strings: {
123
 
            value: {
124
 
                description: "We found some contacts that look pretty similar. We can merge them together if you'd like.",
125
 
                error_msg: "We're sorry. Something has gone wrong. Our engineers have been notified."
126
 
            }
127
 
        }
128
 
    }
129
 
});
130
 
Y.SmartMergeDialog = SmartMergeDialog;
131