| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | /*! * File (upload) dialog plugin for Editor.md * * @file        file-dialog.js * @author      minho * @version     0.1.0 * @updateTime  2017-01-06 * {@link       https://github.com/lifei6671/SmartWiki} * @license     MIT */(function() {    var factory = function (exports) {        var pluginName   = "file-dialog";        exports.fn.fileDialog = function() {            var _this       = this;            var cm          = this.cm;            var lang        = this.lang;            var editor      = this.editor;            var settings    = this.settings;            var cursor      = cm.getCursor();            var selection   = cm.getSelection();            var fileLang   = lang.dialog.file;            var classPrefix = this.classPrefix;            var iframeName  = classPrefix + "file-iframe";            var dialogName  = classPrefix + pluginName, dialog;            cm.focus();            var loading = function(show) {                var _loading = dialog.find("." + classPrefix + "dialog-mask");                _loading[(show) ? "show" : "hide"]();            };            if (editor.find("." + dialogName).length < 1)            {                var guid   = (new Date).getTime();                var action = settings.fileUploadURL + (settings.fileUploadURL.indexOf("?") >= 0 ? "&" : "?") + "guid=" + guid;                if (settings.crossDomainUpload)                {                    action += "&callback=" + settings.uploadCallbackURL + "&dialog_id=editormd-file-dialog-" + guid;                }                var dialogContent = ( (settings.fileUpload) ? "<form action=\"" + action +"\" target=\"" + iframeName + "\" method=\"post\" enctype=\"multipart/form-data\" class=\"" + classPrefix + "form\">" : "<div class=\"" + classPrefix + "form\">" ) +                    ( (settings.fileUpload) ? "<iframe name=\"" + iframeName + "\" id=\"" + iframeName + "\" guid=\"" + guid + "\"></iframe>" : "" ) +                    "<label>文件地址</label>" +                    "<input type=\"text\" data-url />" + (function(){                        return (settings.fileUpload) ? "<div class=\"" + classPrefix + "file-input\">" +                            "<input type=\"file\" name=\"" + classPrefix + "file-file\" />" +                            "<input type=\"submit\" value=\"本地上传\" />" +                            "</div>" : "";                    })() +                    "<br/>" +                    "<label>文件说明</label>" +                    "<input type=\"text\" value=\"" + selection + "\" data-alt />" +                    "<br/>" +                    "<input type='hidden' data-icon>"+                    ( (settings.fileUpload) ? "</form>" : "</div>");                //var imageFooterHTML = "<button class=\"" + classPrefix + "btn " + classPrefix + "image-manager-btn\" style=\"float:left;\">" + fileLang.managerButton + "</button>";                dialog = this.createDialog({                    title      : "文件上传",                    width      : (settings.fileUpload) ? 465 : 380,                    height     : 254,                    name       : dialogName,                    content    : dialogContent,                    mask       : settings.dialogShowMask,                    drag       : settings.dialogDraggable,                    lockScreen : settings.dialogLockScreen,                    maskStyle  : {                        opacity         : settings.dialogMaskOpacity,                        backgroundColor : settings.dialogMaskBgColor                    },                    buttons : {                        enter : [lang.buttons.enter, function() {                            var url  = this.find("[data-url]").val();                            var alt  = this.find("[data-alt]").val();                            var icon  = this.find("[data-icon]").val();                            if (url === "")                            {                                alert(fileLang.fileURLEmpty);                                return false;                            }                            var altAttr = (alt !== "") ? " \"" + alt + "\"" : "";                            if (icon === "" || !icon)                            {                                cm.replaceSelection("[" + alt + "](" + url + altAttr + ")");                            }                            else                            {                                cm.replaceSelection("["+ alt +"](" + url + altAttr + ")");                            }                            if (alt === "") {                                cm.setCursor(cursor.line, cursor.ch + 2);                            }                            this.hide().lockScreen(false).hideMask();                            return false;                        }],                        cancel : [lang.buttons.cancel, function() {                            this.hide().lockScreen(false).hideMask();                            return false;                        }]                    }                });                dialog.attr("id", classPrefix + "file-dialog-" + guid);                if (!settings.fileUpload) {                    return ;                }                var fileInput  = dialog.find("[name=\"" + classPrefix + "file-file\"]");                fileInput.bind("change", function() {                    var fileName  = fileInput.val();                    if (fileName === "")                    {                        alert(fileLang.uploadFileEmpty);                        return false;                    }                    loading(true);                    var submitHandler = function() {                        var uploadIframe = document.getElementById(iframeName);                        uploadIframe.onload = function() {                            loading(false);                            var body = (uploadIframe.contentWindow ? uploadIframe.contentWindow : uploadIframe.contentDocument).document.body;                            var json = (body.innerText) ? body.innerText : ( (body.textContent) ? body.textContent : null);                            json = (typeof JSON.parse !== "undefined") ? JSON.parse(json) : eval("(" + json + ")");                            if(!settings.crossDomainUpload)                            {                                if (json.success === 1)                                {                                    dialog.find("[data-url]").val(json.url);                                    json.alt && dialog.find("[data-alt]").val(json.alt);                                }                                else                                {                                    alert(json.message);                                }                            }                            return false;                        };                    };                    dialog.find("[type=\"submit\"]").bind("click", submitHandler).trigger("click");                });            }            dialog = editor.find("." + dialogName);            dialog.find("[type=\"text\"]").val("");            dialog.find("[type=\"file\"]").val("");            this.dialogShowMask(dialog);            this.dialogLockScreen();            dialog.show();        };    };    // CommonJS/Node.js    if (typeof require === "function" && typeof exports === "object" && typeof module === "object")    {        module.exports = factory;    }    else if (typeof define === "function")  // AMD/CMD/Sea.js    {        if (define.amd) { // for Require.js            define(["editormd"], function(editormd) {                factory(editormd);            });        } else { // for Sea.js            define(function(require) {                var editormd = require("./../../editormd");                factory(editormd);            });        }    }    else    {        factory(window.editormd);    }})();
 |