/*
 * Copyright (C) 2016 Singular Studios (a.k.a Atom Tecnologia) - www.opensingular.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function () {

    const FILE_REGEX_PATTERN = /.*\.(.*)/;
    const SUPPORTED_EXTENSIONS = ['pdf', 'jpg', 'gif', 'png'];

    "use strict";
    if (window.DownloadSupportedBehavior == undefined) {
        window.DownloadSupportedBehavior = function () {
        };
        window.DownloadSupportedBehavior.ajaxDownload = function (url, fileId, filename) {
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: url + '&fileId=' + fileId + '&fileName=' + filename,
                success: function (response, status, request) {
                    if (window.DownloadSupportedBehavior.isContentTypeBrowserFriendly(filename)) {
                        window.open(response.url);
                    } else {
                        var form = $('<form method="GET" action="' + response.url + '">');
                        $('body').append(form);
                        form.submit();
                        form.remove();
                    }
                }
            });
            return false;
        };
        window.DownloadSupportedBehavior.resolveUrl = function (url, fileId, filename, callback) {
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: url + '&fileId=' + fileId + '&fileName=' + filename,
                success: function (response, status, request) {
                    callback(response.url);
                }
            });
            return false;
        };
        window.DownloadSupportedBehavior.isContentTypeBrowserFriendly = function (filename) {
            return FILE_REGEX_PATTERN.test(filename) && SUPPORTED_EXTENSIONS.indexOf(FILE_REGEX_PATTERN.exec(filename)[1]) >= 0;
        }
    }
})();



@@ -990,32 +990,207 @@
 Id, filename) %7B%0A
+            var newWindow;%0A            if (window.DownloadSupportedBehavior.isContentTypeBrowserFriendly(filename)) %7B%0A                newWindow = window.open();%0A            %7D%0A
             $.aj
@@ -1186,32 +1186,32 @@
        $.ajax(%7B%0A
-
                 
@@ -1424,79 +1424,39 @@
 if (
-window.DownloadSupportedBehavior.isContentTypeBrowserFriendly(filename)
+typeof newWindow != %22undefined%22
 ) %7B%0A
@@ -1479,28 +1479,80 @@
         
-w
+newW
 indow.
-open(
+location = location.protocol + %22//%22 + location.host + 
 response
@@ -1547,33 +1547,32 @@
 t + response.url
-)
 ;%0A              

ajuste na visualização de arquivo em nova aba