file-upload-js.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php if(!defined('__TYPECHO_ADMIN__')) exit; ?>
  2. <?php
  3. if (isset($post) && $post instanceof \Typecho\Widget && $post->have()) {
  4. $fileParentContent = $post;
  5. } elseif (isset($page) && $page instanceof \Typecho\Widget && $page->have()) {
  6. $fileParentContent = $page;
  7. }
  8. $phpMaxFilesize = function_exists('ini_get') ? trim(ini_get('upload_max_filesize')) : 0;
  9. if (preg_match("/^([0-9]+)([a-z]{1,2})$/i", $phpMaxFilesize, $matches)) {
  10. $phpMaxFilesize = strtolower($matches[1] . $matches[2] . (1 == strlen($matches[2]) ? 'b' : ''));
  11. }
  12. ?>
  13. <script src="<?php $options->adminStaticUrl('js', 'moxie.js'); ?>"></script>
  14. <script src="<?php $options->adminStaticUrl('js', 'plupload.js'); ?>"></script>
  15. <script>
  16. $(document).ready(function() {
  17. function updateAttacmentNumber () {
  18. var btn = $('#tab-files-btn'),
  19. balloon = $('.balloon', btn),
  20. count = $('#file-list li .insert').length;
  21. if (count > 0) {
  22. if (!balloon.length) {
  23. btn.html($.trim(btn.html()) + ' ');
  24. balloon = $('<span class="balloon"></span>').appendTo(btn);
  25. }
  26. balloon.html(count);
  27. } else if (0 == count && balloon.length > 0) {
  28. balloon.remove();
  29. }
  30. }
  31. $('.upload-area').bind({
  32. dragenter : function () {
  33. $(this).parent().addClass('drag');
  34. },
  35. dragover : function (e) {
  36. $(this).parent().addClass('drag');
  37. },
  38. drop : function () {
  39. $(this).parent().removeClass('drag');
  40. },
  41. dragend : function () {
  42. $(this).parent().removeClass('drag');
  43. },
  44. dragleave : function () {
  45. $(this).parent().removeClass('drag');
  46. }
  47. });
  48. updateAttacmentNumber();
  49. function fileUploadStart (file) {
  50. $('<li id="' + file.id + '" class="loading">'
  51. + file.name + '</li>').appendTo('#file-list');
  52. }
  53. function fileUploadError (error) {
  54. var file = error.file, code = error.code, word;
  55. switch (code) {
  56. case plupload.FILE_SIZE_ERROR:
  57. word = '<?php _e('文件大小超过限制'); ?>';
  58. break;
  59. case plupload.FILE_EXTENSION_ERROR:
  60. word = '<?php _e('文件扩展名不被支持'); ?>';
  61. break;
  62. case plupload.FILE_DUPLICATE_ERROR:
  63. word = '<?php _e('文件已经上传过'); ?>';
  64. break;
  65. case plupload.HTTP_ERROR:
  66. default:
  67. word = '<?php _e('上传出现错误'); ?>';
  68. break;
  69. }
  70. var fileError = '<?php _e('%s 上传失败'); ?>'.replace('%s', file.name),
  71. li, exist = $('#' + file.id);
  72. if (exist.length > 0) {
  73. li = exist.removeClass('loading').html(fileError);
  74. } else {
  75. li = $('<li>' + fileError + '<br />' + word + '</li>').appendTo('#file-list');
  76. }
  77. li.effect('highlight', {color : '#FBC2C4'}, 2000, function () {
  78. $(this).remove();
  79. });
  80. // fix issue #341
  81. this.removeFile(file);
  82. }
  83. var completeFile = null;
  84. function fileUploadComplete (id, url, data) {
  85. var li = $('#' + id).removeClass('loading').data('cid', data.cid)
  86. .data('url', data.url)
  87. .data('image', data.isImage)
  88. .html('<input type="hidden" name="attachment[]" value="' + data.cid + '" />'
  89. + '<a class="insert" target="_blank" href="###" title="<?php _e('点击插入文件'); ?>">' + data.title + '</a><div class="info">' + data.bytes
  90. + ' <a class="file" target="_blank" href="<?php $options->adminUrl('media.php'); ?>?cid='
  91. + data.cid + '" title="<?php _e('编辑'); ?>"><i class="i-edit"></i></a>'
  92. + ' <a class="delete" href="###" title="<?php _e('删除'); ?>"><i class="i-delete"></i></a></div>')
  93. .effect('highlight', 1000);
  94. attachInsertEvent(li);
  95. attachDeleteEvent(li);
  96. updateAttacmentNumber();
  97. if (!completeFile) {
  98. completeFile = data;
  99. }
  100. }
  101. var uploader = null, tabFilesEl = $('#tab-files').bind('init', function () {
  102. uploader = new plupload.Uploader({
  103. browse_button : $('.upload-file').get(0),
  104. url : '<?php $security->index('/action/upload'
  105. . (isset($fileParentContent) ? '?cid=' . $fileParentContent->cid : '')); ?>',
  106. runtimes : 'html5,flash,html4',
  107. flash_swf_url : '<?php $options->adminStaticUrl('js', 'Moxie.swf'); ?>',
  108. drop_element : $('.upload-area').get(0),
  109. filters : {
  110. max_file_size : '<?php echo $phpMaxFilesize ?>',
  111. mime_types : [{'title' : '<?php _e('允许上传的文件'); ?>', 'extensions' : '<?php echo implode(',', $options->allowedAttachmentTypes); ?>'}],
  112. prevent_duplicates : true
  113. },
  114. init : {
  115. FilesAdded : function (up, files) {
  116. for (var i = 0; i < files.length; i ++) {
  117. fileUploadStart(files[i]);
  118. }
  119. completeFile = null;
  120. uploader.start();
  121. },
  122. UploadComplete : function () {
  123. if (completeFile) {
  124. Typecho.uploadComplete(completeFile);
  125. }
  126. },
  127. FileUploaded : function (up, file, result) {
  128. if (200 == result.status) {
  129. var data = $.parseJSON(result.response);
  130. if (data) {
  131. fileUploadComplete(file.id, data[0], data[1]);
  132. uploader.removeFile(file);
  133. return;
  134. }
  135. }
  136. fileUploadError.call(uploader, {
  137. code : plupload.HTTP_ERROR,
  138. file : file
  139. });
  140. },
  141. Error : function (up, error) {
  142. fileUploadError.call(uploader, error);
  143. }
  144. }
  145. });
  146. uploader.init();
  147. });
  148. Typecho.uploadFile = function (file, name) {
  149. if (!uploader) {
  150. $('#tab-files-btn').parent().trigger('click');
  151. }
  152. var timer = setInterval(function () {
  153. if (!uploader) {
  154. return;
  155. }
  156. clearInterval(timer);
  157. timer = null;
  158. uploader.addFile(file, name);
  159. }, 50);
  160. };
  161. function attachInsertEvent (el) {
  162. $('.insert', el).click(function () {
  163. var t = $(this), p = t.parents('li');
  164. Typecho.insertFileToEditor(t.text(), p.data('url'), p.data('image'));
  165. return false;
  166. });
  167. }
  168. function attachDeleteEvent (el) {
  169. var file = $('a.insert', el).text();
  170. $('.delete', el).click(function () {
  171. if (confirm('<?php _e('确认要删除文件 %s 吗?'); ?>'.replace('%s', file))) {
  172. var cid = $(this).parents('li').data('cid');
  173. $.post('<?php $security->index('/action/contents-attachment-edit'); ?>',
  174. {'do' : 'delete', 'cid' : cid},
  175. function () {
  176. $(el).fadeOut(function () {
  177. $(this).remove();
  178. updateAttacmentNumber();
  179. });
  180. });
  181. }
  182. return false;
  183. });
  184. }
  185. $('#file-list li').each(function () {
  186. attachInsertEvent(this);
  187. attachDeleteEvent(this);
  188. });
  189. });
  190. </script>