summaryrefslogtreecommitdiff
path: root/web-ui/app/js/dispatchers/right_pane_dispatcher.js
blob: 834fc795a0bfa2282e2f61b8d831bbcace5e8170 (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
/*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.dispatchers.rightPane.openNoMessageSelectedWithoutPushState, this.initializeNoMessageSelectedPane);
        this.initializeNoMessageSelectedPane();
      });
    }
  }
);