autoupload.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @description
  3. * 1.拖放文件到编辑区域,自动上传并插入到选区
  4. * 2.插入粘贴板的图片,自动上传并插入到选区
  5. * @author Jinqn
  6. * @date 2013-10-14
  7. */
  8. UE.plugin.register("autoupload", function() {
  9. function sendAndInsertFile(file, editor) {
  10. var me = editor;
  11. //模拟数据
  12. var fieldName,
  13. urlPrefix,
  14. maxSize,
  15. allowFiles,
  16. actionUrl,
  17. loadingHtml,
  18. errorHandler,
  19. successHandler,
  20. filetype = /image\/\w+/i.test(file.type) ? "image" : "file",
  21. loadingId = "loading_" + (+new Date()).toString(36);
  22. fieldName = me.getOpt(filetype + "FieldName");
  23. urlPrefix = me.getOpt(filetype + "UrlPrefix");
  24. maxSize = me.getOpt(filetype + "MaxSize");
  25. allowFiles = me.getOpt(filetype + "AllowFiles");
  26. actionUrl = me.getActionUrl(me.getOpt(filetype + "ActionName"));
  27. errorHandler = function(title) {
  28. var loader = me.document.getElementById(loadingId);
  29. loader && domUtils.remove(loader);
  30. me.fireEvent("showmessage", {
  31. id: loadingId,
  32. content: title,
  33. type: "error",
  34. timeout: 4000
  35. });
  36. };
  37. if (filetype == "image") {
  38. loadingHtml =
  39. '<img class="loadingclass" id="' +
  40. loadingId +
  41. '" src="' +
  42. me.options.themePath +
  43. me.options.theme +
  44. '/images/spacer.gif">';
  45. successHandler = function(data) {
  46. var link = urlPrefix + data.url,
  47. loader = me.document.getElementById(loadingId);
  48. if (loader) {
  49. domUtils.removeClasses(loader, "loadingclass");
  50. loader.setAttribute("src", link);
  51. loader.setAttribute("_src", link);
  52. loader.setAttribute("alt", data.original || "");
  53. loader.removeAttribute("id");
  54. me.trigger("contentchange", loader);
  55. }
  56. };
  57. } else {
  58. loadingHtml =
  59. "<p>" +
  60. '<img class="loadingclass" id="' +
  61. loadingId +
  62. '" src="' +
  63. me.options.themePath +
  64. me.options.theme +
  65. '/images/spacer.gif">' +
  66. "</p>";
  67. successHandler = function(data) {
  68. var link = urlPrefix + data.url,
  69. loader = me.document.getElementById(loadingId);
  70. var rng = me.selection.getRange(),
  71. bk = rng.createBookmark();
  72. rng.selectNode(loader).select();
  73. me.execCommand("insertfile", { url: link });
  74. rng.moveToBookmark(bk).select();
  75. };
  76. }
  77. /* 插入loading的占位符 */
  78. me.execCommand("inserthtml", loadingHtml);
  79. /* 判断后端配置是否没有加载成功 */
  80. if (!me.getOpt(filetype + "ActionName")) {
  81. errorHandler(me.getLang("autoupload.errorLoadConfig"));
  82. return;
  83. }
  84. /* 判断文件大小是否超出限制 */
  85. if (file.size > maxSize) {
  86. errorHandler(me.getLang("autoupload.exceedSizeError"));
  87. return;
  88. }
  89. /* 判断文件格式是否超出允许 */
  90. var fileext = file.name ? file.name.substr(file.name.lastIndexOf(".")) : "";
  91. if (
  92. (fileext && filetype != "image") ||
  93. (allowFiles &&
  94. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") == -1)
  95. ) {
  96. errorHandler(me.getLang("autoupload.exceedTypeError"));
  97. return;
  98. }
  99. /* 创建Ajax并提交 */
  100. var xhr = new XMLHttpRequest(),
  101. fd = new FormData(),
  102. params = utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  103. url = utils.formatUrl(
  104. actionUrl + (actionUrl.indexOf("?") == -1 ? "?" : "&") + params
  105. );
  106. fd.append(
  107. fieldName,
  108. file,
  109. file.name || "blob." + file.type.substr("image/".length)
  110. );
  111. fd.append("type", "ajax");
  112. xhr.open("post", url, true);
  113. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  114. xhr.addEventListener("load", function(e) {
  115. try {
  116. var json = new Function("return " + utils.trim(e.target.response))();
  117. if (json.state == "SUCCESS" && json.url) {
  118. successHandler(json);
  119. } else {
  120. errorHandler(json.state);
  121. }
  122. } catch (er) {
  123. errorHandler(me.getLang("autoupload.loadError"));
  124. }
  125. });
  126. xhr.send(fd);
  127. }
  128. function getPasteImage(e) {
  129. return e.clipboardData &&
  130. e.clipboardData.items &&
  131. e.clipboardData.items.length == 1 &&
  132. /^image\//.test(e.clipboardData.items[0].type)
  133. ? e.clipboardData.items
  134. : null;
  135. }
  136. function getDropImage(e) {
  137. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  138. }
  139. return {
  140. outputRule: function(root) {
  141. utils.each(root.getNodesByTagName("img"), function(n) {
  142. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  143. n.parentNode.removeChild(n);
  144. }
  145. });
  146. utils.each(root.getNodesByTagName("p"), function(n) {
  147. if (/\bloadpara\b/.test(n.getAttr("class"))) {
  148. n.parentNode.removeChild(n);
  149. }
  150. });
  151. },
  152. bindEvents: {
  153. defaultOptions: {
  154. //默认间隔时间
  155. enableDragUpload: true,
  156. enablePasteUpload: true
  157. },
  158. //插入粘贴板的图片,拖放插入图片
  159. ready: function(e) {
  160. var me = this;
  161. if (window.FormData && window.FileReader) {
  162. var handler = function(e) {
  163. var hasImg = false,
  164. items;
  165. //获取粘贴板文件列表或者拖放文件列表
  166. items = e.type == "paste" ? getPasteImage(e) : getDropImage(e);
  167. if (items) {
  168. var len = items.length,
  169. file;
  170. while (len--) {
  171. file = items[len];
  172. if (file.getAsFile) file = file.getAsFile();
  173. if (file && file.size > 0) {
  174. sendAndInsertFile(file, me);
  175. hasImg = true;
  176. }
  177. }
  178. hasImg && e.preventDefault();
  179. }
  180. };
  181. if (me.getOpt("enablePasteUpload") !== false) {
  182. domUtils.on(me.body, "paste ", handler);
  183. }
  184. if (me.getOpt("enableDragUpload") !== false) {
  185. domUtils.on(me.body, "drop", handler);
  186. //取消拖放图片时出现的文字光标位置提示
  187. domUtils.on(me.body, "dragover", function(e) {
  188. if (e.dataTransfer.types[0] == "Files") {
  189. e.preventDefault();
  190. }
  191. });
  192. } else {
  193. if (browser.gecko) {
  194. domUtils.on(me.body, "drop", function(e) {
  195. if (getDropImage(e)) {
  196. e.preventDefault();
  197. }
  198. });
  199. }
  200. }
  201. //设置loading的样式
  202. utils.cssRule(
  203. "loading",
  204. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  205. this.options.themePath +
  206. this.options.theme +
  207. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n" +
  208. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  209. this.options.themePath +
  210. this.options.theme +
  211. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  212. "}",
  213. this.document
  214. );
  215. }
  216. }
  217. }
  218. };
  219. });