insertfile.js 4.2 KB

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