handle_iframe_binding.js 3.03 KB
Newer Older
1 2
define(['jquery'], function($) {
    var iframeBinding = function(e) {
3
        var target_element = null;
4 5
        if (typeof(e) === 'undefined') {
            target_element = $('iframe, embed');
6
        } else {
7
            if (typeof(e.nodeName) !== 'undefined') {
8
                target_element = $(e).find('iframe, embed');
9
            } else {
10
                target_element = e.$('iframe, embed');
11 12 13 14 15
            }
        }
        modifyTagContent(target_element);
    };

16
    var modifyTagContent = function(target_element) {
17 18
        target_element.each(function() {
            if ($(this).prop('tagName') === 'IFRAME') {
19
                var ifr_source = $(this).attr('src');
20 21 22

                // Modify iframe src only if it is not empty
                if (ifr_source) {
23
                    var wmode = 'wmode=transparent';
24 25 26 27 28 29 30 31
                    if (ifr_source.indexOf('?') !== -1) {
                        var getQString = ifr_source.split('?');
                        if (getQString[1].search('wmode=transparent') === -1) {
                            var oldString = getQString[1];
                            var newString = getQString[0];
                            $(this).attr('src', newString + '?' + wmode + '&' + oldString);
                        }
                    }
32 33 34
                    // The TinyMCE editor is hosted in an iframe, and before the iframe is
                    // removed we execute this code. To avoid throwing an error when setting the
                    // attr, check that the source doesn't start with the value specified by TinyMCE ('javascript:""').
35
                    else if (ifr_source.lastIndexOf('javascript:', 0) !== 0) {
36
                        $(this).attr('src', ifr_source + '?' + wmode);
37 38 39
                    }
                }
            }
40
            else {
41 42 43 44 45 46 47 48 49 50 51
                $(this).attr('wmode', 'transparent');
            }
        });
    };

    // Modify iframe/embed tags in provided html string
    // Use this method when provided data is just html sting not dom element
    // This method will only modify iframe (add wmode=transparent in url querystring) and embed (add wmode=transparent as attribute)
    // tags in html string so both tags will attach to dom and don't create z-index problem for other popups
    // Note: embed tags should be modified before rendering as they are static objects as compared to iframes
    // Note: this method can modify unintended html (invalid tags) while converting to dom object
52 53
    var iframeBindingHtml = function(html_string) {
        if (html_string) {
54 55 56
            var target_element = null;
            var temp_content = document.createElement('div');
            $(temp_content).html(html_string);
57 58
            target_element = $(temp_content).find('iframe, embed');
            if (target_element.length > 0) {
59 60 61 62 63 64 65 66 67 68 69
                modifyTagContent(target_element);
                html_string = $(temp_content).html();
            }
        }
        return html_string;
    };

    return {
        iframeBinding: iframeBinding,
        iframeBindingHtml: iframeBindingHtml
    };
70
});