summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/EmbedVideo/js/autoResize.js
blob: 294236a7d14edad40dd46ccc5c19eced28499b2c (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
(function(mw, $) {
	$(function() {
        autoResizer(); // run first thing, because we dont need a resize to be broken.

        var autoResizerRTime;
        var autoResizerTimeout = false;
        var autoResizerDelta = 200;

        $(window).resize(function() {
			autoResizerRTime = new Date();
            if (autoResizerTimeout === false) {
                autoResizerTimeout = true;
                setTimeout(autoResizerResizeEnd, autoResizerDelta);
            }
        });

		function isFullscreen() {
			if (document.fullscreenElement != null || document.mozFullScreenElement != null || document.webkitFullscreenElement != null) {
				return true;
			}
			return false;
		}

        function autoResizerResizeEnd() {
			if (isFullscreen()) {
                autoResizerTimeout = false;
				return;
			}
            if (new Date() - autoResizerRTime < autoResizerDelta) {
                setTimeout(autoResizerResizeEnd, autoResizerDelta);
            } else {
                autoResizerTimeout = false;
                autoResizer();
            }
        }

        function autoResizer() {
            $('.autoResize').each(function(){
                var parent = $(this).parent();
                var self = $(this);
                var iframe = self.find('iframe');
                var wrap = self.find('.embedvideowrap');

                if (iframe.width() > parent.width()) {
                    resizeHandler(self, iframe, parent, wrap);
                } else {
                    self.removeClass('autoResized').css('width', '')
                    var originalWidth = iframe.attr("data-orig-width");
                    var originalHeight = iframe.attr("data-orig-height");
                    iframe.width(originalWidth).css('width', originalWidth).attr('width', originalWidth);
                    iframe.height(originalHeight).css('height', originalHeight).attr('height', originalHeight);
                    wrap.width(originalWidth).css('width', originalWidth).attr('width', originalWidth);
                    wrap.height(originalHeight).css('height', originalHeight).attr('height', originalHeight);
                }

                if (!self.hasClass('autoResized') && iframe.width() > parent.width()) {
                    resizeHandler(self, iframe, parent, wrap);
                }
            });
        }

        function resizeHandler(self, iframe, parent, wrap) {
            self.addClass('autoResized');

            if (typeof iframe.attr("data-orig-height") == 'undefined') {
                iframe.attr("data-orig-height", iframe.height());
                iframe.attr("data-orig-width", iframe.width());
            }

            var aspect = iframe.width() / iframe.height();
            var newWidth = parent.width();
            var newHeight = newWidth / aspect;

            self.width(newWidth).css('width', newWidth);
            iframe.width(newWidth).css('width', newWidth).attr('width', newWidth);
            iframe.height(newHeight).css('height', newHeight).attr('height', newHeight);
            wrap.width(newWidth).css('width', newWidth).attr('width', newWidth);
            wrap.height(newHeight).css('height', newHeight).attr('height', newHeight);
        }

    });

}(mediaWiki, jQuery));