video.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. $G("preview").innerHTML = '<video class="previewVideo" src="' + url + '" width="420" height="280" controls="controls"></video>';
  253. }
  254. /**
  255. * 末尾字符检测
  256. * @param str
  257. * @param endStrArr
  258. */
  259. function endWith(str,endStrArr){
  260. for(var i=0,len = endStrArr.length;i<len;i++){
  261. var tmp = endStrArr[i];
  262. if(str.length - tmp.length<0) return false;
  263. if(str.substring(str.length-tmp.length)==tmp){
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. /**
  270. * ajax获取视频信息
  271. */
  272. function getMovie(){
  273. var keywordInput = $G("videoSearchTxt");
  274. if(!keywordInput.getAttribute("hasClick") ||!keywordInput.value){
  275. selectTxt(keywordInput);
  276. return;
  277. }
  278. $G( "searchList" ).innerHTML = lang.loading;
  279. var keyword = keywordInput.value,
  280. type = $G("videoType").value,
  281. str="";
  282. ajax.request(editor.options.getMovieUrl,{
  283. searchKey:keyword,
  284. videoType:type,
  285. onsuccess:function(xhr){
  286. try{
  287. var info = eval("("+xhr.responseText+")");
  288. }catch(e){
  289. return;
  290. }
  291. var videos = info.multiPageResult.results;
  292. var html=["<table width='530'>"];
  293. for(var i=0,ci;ci = videos[i++];){
  294. html.push(
  295. "<tr>" +
  296. "<td><img title='"+lang.clickToSelect+"' ue_video_url='"+ci.outerPlayerUrl+"' alt='"+ci.tags+"' width='106' height='80' src='"+ci.picUrl+"' /> </td>" +
  297. "<td>" +
  298. "<p><a target='_blank' title='"+lang.goToSource+"' href='"+ci.itemUrl+"'>"+ci.title.substr(0,30)+"</a></p>" +
  299. "<p style='height: 62px;line-height: 20px' title='"+ci.description+"'> "+ ci.description.substr(0,95) +" </p>" +
  300. "</td>" +
  301. "</tr>"
  302. );
  303. }
  304. html.push("</table>");
  305. $G("searchList").innerHTML = str = html.length ==2 ?lang.noVideo : html.join("");
  306. var imgs = domUtils.getElementsByTagName($G("searchList"),"img");
  307. if(!imgs)return;
  308. for(var i=0,img;img = imgs[i++];){
  309. domUtils.on(img,"click",function(){
  310. changeSelected(this);
  311. })
  312. }
  313. }
  314. });
  315. }
  316. /**
  317. * 改变对象o的选中状态
  318. * @param o
  319. */
  320. function changeSelected(o){
  321. if ( o.getAttribute( "selected" ) ) {
  322. o.removeAttribute( "selected" );
  323. o.style.cssText = "filter:alpha(Opacity=100);-moz-opacity:1;opacity: 1;border: 2px solid #fff";
  324. } else {
  325. o.setAttribute( "selected", "true" );
  326. o.style.cssText = "filter:alpha(Opacity=50);-moz-opacity:0.5;opacity: 0.5;border:2px solid blue;";
  327. }
  328. }
  329. /* 插入上传视频 */
  330. function insertUpload(){
  331. var videoObjs=[],
  332. uploadDir = editor.options.videoPath,
  333. width = $G('upload_width').value || 420,
  334. height = $G('upload_height').value || 280,
  335. align = findFocus("upload_alignment","name") || 'none';
  336. for(var key in uploadVideoList) {
  337. var file = uploadVideoList[key];
  338. videoObjs.push({
  339. url: uploadDir + file.url,
  340. width:width,
  341. height:height,
  342. align:align
  343. });
  344. }
  345. editor.execCommand('insertvideo', videoObjs, 'upload');
  346. }
  347. /*初始化上传标签*/
  348. function initUpload(){
  349. var settings = {
  350. upload_url:editor.options.videoUrl, //附件上传服务器地址
  351. file_post_name:editor.options.videoFieldName, //向后台提交的表单名
  352. flash_url:"../../third-party/swfupload/swfupload.swf",
  353. flash9_url:"../../third-party/swfupload/swfupload_fp9.swf",
  354. post_params:{"PHPSESSID":"<?php echo session_id(); ?>","fileNameFormat":editor.options.fileNameFormat}, //解决session丢失问题
  355. file_size_limit:"100 MB", //文件大小限制,此处仅是前端flash选择时候的限制,具体还需要和后端结合判断
  356. file_types:"*.*", //允许的扩展名,多个扩展名之间用分号隔开,支持*通配符
  357. file_types_description:"Video Files", //扩展名描述
  358. file_upload_limit:100, //单次可同时上传的文件数目
  359. file_queue_limit:10, //队列中可同时上传的文件数目
  360. custom_settings:{ //自定义设置,用户可在此向服务器传递自定义变量
  361. progressTarget:"fsUploadProgress",
  362. startUploadId:"startUpload"
  363. },
  364. debug:false,
  365. // 按钮设置
  366. button_image_url:"../../themes/default/images/filescan.png",
  367. button_width:"100",
  368. button_height:"25",
  369. button_placeholder_id:"spanButtonPlaceHolder",
  370. button_text:'<span class="theFont">'+lang.browseFiles+'</span>',
  371. button_text_style:".theFont { font-size:14px;}",
  372. button_text_left_padding:10,
  373. button_text_top_padding:4,
  374. // 所有回调函数
  375. swfupload_preload_handler:preLoad,
  376. swfupload_load_failed_handler:loadFailed,
  377. file_queued_handler:fileQueued,
  378. file_queue_error_handler:fileQueueError,
  379. //选择文件完成回调
  380. file_dialog_complete_handler:function(numFilesSelected, numFilesQueued) {
  381. var me = this; //此处的this是swfupload对象
  382. if (numFilesQueued > 0) {
  383. dialog.buttons[0].setDisabled(true);
  384. var start = $G(this.customSettings.startUploadId);
  385. start.style.display = "";
  386. start.onclick = function(){
  387. me.startUpload();
  388. start.style.display = "none";
  389. }
  390. }
  391. },
  392. upload_start_handler:uploadStart,
  393. upload_progress_handler:uploadProgress,
  394. upload_error_handler:uploadError,
  395. upload_success_handler:function (file, serverData) {
  396. try{
  397. var info = eval("("+serverData+")");
  398. }catch(e){}
  399. var progress = new FileProgress(file, this.customSettings.progressTarget);
  400. if(info.state=="SUCCESS"){
  401. progress.setComplete();
  402. progress.setStatus("<span style='color: #0b0;font-weight: bold'>"+lang.uploadSuccess+"</span>");
  403. uploadVideoList.push({url:info.url,type:info.fileType,original:info.original});
  404. progress.toggleCancel(true,this,lang.delSuccessFile);
  405. }else{
  406. progress.setError();
  407. progress.setStatus(info.state);
  408. progress.toggleCancel(true,this,lang.delFailSaveFile);
  409. }
  410. },
  411. //上传完成回调
  412. upload_complete_handler:uploadComplete,
  413. //队列完成回调
  414. queue_complete_handler:function(numFilesUploaded){
  415. dialog.buttons[0].setDisabled(false);
  416. // var status = $G("divStatus");
  417. // var num = status.innerHTML.match(/\d+/g);
  418. // status.innerHTML = ((num && num[0] ?parseInt(num[0]):0) + numFilesUploaded) +lang.statusPrompt;
  419. }
  420. };
  421. var swfupload = new SWFUpload( settings );
  422. };
  423. })();