upfiles.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>upimgs</title>
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
  7. <meta name="apple-mobile-web-app-capable" content="yes" />
  8. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  9. <meta name="format-detection" content="telephone=no">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  11. <style>
  12. html,body{height:100%;margin:0;padding:0;background:#fff;}
  13. ul{margin:0;padding:0;list-style:none;}
  14. #wrap{padding:10px;}
  15. #topbar{padding:10px 0;border-bottom:1px solid #ccc;text-align:right;}
  16. #topbar button {margin:0;margin-left:5px;outline:none;padding: 4px 16px;box-sizing: border-box;
  17. display:inline-block;border:none;border-radius:3px;text-align:center;cursor:pointer;
  18. font-size:14px;line-height:1.5;background-color:#f0f0f0;color:#223;
  19. }
  20. #topbar button.primary{background-color:#3d97d4;color:#fff;}
  21. #topbar button:hover{background-color:#207ab7;color:#fff;}
  22. #topbar button.removeall{float:left}
  23. #file_list {display:grid;grid-gap:10px;grid-template-columns:repeat(auto-fill,minmax(160px,1fr));padding-top:10px;}
  24. #file_list:empty:after{content:'可以直接拖拽文件到这里';color:#777;font-size:0.8em;}
  25. #file_list li{position:relative;display:block;vertical-align:top;padding:5px 5px;border-radius:5px;}
  26. #file_list li.up-over {}
  27. #file_list li.up-now {}
  28. #file_list li.up-now:after{content:'';position:absolute;top:0;left:0;display:block;width:100%;height:100%;background:rgba(255,255,255,0.8) url(loading.gif) center center no-repeat;border-radius:5px;z-index:999;}
  29. #file_list li:hover{background-color:#ddd;}
  30. #file_list li .picbox {display:flex;flex:0 0 auto;justify-content:center;overflow:hidden;position:relative;width:100%;padding-top:100%;align-items:center;}
  31. #file_list li .picbox img {display:block;max-width:100%;max-height:100%;position:absolute;
  32. top:50%;left:50%;transform:translateX(-50%) translateY(-50%);}
  33. #file_list li.up-over .picbox:after{content:url('data:image/svg+xml;%20charset=utf8,%3Csvg%20viewBox%3D%220%200%201024%201024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2224%22%20height%3D%2224%22%3E%3Cpath%20d%3D%22M512%200C229.376%200%200%20229.376%200%20512s229.376%20512%20512%20512%20512-229.376%20512-512S794.624%200%20512%200z%22%20fill%3D%22%234AC711%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22M855.552%20394.752l-358.4%20358.4a50.9952%2050.9952%200%200%201-72.192%200l-204.8-204.8c-18.944-19.968-18.944-51.2%200-71.168a50.5344%2050.5344%200%200%201%2072.192-1.024L460.8%20644.608l322.048-322.048c19.968-18.944%2051.2-18.944%2071.168%200%2020.48%2019.456%2020.992%2051.712%201.536%2072.192z%22%20fill%3D%22%23FFFFFF%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E');position:absolute;bottom:2px;right:2px;z-index:9;}
  34. #file_list li .tools {display:none;position:absolute;bottom:5px;right:5px;z-index:99;}
  35. #file_list li:hover .tools {display:block;}
  36. #file_list li .tools .remove{cursor:pointer;}
  37. #file_list li .tools .remove:after{content:url('data:image/svg+xml;%20charset=utf8,%3Csvg%20width=%2224%22%20height=%2224%22%20viewBox=%220%200%2024%2024%22%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3Cpath%20d=%22M17%206h3a1%201%200%200%201%200%202h-1v11a3%203%200%200%201-3%203H8a3%203%200%200%201-3-3V8H4a1%201%200%201%201%200-2h3V5a3%203%200%200%201%203-3h4a3%203%200%200%201%203%203v1zm-2%200V5a1%201%200%200%200-1-1h-4a1%201%200%200%200-1%201v1h6zm2%202H7v11a1%201%200%200%200%201%201h8a1%201%200%200%200%201-1V8zm-8%203a1%201%200%200%201%202%200v6a1%201%200%200%201-2%200v-6zm4%200a1%201%200%200%201%202%200v6a1%201%200%200%201-2%200v-6z%22%3E%3C/path%3E%3C/svg%3E');}
  38. #file_list li .namebox {font-size:14px;line-height:20px;max-height:40px;overflow:hidden;padding:5px 10px;text-align:center;display:flex;justify-content:center;align-items:flex-start;}
  39. #file_list li .namebox span{word-break:break-all;vertical-align:top;}
  40. </style>
  41. </head>
  42. <body>
  43. <div id="wrap">
  44. <div id="topbar"><button class="addfile primary">+ 添加文件</button><button class="upall primary">全部上传</button><button class="removeall">清空列表</button></div>
  45. <ul id="file_list"></ul>
  46. </div>
  47. <script>
  48. var editor=parent.tinymce.activeEditor;
  49. var o2upimgs=parent["o2upimgs_"+editor.id];
  50. o2upimgs.res = []; //存放本地文件的数组
  51. o2upimgs.iframeWindow = window;
  52. var blobInfo = {file:null}
  53. blobInfo.blob = function(){return this.file;}
  54. var upload_handler = o2upimgs.images_upload_handler;
  55. var upload_base_path = o2upimgs.images_upload_base_path;
  56. //为列表添加排序
  57. function reSort(){
  58. var lis = document.querySelectorAll('#file_list li');
  59. for(var i=0; i<lis.length; i++){
  60. lis[i].setAttribute('data-num',i);
  61. }
  62. }
  63. function addList(files){
  64. var files_sum = files.length;
  65. var vDom = document.createDocumentFragment();
  66. for(var i=0;i<files_sum;i++){
  67. var file = files[i];
  68. var blobUrl = window.URL.createObjectURL(file)
  69. o2upimgs.res.push({file:file,blobUrl:blobUrl,url:''});
  70. var li = document.createElement('li');
  71. li.setAttribute('class','up-no');
  72. li.setAttribute('data-time',file.lastModified);
  73. li.innerHTML='<div class="picbox"><img src="'+blobUrl+'"></div><div class="namebox"><span>'+file.name+'</span></div><div class="tools"><a class="remove"></a></div>';
  74. vDom.appendChild(li);
  75. }
  76. document.querySelector('#file_list').appendChild(vDom);
  77. //reSort();
  78. }
  79. //清空列表
  80. document.querySelector('#topbar .removeall').addEventListener('click', function () {
  81. o2upimgs.res=[];
  82. var lis = document.querySelectorAll('#file_list li');
  83. for(var i=0; i<lis.length; i++){
  84. lis[i].parentNode.removeChild(lis[i]);
  85. }
  86. });
  87. //拖拽添加
  88. document.addEventListener('dragover', function(e){
  89. e.stopPropagation();
  90. e.preventDefault();
  91. e.dataTransfer.dropEffect = 'copy';
  92. });
  93. document.addEventListener('drop', function(e){
  94. e.stopPropagation();
  95. e.preventDefault();
  96. if(!e.dataTransfer.files){return false;}
  97. var dropfiles = e.dataTransfer.files;
  98. if(!(dropfiles.length>0)){return false;}
  99. var exts=o2upimgs.o2upimgs_filetype.replace(/(\s)+/g,'').toLowerCase().split(',');
  100. var files=[];
  101. for( var i=0; i < dropfiles.length; i++ ){
  102. var file = dropfiles[i];
  103. ext = file.name.split('.');
  104. ext = '.'+ext[ext.length-1];
  105. for(var j=0; j<exts.length; j++){
  106. if(exts[j]===ext){
  107. files.push(file);
  108. break;
  109. }
  110. }
  111. }
  112. if(files.length>0){ addList(files) }
  113. });
  114. //添加文件
  115. document.querySelector('#topbar .addfile').addEventListener('click',function(){
  116. debugger;
  117. var input = document.createElement('input');
  118. input.setAttribute('type', 'file');
  119. input.setAttribute('multiple', 'multiple');
  120. input.setAttribute('accept', o2upimgs.o2upimgs_filetype);
  121. input.addEventListener('change', function() {
  122. var files = this.files;
  123. addList(files);
  124. })
  125. input.click();
  126. });
  127. var file_i = 0;
  128. function upAllFiles(n, callback){
  129. var len = o2upimgs.res.length;
  130. file_i = n;
  131. if(len == n){
  132. file_i=0;
  133. document.querySelector('#topbar .upall').innerText='全部上传';
  134. if(callback)callback();
  135. return true;
  136. }
  137. if( o2upimgs.res[n].url!='' ){
  138. n++;
  139. upAllFiles(n, callback)
  140. }else{
  141. var img = document.querySelectorAll('#file_list li')[file_i].querySelector("img");
  142. blobInfo.file=o2upimgs.res[n].file;
  143. upload_handler(blobInfo, function(url, attributes){
  144. if(upload_base_path){
  145. if(upload_base_path.slice(-1)=='/' && url.substr(0,1)=='/' ){
  146. url = upload_base_path + url.slice(1);
  147. }else if(upload_base_path.slice(-1)!='/' && url.substr(0,1)!='/' ){
  148. url = upload_base_path + '/' + url;
  149. }else{
  150. url = upload_base_path + url;
  151. }
  152. }
  153. o2upimgs.res[file_i].url = url;
  154. o2upimgs.res[file_i].attributes = attributes;
  155. // filename = url.split('/').pop();
  156. var li = document.querySelectorAll('#file_list li')[file_i];
  157. li.setAttribute('class','up-over');
  158. // li.querySelector('.namebox span').innerText = filename;
  159. n++;
  160. upAllFiles(n, callback);
  161. },function(err){
  162. document.querySelector('#topbar .upall').innerText='全部上传';
  163. var lis = document.querySelectorAll('#file_list li.up-now');
  164. for(var i=0; i<lis.length; i++){
  165. lis[i].setAttribute('class','up-no');
  166. }
  167. alert(err);
  168. }, img);
  169. }
  170. }
  171. function uploadAll(callback){
  172. var button = document.querySelector('#topbar .upall');
  173. if(button.innerText!='全部上传'){return false;}
  174. if(o2upimgs.res.length>0){
  175. var lis = document.querySelectorAll('#file_list li.up-no');
  176. for(var i=0; i<lis.length; i++) {
  177. var el = lis[i];
  178. el.classList ? el.classList.add('up-now') : el.className += ' up-now';
  179. }
  180. button.innerText='上传中...';
  181. upAllFiles(0, callback);
  182. }
  183. }
  184. function querySave( callback ){
  185. if(o2upimgs.res.length>0){
  186. var lis = document.querySelectorAll('#file_list li.up-now');
  187. if(lis.length > 0){
  188. editor.windowManager.alert('正在上传,请稍等...');
  189. return false;
  190. }
  191. lis = document.querySelectorAll('#file_list li.up-no');
  192. if(lis.length > 0){
  193. editor.windowManager.confirm("还有图片没有上传,是否继续?", function(flag) {
  194. if (flag){
  195. if(callback)callback()
  196. // uploadAll(callback)
  197. }else{
  198. return;
  199. }
  200. });
  201. }else{
  202. if(callback)callback()
  203. }
  204. }
  205. }
  206. document.querySelector('#topbar .upall').addEventListener('click', function(e){
  207. uploadAll();
  208. // if(e.target.innerText!='全部上传'){return false;}
  209. // if(o2upimgs.res.length>0){
  210. // var lis = document.querySelectorAll('#file_list li.up-no');
  211. // for(var i=0; i<lis.length; i++) {
  212. // var el = lis[i];
  213. // el.classList ? el.classList.add('up-now') : el.className += ' up-now';
  214. // }
  215. // e.target.innerText='上传中...';
  216. // upAllFiles(0);
  217. // }
  218. });
  219. var observ_flist = new MutationObserver( function(muList,observe){
  220. if(muList[0].addedNodes.length>0){
  221. for( var i=0; i< muList[0].addedNodes.length; i++){
  222. var el = muList[0].addedNodes[i];
  223. el.querySelector('.remove').addEventListener('click', function(e){
  224. var li = e.target.parentNode.parentNode;
  225. var n = li.getAttribute('data-num');
  226. var el = document.querySelectorAll('#file_list li')[n];
  227. el.parentNode.removeChild(el);
  228. o2upimgs.res.splice(n,1);
  229. });
  230. }
  231. }
  232. reSort();
  233. });
  234. observ_flist.observe(document.querySelector('#file_list'),{childList:true});
  235. </script>
  236. </body>
  237. </html>