summaryrefslogtreecommitdiff
path: root/web-ui/app/js/mail_view/ui/send_button.js
blob: f3df3739c53aa1757fef9960eaffbcdefbbe5026 (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
/*
 * 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 _ */
'use strict';

define([
    'flight/lib/component',
    'flight/lib/utils',
    'page/events',
    'helpers/view_helper'
  ],
  function (defineComponent, utils, events, viewHelper) {

    return defineComponent(sendButton);

    function sendButton() {
      var RECIPIENTS_BOXES_COUNT = 3;

      this.enableButton = function () {
        this.$node.prop('disabled', false);
      };

      this.disableButton = function () {
        this.$node.prop('disabled', true);
      };

      this.atLeastOneFieldHasRecipients = function () {
        return _.any(_.values(this.attr.recipients), function (e) { return !_.isEmpty(e); });
      };

      this.atLeastOneInputHasMail = function () {
        return _.any(_.values(this.attr.inputHasMail), function (e) { return e === true; });
      };

      this.updateButton = function () {
        if (this.atLeastOneInputHasMail() || this.atLeastOneFieldHasRecipients()) {
          this.enableButton();
        } else {
          this.disableButton();
        }
      };

      this.inputHasNoMail = function (ev, data) {
        this.attr.inputHasMail[data.name] = false;
        this.updateButton();
      };

      this.inputHasMail = function (ev, data) {
        this.attr.inputHasMail[data.name] = true;
        this.updateButton();
      };

      this.updateRecipientsForField = function (ev, data) {
        this.attr.recipients[data.recipientsName] = data.newRecipients;
        this.attr.inputHasMail[data.recipientsName] = false;

        this.updateButton();
      };

      this.updateRecipientsAndSendMail = function () {

        this.on(document, events.ui.mail.recipientsUpdated, utils.countThen(RECIPIENTS_BOXES_COUNT, function () {
          this.trigger(document, events.ui.mail.send);
          this.off(document, events.ui.mail.recipientsUpdated);
        }.bind(this)));

        this.trigger(document, events.ui.recipients.doCompleteInput);
        this.disableButton();
        this.$node.text(viewHelper.i18n('sending-mail'));

      };

      this.forceEnableButton = function () {
        this.enableButton();
        this.$node.html(viewHelper.i18n('send-button'));
      };

      this.after('initialize', function () {
        this.attr.recipients = {};
        this.attr.inputHasMail = {};
        this.$node.html(viewHelper.i18n('send-button'));

        this.on(document, events.ui.recipients.inputHasMail, this.inputHasMail);
        this.on(document, events.ui.recipients.inputHasNoMail, this.inputHasNoMail);
        this.on(document, events.ui.recipients.updated, this.updateRecipientsForField);

        this.on(this.$node, 'click', this.updateRecipientsAndSendMail);

        this.on(document, events.dispatchers.rightPane.clear, this.teardown);
        this.on(document, events.ui.sendbutton.enable, this.enableButton);
        this.on(document, events.mail.send_failed, this.forceEnableButton);

        this.disableButton();
      });
    }

  }
);