image.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. tabs.add({
  167. title : lang.remoteImage,
  168. panel : K('.tab1', div)
  169. });
  170. tabs.add({
  171. title : lang.localImage,
  172. panel : K('.tab2', div)
  173. });
  174. tabs.select(tabIndex);
  175. } else if (showRemote) {
  176. K('.tab1', div).show();
  177. } else if (showLocal) {
  178. K('.tab2', div).show();
  179. }
  180. var uploadbutton = K.uploadbutton({
  181. button : K('.ke-upload-button', div)[0],
  182. fieldName : filePostName,
  183. form : K('.ke-form', div),
  184. target : target,
  185. width: 60,
  186. afterUpload : function(data) {
  187. dialog.hideLoading();
  188. if (data.error === 0) {
  189. var url = data.url;
  190. if (formatUploadUrl) {
  191. url = K.formatUrl(url, 'absolute');
  192. }
  193. if (self.afterUpload) {
  194. self.afterUpload.call(self, url, data, name);
  195. }
  196. if (!fillDescAfterUploadImage) {
  197. clickFn.call(self, url, data.title, data.width, data.height, data.border, data.align);
  198. } else {
  199. K(".ke-dialog-row #remoteUrl", div).val(url);
  200. K(".ke-tabs-li", div)[0].click();
  201. K(".ke-refresh-btn", div).click();
  202. }
  203. } else {
  204. alert(data.message);
  205. }
  206. },
  207. afterError : function(html) {
  208. dialog.hideLoading();
  209. self.errorDialog(html);
  210. }
  211. });
  212. uploadbutton.fileBox.change(function(e) {
  213. localUrlBox.val(uploadbutton.fileBox.val());
  214. });
  215. if (allowFileManager) {
  216. viewServerBtn.click(function(e) {
  217. self.loadPlugin('filemanager', function() {
  218. self.plugin.filemanagerDialog({
  219. viewType : 'VIEW',
  220. dirName : 'image',
  221. clickFn : function(url, title) {
  222. if (self.dialogs.length > 1) {
  223. K('[name="url"]', div).val(url);
  224. if (self.afterSelectFile) {
  225. self.afterSelectFile.call(self, url);
  226. }
  227. self.hideDialog();
  228. }
  229. }
  230. });
  231. });
  232. });
  233. } else {
  234. viewServerBtn.hide();
  235. }
  236. var originalWidth = 0, originalHeight = 0;
  237. function setSize(width, height) {
  238. widthBox.val(width);
  239. heightBox.val(height);
  240. originalWidth = width;
  241. originalHeight = height;
  242. }
  243. refreshBtn.click(function(e) {
  244. var tempImg = K('<img src="' + urlBox.val() + '" />', document).css({
  245. position : 'absolute',
  246. visibility : 'hidden',
  247. top : 0,
  248. left : '-1000px'
  249. });
  250. tempImg.bind('load', function() {
  251. setSize(tempImg.width(), tempImg.height());
  252. tempImg.remove();
  253. });
  254. K(document.body).append(tempImg);
  255. });
  256. widthBox.change(function(e) {
  257. if (originalWidth > 0) {
  258. heightBox.val(Math.round(originalHeight / originalWidth * parseInt(this.value, 10)));
  259. }
  260. });
  261. heightBox.change(function(e) {
  262. if (originalHeight > 0) {
  263. widthBox.val(Math.round(originalWidth / originalHeight * parseInt(this.value, 10)));
  264. }
  265. });
  266. urlBox.val(options.imageUrl);
  267. setSize(options.imageWidth, options.imageHeight);
  268. titleBox.val(options.imageTitle);
  269. alignBox.each(function() {
  270. if (this.value === options.imageAlign) {
  271. this.checked = true;
  272. return false;
  273. }
  274. });
  275. if (showRemote && tabIndex === 0) {
  276. urlBox[0].focus();
  277. urlBox[0].select();
  278. }
  279. return dialog;
  280. };
  281. self.plugin.image = {
  282. edit : function() {
  283. var img = self.plugin.getSelectedImage();
  284. self.plugin.imageDialog({
  285. imageUrl : img ? img.attr('data-ke-src') : 'http://',
  286. imageWidth : img ? img.width() : '',
  287. imageHeight : img ? img.height() : '',
  288. imageTitle : img ? img.attr('title') : '',
  289. imageAlign : img ? img.attr('align') : '',
  290. showRemote : allowImageRemote,
  291. showLocal : allowImageUpload,
  292. tabIndex: img ? 0 : imageTabIndex,
  293. clickFn : function(url, title, width, height, border, align) {
  294. if (img) {
  295. img.attr('src', url);
  296. img.attr('data-ke-src', url);
  297. img.attr('width', width);
  298. img.attr('height', height);
  299. img.attr('title', title);
  300. img.attr('align', align);
  301. img.attr('alt', title);
  302. } else {
  303. self.exec('insertimage', url, title, width, height, border, align);
  304. }
  305. // Bugfix: [Firefox] 上传图片后,总是出现正在加载的样式,需要延迟执行hideDialog
  306. setTimeout(function() {
  307. self.hideDialog().focus();
  308. }, 0);
  309. }
  310. });
  311. },
  312. 'delete' : function() {
  313. var target = self.plugin.getSelectedImage();
  314. if (target.parent().name == 'a') {
  315. target = target.parent();
  316. }
  317. target.remove();
  318. // [IE] 删除图片后立即点击图片按钮出错
  319. self.addBookmark();
  320. }
  321. };
  322. self.clickToolbar(name, self.plugin.image.edit);
  323. });