image.js 9.8 KB

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