video.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: taoqili
  4. * Date: 12-2-20
  5. * Time: 上午11:19
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. (function(){
  9. var video = {},
  10. uploadVideoList = [],
  11. isModifyUploadVideo = false,
  12. uploadFile;
  13. window.onload = function(){
  14. $focus($G("videoUrl"));
  15. initTabs();
  16. initVideo();
  17. initUpload();
  18. };
  19. /* 初始化tab标签 */
  20. function initTabs(){
  21. var tabs = $G('tabHeads').children;
  22. for (var i = 0; i < tabs.length; i++) {
  23. domUtils.on(tabs[i], "click", function (e) {
  24. var j, bodyId, target = e.target || e.srcElement;
  25. for (j = 0; j < tabs.length; j++) {
  26. bodyId = tabs[j].getAttribute('data-content-id');
  27. if(tabs[j] == target){
  28. domUtils.addClass(tabs[j], 'focus');
  29. domUtils.addClass($G(bodyId), 'focus');
  30. }else {
  31. domUtils.removeClasses(tabs[j], 'focus');
  32. domUtils.removeClasses($G(bodyId), 'focus');
  33. }
  34. }
  35. });
  36. }
  37. }
  38. function initVideo(){
  39. createAlignButton( ["videoFloat", "upload_alignment"] );
  40. addUrlChangeListener($G("videoUrl"));
  41. addOkListener();
  42. //编辑视频时初始化相关信息
  43. (function(){
  44. var img = editor.selection.getRange().getClosedNode(),url;
  45. if(img && img.className){
  46. var hasFakedClass = (img.className == "edui-faked-video"),
  47. hasUploadClass = img.className.indexOf("edui-upload-video")!=-1;
  48. if(hasFakedClass || hasUploadClass) {
  49. $G("videoUrl").value = url = img.getAttribute("_url");
  50. $G("videoWidth").value = img.width;
  51. $G("videoHeight").value = img.height;
  52. var align = domUtils.getComputedStyle(img,"float"),
  53. parentAlign = domUtils.getComputedStyle(img.parentNode,"text-align");
  54. updateAlignButton(parentAlign==="center"?"center":align);
  55. }
  56. if(hasUploadClass) {
  57. isModifyUploadVideo = true;
  58. }
  59. }
  60. createPreviewVideo(url);
  61. })();
  62. }
  63. /**
  64. * 监听确认和取消两个按钮事件,用户执行插入或者清空正在播放的视频实例操作
  65. */
  66. function addOkListener(){
  67. dialog.onok = function(){
  68. $G("preview").innerHTML = "";
  69. var currentTab = findFocus("tabHeads","tabSrc");
  70. switch(currentTab){
  71. case "video":
  72. return insertSingle();
  73. break;
  74. case "videoSearch":
  75. return insertSearch("searchList");
  76. break;
  77. case "upload":
  78. return insertUpload();
  79. break;
  80. }
  81. };
  82. dialog.oncancel = function(){
  83. $G("preview").innerHTML = "";
  84. };
  85. }
  86. /**
  87. * 依据传入的align值更新按钮信息
  88. * @param align
  89. */
  90. function updateAlignButton( align ) {
  91. var aligns = $G( "videoFloat" ).children;
  92. for ( var i = 0, ci; ci = aligns[i++]; ) {
  93. if ( ci.getAttribute( "name" ) == align ) {
  94. if ( ci.className !="focus" ) {
  95. ci.className = "focus";
  96. }
  97. } else {
  98. if ( ci.className =="focus" ) {
  99. ci.className = "";
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * 将单个视频信息插入编辑器中
  106. */
  107. function insertSingle(){
  108. var width = $G("videoWidth"),
  109. height = $G("videoHeight"),
  110. url=$G('videoUrl').value,
  111. align = findFocus("videoFloat","name");
  112. if(!url) return false;
  113. if ( !checkNum( [width, height] ) ) return false;
  114. editor.execCommand('insertvideo', {
  115. url: convert_url(url),
  116. width: width.value,
  117. height: height.value,
  118. align: align
  119. }, isModifyUploadVideo ? 'upload':null);
  120. }
  121. /**
  122. * 将元素id下的所有代表视频的图片插入编辑器中
  123. * @param id
  124. */
  125. function insertSearch(id){
  126. var imgs = domUtils.getElementsByTagName($G(id),"img"),
  127. videoObjs=[];
  128. for(var i=0,img; img=imgs[i++];){
  129. if(img.getAttribute("selected")){
  130. videoObjs.push({
  131. url:img.getAttribute("ue_video_url"),
  132. width:420,
  133. height:280,
  134. align:"none"
  135. });
  136. }
  137. }
  138. editor.execCommand('insertvideo',videoObjs);
  139. }
  140. /**
  141. * 找到id下具有focus类的节点并返回该节点下的某个属性
  142. * @param id
  143. * @param returnProperty
  144. */
  145. function findFocus( id, returnProperty ) {
  146. var tabs = $G( id ).children,
  147. property;
  148. for ( var i = 0, ci; ci = tabs[i++]; ) {
  149. if ( ci.className=="focus" ) {
  150. property = ci.getAttribute( returnProperty );
  151. break;
  152. }
  153. }
  154. return property;
  155. }
  156. function convert_url(url){
  157. if ( !url ) return '';
  158. url = utils.trim(url)
  159. .replace(/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i, 'player.youku.com/player.php/sid/$1/v.swf')
  160. .replace(/(www\.)?youtube\.com\/watch\?v=([\w\-]+)/i, "www.youtube.com/v/$2")
  161. .replace(/youtu.be\/(\w+)$/i, "www.youtube.com/v/$1")
  162. .replace(/v\.ku6\.com\/.+\/([\w\.]+)\.html.*$/i, "player.ku6.com/refer/$1/v.swf")
  163. .replace(/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "player.56.com/v_$1.swf")
  164. .replace(/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "player.56.com/v_$1.swf")
  165. .replace(/v\.pps\.tv\/play_([\w]+)\.html.*$/i, "player.pps.tv/player/sid/$1/v.swf")
  166. .replace(/www\.letv\.com\/ptv\/vplay\/([\d]+)\.html.*$/i, "i7.imgs.letv.com/player/swfPlayer.swf?id=$1&autoplay=0")
  167. .replace(/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i, "www.tudou.com/v/$1")
  168. .replace(/v\.qq\.com\/cover\/[\w]+\/[\w]+\/([\w]+)\.html/i, "static.video.qq.com/TPout.swf?vid=$1")
  169. .replace(/v\.qq\.com\/.+[\?\&]vid=([^&]+).*$/i, "static.video.qq.com/TPout.swf?vid=$1")
  170. .replace(/my\.tv\.sohu\.com\/[\w]+\/[\d]+\/([\d]+)\.shtml.*$/i, "share.vrs.sohu.com/my/v.swf&id=$1");
  171. return url;
  172. }
  173. /**
  174. * 检测传入的所有input框中输入的长宽是否是正数
  175. * @param nodes input框集合,
  176. */
  177. function checkNum( nodes ) {
  178. for ( var i = 0, ci; ci = nodes[i++]; ) {
  179. var value = ci.value;
  180. if ( !isNumber( value ) && value) {
  181. alert( lang.numError );
  182. ci.value = "";
  183. ci.focus();
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. /**
  190. * 数字判断
  191. * @param value
  192. */
  193. function isNumber( value ) {
  194. return /(0|^[1-9]\d*$)/.test( value );
  195. }
  196. /**
  197. * 创建图片浮动选择按钮
  198. * @param ids
  199. */
  200. function createAlignButton( ids ) {
  201. for ( var i = 0, ci; ci = ids[i++]; ) {
  202. var floatContainer = $G( ci ),
  203. nameMaps = {"none":lang['default'], "left":lang.floatLeft, "right":lang.floatRight, "center":lang.block};
  204. for ( var j in nameMaps ) {
  205. var div = document.createElement( "div" );
  206. div.setAttribute( "name", j );
  207. if ( j == "none" ) div.className="focus";
  208. div.style.cssText = "background:url(images/" + j + "_focus.jpg);";
  209. div.setAttribute( "title", nameMaps[j] );
  210. floatContainer.appendChild( div );
  211. }
  212. switchSelect( ci );
  213. }
  214. }
  215. /**
  216. * 选择切换
  217. * @param selectParentId
  218. */
  219. function switchSelect( selectParentId ) {
  220. var selects = $G( selectParentId ).children;
  221. for ( var i = 0, ci; ci = selects[i++]; ) {
  222. domUtils.on( ci, "click", function () {
  223. for ( var j = 0, cj; cj = selects[j++]; ) {
  224. cj.className = "";
  225. cj.removeAttribute && cj.removeAttribute( "class" );
  226. }
  227. this.className = "focus";
  228. } )
  229. }
  230. }
  231. /**
  232. * 监听url改变事件
  233. * @param url
  234. */
  235. function addUrlChangeListener(url){
  236. if (browser.ie) {
  237. url.onpropertychange = function () {
  238. createPreviewVideo( this.value );
  239. }
  240. } else {
  241. url.addEventListener( "input", function () {
  242. createPreviewVideo( this.value );
  243. }, false );
  244. }
  245. }
  246. /**
  247. * 根据url生成视频预览
  248. * @param url
  249. */
  250. function createPreviewVideo(url){
  251. if ( !url )return;
  252. if(url.indexOf("plugins") > -1 ){
  253. var nurl = url.replace('plugins/ueditor_full/', '');
  254. url = '../../'+nurl;
  255. }
  256. var conUrl = convert_url(url);
  257. conUrl = utils.unhtmlForUrl(conUrl);
  258. $G("preview").innerHTML = '<video class="previewVideo" src="' + conUrl + '" width="420" height="280" controls="controls"></video>';
  259. }
  260. /* 插入上传视频 */
  261. function insertUpload(){
  262. var videoObjs=[],
  263. uploadDir = editor.getOpt('videoUrlPrefix'),
  264. width = parseInt($G('upload_width').value, 10) || 420,
  265. height = parseInt($G('upload_height').value, 10) || 280,
  266. align = findFocus("upload_alignment","name") || 'none';
  267. for(var key in uploadVideoList) {
  268. var file = uploadVideoList[key];
  269. videoObjs.push({
  270. url: uploadDir + file.url,
  271. width:width,
  272. height:height,
  273. align:align
  274. });
  275. }
  276. var count = uploadFile.getQueueCount();
  277. if (count) {
  278. $('.info', '#queueList').html('<span style="color:red;">' + '还有2个未上传文件'.replace(/[\d]/, count) + '</span>');
  279. return false;
  280. } else {
  281. editor.execCommand('insertvideo', videoObjs, 'upload');
  282. }
  283. }
  284. /*初始化上传标签*/
  285. function initUpload(){
  286. uploadFile = new UploadFile('queueList');
  287. }
  288. /* 上传附件 */
  289. function UploadFile(target) {
  290. this.$wrap = target.constructor == String ? $('#' + target) : $(target);
  291. this.init();
  292. }
  293. UploadFile.prototype = {
  294. init: function () {
  295. this.fileList = [];
  296. this.initContainer();
  297. this.initUploader();
  298. },
  299. initContainer: function () {
  300. this.$queue = this.$wrap.find('.filelist');
  301. },
  302. /* 初始化容器 */
  303. initUploader: function () {
  304. var _this = this,
  305. $ = jQuery, // just in case. Make sure it's not an other libaray.
  306. $wrap = _this.$wrap,
  307. // 图片容器
  308. $queue = $wrap.find('.filelist'),
  309. // 状态栏,包括进度和控制按钮
  310. $statusBar = $wrap.find('.statusBar'),
  311. // 文件总体选择信息。
  312. $info = $statusBar.find('.info'),
  313. // 上传按钮
  314. $upload = $wrap.find('.uploadBtn'),
  315. // 上传按钮
  316. $filePickerBtn = $wrap.find('.filePickerBtn'),
  317. // 上传按钮
  318. $filePickerBlock = $wrap.find('.filePickerBlock'),
  319. // 没选择文件之前的内容。
  320. $placeHolder = $wrap.find('.placeholder'),
  321. // 总体进度条
  322. $progress = $statusBar.find('.progress').hide(),
  323. // 添加的文件数量
  324. fileCount = 0,
  325. // 添加的文件总大小
  326. fileSize = 0,
  327. // 优化retina, 在retina下这个值是2
  328. ratio = window.devicePixelRatio || 1,
  329. // 缩略图大小
  330. thumbnailWidth = 113 * ratio,
  331. thumbnailHeight = 113 * ratio,
  332. // 可能有pedding, ready, uploading, confirm, done.
  333. state = '',
  334. // 所有文件的进度信息,key为file id
  335. percentages = {},
  336. supportTransition = (function () {
  337. var s = document.createElement('p').style,
  338. r = 'transition' in s ||
  339. 'WebkitTransition' in s ||
  340. 'MozTransition' in s ||
  341. 'msTransition' in s ||
  342. 'OTransition' in s;
  343. s = null;
  344. return r;
  345. })(),
  346. // WebUploader实例
  347. uploader,
  348. actionUrl = editor.getActionUrl(editor.getOpt('videoActionName')),
  349. fileMaxSize = editor.getOpt('videoMaxSize'),
  350. acceptExtensions = (editor.getOpt('videoAllowFiles') || []).join('').replace(/\./g, ',').replace(/^[,]/, '');;
  351. if (!WebUploader.Uploader.support()) {
  352. $('#filePickerReady').after($('<div>').html(lang.errorNotSupport)).hide();
  353. return;
  354. } else if (!editor.getOpt('videoActionName')) {
  355. $('#filePickerReady').after($('<div>').html(lang.errorLoadConfig)).hide();
  356. return;
  357. }
  358. uploader = _this.uploader = WebUploader.create({
  359. pick: {
  360. id: '#filePickerReady',
  361. label: lang.uploadSelectFile
  362. },
  363. swf: '../../third-party/webuploader/Uploader.swf',
  364. server: actionUrl,
  365. fileVal: editor.getOpt('videoFieldName'),
  366. duplicate: true,
  367. fileSingleSizeLimit: fileMaxSize,
  368. compress: false
  369. });
  370. uploader.addButton({
  371. id: '#filePickerBlock'
  372. });
  373. uploader.addButton({
  374. id: '#filePickerBtn',
  375. label: lang.uploadAddFile
  376. });
  377. setState('pedding');
  378. // 当有文件添加进来时执行,负责view的创建
  379. function addFile(file) {
  380. var $li = $('<li id="' + file.id + '">' +
  381. '<p class="title">' + file.name + '</p>' +
  382. '<p class="imgWrap"></p>' +
  383. '<p class="progress"><span></span></p>' +
  384. '</li>'),
  385. $btns = $('<div class="file-panel">' +
  386. '<span class="cancel">' + lang.uploadDelete + '</span>' +
  387. '<span class="rotateRight">' + lang.uploadTurnRight + '</span>' +
  388. '<span class="rotateLeft">' + lang.uploadTurnLeft + '</span></div>').appendTo($li),
  389. $prgress = $li.find('p.progress span'),
  390. $wrap = $li.find('p.imgWrap'),
  391. $info = $('<p class="error"></p>').hide().appendTo($li),
  392. showError = function (code) {
  393. switch (code) {
  394. case 'exceed_size':
  395. text = lang.errorExceedSize;
  396. break;
  397. case 'interrupt':
  398. text = lang.errorInterrupt;
  399. break;
  400. case 'http':
  401. text = lang.errorHttp;
  402. break;
  403. case 'not_allow_type':
  404. text = lang.errorFileType;
  405. break;
  406. default:
  407. text = lang.errorUploadRetry;
  408. break;
  409. }
  410. $info.text(text).show();
  411. };
  412. if (file.getStatus() === 'invalid') {
  413. showError(file.statusText);
  414. } else {
  415. $wrap.text(lang.uploadPreview);
  416. if ('|png|jpg|jpeg|bmp|gif|'.indexOf('|'+file.ext.toLowerCase()+'|') == -1) {
  417. $wrap.empty().addClass('notimage').append('<i class="file-preview file-type-' + file.ext.toLowerCase() + '"></i>' +
  418. '<span class="file-title">' + file.name + '</span>');
  419. } else {
  420. if (browser.ie && browser.version <= 7) {
  421. $wrap.text(lang.uploadNoPreview);
  422. } else {
  423. uploader.makeThumb(file, function (error, src) {
  424. if (error || !src || (/^data:/.test(src) && browser.ie && browser.version <= 7)) {
  425. $wrap.text(lang.uploadNoPreview);
  426. } else {
  427. var $img = $('<img src="' + src + '">');
  428. $wrap.empty().append($img);
  429. $img.on('error', function () {
  430. $wrap.text(lang.uploadNoPreview);
  431. });
  432. }
  433. }, thumbnailWidth, thumbnailHeight);
  434. }
  435. }
  436. percentages[ file.id ] = [ file.size, 0 ];
  437. file.rotation = 0;
  438. /* 检查文件格式 */
  439. if (!file.ext || acceptExtensions.indexOf(file.ext.toLowerCase()) == -1) {
  440. showError('not_allow_type');
  441. uploader.removeFile(file);
  442. }
  443. }
  444. file.on('statuschange', function (cur, prev) {
  445. if (prev === 'progress') {
  446. $prgress.hide().width(0);
  447. } else if (prev === 'queued') {
  448. $li.off('mouseenter mouseleave');
  449. $btns.remove();
  450. }
  451. // 成功
  452. if (cur === 'error' || cur === 'invalid') {
  453. showError(file.statusText);
  454. percentages[ file.id ][ 1 ] = 1;
  455. } else if (cur === 'interrupt') {
  456. showError('interrupt');
  457. } else if (cur === 'queued') {
  458. percentages[ file.id ][ 1 ] = 0;
  459. } else if (cur === 'progress') {
  460. $info.hide();
  461. $prgress.css('display', 'block');
  462. } else if (cur === 'complete') {
  463. }
  464. $li.removeClass('state-' + prev).addClass('state-' + cur);
  465. });
  466. $li.on('mouseenter', function () {
  467. $btns.stop().animate({height: 30});
  468. });
  469. $li.on('mouseleave', function () {
  470. $btns.stop().animate({height: 0});
  471. });
  472. $btns.on('click', 'span', function () {
  473. var index = $(this).index(),
  474. deg;
  475. switch (index) {
  476. case 0:
  477. uploader.removeFile(file);
  478. return;
  479. case 1:
  480. file.rotation += 90;
  481. break;
  482. case 2:
  483. file.rotation -= 90;
  484. break;
  485. }
  486. if (supportTransition) {
  487. deg = 'rotate(' + file.rotation + 'deg)';
  488. $wrap.css({
  489. '-webkit-transform': deg,
  490. '-mos-transform': deg,
  491. '-o-transform': deg,
  492. 'transform': deg
  493. });
  494. } else {
  495. $wrap.css('filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + (~~((file.rotation / 90) % 4 + 4) % 4) + ')');
  496. }
  497. });
  498. $li.insertBefore($filePickerBlock);
  499. }
  500. // 负责view的销毁
  501. function removeFile(file) {
  502. var $li = $('#' + file.id);
  503. delete percentages[ file.id ];
  504. updateTotalProgress();
  505. $li.off().find('.file-panel').off().end().remove();
  506. }
  507. function updateTotalProgress() {
  508. var loaded = 0,
  509. total = 0,
  510. spans = $progress.children(),
  511. percent;
  512. $.each(percentages, function (k, v) {
  513. total += v[ 0 ];
  514. loaded += v[ 0 ] * v[ 1 ];
  515. });
  516. percent = total ? loaded / total : 0;
  517. spans.eq(0).text(Math.round(percent * 100) + '%');
  518. spans.eq(1).css('width', Math.round(percent * 100) + '%');
  519. updateStatus();
  520. }
  521. function setState(val, files) {
  522. if (val != state) {
  523. var stats = uploader.getStats();
  524. $upload.removeClass('state-' + state);
  525. $upload.addClass('state-' + val);
  526. switch (val) {
  527. /* 未选择文件 */
  528. case 'pedding':
  529. $queue.addClass('element-invisible');
  530. $statusBar.addClass('element-invisible');
  531. $placeHolder.removeClass('element-invisible');
  532. $progress.hide(); $info.hide();
  533. uploader.refresh();
  534. break;
  535. /* 可以开始上传 */
  536. case 'ready':
  537. $placeHolder.addClass('element-invisible');
  538. $queue.removeClass('element-invisible');
  539. $statusBar.removeClass('element-invisible');
  540. $progress.hide(); $info.show();
  541. $upload.text(lang.uploadStart);
  542. uploader.refresh();
  543. break;
  544. /* 上传中 */
  545. case 'uploading':
  546. $progress.show(); $info.hide();
  547. $upload.text(lang.uploadPause);
  548. break;
  549. /* 暂停上传 */
  550. case 'paused':
  551. $progress.show(); $info.hide();
  552. $upload.text(lang.uploadContinue);
  553. break;
  554. case 'confirm':
  555. $progress.show(); $info.hide();
  556. $upload.text(lang.uploadStart);
  557. stats = uploader.getStats();
  558. if (stats.successNum && !stats.uploadFailNum) {
  559. setState('finish');
  560. return;
  561. }
  562. break;
  563. case 'finish':
  564. $progress.hide(); $info.show();
  565. if (stats.uploadFailNum) {
  566. $upload.text(lang.uploadRetry);
  567. } else {
  568. $upload.text(lang.uploadStart);
  569. }
  570. break;
  571. }
  572. state = val;
  573. updateStatus();
  574. }
  575. if (!_this.getQueueCount()) {
  576. $upload.addClass('disabled')
  577. } else {
  578. $upload.removeClass('disabled')
  579. }
  580. }
  581. function updateStatus() {
  582. var text = '', stats;
  583. if (state === 'ready') {
  584. text = lang.updateStatusReady.replace('_', fileCount).replace('_KB', WebUploader.formatSize(fileSize));
  585. } else if (state === 'confirm') {
  586. stats = uploader.getStats();
  587. if (stats.uploadFailNum) {
  588. text = lang.updateStatusConfirm.replace('_', stats.successNum).replace('_', stats.successNum);
  589. }
  590. } else {
  591. stats = uploader.getStats();
  592. text = lang.updateStatusFinish.replace('_', fileCount).
  593. replace('_KB', WebUploader.formatSize(fileSize)).
  594. replace('_', stats.successNum);
  595. if (stats.uploadFailNum) {
  596. text += lang.updateStatusError.replace('_', stats.uploadFailNum);
  597. }
  598. }
  599. $info.html(text);
  600. }
  601. uploader.on('fileQueued', function (file) {
  602. fileCount++;
  603. fileSize += file.size;
  604. if (fileCount === 1) {
  605. $placeHolder.addClass('element-invisible');
  606. $statusBar.show();
  607. }
  608. addFile(file);
  609. });
  610. uploader.on('fileDequeued', function (file) {
  611. fileCount--;
  612. fileSize -= file.size;
  613. removeFile(file);
  614. updateTotalProgress();
  615. });
  616. uploader.on('filesQueued', function (file) {
  617. if (!uploader.isInProgress() && (state == 'pedding' || state == 'finish' || state == 'confirm' || state == 'ready')) {
  618. setState('ready');
  619. }
  620. updateTotalProgress();
  621. });
  622. uploader.on('all', function (type, files) {
  623. switch (type) {
  624. case 'uploadFinished':
  625. setState('confirm', files);
  626. break;
  627. case 'startUpload':
  628. /* 添加额外的GET参数 */
  629. var params = utils.serializeParam(editor.queryCommandValue('serverparam')) || '',
  630. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?':'&') + 'encode=utf-8&' + params);
  631. uploader.option('server', url);
  632. setState('uploading', files);
  633. break;
  634. case 'stopUpload':
  635. setState('paused', files);
  636. break;
  637. }
  638. });
  639. uploader.on('uploadBeforeSend', function (file, data, header) {
  640. //这里可以通过data对象添加POST参数
  641. header['X_Requested_With'] = 'XMLHttpRequest';
  642. });
  643. uploader.on('uploadProgress', function (file, percentage) {
  644. var $li = $('#' + file.id),
  645. $percent = $li.find('.progress span');
  646. $percent.css('width', percentage * 100 + '%');
  647. percentages[ file.id ][ 1 ] = percentage;
  648. updateTotalProgress();
  649. });
  650. uploader.on('uploadSuccess', function (file, ret) {
  651. var $file = $('#' + file.id);
  652. try {
  653. var responseText = (ret._raw || ret),
  654. json = utils.str2json(responseText);
  655. if (json.state == 'SUCCESS') {
  656. uploadVideoList.push({
  657. 'url': json.url,
  658. 'type': json.type,
  659. 'original':json.original
  660. });
  661. $file.append('<span class="success"></span>');
  662. } else {
  663. $file.find('.error').text(json.state).show();
  664. }
  665. } catch (e) {
  666. $file.find('.error').text(lang.errorServerUpload).show();
  667. }
  668. });
  669. uploader.on('uploadError', function (file, code) {
  670. });
  671. uploader.on('error', function (code, file) {
  672. if (code == 'Q_TYPE_DENIED' || code == 'F_EXCEED_SIZE') {
  673. addFile(file);
  674. }
  675. });
  676. uploader.on('uploadComplete', function (file, ret) {
  677. });
  678. $upload.on('click', function () {
  679. if ($(this).hasClass('disabled')) {
  680. return false;
  681. }
  682. if (state === 'ready') {
  683. uploader.upload();
  684. } else if (state === 'paused') {
  685. uploader.upload();
  686. } else if (state === 'uploading') {
  687. uploader.stop();
  688. }
  689. });
  690. $upload.addClass('state-' + state);
  691. updateTotalProgress();
  692. },
  693. getQueueCount: function () {
  694. var file, i, status, readyFile = 0, files = this.uploader.getFiles();
  695. for (i = 0; file = files[i++]; ) {
  696. status = file.getStatus();
  697. if (status == 'queued' || status == 'uploading' || status == 'progress') readyFile++;
  698. }
  699. return readyFile;
  700. },
  701. refresh: function(){
  702. this.uploader.refresh();
  703. }
  704. };
  705. })();