media.js 5.5 KB

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