video.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. editor.setOpt({
  10. videoFieldName:"upfile"
  11. });
  12. var video = {},
  13. uploadVideoList = [],
  14. isModifyUploadVideo = false;
  15. window.onload = function(){
  16. $focus($G("videoUrl"));
  17. initTabs();
  18. initVideo();
  19. initUpload();
  20. };
  21. /* 初始化tab标签 */
  22. function initTabs(){
  23. var tabs = $G('tabHeads').children;
  24. for (var i = 0; i < tabs.length; i++) {
  25. domUtils.on(tabs[i], "click", function (e) {
  26. var target = e.target || e.srcElement;
  27. for (var j = 0; j < tabs.length; j++) {
  28. if(tabs[j] == target){
  29. tabs[j].className = "focus";
  30. $G(tabs[j].getAttribute('data-content-id')).style.display = "block";
  31. }else {
  32. tabs[j].className = "";
  33. $G(tabs[j].getAttribute('data-content-id')).style.display = "none";
  34. }
  35. }
  36. });
  37. }
  38. }
  39. function initVideo(){
  40. createAlignButton( ["videoFloat", "upload_alignment"] );
  41. addUrlChangeListener($G("videoUrl"));
  42. addOkListener();
  43. //编辑视频时初始化相关信息
  44. (function(){
  45. var img = editor.selection.getRange().getClosedNode(),url;
  46. if(img && img.className){
  47. var hasFakedClass = (img.className == "edui-faked-video"),
  48. hasUploadClass = img.className.indexOf("edui-upload-video")!=-1;
  49. if(hasFakedClass || hasUploadClass) {
  50. $G("videoUrl").value = url = img.getAttribute("_url");
  51. $G("videoWidth").value = img.width;
  52. $G("videoHeight").value = img.height;
  53. var align = domUtils.getComputedStyle(img,"float"),
  54. parentAlign = domUtils.getComputedStyle(img.parentNode,"text-align");
  55. updateAlignButton(parentAlign==="center"?"center":align);
  56. }
  57. if(hasUploadClass) {
  58. isModifyUploadVideo = true;
  59. }
  60. }
  61. createPreviewVideo(url);
  62. })();
  63. };
  64. /**
  65. * 监听确认和取消两个按钮事件,用户执行插入或者清空正在播放的视频实例操作
  66. */
  67. function addOkListener(){
  68. dialog.onok = function(){
  69. $G("preview").innerHTML = "";
  70. var currentTab = findFocus("tabHeads","tabSrc");
  71. switch(currentTab){
  72. case "video":
  73. return insertSingle();
  74. break;
  75. case "videoSearch":
  76. return insertSearch("searchList");
  77. break;
  78. case "upload":
  79. return insertUpload();
  80. break;
  81. }
  82. };
  83. dialog.oncancel = function(){
  84. $G("preview").innerHTML = "";
  85. };
  86. }
  87. function selectTxt(node){
  88. if(node.select){
  89. node.select();
  90. }else{
  91. var r = node.createTextRange && node.createTextRange();
  92. r.select();
  93. }
  94. }
  95. /**
  96. * 依据传入的align值更新按钮信息
  97. * @param align
  98. */
  99. function updateAlignButton( align ) {
  100. var aligns = $G( "videoFloat" ).children;
  101. for ( var i = 0, ci; ci = aligns[i++]; ) {
  102. if ( ci.getAttribute( "name" ) == align ) {
  103. if ( ci.className !="focus" ) {
  104. ci.className = "focus";
  105. }
  106. } else {
  107. if ( ci.className =="focus" ) {
  108. ci.className = "";
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * 将单个视频信息插入编辑器中
  115. */
  116. function insertSingle(){
  117. var width = $G("videoWidth"),
  118. height = $G("videoHeight"),
  119. url=$G('videoUrl').value,
  120. align = findFocus("videoFloat","name");
  121. if(!url) return false;
  122. if ( !checkNum( [width, height] ) ) return false;
  123. editor.execCommand('insertvideo', {
  124. url: convert_url(url),
  125. width: width.value,
  126. height: height.value,
  127. align: align
  128. }, isModifyUploadVideo ? 'upload':null);
  129. }
  130. /**
  131. * 将元素id下的所有代表视频的图片插入编辑器中
  132. * @param id
  133. */
  134. function insertSearch(id){
  135. var imgs = domUtils.getElementsByTagName($G(id),"img"),
  136. videoObjs=[];
  137. for(var i=0,img; img=imgs[i++];){
  138. if(img.getAttribute("selected")){
  139. videoObjs.push({
  140. url:img.getAttribute("ue_video_url"),
  141. width:420,
  142. height:280,
  143. align:"none"
  144. });
  145. }
  146. }
  147. editor.execCommand('insertvideo',videoObjs);
  148. }
  149. /**
  150. * 找到id下具有focus类的节点并返回该节点下的某个属性
  151. * @param id
  152. * @param returnProperty
  153. */
  154. function findFocus( id, returnProperty ) {
  155. var tabs = $G( id ).children,
  156. property;
  157. for ( var i = 0, ci; ci = tabs[i++]; ) {
  158. if ( ci.className=="focus" ) {
  159. property = ci.getAttribute( returnProperty );
  160. break;
  161. }
  162. }
  163. return property;
  164. }
  165. function convert_url(s){
  166. return s.replace(/http:\/\/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i,"http://www.tudou.com/v/$1")
  167. .replace(/http:\/\/www\.youtube\.com\/watch\?v=([\w\-]+)/i,"http://www.youtube.com/v/$1")
  168. .replace(/http:\/\/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i,"http://player.youku.com/player.php/sid/$1")
  169. .replace(/http:\/\/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "http://player.56.com/v_$1.swf")
  170. .replace(/http:\/\/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "http://player.56.com/v_$1.swf")
  171. .replace(/http:\/\/v\.ku6\.com\/.+\/([^.]+)\.html/i, "http://player.ku6.com/refer/$1/v.swf");
  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 matches = url.match(/youtu.be\/(\w+)$/) || url.match(/youtube\.com\/watch\?v=(\w+)/) || url.match(/youtube.com\/v\/(\w+)/),
  253. youku = url.match(/youku\.com\/v_show\/id_(\w+)/),
  254. youkuPlay = /player\.youku\.com/ig.test(url);
  255. if(!youkuPlay){
  256. if (matches){
  257. url = "https://www.youtube.com/v/" + matches[1] + "?version=3&feature=player_embedded";
  258. }else if(youku){
  259. url = "http://player.youku.com/player.php/sid/"+youku[1]+"/v.swf"
  260. }else if(!endWith(url,[".swf",".flv",".wmv"])){
  261. $G("preview").innerHTML = lang.urlError;
  262. return;
  263. }
  264. }else{
  265. url = url.replace(/\?f=.*/,"");
  266. }
  267. $G("preview").innerHTML = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  268. ' src="' + url + '"' +
  269. ' width="' + 420 + '"' +
  270. ' height="' + 280 + '"' +
  271. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" ></embed>';
  272. }
  273. /**
  274. * 末尾字符检测
  275. * @param str
  276. * @param endStrArr
  277. */
  278. function endWith(str,endStrArr){
  279. for(var i=0,len = endStrArr.length;i<len;i++){
  280. var tmp = endStrArr[i];
  281. if(str.length - tmp.length<0) return false;
  282. if(str.substring(str.length-tmp.length)==tmp){
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. /**
  289. * ajax获取视频信息
  290. */
  291. function getMovie(){
  292. var keywordInput = $G("videoSearchTxt");
  293. if(!keywordInput.getAttribute("hasClick") ||!keywordInput.value){
  294. selectTxt(keywordInput);
  295. return;
  296. }
  297. $G( "searchList" ).innerHTML = lang.loading;
  298. var keyword = keywordInput.value,
  299. type = $G("videoType").value,
  300. str="";
  301. ajax.request(editor.options.getMovieUrl,{
  302. searchKey:keyword,
  303. videoType:type,
  304. onsuccess:function(xhr){
  305. try{
  306. var info = eval("("+xhr.responseText+")");
  307. }catch(e){
  308. return;
  309. }
  310. var videos = info.multiPageResult.results;
  311. var html=["<table width='530'>"];
  312. for(var i=0,ci;ci = videos[i++];){
  313. html.push(
  314. "<tr>" +
  315. "<td><img title='"+lang.clickToSelect+"' ue_video_url='"+ci.outerPlayerUrl+"' alt='"+ci.tags+"' width='106' height='80' src='"+ci.picUrl+"' /> </td>" +
  316. "<td>" +
  317. "<p><a target='_blank' title='"+lang.goToSource+"' href='"+ci.itemUrl+"'>"+ci.title.substr(0,30)+"</a></p>" +
  318. "<p style='height: 62px;line-height: 20px' title='"+ci.description+"'> "+ ci.description.substr(0,95) +" </p>" +
  319. "</td>" +
  320. "</tr>"
  321. );
  322. }
  323. html.push("</table>");
  324. $G("searchList").innerHTML = str = html.length ==2 ?lang.noVideo : html.join("");
  325. var imgs = domUtils.getElementsByTagName($G("searchList"),"img");
  326. if(!imgs)return;
  327. for(var i=0,img;img = imgs[i++];){
  328. domUtils.on(img,"click",function(){
  329. changeSelected(this);
  330. })
  331. }
  332. }
  333. });
  334. }
  335. /**
  336. * 改变对象o的选中状态
  337. * @param o
  338. */
  339. function changeSelected(o){
  340. if ( o.getAttribute( "selected" ) ) {
  341. o.removeAttribute( "selected" );
  342. o.style.cssText = "filter:alpha(Opacity=100);-moz-opacity:1;opacity: 1;border: 2px solid #fff";
  343. } else {
  344. o.setAttribute( "selected", "true" );
  345. o.style.cssText = "filter:alpha(Opacity=50);-moz-opacity:0.5;opacity: 0.5;border:2px solid blue;";
  346. }
  347. }
  348. /* 插入上传视频 */
  349. function insertUpload(){
  350. var videoObjs=[],
  351. uploadDir = editor.options.videoPath,
  352. width = $G('upload_width').value || 420,
  353. height = $G('upload_height').value || 280,
  354. align = findFocus("upload_alignment","name") || 'none';
  355. for(var key in uploadVideoList) {
  356. var file = uploadVideoList[key];
  357. videoObjs.push({
  358. url: uploadDir + file.url,
  359. width:width,
  360. height:height,
  361. align:align
  362. });
  363. }
  364. editor.execCommand('insertvideo', videoObjs, 'upload');
  365. }
  366. /*初始化上传标签*/
  367. function initUpload(){
  368. var settings = {
  369. upload_url:editor.options.videoUrl, //附件上传服务器地址
  370. file_post_name:editor.options.videoFieldName, //向后台提交的表单名
  371. flash_url:"../../third-party/swfupload/swfupload.swf",
  372. flash9_url:"../../third-party/swfupload/swfupload_fp9.swf",
  373. post_params:{"PHPSESSID":"<?php echo session_id(); ?>","fileNameFormat":editor.options.fileNameFormat}, //解决session丢失问题
  374. file_size_limit:"100 MB", //文件大小限制,此处仅是前端flash选择时候的限制,具体还需要和后端结合判断
  375. file_types:"*.*", //允许的扩展名,多个扩展名之间用分号隔开,支持*通配符
  376. file_types_description:"Video Files", //扩展名描述
  377. file_upload_limit:100, //单次可同时上传的文件数目
  378. file_queue_limit:10, //队列中可同时上传的文件数目
  379. custom_settings:{ //自定义设置,用户可在此向服务器传递自定义变量
  380. progressTarget:"fsUploadProgress",
  381. startUploadId:"startUpload"
  382. },
  383. debug:false,
  384. // 按钮设置
  385. button_image_url:"../../themes/default/images/filescan.png",
  386. button_width:"100",
  387. button_height:"25",
  388. button_placeholder_id:"spanButtonPlaceHolder",
  389. button_text:'<span class="theFont">'+lang.browseFiles+'</span>',
  390. button_text_style:".theFont { font-size:14px;}",
  391. button_text_left_padding:10,
  392. button_text_top_padding:4,
  393. // 所有回调函数
  394. swfupload_preload_handler:preLoad,
  395. swfupload_load_failed_handler:loadFailed,
  396. file_queued_handler:fileQueued,
  397. file_queue_error_handler:fileQueueError,
  398. //选择文件完成回调
  399. file_dialog_complete_handler:function(numFilesSelected, numFilesQueued) {
  400. var me = this; //此处的this是swfupload对象
  401. if (numFilesQueued > 0) {
  402. dialog.buttons[0].setDisabled(true);
  403. var start = $G(this.customSettings.startUploadId);
  404. start.style.display = "";
  405. start.onclick = function(){
  406. me.startUpload();
  407. start.style.display = "none";
  408. }
  409. }
  410. },
  411. upload_start_handler:uploadStart,
  412. upload_progress_handler:uploadProgress,
  413. upload_error_handler:uploadError,
  414. upload_success_handler:function (file, serverData) {
  415. try{
  416. var info = eval("("+serverData+")");
  417. }catch(e){}
  418. var progress = new FileProgress(file, this.customSettings.progressTarget);
  419. if(info.state=="SUCCESS"){
  420. progress.setComplete();
  421. progress.setStatus("<span style='color: #0b0;font-weight: bold'>"+lang.uploadSuccess+"</span>");
  422. uploadVideoList.push({url:info.url,type:info.fileType,original:info.original});
  423. progress.toggleCancel(true,this,lang.delSuccessFile);
  424. }else{
  425. progress.setError();
  426. progress.setStatus(info.state);
  427. progress.toggleCancel(true,this,lang.delFailSaveFile);
  428. }
  429. },
  430. //上传完成回调
  431. upload_complete_handler:uploadComplete,
  432. //队列完成回调
  433. queue_complete_handler:function(numFilesUploaded){
  434. dialog.buttons[0].setDisabled(false);
  435. // var status = $G("divStatus");
  436. // var num = status.innerHTML.match(/\d+/g);
  437. // status.innerHTML = ((num && num[0] ?parseInt(num[0]):0) + numFilesUploaded) +lang.statusPrompt;
  438. }
  439. };
  440. var swfupload = new SWFUpload( settings );
  441. };
  442. })();