UU跑腿标准版

insertfile.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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('insertfile', function(K) {
  10. var self = this, name = 'insertfile',
  11. allowFileUpload = K.undef(self.allowFileUpload, true),
  12. allowFileManager = K.undef(self.allowFileManager, false),
  13. uploadJson = K.undef(self.uploadJson, self.basePath + 'php/upload_json.php'),
  14. lang = self.lang(name + '.');
  15. self.plugin.fileDialog = function(options) {
  16. var fileUrl = K.undef(options.fileUrl, 'http://'),
  17. fileTitle = K.undef(options.fileTitle, ''),
  18. clickFn = options.clickFn;
  19. var html = [
  20. '<div style="padding:10px 20px;">',
  21. '<div class="ke-dialog-row">',
  22. '<label for="keUrl" style="width:60px;">' + lang.url + '</label>',
  23. '<input type="text" id="keUrl" name="url" class="ke-input-text" style="width:160px;" /> &nbsp;',
  24. '<input type="button" class="ke-upload-button" value="' + lang.upload + '" /> &nbsp;',
  25. '<span class="ke-button-common ke-button-outer">',
  26. '<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
  27. '</span>',
  28. '</div>',
  29. //title
  30. '<div class="ke-dialog-row">',
  31. '<label for="keTitle" style="width:60px;">' + lang.title + '</label>',
  32. '<input type="text" id="keTitle" class="ke-input-text" name="title" value="" style="width:160px;" /></div>',
  33. '</div>',
  34. //form end
  35. '</form>',
  36. '</div>'
  37. ].join('');
  38. var dialog = self.createDialog({
  39. name : name,
  40. width : 450,
  41. height : 180,
  42. title : self.lang(name),
  43. body : html,
  44. yesBtn : {
  45. name : self.lang('yes'),
  46. click : function(e) {
  47. var url = K.trim(urlBox.val()),
  48. title = titleBox.val();
  49. if (url == 'http://' || K.invalidUrl(url)) {
  50. alert(self.lang('invalidUrl'));
  51. urlBox[0].focus();
  52. return;
  53. }
  54. if (K.trim(title) === '') {
  55. title = url;
  56. }
  57. clickFn.call(self, url, title);
  58. }
  59. }
  60. }),
  61. div = dialog.div;
  62. var urlBox = K('[name="url"]', div),
  63. viewServerBtn = K('[name="viewServer"]', div),
  64. titleBox = K('[name="title"]', div);
  65. if (allowFileUpload) {
  66. var uploadbutton = K.uploadbutton({
  67. button : K('.ke-upload-button', div)[0],
  68. fieldName : 'imgFile',
  69. url : K.addParam(uploadJson, 'dir=file'),
  70. afterUpload : function(data) {
  71. dialog.hideLoading();
  72. if (data.error === 0) {
  73. var url = K.formatUrl(data.url, 'absolute');
  74. urlBox.val(url);
  75. if (self.afterUpload) {
  76. self.afterUpload.call(self, url);
  77. }
  78. alert(self.lang('uploadSuccess'));
  79. } else {
  80. alert(data.message);
  81. }
  82. },
  83. afterError : function(html) {
  84. dialog.hideLoading();
  85. self.errorDialog(html);
  86. }
  87. });
  88. uploadbutton.fileBox.change(function(e) {
  89. dialog.showLoading(self.lang('uploadLoading'));
  90. uploadbutton.submit();
  91. });
  92. } else {
  93. K('.ke-upload-button', div).hide();
  94. urlBox.width(250);
  95. }
  96. if (allowFileManager) {
  97. viewServerBtn.click(function(e) {
  98. self.loadPlugin('filemanager', function() {
  99. self.plugin.filemanagerDialog({
  100. viewType : 'LIST',
  101. dirName : 'file',
  102. clickFn : function(url, title) {
  103. if (self.dialogs.length > 1) {
  104. K('[name="url"]', div).val(url);
  105. self.hideDialog();
  106. }
  107. }
  108. });
  109. });
  110. });
  111. } else {
  112. viewServerBtn.hide();
  113. }
  114. urlBox.val(fileUrl);
  115. titleBox.val(fileTitle);
  116. urlBox[0].focus();
  117. urlBox[0].select();
  118. };
  119. self.clickToolbar(name, function() {
  120. self.plugin.fileDialog({
  121. clickFn : function(url, title) {
  122. var html = '<a href="' + url + '" data-ke-src="' + url + '" target="_blank">' + title + '</a>';
  123. self.insertHtml(html).hideDialog().focus();
  124. }
  125. });
  126. });
  127. });