insertfile.js 4.3 KB

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