insertfile.js 4.4 KB

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