summaryrefslogtreecommitdiff
path: root/web-ui/app/js/dispatchers/right_pane_dispatcher.js
blob: 77d578e0a7994a7c1d407462cda17be409066511 (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
/*
 * 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 Pixelated */

define(
  [
    'flight/lib/component',
    'mail_view/ui/compose_box',
    'mail_view/ui/mail_view',
    'mail_view/ui/reply_section',
    'mail_view/ui/draft_box',
    'mail_view/ui/no_message_selected_pane',
    'page/events'
  ],

  function(defineComponent, ComposeBox, MailView, ReplySection, DraftBox, NoMessageSelectedPane, events) {
    'use strict';

    return defineComponent(rightPaneDispatcher);

    function rightPaneDispatcher() {
      this.defaultAttrs({
        rightPane: '#right-pane',
        composeBox: 'compose-box',
        mailView: 'mail-view',
        noMessageSelectedPane: 'no-message-selected-pane',
        replySection: 'reply-section',
        draftBox: 'draft-box',
        currentTag: ''
      });

      this.createAndAttach = function(newContainer) {
        var stage = $('<div>', { id: newContainer });
        this.select('rightPane').append(stage);
        return stage;
      };

      this.reset = function (newContainer) {
        this.trigger(document, events.dispatchers.rightPane.clear);
        this.select('rightPane').empty();
        var stage = this.createAndAttach(newContainer);
        return stage;
      };

      this.openComposeBox = function() {
        var stage = this.reset(this.attr.composeBox);
        ComposeBox.attachTo(stage, {currentTag: this.attr.currentTag});
      };

      this.openMail = function(ev, data) {
        var stage = this.reset(this.attr.mailView);
        MailView.attachTo(stage, data);

        var replySectionContainer = this.createAndAttach(this.attr.replySection);
        ReplySection.attachTo(replySectionContainer, { ident: data.ident });
      };

      this.initializeNoMessageSelectedPane = function () {
        var stage = this.reset(this.attr.noMessageSelectedPane);
        NoMessageSelectedPane.attachTo(stage);
        this.trigger(document, events.dispatchers.middlePane.cleanSelected);
      };

      this.openNoMessageSelectedPane = function(ev, data) {
        this.initializeNoMessageSelectedPane();

        this.trigger(document, events.router.pushState, { tag: this.attr.currentTag, isDisplayNoMessageSelected: true });
      };

      this.openDraft = function (ev, data) {
        var stage = this.reset(this.attr.draftBox);
        DraftBox.attachTo(stage, { mailIdent: data.ident, currentTag: this.attr.currentTag });
      };

      this.selectTag = function(ev, data) {
        this.trigger(document, events.ui.tags.loaded, {tag: data.tag});
      };

      this.saveTag = function(ev, data) {
        this.attr.currentTag = data.tag;
      };

      this.after('initialize', function () {
        this.on(document, events.dispatchers.rightPane.openComposeBox, this.openComposeBox);
        this.on(document, events.dispatchers.rightPane.openDraft, this.openDraft);
        this.on(document, events.ui.mail.open, this.openMail);
        this.on(document, events.dispatchers.rightPane.openNoMessageSelected, this.openNoMessageSelectedPane);
        this.on(document, events.dispatchers.rightPane.selectTag, this.selectTag);
        this.on(document, events.ui.tag.selected, this.saveTag);
        this.on(document, events.ui.tag.select, this.saveTag);
        this.on(document, events.dispatchers.rightPane.openNoMessageSelectedWithoutPushState, this.initializeNoMessageSelectedPane);
        this.initializeNoMessageSelectedPane();
      });
    }
  }
);