summaryrefslogtreecommitdiff
path: root/web-ui/app/js/mixins/with_mail_sandbox.js
blob: 1a51840d06aade378eecff6660aa06de816cd94d (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
/*
 * 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(
  ['helpers/view_helper', 'page/events'],
  function(viewHelpers, events) {
    'use strict';

    function withMailSandbox() {
      this.showMailOnSandbox = function(mail) {
        var that = this;
        var $iframe = $("#read-sandbox");
        var iframe = $iframe[0];
        var content = viewHelpers.formatMailBody(mail);

        window.addEventListener('message', function(e) {
          if (e.origin === 'null' && e.source === iframe.contentWindow) {
            that.trigger(document, events.ui.replyBox.showReplyContainer);
            that.trigger(document, events.search.highlightResults, {where: '.mail-read-view__header'});
          }
        });

        iframe.onload = function() {
          if ($iframe.iFrameResize) {
            // use iframe-resizer to dynamically adapt iframe size to its content
            var config = {
              resizedCallback: scaleToFit,
              checkOrigin: false
            };
            $iframe.iFrameResize(config);
          }

          iframe.contentWindow.postMessage({
            html: content
          }, '*');

          // transform scale iframe to fit container width
          // necessary if iframe is wider than container
          function scaleToFit() {
            var parentWidth = $iframe.parent().width();
            var w = $iframe.width();
            var scale = 'none';

            // only scale html mails
            if (mail && mail.htmlBody && (w > parentWidth)) {
                scale = parentWidth / w;
                scale = 'scale(' + scale + ',' + scale + ')';
            }

            $iframe.css({
                '-webkit-transform-origin': '0 0',
                '-moz-transform-origin': '0 0',
                '-ms-transform-origin': '0 0',
                'transform-origin': '0 0',
                '-webkit-transform': scale,
                '-moz-transform': scale,
                '-ms-transform': scale,
                'transform': scale
            });
          }
        };
      };
    }

    return withMailSandbox;
  }
);