summaryrefslogtreecommitdiff
path: root/web-ui/app/js/mail_view/ui/recipients/recipients.js
blob: d2a5160db38de7eb5951ffba21ad2858c99dba1e (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
/*
 * Copyright (c) 2014 ThoughtWorks, Inc.
 *
 * Pixelated is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pixelated is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
 */

define(
  [
    'flight/lib/component',
    'views/templates',
    'page/events',
    'mail_view/ui/recipients/recipients_input',
    'mail_view/ui/recipients/recipient',
    'mail_view/ui/recipients/recipients_iterator'
  ],
  function (defineComponent, templates, events, RecipientsInput, Recipient, RecipientsIterator) {
    'use strict';

    return defineComponent(recipients);

    function recipients() {
      this.defaultAttrs({
        navigationHandler: '.recipients-navigation-handler'
      });

      function getAddresses(recipients) {
        return _.flatten(_.map(recipients, function (e) { return e.attr.address;}));
      }

      function moveLeft() { this.attr.iterator.moveLeft(); }
      function moveRight() { this.attr.iterator.moveRight(); }
      function deleteCurrentRecipient() {
        this.attr.iterator.deleteCurrent();
        this.addressesUpdated();
      }

      var SPECIAL_KEYS_ACTIONS = {
        8: deleteCurrentRecipient,
        46: deleteCurrentRecipient,
        37: moveLeft,
        39: moveRight
      };

      this.addRecipient = function(recipient) {
        var newRecipient = Recipient.prototype.renderAndPrepend(this.$node, recipient);
        this.attr.recipients.push(newRecipient);
      };

      this.recipientEntered = function (event, recipient) {
        this.addRecipient(recipient);
        this.addressesUpdated();
      };

      this.deleteLastRecipient = function () {
        this.attr.recipients.pop().destroy();
        this.addressesUpdated();
      };

      this.enterNavigationMode = function () {
        this.attr.iterator = new RecipientsIterator({
          elements: this.attr.recipients,
          exitInput: this.attr.input.$node
        });

        this.attr.iterator.current().doSelect();
        this.attr.input.$node.blur();
        this.select('navigationHandler').focus();
      };

      this.leaveNavigationMode = function () {
        if(this.attr.iterator) { this.attr.iterator.current().unselect(); }
        this.attr.iterator = null;
      };

      this.selectLastRecipient = function () {
        if (this.attr.recipients.length === 0) { return; }
        this.enterNavigationMode();
      };

      this.attachInput = function () {
        this.attr.input = RecipientsInput.prototype.attachAndReturn(this.$node.find('input[type=text]'), this.attr.name);
      };

      this.processSpecialKey = function (event) {
        if(SPECIAL_KEYS_ACTIONS.hasOwnProperty(event.which)) { SPECIAL_KEYS_ACTIONS[event.which].apply(this); }
      };

      this.initializeAddresses = function () {
        _.each(_.flatten(this.attr.addresses), function (address) {
          this.addRecipient({ address: address, name: this.attr.name });
        }.bind(this));
      };

      this.addressesUpdated = function() {
        this.trigger(document, events.ui.recipients.updated, {recipientsName: this.attr.name, newRecipients: getAddresses(this.attr.recipients)});
      };

      this.doCompleteRecipients = function () {
        var address = this.attr.input.$node.val();
        if (!_.isEmpty(address)) {
          var recipient = Recipient.prototype.renderAndPrepend(this.$node, { name: this.attr.name, address: address });
          this.attr.recipients.push(recipient);
          this.attr.input.$node.val('');
        }

        this.trigger(document, events.ui.recipients.updated, {
          recipientsName: this.attr.name,
          newRecipients: getAddresses(this.attr.recipients),
          skipSaveDraft: true
        });

      };

      this.after('initialize', function () {
        this.attr.recipients = [];
        this.attachInput();
        this.initializeAddresses();

        this.on(events.ui.recipients.deleteLast, this.deleteLastRecipient);
        this.on(events.ui.recipients.selectLast, this.selectLastRecipient);
        this.on(events.ui.recipients.entered, this.recipientEntered);

        this.on(document, events.ui.recipients.doCompleteInput, this.doCompleteRecipients);

        this.on(this.attr.input.$node, 'focus', this.leaveNavigationMode);
        this.on(this.select('navigationHandler'), 'keydown', this.processSpecialKey);

        this.on(document, events.dispatchers.rightPane.clear, this.teardown);
      });
    }
  });