You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

788 lines
29 KiB

7 years ago
  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. var conUrl = convert_url(url);
  253. $G("preview").innerHTML = '<div class="previewMsg"><span>'+lang.urlError+'</span></div>'+
  254. '<embed class="previewVideo" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  255. ' src="' + conUrl + '"' +
  256. ' width="' + 420 + '"' +
  257. ' height="' + 280 + '"' +
  258. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >' +
  259. '</embed>';
  260. }
  261. /* 插入上传视频 */
  262. function insertUpload(){
  263. var videoObjs=[],
  264. uploadDir = editor.getOpt('videoUrlPrefix'),
  265. width = $G('upload_width').value || 420,
  266. height = $G('upload_height').value || 280,
  267. align = findFocus("upload_alignment","name") || 'none';
  268. for(var key in uploadVideoList) {
  269. var file = uploadVideoList[key];
  270. videoObjs.push({
  271. url: uploadDir + file.url,
  272. width:width,
  273. height:height,
  274. align:align
  275. });
  276. }
  277. var count = uploadFile.getQueueCount();
  278. if (count) {
  279. $('.info', '#queueList').html('<span style="color:red;">' + '还有2个未上传文件'.replace(/[\d]/, count) + '</span>');
  280. return false;
  281. } else {
  282. editor.execCommand('insertvideo', videoObjs, 'upload');
  283. }
  284. }
  285. /*初始化上传标签*/
  286. function initUpload(){
  287. uploadFile = new UploadFile('queueList');
  288. }
  289. /* 上传附件 */
  290. function UploadFile(target) {
  291. this.$wrap = target.constructor == String ? $('#' + target) : $(target);
  292. this.init();
  293. }
  294. UploadFile.prototype = {
  295. init: function () {
  296. this.fileList = [];
  297. this.initContainer();
  298. this.initUploader();
  299. },
  300. initContainer: function () {
  301. this.$queue = this.$wrap.find('.filelist');
  302. },
  303. /* 初始化容器 */
  304. initUploader: function () {
  305. var _this = this,
  306. $ = jQuery, // just in case. Make sure it's not an other libaray.
  307. $wrap = _this.$wrap,
  308. // 图片容器
  309. $queue = $wrap.find('.filelist'),
  310. // 状态栏,包括进度和控制按钮
  311. $statusBar = $wrap.find('.statusBar'),
  312. // 文件总体选择信息。
  313. $info = $statusBar.find('.info'),
  314. // 上传按钮
  315. $upload = $wrap.find('.uploadBtn'),
  316. // 上传按钮
  317. $filePickerBtn = $wrap.find('.filePickerBtn'),
  318. // 上传按钮
  319. $filePickerBlock = $wrap.find('.filePickerBlock'),
  320. // 没选择文件之前的内容。
  321. $placeHolder = $wrap.find('.placeholder'),
  322. // 总体进度条
  323. $progress = $statusBar.find('.progress').hide(),
  324. // 添加的文件数量
  325. fileCount = 0,
  326. // 添加的文件总大小
  327. fileSize = 0,
  328. // 优化retina, 在retina下这个值是2
  329. ratio = window.devicePixelRatio || 1,
  330. // 缩略图大小
  331. thumbnailWidth = 113 * ratio,
  332. thumbnailHeight = 113 * ratio,
  333. // 可能有pedding, ready, uploading, confirm, done.
  334. state = '',
  335. // 所有文件的进度信息,key为file id
  336. percentages = {},
  337. supportTransition = (function () {
  338. var s = document.createElement('p').style,
  339. r = 'transition' in s ||
  340. 'WebkitTransition' in s ||
  341. 'MozTransition' in s ||
  342. 'msTransition' in s ||
  343. 'OTransition' in s;
  344. s = null;
  345. return r;
  346. })(),
  347. // WebUploader实例
  348. uploader,
  349. actionUrl = editor.getActionUrl(editor.getOpt('videoActionName')),
  350. fileMaxSize = editor.getOpt('videoMaxSize'),
  351. acceptExtensions = (editor.getOpt('videoAllowFiles') || []).join('').replace(/\./g, ',').replace(/^[,]/, '');;
  352. if (!WebUploader.Uploader.support()) {
  353. $('#filePickerReady').after($('<div>').html(lang.errorNotSupport)).hide();
  354. return;
  355. } else if (!editor.getOpt('videoActionName')) {
  356. $('#filePickerReady').after($('<div>').html(lang.errorLoadConfig)).hide();
  357. return;
  358. }
  359. uploader = _this.uploader = WebUploader.create({
  360. pick: {
  361. id: '#filePickerReady',
  362. label: lang.uploadSelectFile
  363. },
  364. swf: '../../third-party/webuploader/Uploader.swf',
  365. server: actionUrl,
  366. fileVal: editor.getOpt('videoFieldName'),
  367. duplicate: true,
  368. fileSingleSizeLimit: fileMaxSize,
  369. compress: false
  370. });
  371. uploader.addButton({
  372. id: '#filePickerBlock'
  373. });
  374. uploader.addButton({
  375. id: '#filePickerBtn',
  376. label: lang.uploadAddFile
  377. });
  378. setState('pedding');
  379. // 当有文件添加进来时执行,负责view的创建
  380. function addFile(file) {
  381. var $li = $('<li id="' + file.id + '">' +
  382. '<p class="title">' + file.name + '</p>' +
  383. '<p class="imgWrap"></p>' +
  384. '<p class="progress"><span></span></p>' +
  385. '</li>'),
  386. $btns = $('<div class="file-panel">' +
  387. '<span class="cancel">' + lang.uploadDelete + '</span>' +
  388. '<span class="rotateRight">' + lang.uploadTurnRight + '</span>' +
  389. '<span class="rotateLeft">' + lang.uploadTurnLeft + '</span></div>').appendTo($li),
  390. $prgress = $li.find('p.progress span'),
  391. $wrap = $li.find('p.imgWrap'),
  392. $info = $('<p class="error"></p>').hide().appendTo($li),
  393. showError = function (code) {
  394. switch (code) {
  395. case 'exceed_size':
  396. text = lang.errorExceedSize;
  397. break;
  398. case 'interrupt':
  399. text = lang.errorInterrupt;
  400. break;
  401. case 'http':
  402. text = lang.errorHttp;
  403. break;
  404. case 'not_allow_type':
  405. text = lang.errorFileType;
  406. break;
  407. default:
  408. text = lang.errorUploadRetry;
  409. break;
  410. }
  411. $info.text(text).show();
  412. };
  413. if (file.getStatus() === 'invalid') {
  414. showError(file.statusText);
  415. } else {
  416. $wrap.text(lang.uploadPreview);
  417. if ('|png|jpg|jpeg|bmp|gif|'.indexOf('|'+file.ext.toLowerCase()+'|') == -1) {
  418. $wrap.empty().addClass('notimage').append('<i class="file-preview file-type-' + file.ext.toLowerCase() + '"></i>' +
  419. '<span class="file-title">' + file.name + '</span>');
  420. } else {
  421. if (browser.ie && browser.version <= 7) {
  422. $wrap.text(lang.uploadNoPreview);
  423. } else {
  424. uploader.makeThumb(file, function (error, src) {
  425. if (error || !src || (/^data:/.test(src) && browser.ie && browser.version <= 7)) {
  426. $wrap.text(lang.uploadNoPreview);
  427. } else {
  428. var $img = $('<img src="' + src + '">');
  429. $wrap.empty().append($img);
  430. $img.on('error', function () {
  431. $wrap.text(lang.uploadNoPreview);
  432. });
  433. }
  434. }, thumbnailWidth, thumbnailHeight);
  435. }
  436. }
  437. percentages[ file.id ] = [ file.size, 0 ];
  438. file.rotation = 0;
  439. /* 检查文件格式 */
  440. if (!file.ext || acceptExtensions.indexOf(file.ext.toLowerCase()) == -1) {
  441. showError('not_allow_type');
  442. uploader.removeFile(file);
  443. }
  444. }
  445. file.on('statuschange', function (cur, prev) {
  446. if (prev === 'progress') {
  447. $prgress.hide().width(0);
  448. } else if (prev === 'queued') {
  449. $li.off('mouseenter mouseleave');
  450. $btns.remove();
  451. }
  452. // 成功
  453. if (cur === 'error' || cur === 'invalid') {
  454. showError(file.statusText);
  455. percentages[ file.id ][ 1 ] = 1;
  456. } else if (cur === 'interrupt') {
  457. showError('interrupt');
  458. } else if (cur === 'queued') {
  459. percentages[ file.id ][ 1 ] = 0;
  460. } else if (cur === 'progress') {
  461. $info.hide();
  462. $prgress.css('display', 'block');
  463. } else if (cur === 'complete') {
  464. }
  465. $li.removeClass('state-' + prev).addClass('state-' + cur);
  466. });
  467. $li.on('mouseenter', function () {
  468. $btns.stop().animate({height: 30});
  469. });
  470. $li.on('mouseleave', function () {
  471. $btns.stop().animate({height: 0});
  472. });
  473. $btns.on('click', 'span', function () {
  474. var index = $(this).index(),
  475. deg;
  476. switch (index) {
  477. case 0:
  478. uploader.removeFile(file);
  479. return;
  480. case 1:
  481. file.rotation += 90;
  482. break;
  483. case 2:
  484. file.rotation -= 90;
  485. break;
  486. }
  487. if (supportTransition) {
  488. deg = 'rotate(' + file.rotation + 'deg)';
  489. $wrap.css({
  490. '-webkit-transform': deg,
  491. '-mos-transform': deg,
  492. '-o-transform': deg,
  493. 'transform': deg
  494. });
  495. } else {
  496. $wrap.css('filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + (~~((file.rotation / 90) % 4 + 4) % 4) + ')');
  497. }
  498. });
  499. $li.insertBefore($filePickerBlock);
  500. }
  501. // 负责view的销毁
  502. function removeFile(file) {
  503. var $li = $('#' + file.id);
  504. delete percentages[ file.id ];
  505. updateTotalProgress();
  506. $li.off().find('.file-panel').off().end().remove();
  507. }
  508. function updateTotalProgress() {
  509. var loaded = 0,
  510. total = 0,
  511. spans = $progress.children(),
  512. percent;
  513. $.each(percentages, function (k, v) {
  514. total += v[ 0 ];
  515. loaded += v[ 0 ] * v[ 1 ];
  516. });
  517. percent = total ? loaded / total : 0;
  518. spans.eq(0).text(Math.round(percent * 100) + '%');
  519. spans.eq(1).css('width', Math.round(percent * 100) + '%');
  520. updateStatus();
  521. }
  522. function setState(val, files) {
  523. if (val != state) {
  524. var stats = uploader.getStats();
  525. $upload.removeClass('state-' + state);
  526. $upload.addClass('state-' + val);
  527. switch (val) {
  528. /* 未选择文件 */
  529. case 'pedding':
  530. $queue.addClass('element-invisible');
  531. $statusBar.addClass('element-invisible');
  532. $placeHolder.removeClass('element-invisible');
  533. $progress.hide(); $info.hide();
  534. uploader.refresh();
  535. break;
  536. /* 可以开始上传 */
  537. case 'ready':
  538. $placeHolder.addClass('element-invisible');
  539. $queue.removeClass('element-invisible');
  540. $statusBar.removeClass('element-invisible');
  541. $progress.hide(); $info.show();
  542. $upload.text(lang.uploadStart);
  543. uploader.refresh();
  544. break;
  545. /* 上传中 */
  546. case 'uploading':
  547. $progress.show(); $info.hide();
  548. $upload.text(lang.uploadPause);
  549. break;
  550. /* 暂停上传 */
  551. case 'paused':
  552. $progress.show(); $info.hide();
  553. $upload.text(lang.uploadContinue);
  554. break;
  555. case 'confirm':
  556. $progress.show(); $info.hide();
  557. $upload.text(lang.uploadStart);
  558. stats = uploader.getStats();
  559. if (stats.successNum && !stats.uploadFailNum) {
  560. setState('finish');
  561. return;
  562. }
  563. break;
  564. case 'finish':
  565. $progress.hide(); $info.show();
  566. if (stats.uploadFailNum) {
  567. $upload.text(lang.uploadRetry);
  568. } else {
  569. $upload.text(lang.uploadStart);
  570. }
  571. break;
  572. }
  573. state = val;
  574. updateStatus();
  575. }
  576. if (!_this.getQueueCount()) {
  577. $upload.addClass('disabled')
  578. } else {
  579. $upload.removeClass('disabled')
  580. }
  581. }
  582. function updateStatus() {
  583. var text = '', stats;
  584. if (state === 'ready') {
  585. text = lang.updateStatusReady.replace('_', fileCount).replace('_KB', WebUploader.formatSize(fileSize));
  586. } else if (state === 'confirm') {
  587. stats = uploader.getStats();
  588. if (stats.uploadFailNum) {
  589. text = lang.updateStatusConfirm.replace('_', stats.successNum).replace('_', stats.successNum);
  590. }
  591. } else {
  592. stats = uploader.getStats();
  593. text = lang.updateStatusFinish.replace('_', fileCount).
  594. replace('_KB', WebUploader.formatSize(fileSize)).
  595. replace('_', stats.successNum);
  596. if (stats.uploadFailNum) {
  597. text += lang.updateStatusError.replace('_', stats.uploadFailNum);
  598. }
  599. }
  600. $info.html(text);
  601. }
  602. uploader.on('fileQueued', function (file) {
  603. fileCount++;
  604. fileSize += file.size;
  605. if (fileCount === 1) {
  606. $placeHolder.addClass('element-invisible');
  607. $statusBar.show();
  608. }
  609. addFile(file);
  610. });
  611. uploader.on('fileDequeued', function (file) {
  612. fileCount--;
  613. fileSize -= file.size;
  614. removeFile(file);
  615. updateTotalProgress();
  616. });
  617. uploader.on('filesQueued', function (file) {
  618. if (!uploader.isInProgress() && (state == 'pedding' || state == 'finish' || state == 'confirm' || state == 'ready')) {
  619. setState('ready');
  620. }
  621. updateTotalProgress();
  622. });
  623. uploader.on('all', function (type, files) {
  624. switch (type) {
  625. case 'uploadFinished':
  626. setState('confirm', files);
  627. break;
  628. case 'startUpload':
  629. /* 添加额外的GET参数 */
  630. var params = utils.serializeParam(editor.queryCommandValue('serverparam')) || '',
  631. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?':'&') + 'encode=utf-8&' + params);
  632. uploader.option('server', url);
  633. setState('uploading', files);
  634. break;
  635. case 'stopUpload':
  636. setState('paused', files);
  637. break;
  638. }
  639. });
  640. uploader.on('uploadBeforeSend', function (file, data, header) {
  641. //这里可以通过data对象添加POST参数
  642. header['X_Requested_With'] = 'XMLHttpRequest';
  643. });
  644. uploader.on('uploadProgress', function (file, percentage) {
  645. var $li = $('#' + file.id),
  646. $percent = $li.find('.progress span');
  647. $percent.css('width', percentage * 100 + '%');
  648. percentages[ file.id ][ 1 ] = percentage;
  649. updateTotalProgress();
  650. });
  651. uploader.on('uploadSuccess', function (file, ret) {
  652. var $file = $('#' + file.id);
  653. try {
  654. var responseText = (ret._raw || ret),
  655. json = utils.str2json(responseText);
  656. if (json.state == 'SUCCESS') {
  657. uploadVideoList.push({
  658. 'url': json.url,
  659. 'type': json.type,
  660. 'original':json.original
  661. });
  662. $file.append('<span class="success"></span>');
  663. } else {
  664. $file.find('.error').text(json.state).show();
  665. }
  666. } catch (e) {
  667. $file.find('.error').text(lang.errorServerUpload).show();
  668. }
  669. });
  670. uploader.on('uploadError', function (file, code) {
  671. });
  672. uploader.on('error', function (code, file) {
  673. if (code == 'Q_TYPE_DENIED' || code == 'F_EXCEED_SIZE') {
  674. addFile(file);
  675. }
  676. });
  677. uploader.on('uploadComplete', function (file, ret) {
  678. });
  679. $upload.on('click', function () {
  680. if ($(this).hasClass('disabled')) {
  681. return false;
  682. }
  683. if (state === 'ready') {
  684. uploader.upload();
  685. } else if (state === 'paused') {
  686. uploader.upload();
  687. } else if (state === 'uploading') {
  688. uploader.stop();
  689. }
  690. });
  691. $upload.addClass('state-' + state);
  692. updateTotalProgress();
  693. },
  694. getQueueCount: function () {
  695. var file, i, status, readyFile = 0, files = this.uploader.getFiles();
  696. for (i = 0; file = files[i++]; ) {
  697. status = file.getStatus();
  698. if (status == 'queued' || status == 'uploading' || status == 'progress') readyFile++;
  699. }
  700. return readyFile;
  701. },
  702. refresh: function(){
  703. this.uploader.refresh();
  704. }
  705. };
  706. })();