summaryrefslogtreecommitdiff
path: root/web-ui/app/js/mail_view/ui/recipients/recipients_input.js
blob: c1c32d7711d7279ba9a8e6cf6d8e3d7b2cc6bbc2 (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
/*
 * 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/>.
 */
/*global _*/
/*global Bloodhound */
'use strict';

define([
    'flight/lib/component',
    'page/events',
    'features'
  ],
  function (defineComponent, events, features) {

    function recipientsInput() {
      var EXIT_KEY_CODE_MAP = {
          8: 'backspace',
          37: 'left'
        },
        ENTER_ADDRESS_KEY_CODE_MAP = {
          9: 'tab',
          186: 'semicolon',
          188: 'comma',
          13: 'enter',
          27: 'esc'
        },
        EVENT_FOR = {
          8: events.ui.recipients.deleteLast,
          37: events.ui.recipients.selectLast
        },
        self;

      var extractContactNames = function (response) {
          return _.map(response, function(a) { return { value: a }; });
      };

      function createEmailCompleter() {
        var emailCompleter = new Bloodhound({
          datumTokenizer: function (d) {
            return [d.value];
          },
          queryTokenizer: function (q) {
            return [q.trim()];
          },
          remote: {
            url: '/contacts?q=%QUERY',
            filter: extractContactNames
          }
        });
        emailCompleter.initialize();
        return emailCompleter;
      }

      function reset(node) {
        node.typeahead('val', '');
      }

      function caretIsInTheBeginningOfInput(input) {
        return input.selectionStart === 0;
      }

      function isExitKey(keyPressed) {
        return EXIT_KEY_CODE_MAP.hasOwnProperty(keyPressed);
      }

      function isEnterAddressKey(keyPressed) {
        return ENTER_ADDRESS_KEY_CODE_MAP.hasOwnProperty(keyPressed);
      }

      this.processSpecialKey = function (event) {
        var keyPressed = event.which;

        if (isExitKey(keyPressed) && caretIsInTheBeginningOfInput(this.$node[0])) {
          this.trigger(EVENT_FOR[keyPressed]);
          return;
        }

        if (!event.shiftKey && isEnterAddressKey(keyPressed)) {
          this.tokenizeRecipient(event);

          if ((keyPressed !== 9 /* tab */)) {
            event.preventDefault();
          }
        }

      };

      this.tokenizeRecipient = function (event) {
        if (_.isEmpty(this.$node.val().trim())) {
          return;
        }

        this.recipientSelected(null, {value: this.$node.val() });
        event.preventDefault();
      };

      this.recipientSelected = function (event, data) {
        var value = (data && data.value) || this.$node.val();
        var that = this;
        _.each(value.split(/[,;]/), function(address) {
            if (!_.isEmpty(address.trim())) {
                that.trigger(that.$node, events.ui.recipients.entered, { name: that.attr.name, address: address.trim() });
            }
        });
        reset(this.$node);
      };

      this.init = function () {
        this.$node.typeahead({
          hint: true,
          highlight: true,
          minLength: 1
        }, {
          source: createEmailCompleter().ttAdapter()
        });
      };

      this.attachAndReturn = function (node, name) {
        var input = new this.constructor();
        input.initialize(node, { name: name});
        return input;
      };

      this.warnSendButtonOfInputState = function () {
        var toTrigger = _.isEmpty(this.$node.val()) ? events.ui.recipients.inputHasNoMail : events.ui.recipients.inputHasMail;
        this.trigger(document, toTrigger, { name: this.attr.name });
      };

      this.after('initialize', function () {
        self = this;
        this.init();
        this.on('typeahead:selected typeahead:autocompleted', this.recipientSelected);
        this.on(this.$node, 'blur', this.tokenizeRecipient);
        this.on(this.$node, 'keydown', this.processSpecialKey);
        this.on(this.$node, 'keyup', this.warnSendButtonOfInputState);

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

    return defineComponent(recipientsInput);

  }
);