image.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. allowFileManager = K.undef(self.allowFileManager, false),
  13. uploadJson = K.undef(self.uploadJson, self.basePath + 'php/upload_json.php'),
  14. imgPath = self.basePath + 'plugins/image/images/',
  15. lang = self.lang(name + '.');
  16. self.plugin.imageDialog = function(options) {
  17. var imageUrl = K.undef(options.imageUrl, 'http://'),
  18. imageWidth = K.undef(options.imageWidth, ''),
  19. imageHeight = K.undef(options.imageHeight, ''),
  20. imageTitle = K.undef(options.imageTitle, ''),
  21. imageAlign = K.undef(options.imageAlign, ''),
  22. clickFn = options.clickFn;
  23. var html = [
  24. '<div style="padding:10px 20px;">',
  25. //tabs
  26. '<div class="tabs"></div>',
  27. //url or file
  28. '<div class="ke-dialog-row">',
  29. '<div class="tab1" style="display:none;">',
  30. '<label for="keUrl" style="width:60px;">' + lang.remoteUrl + '</label>',
  31. '<input type="text" id="keUrl" class="ke-input-text" name="url" value="" style="width:200px;" /> &nbsp;',
  32. '<span class="ke-button-common ke-button-outer">',
  33. '<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
  34. '</span>',
  35. '</div>',
  36. '<div class="tab2" style="display:none;">',
  37. '<label style="width:60px;">' + lang.localUrl + '</label>',
  38. '<input type="text" name="localUrl" class="ke-input-text" tabindex="-1" style="width:200px;" readonly="true" /> &nbsp;',
  39. '<input type="button" class="ke-upload-button" value="' + lang.viewServer + '" />',
  40. '</div>',
  41. '</div>',
  42. //size
  43. '<div class="ke-dialog-row">',
  44. '<label for="keWidth" style="width:60px;">' + lang.size + '</label>',
  45. lang.width + ' <input type="text" id="keWidth" class="ke-input-text ke-input-number" name="width" value="" maxlength="4" /> ',
  46. lang.height + ' <input type="text" class="ke-input-text ke-input-number" name="height" value="" maxlength="4" /> ',
  47. '<img class="ke-refresh-btn" src="' + imgPath + 'refresh.gif" width="16" height="16" alt="" style="cursor:pointer;" />',
  48. '</div>',
  49. //align
  50. '<div class="ke-dialog-row">',
  51. '<label style="width:60px;">' + lang.align + '</label>',
  52. '<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="" />',
  53. ' <input type="radio" name="align" class="ke-inline-block" value="left" /> <img name="leftImg" src="' + imgPath + 'align_left.gif" width="23" height="25" alt="" />',
  54. ' <input type="radio" name="align" class="ke-inline-block" value="right" /> <img name="rightImg" src="' + imgPath + 'align_right.gif" width="23" height="25" alt="" />',
  55. '</div>',
  56. //title
  57. '<div class="ke-dialog-row">',
  58. '<label for="keTitle" style="width:60px;">' + lang.imgTitle + '</label>',
  59. '<input type="text" id="keTitle" class="ke-input-text" name="title" value="" style="width:200px;" /></div>',
  60. '</div>',
  61. '</div>'
  62. ].join('');
  63. var dialogWidth = allowImageUpload ? 450 : 400;
  64. dialogHeight = allowImageUpload ? 300 : 250;
  65. var dialog = self.createDialog({
  66. name : name,
  67. width : dialogWidth,
  68. height : dialogHeight,
  69. title : self.lang(name),
  70. body : html,
  71. yesBtn : {
  72. name : self.lang('yes'),
  73. click : function(e) {
  74. // insert local image
  75. if (tabs && tabs.selectedIndex === 1) {
  76. dialog.showLoading(self.lang('uploadLoading'));
  77. uploadbutton.submit();
  78. localUrlBox.val('');
  79. return;
  80. }
  81. // insert remote image
  82. var url = K.trim(urlBox.val()),
  83. width = widthBox.val(),
  84. height = heightBox.val(),
  85. title = titleBox.val(),
  86. align = '';
  87. alignBox.each(function() {
  88. if (this.checked) {
  89. align = this.value;
  90. return false;
  91. }
  92. });
  93. if (url == 'http://' || K.invalidUrl(url)) {
  94. alert(self.lang('invalidUrl'));
  95. urlBox[0].focus();
  96. return;
  97. }
  98. if (!/^\d*$/.test(width)) {
  99. alert(self.lang('invalidWidth'));
  100. widthBox[0].focus();
  101. return;
  102. }
  103. if (!/^\d*$/.test(height)) {
  104. alert(self.lang('invalidHeight'));
  105. heightBox[0].focus();
  106. return;
  107. }
  108. clickFn.call(self, url, title, width, height, 0, align);
  109. }
  110. },
  111. beforeRemove : function() {
  112. viewServerBtn.remove();
  113. widthBox.remove();
  114. heightBox.remove();
  115. refreshBtn.remove();
  116. uploadbutton.remove();
  117. }
  118. }),
  119. div = dialog.div;
  120. var tabs;
  121. if (allowImageUpload) {
  122. tabs = K.tabs({
  123. src : K('.tabs', div),
  124. afterSelect : function(i) {
  125. }
  126. });
  127. tabs.add({
  128. title : lang.remoteImage,
  129. panel : K('.tab1', div)
  130. });
  131. tabs.add({
  132. title : lang.localImage,
  133. panel : K('.tab2', div)
  134. });
  135. tabs.select(0);
  136. } else {
  137. K('.tab1', div).show();
  138. }
  139. var urlBox = K('[name="url"]', div),
  140. localUrlBox = K('[name="localUrl"]', div),
  141. viewServerBtn = K('[name="viewServer"]', div),
  142. widthBox = K('[name="width"]', div),
  143. heightBox = K('[name="height"]', div),
  144. refreshBtn = K('.ke-refresh-btn', div),
  145. titleBox = K('[name="title"]', div),
  146. alignBox = K('[name="align"]');
  147. var uploadbutton = K.uploadbutton({
  148. button : K('.ke-upload-button', div)[0],
  149. fieldName : 'imgFile',
  150. url : K.addParam(uploadJson, 'dir=image'),
  151. afterUpload : function(data) {
  152. dialog.hideLoading();
  153. if (data.error === 0) {
  154. var width = widthBox.val(),
  155. height = heightBox.val(),
  156. title = titleBox.val(),
  157. align = '';
  158. alignBox.each(function() {
  159. if (this.checked) {
  160. align = this.value;
  161. return false;
  162. }
  163. });
  164. var url = data.url;
  165. //alert(url);
  166. clickFn.call(self, url, title, width, height, 0, align);
  167. if (self.afterUpload) {
  168. self.afterUpload.call(self, url);
  169. }
  170. } else {
  171. alert(data.message);
  172. }
  173. },
  174. afterError : function(html) {
  175. dialog.hideLoading();
  176. self.errorDialog(html);
  177. }
  178. });
  179. uploadbutton.fileBox.change(function(e) {
  180. localUrlBox.val(uploadbutton.fileBox.val());
  181. });
  182. if (allowFileManager) {
  183. viewServerBtn.click(function(e) {
  184. self.loadPlugin('filemanager', function() {
  185. self.plugin.filemanagerDialog({
  186. viewType : 'VIEW',
  187. dirName : 'image',
  188. clickFn : function(url, title) {
  189. if (self.dialogs.length > 1) {
  190. K('[name="url"]', div).val(url);
  191. self.hideDialog();
  192. }
  193. }
  194. });
  195. });
  196. });
  197. } else {
  198. viewServerBtn.hide();
  199. }
  200. var originalWidth = 0, originalHeight = 0;
  201. function setSize(width, height) {
  202. widthBox.val(width);
  203. heightBox.val(height);
  204. originalWidth = width;
  205. originalHeight = height;
  206. }
  207. refreshBtn.click(function(e) {
  208. var tempImg = K('<img src="' + urlBox.val() + '" />', document).css({
  209. position : 'absolute',
  210. visibility : 'hidden',
  211. top : 0,
  212. left : '-1000px'
  213. });
  214. K(document.body).append(tempImg);
  215. setSize(tempImg.width(), tempImg.height());
  216. tempImg.remove();
  217. });
  218. widthBox.change(function(e) {
  219. if (originalWidth > 0) {
  220. heightBox.val(Math.round(originalHeight / originalWidth * parseInt(this.value, 10)));
  221. }
  222. });
  223. heightBox.change(function(e) {
  224. if (originalHeight > 0) {
  225. widthBox.val(Math.round(originalWidth / originalHeight * parseInt(this.value, 10)));
  226. }
  227. });
  228. urlBox.val(options.imageUrl);
  229. setSize(options.imageWidth, options.imageHeight);
  230. titleBox.val(options.imageTitle);
  231. alignBox.each(function() {
  232. if (this.value === options.imageAlign) {
  233. this.checked = true;
  234. return false;
  235. }
  236. });
  237. urlBox[0].focus();
  238. urlBox[0].select();
  239. return dialog;
  240. };
  241. self.plugin.image = {
  242. edit : function() {
  243. var img = self.plugin.getSelectedImage();
  244. self.plugin.imageDialog({
  245. imageUrl : img ? img.attr('data-ke-src') : 'http://',
  246. imageWidth : img ? img.width() : '',
  247. imageHeight : img ? img.height() : '',
  248. imageTitle : img ? img.attr('title') : '',
  249. imageAlign : img ? img.attr('align') : '',
  250. clickFn : function(url, title, width, height, border, align) {
  251. self.exec('insertimage', url, title, width, height, border, align);
  252. // Bugfix: [Firefox] 上传图片后,总是出现正在加载的样式,需要延迟执行hideDialog
  253. setTimeout(function() {
  254. self.hideDialog().focus();
  255. }, 0);
  256. }
  257. });
  258. },
  259. 'delete' : function() {
  260. self.plugin.getSelectedImage().remove();
  261. }
  262. };
  263. self.clickToolbar(name, self.plugin.image.edit);
  264. });