file-dialog.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. * File (upload) dialog plugin for Editor.md
  3. *
  4. * @file file-dialog.js
  5. * @author minho
  6. * @version 0.1.0
  7. * @updateTime 2017-01-06
  8. * {@link https://github.com/lifei6671/SmartWiki}
  9. * @license MIT
  10. */
  11. (function() {
  12. var factory = function (exports) {
  13. var pluginName = "file-dialog";
  14. exports.fn.fileDialog = function() {
  15. var _this = this;
  16. var cm = this.cm;
  17. var lang = this.lang;
  18. var editor = this.editor;
  19. var settings = this.settings;
  20. var cursor = cm.getCursor();
  21. var selection = cm.getSelection();
  22. var fileLang = lang.dialog.file;
  23. var classPrefix = this.classPrefix;
  24. var iframeName = classPrefix + "file-iframe";
  25. var dialogName = classPrefix + pluginName, dialog;
  26. cm.focus();
  27. var loading = function(show) {
  28. var _loading = dialog.find("." + classPrefix + "dialog-mask");
  29. _loading[(show) ? "show" : "hide"]();
  30. };
  31. if (editor.find("." + dialogName).length < 1)
  32. {
  33. var guid = (new Date).getTime();
  34. var action = settings.fileUploadURL + (settings.fileUploadURL.indexOf("?") >= 0 ? "&" : "?") + "guid=" + guid;
  35. if (settings.crossDomainUpload)
  36. {
  37. action += "&callback=" + settings.uploadCallbackURL + "&dialog_id=editormd-file-dialog-" + guid;
  38. }
  39. var dialogContent = ( (settings.fileUpload) ? "<form action=\"" + action +"\" target=\"" + iframeName + "\" method=\"post\" enctype=\"multipart/form-data\" class=\"" + classPrefix + "form\">" : "<div class=\"" + classPrefix + "form\">" ) +
  40. ( (settings.fileUpload) ? "<iframe name=\"" + iframeName + "\" id=\"" + iframeName + "\" guid=\"" + guid + "\"></iframe>" : "" ) +
  41. "<label>文件地址</label>" +
  42. "<input type=\"text\" data-url />" + (function(){
  43. return (settings.fileUpload) ? "<div class=\"" + classPrefix + "file-input\">" +
  44. "<input type=\"file\" name=\"" + classPrefix + "file-file\" />" +
  45. "<input type=\"submit\" value=\"本地上传\" />" +
  46. "</div>" : "";
  47. })() +
  48. "<br/>" +
  49. "<label>文件说明</label>" +
  50. "<input type=\"text\" value=\"" + selection + "\" data-alt />" +
  51. "<br/>" +
  52. "<input type='hidden' data-icon>"+
  53. ( (settings.fileUpload) ? "</form>" : "</div>");
  54. //var imageFooterHTML = "<button class=\"" + classPrefix + "btn " + classPrefix + "image-manager-btn\" style=\"float:left;\">" + fileLang.managerButton + "</button>";
  55. dialog = this.createDialog({
  56. title : "文件上传",
  57. width : (settings.fileUpload) ? 465 : 380,
  58. height : 254,
  59. name : dialogName,
  60. content : dialogContent,
  61. mask : settings.dialogShowMask,
  62. drag : settings.dialogDraggable,
  63. lockScreen : settings.dialogLockScreen,
  64. maskStyle : {
  65. opacity : settings.dialogMaskOpacity,
  66. backgroundColor : settings.dialogMaskBgColor
  67. },
  68. buttons : {
  69. enter : [lang.buttons.enter, function() {
  70. var url = this.find("[data-url]").val();
  71. var alt = this.find("[data-alt]").val();
  72. var icon = this.find("[data-icon]").val();
  73. if (url === "")
  74. {
  75. alert(fileLang.fileURLEmpty);
  76. return false;
  77. }
  78. var altAttr = (alt !== "") ? " \"" + alt + "\"" : "";
  79. if (icon === "" || !icon)
  80. {
  81. cm.replaceSelection("[" + alt + "](" + url + altAttr + ")");
  82. }
  83. else
  84. {
  85. cm.replaceSelection("[![" + alt + "](" + icon + ")"+ alt +"](" + url + altAttr + ")");
  86. }
  87. if (alt === "") {
  88. cm.setCursor(cursor.line, cursor.ch + 2);
  89. }
  90. this.hide().lockScreen(false).hideMask();
  91. return false;
  92. }],
  93. cancel : [lang.buttons.cancel, function() {
  94. this.hide().lockScreen(false).hideMask();
  95. return false;
  96. }]
  97. }
  98. });
  99. dialog.attr("id", classPrefix + "file-dialog-" + guid);
  100. if (!settings.fileUpload) {
  101. return ;
  102. }
  103. var fileInput = dialog.find("[name=\"" + classPrefix + "file-file\"]");
  104. fileInput.bind("change", function() {
  105. var fileName = fileInput.val();
  106. if (fileName === "")
  107. {
  108. alert(fileLang.uploadFileEmpty);
  109. return false;
  110. }
  111. loading(true);
  112. var submitHandler = function() {
  113. var uploadIframe = document.getElementById(iframeName);
  114. uploadIframe.onload = function() {
  115. loading(false);
  116. var body = (uploadIframe.contentWindow ? uploadIframe.contentWindow : uploadIframe.contentDocument).document.body;
  117. var json = (body.innerText) ? body.innerText : ( (body.textContent) ? body.textContent : null);
  118. json = (typeof JSON.parse !== "undefined") ? JSON.parse(json) : eval("(" + json + ")");
  119. if(!settings.crossDomainUpload)
  120. {
  121. if (json.success === 1)
  122. {
  123. dialog.find("[data-url]").val(json.url);
  124. json.alt && dialog.find("[data-alt]").val(json.alt);
  125. }
  126. else
  127. {
  128. alert(json.message);
  129. }
  130. }
  131. return false;
  132. };
  133. };
  134. dialog.find("[type=\"submit\"]").bind("click", submitHandler).trigger("click");
  135. });
  136. }
  137. dialog = editor.find("." + dialogName);
  138. dialog.find("[type=\"text\"]").val("");
  139. dialog.find("[type=\"file\"]").val("");
  140. this.dialogShowMask(dialog);
  141. this.dialogLockScreen();
  142. dialog.show();
  143. };
  144. };
  145. // CommonJS/Node.js
  146. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  147. {
  148. module.exports = factory;
  149. }
  150. else if (typeof define === "function") // AMD/CMD/Sea.js
  151. {
  152. if (define.amd) { // for Require.js
  153. define(["editormd"], function(editormd) {
  154. factory(editormd);
  155. });
  156. } else { // for Sea.js
  157. define(function(require) {
  158. var editormd = require("./../../editormd");
  159. factory(editormd);
  160. });
  161. }
  162. }
  163. else
  164. {
  165. factory(window.editormd);
  166. }
  167. })();