/source/Plug-in/kind/plugins/insertfile/insertfile.js

http://prosporous.googlecode.com/ · JavaScript · 129 lines · 115 code · 4 blank · 10 comment · 9 complexity · a8273780b78d5de5a8936ba45a41018a MD5 · raw file

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