image.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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"/>' +
  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">'),
  166. input = input[0];
  167. $(me.dialog).delegate( ".edui-image-file", "change", function ( e ) {
  168. if ( !this.parentNode ) {
  169. return;
  170. }
  171. $('<iframe name="up" style="display: none"></iframe>').insertBefore(me.dialog).on('load', function(){
  172. var r = this.contentWindow.document.body.innerHTML;
  173. if(r == '')return;
  174. me.uploadComplete(r);
  175. $(this).unbind('load');
  176. $(this).remove();
  177. });
  178. $(this).parent()[0].submit();
  179. Upload.updateInput( input );
  180. me.toggleMask("Loading....");
  181. callback && callback();
  182. });
  183. return me;
  184. },
  185. //更新input
  186. updateInput: function ( inputField ) {
  187. $( ".edui-image-file", this.dialog ).each( function ( index, ele ) {
  188. ele.parentNode.replaceChild( inputField.cloneNode( true ), ele );
  189. } );
  190. },
  191. //更新上传框
  192. updateView: function () {
  193. if ( Upload.showCount !== 0 ) {
  194. return;
  195. }
  196. $(".edui-image-upload2", this.dialog).hide();
  197. $(".edui-image-dragTip", this.dialog).show();
  198. $(".edui-image-upload1", this.dialog).show();
  199. },
  200. drag: function () {
  201. var me = this;
  202. //做拽上传的支持
  203. if (!UM.browser.ie9below) {
  204. me.dialog.find('.edui-image-content').on('drop',function (e) {
  205. //获取文件列表
  206. var fileList = e.originalEvent.dataTransfer.files;
  207. var img = document.createElement('img');
  208. var hasImg = false;
  209. $.each(fileList, function (i, f) {
  210. if (/^image/.test(f.type)) {
  211. //创建图片的base64
  212. Base.createImgBase64(img, f, me.dialog);
  213. var xhr = new XMLHttpRequest();
  214. xhr.open("post", me.editor.getOpt('imageUrl') + "?type=ajax", true);
  215. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  216. //模拟数据
  217. var fd = new FormData();
  218. fd.append(me.editor.getOpt('imageFieldName'), f);
  219. xhr.send(fd);
  220. xhr.addEventListener('load', function (e) {
  221. var r = e.target.response, json;
  222. me.uploadComplete(r);
  223. if (i == fileList.length - 1) {
  224. $(img).remove()
  225. }
  226. });
  227. hasImg = true;
  228. }
  229. });
  230. if (hasImg) {
  231. e.preventDefault();
  232. me.toggleMask("Loading....");
  233. }
  234. }).on('dragover', function (e) {
  235. e.preventDefault();
  236. });
  237. }
  238. },
  239. toggleMask: function (html) {
  240. var me = this;
  241. var $mask = $(".edui-image-mask", me.dialog);
  242. if (html) {
  243. if (!(UM.browser.ie && UM.browser.version <= 9)) {
  244. $(".edui-image-dragTip", me.dialog).css( "display", "none" );
  245. }
  246. $(".edui-image-upload1", me.dialog).css( "display", "none" );
  247. $mask.addClass("edui-active").html(html);
  248. } else {
  249. $mask.removeClass("edui-active").html();
  250. if ( Upload.showCount > 0 ) {
  251. return me;
  252. }
  253. if (!(UM.browser.ie && UM.browser.version <= 9) ){
  254. $(".edui-image-dragTip", me.dialog).css("display", "block");
  255. }
  256. $(".edui-image-upload1", me.dialog).css( "display", "block" );
  257. }
  258. return me;
  259. }
  260. };
  261. /*
  262. * 网络图片
  263. * */
  264. var NetWork = {
  265. init: function (editor, $w) {
  266. var me = this;
  267. me.editor = editor;
  268. me.dialog = $w;
  269. me.initEvt();
  270. },
  271. initEvt: function () {
  272. var me = this,
  273. url,
  274. $ele = $(".edui-image-searchTxt", me.dialog);
  275. $(".edui-image-searchAdd", me.dialog).on("click", function () {
  276. url = Base.checkURL($ele.val());
  277. if (url) {
  278. $("<img src='" + url + "' class='edui-image-pic' />").on("load", function () {
  279. var $item = $("<div class='edui-image-item'><div class='edui-image-close'></div></div>").append(this);
  280. $(".edui-image-searchRes", me.dialog).append($item);
  281. Base.scale(this, 120);
  282. $item.width($(this).width());
  283. Base.close($(this));
  284. $ele.val("");
  285. });
  286. }
  287. })
  288. .hover(function () {
  289. $(this).toggleClass("hover");
  290. });
  291. }
  292. };
  293. var $tab = null,
  294. currentDialog = null;
  295. UM.registerWidget('image', {
  296. tpl: "<link rel=\"stylesheet\" type=\"text/css\" href=\"<%=image_url%>image.css\">" +
  297. "<div class=\"edui-image-wrapper\">" +
  298. "<ul class=\"edui-tab-nav\">" +
  299. "<li class=\"edui-tab-item edui-active\"><a data-context=\".edui-image-local\" class=\"edui-tab-text\"><%=lang_tab_local%></a></li>" +
  300. "<li class=\"edui-tab-item\"><a data-context=\".edui-image-JimgSearch\" class=\"edui-tab-text\"><%=lang_tab_imgSearch%></a></li>" +
  301. "</ul>" +
  302. "<div class=\"edui-tab-content\">" +
  303. "<div class=\"edui-image-local edui-tab-pane edui-active\">" +
  304. "<div class=\"edui-image-content\"></div>" +
  305. "<div class=\"edui-image-mask\"></div>" +
  306. "<div class=\"edui-image-dragTip\"><%=lang_input_dragTip%></div>" +
  307. "</div>" +
  308. "<div class=\"edui-image-JimgSearch edui-tab-pane\">" +
  309. "<div class=\"edui-image-searchBar\">" +
  310. "<table><tr><td><input class=\"edui-image-searchTxt\" type=\"text\"></td>" +
  311. "<td><div class=\"edui-image-searchAdd\"><%=lang_btn_add%></div></td></tr></table>" +
  312. "</div>" +
  313. "<div class=\"edui-image-searchRes\"></div>" +
  314. "</div>" +
  315. "</div>" +
  316. "</div>",
  317. initContent: function (editor, $dialog) {
  318. var lang = editor.getLang('image')["static"],
  319. opt = $.extend({}, lang, {
  320. image_url: UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/image/'
  321. });
  322. Upload.showCount = 0;
  323. if (lang) {
  324. var html = $.parseTmpl(this.tpl, opt);
  325. }
  326. currentDialog = $dialog.edui();
  327. this.root().html(html);
  328. },
  329. initEvent: function (editor, $w) {
  330. $tab = $.eduitab({selector: ".edui-image-wrapper"})
  331. .edui().on("beforeshow", function (e) {
  332. e.stopPropagation();
  333. });
  334. Upload.init(editor, $w);
  335. NetWork.init(editor, $w);
  336. },
  337. buttons: {
  338. 'ok': {
  339. exec: function (editor, $w) {
  340. var sel = "",
  341. index = $tab.activate();
  342. if (index == 0) {
  343. sel = ".edui-image-content .edui-image-pic";
  344. } else if (index == 1) {
  345. sel = ".edui-image-searchRes .edui-image-pic";
  346. }
  347. var list = Base.getAllPic(sel, $w, editor);
  348. if (index != -1) {
  349. editor.execCommand('insertimage', list);
  350. }
  351. }
  352. },
  353. 'cancel': {}
  354. },
  355. width: 700,
  356. height: 408
  357. }, function (editor, $w, url, state) {
  358. Base.callback(editor, $w, url, state)
  359. })
  360. })();