image.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. * Copyright (C) 2006-2011 kindsoft.net
  4. *
  5. * @author Roddy <luolonghao@gmail.com>
  6. * @site http://www.kindsoft.net/
  7. * @licence http://www.kindsoft.net/license.php
  8. *******************************************************************************/
  9. KindEditor.plugin('image', function(K) {
  10. var self = this, name = 'image',
  11. allowImageUpload = K.undef(self.allowImageUpload, true),
  12. allowImageRemote = K.undef(self.allowImageRemote, true),
  13. formatUploadUrl = K.undef(self.formatUploadUrl, true),
  14. allowFileManager = K.undef(self.allowFileManager, false),
  15. uploadJson = K.undef(self.uploadJson, self.basePath + 'php/upload_json.php'),
  16. imageTabIndex = K.undef(self.imageTabIndex, 0),
  17. imgPath = self.pluginsPath + 'image/images/',
  18. extraParams = K.undef(self.extraFileUploadParams, {}),
  19. filePostName = K.undef(self.filePostName, 'imgFile'),
  20. fillDescAfterUploadImage = K.undef(self.fillDescAfterUploadImage, false),
  21. lang = self.lang(name + '.');
  22. self.plugin.imageDialog = function(options) {
  23. var imageUrl = options.imageUrl,
  24. imageWidth = K.undef(options.imageWidth, ''),
  25. imageHeight = K.undef(options.imageHeight, ''),
  26. imageTitle = K.undef(options.imageTitle, ''),
  27. imageAlign = K.undef(options.imageAlign, ''),
  28. showRemote = K.undef(options.showRemote, true),
  29. showLocal = K.undef(options.showLocal, true),
  30. tabIndex = K.undef(options.tabIndex, 0),
  31. clickFn = options.clickFn;
  32. var target = 'kindeditor_upload_iframe_' + new Date().getTime();
  33. var hiddenElements = [];
  34. for(var k in extraParams){
  35. hiddenElements.push('<input type="hidden" name="' + k + '" value="' + extraParams[k] + '" />');
  36. }
  37. var html = [
  38. '<div class="ke-dialog-content-inner">',
  39. //tabs
  40. '<div class="tabs"></div>',
  41. //remote image - start
  42. '<div class="tab1" style="display:none;">',
  43. //url
  44. '<div class="ke-dialog-row ke-clearfix">',
  45. '<label for="remoteUrl" class="row-left">' + lang.remoteUrl + ':</label>',
  46. '<div class="row-right">',
  47. '<input type="text" id="remoteUrl" class="ke-input-text" name="url" value="" style="width:250px;" /> &nbsp;',
  48. '<span class="ke-button-common ke-button-outer">',
  49. '<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
  50. '</span>',
  51. '</div>',
  52. '</div>',
  53. //size
  54. '<div class="ke-dialog-row ke-clearfix ">',
  55. '<label for="remoteWidth" class="row-left">' + lang.size + ':</label>',
  56. '<div class="row-right">'+lang.width + ' <input type="text" id="remoteWidth" class="ke-input-text ke-input-number" name="width" value="" maxlength="4" /> ',
  57. lang.height + ' <input type="text" class="ke-input-text ke-input-number" name="height" value="" maxlength="4" /> ',
  58. '<img class="ke-refresh-btn" src="' + imgPath + 'refresh.png" width="16" height="16" alt="" style="cursor:pointer;" title="' + lang.resetSize + '" />',
  59. '</div>',
  60. '</div>',
  61. //align
  62. '<div class="ke-dialog-row ke-clearfix">',
  63. '<label class="row-left">' + lang.align + ':</label>',
  64. '<div class="row-right">',
  65. '<span><input type="radio" name="align" class="ke-inline-block" value="" checked="checked" /> ' +
  66. '<img name="defaultImg" src="' + imgPath + 'align_top.gif" width="23" height="25" alt="" /></span>',
  67. ' <span><input type="radio" name="align" class="ke-inline-block" value="left" /> ' +
  68. '<img name="leftImg" src="' + imgPath + 'align_left.gif" width="23" height="25" alt="" /></span>',
  69. ' <span><input type="radio" name="align" class="ke-inline-block" value="right" /> ' +
  70. '<img name="rightImg" src="' + imgPath + 'align_right.gif" width="23" height="25" alt="" /></span>',
  71. '</div>',
  72. '</div>',
  73. //title
  74. '<div class="ke-dialog-row ke-clearfix">',
  75. '<label for="remoteTitle" class="row-left">' + lang.imgTitle + ':</label>',
  76. '<div class="row-right">',
  77. '<input type="text" id="remoteTitle" class="ke-input-text" name="title" value="" style="width:250px;" />',
  78. '</div>',
  79. '</div>',
  80. '</div>',
  81. //remote image - end
  82. //local upload - start
  83. '<div class="tab2" style="display:none;">',
  84. '<iframe name="' + target + '" style="display:none;"></iframe>',
  85. '<form class="ke-upload-area ke-form" method="post" enctype="multipart/form-data" target="' + target + '" action="' + K.addParam(uploadJson, 'fileType=image') + '">',
  86. //file
  87. '<div class="ke-dialog-row ke-clearfix">',
  88. hiddenElements.join(''),
  89. '<label class="row-left">' + lang.localUrl + ':</label>',
  90. '<div class="row-right">',
  91. '<input type="text" name="localUrl" class="ke-input-text" tabindex="-1" style="width:250px;" readonly="true" /> &nbsp;',
  92. '<input type="button" class="ke-upload-button" value="' + lang.upload + '" />',
  93. '</div>',
  94. '</div>',
  95. '</form>',
  96. '</div>',
  97. //local upload - end
  98. '</div>'
  99. ].join('');
  100. var dialogWidth = showLocal || allowFileManager ? 450 : 400,
  101. dialogHeight = showLocal && showRemote ? 310 : 260;
  102. var dialog = self.createDialog({
  103. name : name,
  104. width : dialogWidth,
  105. height : dialogHeight,
  106. title : self.lang(name),
  107. body : html,
  108. yesBtn : {
  109. name : self.lang('yes'),
  110. click : function(e) {
  111. // Bugfix: http://code.google.com/p/kindeditor/issues/detail?id=319
  112. if (dialog.isLoading) {
  113. return;
  114. }
  115. // insert local image
  116. if (showLocal && showRemote && tabs && tabs.selectedIndex === 1 || !showRemote) {
  117. if (uploadbutton.fileBox.val() == '') {
  118. K.options.errorMsgHandler(self.lang('pleaseSelectFile'), "error");
  119. return;
  120. }
  121. dialog.showLoading(self.lang('uploadLoading'));
  122. uploadbutton.submit();
  123. localUrlBox.val('');
  124. return;
  125. }
  126. // insert remote image
  127. var url = K.trim(urlBox.val()),
  128. width = widthBox.val(),
  129. height = heightBox.val(),
  130. title = titleBox.val(),
  131. align = '';
  132. alignBox.each(function() {
  133. if (this.checked) {
  134. align = this.value;
  135. return false;
  136. }
  137. });
  138. if (url == 'http://' || K.invalidUrl(url)) {
  139. K.options.errorMsgHandler(self.lang('invalidUrl'), "error");
  140. urlBox[0].focus();
  141. return;
  142. }
  143. if (!/^\d*[%]?$/.test(width)) {
  144. K.options.errorMsgHandler(self.lang('invalidWidth'), "error");
  145. widthBox[0].focus();
  146. return;
  147. }
  148. if (!/^\d*[%]?$/.test(height)) {
  149. K.options.errorMsgHandler(self.lang('invalidHeight'), "error");
  150. heightBox[0].focus();
  151. return;
  152. }
  153. clickFn.call(self, url, title, width, height, 0, align);
  154. }
  155. },
  156. beforeRemove : function() {
  157. viewServerBtn.unbind();
  158. widthBox.unbind();
  159. heightBox.unbind();
  160. refreshBtn.unbind();
  161. }
  162. }),
  163. div = dialog.div;
  164. var urlBox = K('[name="url"]', div),
  165. localUrlBox = K('[name="localUrl"]', div),
  166. viewServerBtn = K('[name="viewServer"]', div),
  167. widthBox = K('.tab1 [name="width"]', div),
  168. heightBox = K('.tab1 [name="height"]', div),
  169. refreshBtn = K('.ke-refresh-btn', div),
  170. titleBox = K('.tab1 [name="title"]', div),
  171. alignBox = K('.tab1 [name="align"]', div);
  172. var tabs;
  173. if (showRemote && showLocal) {
  174. tabs = K.tabs({
  175. src : K('.tabs', div),
  176. afterSelect : function(i) {}
  177. });
  178. tabs.add({
  179. title : lang.remoteImage,
  180. panel : K('.tab1', div)
  181. });
  182. tabs.add({
  183. title : lang.localImage,
  184. panel : K('.tab2', div)
  185. });
  186. tabs.select(tabIndex);
  187. } else if (showRemote) {
  188. K('.tab1', div).show();
  189. } else if (showLocal) {
  190. K('.tab2', div).show();
  191. }
  192. var uploadbutton = K.uploadbutton({
  193. button : K('.ke-upload-button', div)[0],
  194. fieldName : filePostName,
  195. form : K('.ke-form', div),
  196. target : target,
  197. width: 60,
  198. afterUpload : function(data) {
  199. dialog.hideLoading();
  200. if (data.code == "000") {
  201. K.options.errorMsgHandler(self.lang('uploadSuccess'), "ok");
  202. var url = data.data.url;
  203. if (formatUploadUrl) {
  204. url = K.formatUrl(url, 'absolute');
  205. }
  206. if (!fillDescAfterUploadImage) {
  207. clickFn.call(self, url, data.title, data.width, data.height, data.border, data.align);
  208. } else {
  209. K(".ke-dialog-row ke-clearfix #remoteUrl", div).val(url);
  210. K(".ke-tabs-li", div)[0].click();
  211. K(".ke-refresh-btn", div).click();
  212. }
  213. } else {
  214. K.options.errorMsgHandler(data.message, "error");
  215. }
  216. },
  217. afterError : function(html) {
  218. dialog.hideLoading();
  219. self.errorDialog(html);
  220. }
  221. });
  222. uploadbutton.fileBox.change(function(e) {
  223. localUrlBox.val(uploadbutton.fileBox.val());
  224. });
  225. if (allowFileManager) {
  226. viewServerBtn.click(function(e) {
  227. self.loadPlugin('filemanager', function() {
  228. self.plugin.filemanagerDialog({
  229. dirName : 'image',
  230. clickFn : function(url) {
  231. K('[name="url"]', div).val(url);
  232. if (self.afterSelectFile) {
  233. self.afterSelectFile.call(self, url);
  234. }
  235. }
  236. });
  237. });
  238. });
  239. } else {
  240. viewServerBtn.hide();
  241. }
  242. var originalWidth = 0, originalHeight = 0;
  243. function setSize(width, height) {
  244. widthBox.val(width);
  245. heightBox.val(height);
  246. originalWidth = width;
  247. originalHeight = height;
  248. }
  249. refreshBtn.click(function(e) {
  250. var tempImg = K('<img src="' + urlBox.val() + '" />', document).css({
  251. position : 'absolute',
  252. visibility : 'hidden',
  253. top : 0,
  254. left : '-1000px'
  255. });
  256. tempImg.bind('load', function() {
  257. setSize(tempImg.width(), tempImg.height());
  258. tempImg.remove();
  259. });
  260. K(document.body).append(tempImg);
  261. });
  262. widthBox.change(function(e) {
  263. if (originalWidth > 0 && this.value) {
  264. heightBox.val(Math.round(originalHeight / originalWidth * parseInt(this.value, 10)));
  265. }
  266. });
  267. heightBox.change(function(e) {
  268. if (originalHeight > 0 && this.value) {
  269. widthBox.val(Math.round(originalWidth / originalHeight * parseInt(this.value, 10)));
  270. }
  271. });
  272. urlBox.val(options.imageUrl);
  273. setSize(options.imageWidth, options.imageHeight);
  274. titleBox.val(options.imageTitle);
  275. alignBox.each(function() {
  276. if (this.value === options.imageAlign) {
  277. this.checked = true;
  278. return false;
  279. }
  280. });
  281. if (showRemote && tabIndex === 0) {
  282. urlBox[0].focus();
  283. urlBox[0].select();
  284. }
  285. return dialog;
  286. };
  287. self.plugin.image = {
  288. edit : function() {
  289. var img = self.plugin.getSelectedImage();
  290. self.plugin.imageDialog({
  291. imageUrl : img ? img.attr('data-ke-src') : 'http://',
  292. imageWidth: img ? img.attr('width') : '',
  293. imageHeight: img ? img.attr('height') : '',
  294. imageTitle : img ? img.attr('title') : '',
  295. imageAlign : img ? img.attr('align') : '',
  296. showRemote : allowImageRemote,
  297. showLocal : allowImageUpload,
  298. tabIndex: img ? 0 : imageTabIndex,
  299. clickFn : function(url, title, width, height, border, align) {
  300. if (img) {
  301. img.attr('src', url);
  302. img.attr('data-ke-src', url);
  303. img.attr('width', width);
  304. img.attr('height', height);
  305. img.attr('title', title);
  306. img.attr('align', align);
  307. img.attr('alt', title);
  308. } else {
  309. self.exec('insertimage', url, title, width, height, border, align);
  310. }
  311. // Bugfix: [Firefox] 上传图片后,总是出现正在加载的样式,需要延迟执行hideDialog
  312. setTimeout(function() {
  313. self.hideDialog().focus();
  314. }, 0);
  315. }
  316. });
  317. },
  318. 'delete' : function() {
  319. var target = self.plugin.getSelectedImage();
  320. if (target.parent().name == 'a') {
  321. target = target.parent();
  322. }
  323. target.remove();
  324. // [IE] 删除图片后立即点击图片按钮出错
  325. self.addBookmark();
  326. }
  327. };
  328. self.clickToolbar(name, self.plugin.image.edit);
  329. });