media.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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('media', function(K) {
  10. var self = this, name = 'media', lang = self.lang(name + '.'),
  11. allowMediaUpload = K.undef(self.allowMediaUpload, true),
  12. allowFileManager = K.undef(self.allowFileManager, false),
  13. uploadJson = K.undef(self.uploadJson, self.basePath + 'php/upload_json.php');
  14. self.plugin.media = {
  15. edit : function() {
  16. var html = [
  17. '<div style="padding:10px 20px;">',
  18. //url
  19. '<div class="ke-dialog-row">',
  20. '<label for="keUrl" style="width:60px;">' + lang.url + '</label>',
  21. '<input class="ke-input-text" type="text" id="keUrl" name="url" value="" style="width:160px;" /> &nbsp;',
  22. '<input type="button" class="ke-upload-button" value="' + lang.upload + '" /> &nbsp;',
  23. '<span class="ke-button-common ke-button-outer">',
  24. '<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
  25. '</span>',
  26. '</div>',
  27. //width
  28. '<div class="ke-dialog-row">',
  29. '<label for="keWidth" style="width:60px;">' + lang.width + '</label>',
  30. '<input type="text" id="keWidth" class="ke-input-text ke-input-number" name="width" value="550" maxlength="4" />',
  31. '</div>',
  32. //height
  33. '<div class="ke-dialog-row">',
  34. '<label for="keHeight" style="width:60px;">' + lang.height + '</label>',
  35. '<input type="text" id="keHeight" class="ke-input-text ke-input-number" name="height" value="400" maxlength="4" />',
  36. '</div>',
  37. //autostart
  38. '<div class="ke-dialog-row">',
  39. '<label for="keAutostart">' + lang.autostart + '</label>',
  40. '<input type="checkbox" id="keAutostart" name="autostart" value="" /> ',
  41. '</div>',
  42. '</div>'
  43. ].join('');
  44. var dialog = self.createDialog({
  45. name : name,
  46. width : 450,
  47. height : 230,
  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. width = widthBox.val(),
  55. height = heightBox.val();
  56. if (url == 'http://' || K.invalidUrl(url)) {
  57. alert(self.lang('invalidUrl'));
  58. urlBox[0].focus();
  59. return;
  60. }
  61. if (!/^\d*$/.test(width)) {
  62. alert(self.lang('invalidWidth'));
  63. widthBox[0].focus();
  64. return;
  65. }
  66. if (!/^\d*$/.test(height)) {
  67. alert(self.lang('invalidHeight'));
  68. heightBox[0].focus();
  69. return;
  70. }
  71. var html = K.mediaImg(self.themesPath + 'common/blank.gif', {
  72. src : url,
  73. type : K.mediaType(url),
  74. width : width,
  75. height : height,
  76. autostart : autostartBox[0].checked ? 'true' : 'false',
  77. loop : 'true'
  78. });
  79. self.insertHtml(html).hideDialog().focus();
  80. }
  81. }
  82. }),
  83. div = dialog.div,
  84. urlBox = K('[name="url"]', div),
  85. viewServerBtn = K('[name="viewServer"]', div),
  86. widthBox = K('[name="width"]', div),
  87. heightBox = K('[name="height"]', div),
  88. autostartBox = K('[name="autostart"]', div);
  89. urlBox.val('http://');
  90. if (allowMediaUpload) {
  91. var uploadbutton = K.uploadbutton({
  92. button : K('.ke-upload-button', div)[0],
  93. fieldName : 'imgFile',
  94. url : K.addParam(uploadJson, 'dir=media'),
  95. afterUpload : function(data) {
  96. dialog.hideLoading();
  97. if (data.error === 0) {
  98. var url = K.formatUrl(data.url, 'absolute');
  99. urlBox.val(url);
  100. if (self.afterUpload) {
  101. self.afterUpload.call(self, url);
  102. }
  103. alert(self.lang('uploadSuccess'));
  104. } else {
  105. alert(data.message);
  106. }
  107. },
  108. afterError : function(html) {
  109. dialog.hideLoading();
  110. self.errorDialog(html);
  111. }
  112. });
  113. uploadbutton.fileBox.change(function(e) {
  114. dialog.showLoading(self.lang('uploadLoading'));
  115. uploadbutton.submit();
  116. });
  117. } else {
  118. K('.ke-upload-button', div).hide();
  119. urlBox.width(250);
  120. }
  121. if (allowFileManager) {
  122. viewServerBtn.click(function(e) {
  123. self.loadPlugin('filemanager', function() {
  124. self.plugin.filemanagerDialog({
  125. viewType : 'LIST',
  126. dirName : 'media',
  127. clickFn : function(url, title) {
  128. if (self.dialogs.length > 1) {
  129. K('[name="url"]', div).val(url);
  130. self.hideDialog();
  131. }
  132. }
  133. });
  134. });
  135. });
  136. } else {
  137. viewServerBtn.hide();
  138. }
  139. var img = self.plugin.getSelectedMedia();
  140. if (img) {
  141. var attrs = K.mediaAttrs(img.attr('data-ke-tag'));
  142. urlBox.val(attrs.src);
  143. widthBox.val(K.removeUnit(img.css('width')) || attrs.width || 0);
  144. heightBox.val(K.removeUnit(img.css('height')) || attrs.height || 0);
  145. autostartBox[0].checked = (attrs.autostart === 'true');
  146. }
  147. urlBox[0].focus();
  148. urlBox[0].select();
  149. },
  150. 'delete' : function() {
  151. self.plugin.getSelectedMedia().remove();
  152. }
  153. };
  154. self.clickToolbar(name, self.plugin.media.edit);
  155. });