image.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. (function () {
  2. var utils = UM.utils,
  3. browser = UM.browser,
  4. Base = {
  5. checkURL: function (url) {
  6. if (!url) return false;
  7. url = utils.trim(url);
  8. if (url.length <= 0) {
  9. return false;
  10. }
  11. if (url.search(/http:\/\/|https:\/\//) !== 0) {
  12. url += 'http://';
  13. }
  14. url = url.replace(/\?[\s\S]*$/, "");
  15. if (!/(.gif|.jpg|.jpeg|.png)$/i.test(url)) {
  16. return false;
  17. }
  18. return url;
  19. },
  20. getAllPic: function (sel, $w, editor) {
  21. var me = this,
  22. arr = [],
  23. $imgs = $(sel, $w);
  24. $.each($imgs, function (index, node) {
  25. $(node).removeAttr("width").removeAttr("height");
  26. // if (node.width > editor.options.initialFrameWidth) {
  27. // me.scale(node, editor.options.initialFrameWidth -
  28. // parseInt($(editor.body).css("padding-left")) -
  29. // parseInt($(editor.body).css("padding-right")));
  30. // }
  31. return arr.push({
  32. _src: node.src,
  33. src: node.src
  34. });
  35. });
  36. return arr;
  37. },
  38. scale: function (img, max, oWidth, oHeight) {
  39. var width = 0, height = 0, percent, ow = img.width || oWidth, oh = img.height || oHeight;
  40. if (ow > max || oh > max) {
  41. if (ow >= oh) {
  42. if (width = ow - max) {
  43. percent = (width / ow).toFixed(2);
  44. img.height = oh - oh * percent;
  45. img.width = max;
  46. }
  47. } else {
  48. if (height = oh - max) {
  49. percent = (height / oh).toFixed(2);
  50. img.width = ow - ow * percent;
  51. img.height = max;
  52. }
  53. }
  54. }
  55. return this;
  56. },
  57. close: function ($img) {
  58. $img.css({
  59. top: ($img.parent().height() - $img.height()) / 2,
  60. left: ($img.parent().width() - $img.width()) / 2
  61. }).prev().on("click", function () {
  62. if ($(this).parent().remove().hasClass("edui-image-upload-item")) {
  63. //显示图片计数-1
  64. Upload.showCount--;
  65. Upload.updateView();
  66. }
  67. });
  68. return this;
  69. },
  70. createImgBase64: function (img, file, $w) {
  71. if (browser.webkit) {
  72. //Chrome8+
  73. img.src = window.webkitURL.createObjectURL(file);
  74. } else if (browser.gecko) {
  75. //FF4+
  76. img.src = window.URL.createObjectURL(file);
  77. } else {
  78. //实例化file reader对象
  79. var reader = new FileReader();
  80. reader.onload = function (e) {
  81. img.src = this.result;
  82. $w.append(img);
  83. };
  84. reader.readAsDataURL(file);
  85. }
  86. },
  87. callback: function (editor, $w, url, state) {
  88. if (state == "SUCCESS") {
  89. //显示图片计数+1
  90. Upload.showCount++;
  91. var $img = $("<img src='" + editor.options.imagePath + url + "' class='edui-image-pic' />"),
  92. $item = $("<div class='edui-image-item edui-image-upload-item'><div class='edui-image-close'></div></div>").append($img);
  93. if ($(".edui-image-upload2", $w).length < 1) {
  94. $(".edui-image-content", $w).append($item);
  95. Upload.render(".edui-image-content", 2)
  96. .config(".edui-image-upload2");
  97. } else {
  98. $(".edui-image-upload2", $w).before($item).show();
  99. }
  100. $img.on("load", function () {
  101. Base.scale(this, 120);
  102. Base.close($(this));
  103. $(".edui-image-content", $w).focus();
  104. });
  105. } else {
  106. currentDialog.showTip(state);
  107. window.setTimeout(function () {
  108. currentDialog.hideTip();
  109. }, 3000);
  110. }
  111. Upload.toggleMask();
  112. }
  113. };
  114. /*
  115. * 本地上传
  116. * */
  117. var Upload = {
  118. showCount: 0,
  119. uploadTpl: '<div class="edui-image-upload%%">' +
  120. '<span class="edui-image-icon"></span>' +
  121. '<form class="edui-image-form" method="post" enctype="multipart/form-data" target="up">' +
  122. '<input style=\"filter: alpha(opacity=0);\" class="edui-image-file" type="file" hidefocus name="upfile" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp,image/webp"/>' +
  123. '</form>' +
  124. '</div>',
  125. init: function (editor, $w) {
  126. var me = this;
  127. me.editor = editor;
  128. me.dialog = $w;
  129. me.render(".edui-image-local", 1);
  130. me.config(".edui-image-upload1");
  131. me.submit();
  132. me.drag();
  133. $(".edui-image-upload1").hover(function () {
  134. $(".edui-image-icon", this).toggleClass("hover");
  135. });
  136. if (!(UM.browser.ie && UM.browser.version <= 9)) {
  137. $(".edui-image-dragTip", me.dialog).css("display", "block");
  138. }
  139. return me;
  140. },
  141. render: function (sel, t) {
  142. var me = this;
  143. $(sel, me.dialog).append($(me.uploadTpl.replace(/%%/g, t)));
  144. return me;
  145. },
  146. config: function (sel) {
  147. var me = this,
  148. url = me.editor.options.imageUrl;
  149. url = url + (url.indexOf("?") == -1 ? "?" : "&") + "editorid=" + me.editor.id;//初始form提交地址;
  150. $("form", $(sel, me.dialog)).attr("action", url);
  151. return me;
  152. },
  153. uploadComplete: function (r) {
  154. var me = this;
  155. try {
  156. var json = eval('(' + r + ')');
  157. Base.callback(me.editor, me.dialog, json.url, json.state);
  158. } catch (e) {
  159. var lang = me.editor.getLang('image');
  160. Base.callback(me.editor, me.dialog, '', (lang && lang.uploadError) || 'Error!');
  161. }
  162. },
  163. submit: function (callback) {
  164. var me = this,
  165. input = $('<input style="filter: alpha(opacity=0);" class="edui-image-file" type="file" hidefocus="" name="upfile" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp,image/webp">'),
  166. input = input[0];
  167. $(me.dialog).delegate(".edui-image-file", "change", function (e) {
  168. $(this).trigger("edui.file.change", [Upload, me, input, callback]);
  169. return;
  170. if (!this.parentNode) {
  171. return;
  172. }
  173. $('<iframe name="up" style="display: none"></iframe>').insertBefore(me.dialog).on('load', function () {
  174. var r = this.contentWindow.document.body.innerHTML;
  175. if (r == '') return;
  176. me.uploadComplete(r);
  177. $(this).unbind('load');
  178. $(this).remove();
  179. });
  180. $(this).parent()[0].submit();
  181. Upload.updateInput(input);
  182. me.toggleMask("Loading....");
  183. callback && callback();
  184. });
  185. return me;
  186. },
  187. //更新input
  188. updateInput: function (inputField) {
  189. $(".edui-image-file", this.dialog).each(function (index, ele) {
  190. ele.parentNode.replaceChild(inputField.cloneNode(true), ele);
  191. });
  192. },
  193. //更新上传框
  194. updateView: function () {
  195. if (Upload.showCount !== 0) {
  196. return;
  197. }
  198. $(".edui-image-upload2", this.dialog).hide();
  199. $(".edui-image-dragTip", this.dialog).show();
  200. $(".edui-image-upload1", this.dialog).show();
  201. },
  202. drag: function () {
  203. var me = this;
  204. //做拽上传的支持
  205. if (!UM.browser.ie9below) {
  206. me.dialog.find('.edui-image-content').on('drop', function (e) {
  207. //获取文件列表
  208. var fileList = e.originalEvent.dataTransfer.files;
  209. var img = document.createElement('img');
  210. var hasImg = false;
  211. $.each(fileList, function (i, f) {
  212. if (/^image/.test(f.type)) {
  213. var uploadCallback = me.editor.getOpt('imageUploadCallback');
  214. if (uploadCallback && typeof uploadCallback === 'function') {
  215. uploadCallback.call(me, f, function (url, data) {
  216. me.uploadComplete(JSON.stringify({url: url, state: "SUCCESS"}));
  217. });
  218. } else {
  219. //创建图片的base64
  220. Base.createImgBase64(img, f, me.dialog);
  221. var xhr = new XMLHttpRequest();
  222. xhr.open("post", me.editor.getOpt('imageUrl') + "?type=ajax", true);
  223. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  224. //模拟数据
  225. var fd = new FormData();
  226. fd.append(me.editor.getOpt('imageFieldName'), f);
  227. xhr.send(fd);
  228. xhr.addEventListener('load', function (e) {
  229. var r = e.target.response, json;
  230. me.uploadComplete(r);
  231. if (i == fileList.length - 1) {
  232. $(img).remove()
  233. }
  234. });
  235. }
  236. hasImg = true;
  237. }
  238. });
  239. if (hasImg) {
  240. e.preventDefault();
  241. me.toggleMask("Loading....");
  242. }
  243. }).on('dragover', function (e) {
  244. e.preventDefault();
  245. });
  246. }
  247. },
  248. toggleMask: function (html) {
  249. var me = this;
  250. var $mask = $(".edui-image-mask", me.dialog);
  251. if (html) {
  252. if (!(UM.browser.ie && UM.browser.version <= 9)) {
  253. $(".edui-image-dragTip", me.dialog).css("display", "none");
  254. }
  255. $(".edui-image-upload1", me.dialog).css("display", "none");
  256. $mask.addClass("edui-active").html(html);
  257. } else {
  258. $mask.removeClass("edui-active").html();
  259. if (Upload.showCount > 0) {
  260. return me;
  261. }
  262. if (!(UM.browser.ie && UM.browser.version <= 9)) {
  263. $(".edui-image-dragTip", me.dialog).css("display", "block");
  264. }
  265. $(".edui-image-upload1", me.dialog).css("display", "block");
  266. }
  267. return me;
  268. }
  269. };
  270. /*
  271. * 网络图片
  272. * */
  273. var NetWork = {
  274. init: function (editor, $w) {
  275. var me = this;
  276. me.editor = editor;
  277. me.dialog = $w;
  278. me.initEvt();
  279. },
  280. initEvt: function () {
  281. var me = this,
  282. url,
  283. $ele = $(".edui-image-searchTxt", me.dialog);
  284. $(".edui-image-searchAdd", me.dialog).on("click", function () {
  285. url = Base.checkURL($ele.val());
  286. if (url) {
  287. $("<img src='" + url + "' class='edui-image-pic' />").on("load", function () {
  288. var $item = $("<div class='edui-image-item'><div class='edui-image-close'></div></div>").append(this);
  289. $(".edui-image-searchRes", me.dialog).append($item);
  290. Base.scale(this, 120);
  291. $item.width($(this).width());
  292. Base.close($(this));
  293. $ele.val("");
  294. });
  295. }
  296. })
  297. .hover(function () {
  298. $(this).toggleClass("hover");
  299. });
  300. }
  301. };
  302. /*
  303. * 选择图片
  304. * */
  305. var Select = {
  306. init: function (editor, $w) {
  307. var me = this;
  308. me.editor = editor;
  309. me.dialog = $w;
  310. me.initEvt();
  311. },
  312. initEvt: function () {
  313. var me = this;
  314. $(".edui-image-select", me.dialog).on("click", function () {
  315. var selectUrl = typeof Config !== 'undefined' && Config.modulename === 'index' ? 'user/attachment' : 'general/attachment/select';
  316. parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=image/*", "选择", {
  317. callback: function (data) {
  318. var urlArr = data.url.split(/\,/);
  319. urlArr.forEach(function (item, index) {
  320. var url = Fast.api.cdnurl(item);
  321. $("<img src='" + url + "' class='edui-image-pic' />").on("load", function () {
  322. var $item = $("<div class='edui-image-item'><div class='edui-image-close'></div></div>").append(this);
  323. $(".edui-image-selectRes", me.dialog).append($item);
  324. Base.scale(this, 120);
  325. $item.width($(this).width());
  326. Base.close($(this));
  327. });
  328. });
  329. }
  330. });
  331. return false;
  332. });
  333. }
  334. };
  335. var $tab = null,
  336. currentDialog = null;
  337. UM.registerWidget('image', {
  338. tpl: "<link rel=\"stylesheet\" type=\"text/css\" href=\"<%=image_url%>image.css\">" +
  339. "<div class=\"edui-image-wrapper\">" +
  340. "<ul class=\"edui-tab-nav\">" +
  341. "<li class=\"edui-tab-item edui-active\"><a data-context=\".edui-image-local\" class=\"edui-tab-text\"><%=lang_tab_local%></a></li>" +
  342. "<li class=\"edui-tab-item\"><a data-context=\".edui-image-JimgSearch\" class=\"edui-tab-text\"><%=lang_tab_imgSearch%></a></li>" +
  343. "<li class=\"edui-tab-item\"><a data-context=\".edui-image-JimgSelect\" class=\"edui-tab-text\"><%=lang_tab_imgSelect%></a></li>" +
  344. "</ul>" +
  345. "<div class=\"edui-tab-content\">" +
  346. "<div class=\"edui-image-local edui-tab-pane edui-active\">" +
  347. "<div class=\"edui-image-content\"></div>" +
  348. "<div class=\"edui-image-mask\"></div>" +
  349. "<div class=\"edui-image-dragTip\"><%=lang_input_dragTip%></div>" +
  350. "</div>" +
  351. "<div class=\"edui-image-JimgSearch edui-tab-pane\">" +
  352. "<div class=\"edui-image-searchBar\">" +
  353. "<table><tr><td><input class=\"edui-image-searchTxt\" type=\"text\"></td>" +
  354. "<td><div class=\"edui-image-searchAdd\"><%=lang_btn_add%></div></td></tr></table>" +
  355. "</div>" +
  356. "<div class=\"edui-image-searchRes\"></div>" +
  357. "</div>" +
  358. "<div class=\"edui-image-JimgSelect edui-tab-pane\">" +
  359. "<div class=\"edui-image-selectBar\"><div class=\"edui-image-select\"><span class=\"edui-image-icon\"></span></div></div>" +
  360. "<div class=\"edui-image-selectRes\"></div>" +
  361. "<div class=\"edui-image-mask\"></div>" +
  362. "</div>" +
  363. "</div>" +
  364. "</div>",
  365. initContent: function (editor, $dialog) {
  366. var lang = editor.getLang('image')["static"],
  367. opt = $.extend({}, lang, {
  368. image_url: UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/image/'
  369. });
  370. Upload.showCount = 0;
  371. if (lang) {
  372. var html = $.parseTmpl(this.tpl, opt);
  373. }
  374. currentDialog = $dialog.edui();
  375. this.root().html(html);
  376. },
  377. initEvent: function (editor, $w) {
  378. $tab = $.eduitab({selector: ".edui-image-wrapper"})
  379. .edui().on("beforeshow", function (e) {
  380. e.stopPropagation();
  381. });
  382. Upload.init(editor, $w);
  383. NetWork.init(editor, $w);
  384. Select.init(editor, $w);
  385. },
  386. buttons: {
  387. 'ok': {
  388. exec: function (editor, $w) {
  389. var sel = "",
  390. index = $tab.activate();
  391. if (index == 0) {
  392. sel = ".edui-image-content .edui-image-pic";
  393. } else if (index == 1) {
  394. sel = ".edui-image-searchRes .edui-image-pic";
  395. } else if (index == 2) {
  396. sel = ".edui-image-selectRes .edui-image-pic";
  397. }
  398. var list = Base.getAllPic(sel, $w, editor);
  399. if (index != -1) {
  400. editor.execCommand('insertimage', list);
  401. }
  402. }
  403. },
  404. 'cancel': {}
  405. },
  406. width: 700,
  407. height: 408
  408. }, function (editor, $w, url, state) {
  409. Base.callback(editor, $w, url, state)
  410. })
  411. })();