UU跑腿标准版

image.js 8.9KB

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