image.js 15 KB

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