video.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. var video = {};
  9. (function(){
  10. video.init = function(){
  11. switchTab("videoTab");
  12. createAlignButton( ["videoFloat"] );
  13. addUrlChangeListener($G("videoUrl"));
  14. addOkListener();
  15. addSearchListener();
  16. //编辑视频时初始化相关信息
  17. (function(){
  18. var img = editor.selection.getRange().getClosedNode(),url;
  19. if(img && img.className == "edui-faked-video"){
  20. $G("videoUrl").value = url = img.getAttribute("_url");
  21. $G("videoWidth").value = img.width;
  22. $G("videoHeight").value = img.height;
  23. var align = domUtils.getComputedStyle(img,"float"),
  24. parentAlign = domUtils.getComputedStyle(img.parentNode,"text-align");
  25. updateAlignButton(parentAlign==="center"?"center":align);
  26. }
  27. createPreviewVideo(url);
  28. })();
  29. };
  30. /**
  31. * 监听确认和取消两个按钮事件,用户执行插入或者清空正在播放的视频实例操作
  32. */
  33. function addOkListener(){
  34. dialog.onok = function(){
  35. $G("preview").innerHTML = "";
  36. var currentTab = findFocus("tabHeads","tabSrc");
  37. switch(currentTab){
  38. case "video":
  39. return insertSingle();
  40. break;
  41. case "videoSearch":
  42. return insertSearch("searchList");
  43. break;
  44. }
  45. };
  46. dialog.oncancel = function(){
  47. $G("preview").innerHTML = "";
  48. };
  49. }
  50. function selectTxt(node){
  51. if(node.select){
  52. node.select();
  53. }else{
  54. var r = node.createTextRange && node.createTextRange();
  55. r.select();
  56. }
  57. }
  58. /**
  59. * 依据传入的align值更新按钮信息
  60. * @param align
  61. */
  62. function updateAlignButton( align ) {
  63. var aligns = $G( "videoFloat" ).children;
  64. for ( var i = 0, ci; ci = aligns[i++]; ) {
  65. if ( ci.getAttribute( "name" ) == align ) {
  66. if ( ci.className !="focus" ) {
  67. ci.className = "focus";
  68. }
  69. } else {
  70. if ( ci.className =="focus" ) {
  71. ci.className = "";
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * 将单个视频信息插入编辑器中
  78. */
  79. function insertSingle(){
  80. var width = $G("videoWidth"),
  81. height = $G("videoHeight"),
  82. url=$G('videoUrl').value,
  83. align = findFocus("videoFloat","name");
  84. if(!url) return false;
  85. if ( !checkNum( [width, height] ) ) return false;
  86. editor.execCommand('insertvideo', {
  87. url: convert_url(url),
  88. width: width.value,
  89. height: height.value,
  90. align: align
  91. });
  92. }
  93. /**
  94. * 将元素id下的所有代表视频的图片插入编辑器中
  95. * @param id
  96. */
  97. function insertSearch(id){
  98. var imgs = domUtils.getElementsByTagName($G(id),"img"),
  99. videoObjs=[];
  100. for(var i=0,img; img=imgs[i++];){
  101. if(img.getAttribute("selected")){
  102. videoObjs.push({
  103. url:img.getAttribute("ue_video_url"),
  104. width:420,
  105. height:280,
  106. align:"none"
  107. });
  108. }
  109. }
  110. editor.execCommand('insertvideo',videoObjs);
  111. }
  112. /**
  113. * 找到id下具有focus类的节点并返回该节点下的某个属性
  114. * @param id
  115. * @param returnProperty
  116. */
  117. function findFocus( id, returnProperty ) {
  118. var tabs = $G( id ).children,
  119. property;
  120. for ( var i = 0, ci; ci = tabs[i++]; ) {
  121. if ( ci.className=="focus" ) {
  122. property = ci.getAttribute( returnProperty );
  123. break;
  124. }
  125. }
  126. return property;
  127. }
  128. function convert_url(s){
  129. return s.replace(/http:\/\/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i,"http://www.tudou.com/v/$1")
  130. .replace(/http:\/\/www\.youtube\.com\/watch\?v=([\w\-]+)/i,"http://www.youtube.com/v/$1")
  131. .replace(/http:\/\/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i,"http://player.youku.com/player.php/sid/$1")
  132. .replace(/http:\/\/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "http://player.56.com/v_$1.swf")
  133. .replace(/http:\/\/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "http://player.56.com/v_$1.swf")
  134. .replace(/http:\/\/v\.ku6\.com\/.+\/([^.]+)\.html/i, "http://player.ku6.com/refer/$1/v.swf");
  135. }
  136. /**
  137. * 检测传入的所有input框中输入的长宽是否是正数
  138. * @param nodes input框集合,
  139. */
  140. function checkNum( nodes ) {
  141. for ( var i = 0, ci; ci = nodes[i++]; ) {
  142. var value = ci.value;
  143. if ( !isNumber( value ) && value) {
  144. alert( lang.numError );
  145. ci.value = "";
  146. ci.focus();
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. /**
  153. * 数字判断
  154. * @param value
  155. */
  156. function isNumber( value ) {
  157. return /(0|^[1-9]\d*$)/.test( value );
  158. }
  159. /**
  160. * tab切换
  161. * @param tabParentId
  162. * @param keepFocus 当此值为真时,切换按钮上会保留focus的样式
  163. */
  164. function switchTab( tabParentId,keepFocus ) {
  165. var tabElements = $G( tabParentId ).children,
  166. tabHeads = tabElements[0].children,
  167. tabBodys = tabElements[1].children;
  168. for ( var i = 0, length = tabHeads.length; i < length; i++ ) {
  169. var head = tabHeads[i];
  170. domUtils.on( head, "click", function () {
  171. //head样式更改
  172. for ( var k = 0, len = tabHeads.length; k < len; k++ ) {
  173. if(!keepFocus)tabHeads[k].className = "";
  174. }
  175. this.className = "focus";
  176. //body显隐
  177. var tabSrc = this.getAttribute( "tabSrc" );
  178. for ( var j = 0, length = tabBodys.length; j < length; j++ ) {
  179. var body = tabBodys[j],
  180. id = body.getAttribute( "id" );
  181. if ( id == tabSrc ) {
  182. body.style.display = "";
  183. if(id=="videoSearch"){
  184. selectTxt($G("videoSearchTxt"));
  185. }
  186. if(id=="video"){
  187. selectTxt($G("videoUrl"));
  188. }
  189. } else {
  190. body.style.display = "none";
  191. }
  192. }
  193. } );
  194. }
  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. * 视频搜索相关注册事件
  350. */
  351. function addSearchListener(){
  352. domUtils.on($G("videoSearchBtn"),"click",getMovie);
  353. domUtils.on($G( "videoSearchTxt" ),"click",function () {
  354. if ( this.value == lang.static.videoSearchTxt.value ) {
  355. this.value = "";
  356. }
  357. this.setAttribute("hasClick","true");
  358. selectTxt(this);
  359. });
  360. $G( "videoSearchTxt" ).onkeyup = function(){
  361. this.setAttribute("hasClick","true");
  362. this.onkeyup = null;
  363. };
  364. domUtils.on($G( "videoSearchReset" ),"click",function () {
  365. var txt = $G( "videoSearchTxt" );
  366. txt.value = "";
  367. selectTxt(txt);
  368. $G( "searchList" ).innerHTML = "";
  369. });
  370. domUtils.on($G( "videoType" ),"change", getMovie);
  371. domUtils.on($G( "videoSearchTxt" ), "keyup", function ( evt ) {
  372. if ( evt.keyCode == 13 ) {
  373. getMovie();
  374. }
  375. } )
  376. }
  377. })();