/** 
    @ Name : Careff Javascript Library 
    @ Author : 
    @ Date : 2017-5-1
    @ Email : 
    @ Version : 1.0 
    @ Update : 
    功能： 开始建立自己的JS类库，需要按照功能进行分类；
     */
(function () {
    if (!window.usexportpng) {
        window.usexportpng = {};
    };
    window.usexportpng = {
        NAME: "usexportpng",
        VERSION: '1.0',
        DATE: '2018-04-13',
        INDEX:10,
        /** 
       @ Name :保存数据
       @ Update : 
       */
       UsInitExportDashboardPNG: function () {
            var pageexportbtn = "#pageexportbtn";
            $(pageexportbtn).removeClass("nodisplay");
            $(pageexportbtn).click(function () {
                usexportpng.UsTakeScreenshot(pageexportbtn, 'page-content');
            });
       },
       UsTakeScreenshot: function (pagebtn, pagecontent) {
            $(pagebtn).hide();
            html2canvas(document.getElementById(pagecontent), {
                onrendered: function (c) {
                    var isIE = function () { //ie?  
                        if (!!window.ActiveXObject || "ActiveXObject" in window)
                            return true;
                        else
                            return false;
                    }
                    var saveFileForIE = function (canvas, fileName) {
                        var blob = canvas.msToBlob();
                        navigator.msSaveBlob(blob, fileName);
                    }
                    var _fixType = function (type) {
                        type = type.toLowerCase().replace(/jpg/i, 'jpeg');
                        var r = type.match(/png|jpeg|bmp|gif/)[0];
                        return 'image/' + r;
                    };
                    var saveFile = function (data, filename) {
                        var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                        save_link.href = data;
                        save_link.download = filename;
                        var event = document.createEvent('MouseEvents');
                        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                        save_link.dispatchEvent(event);
                    };
                    var type = 'png';
                    var imgData = c.toDataURL(type);
                    imgData = imgData.replace(_fixType(type), 'image/octet-stream');
                    var filename = document.title + '.' + type;
                    $(pagebtn).show();
                    var explorer = navigator.userAgent;
                    if (isIE()) {
                        saveFileForIE(c, filename);
                    }
                    else {
                        saveFile(imgData, filename);
                    }
                },
            });
        }
    };

})();