video.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. updateAlignButton(img.getAttribute("align"));
  24. }
  25. createPreviewVideo(url);
  26. })();
  27. };
  28. /**
  29. * 监听确认和取消两个按钮事件,用户执行插入或者清空正在播放的视频实例操作
  30. */
  31. function addOkListener(){
  32. dialog.onok = function(){
  33. $G("preview").innerHTML = "";
  34. var currentTab = findFocus("tabHeads","tabSrc");
  35. switch(currentTab){
  36. case "video":
  37. return insertSingle();
  38. break;
  39. case "videoSearch":
  40. return insertSearch("searchList");
  41. break;
  42. }
  43. };
  44. dialog.oncancel = function(){
  45. $G("preview").innerHTML = "";
  46. };
  47. }
  48. function selectTxt(node){
  49. if(node.select){
  50. node.select();
  51. }else{
  52. var r = node.createTextRange && node.createTextRange();
  53. r.select();
  54. }
  55. }
  56. /**
  57. * 依据传入的align值更新按钮信息
  58. * @param align
  59. */
  60. function updateAlignButton( align ) {
  61. var aligns = $G( "videoFloat" ).children;
  62. for ( var i = 0, ci; ci = aligns[i++]; ) {
  63. if ( ci.getAttribute( "name" ) == align ) {
  64. if ( ci.className !="focus" ) {
  65. ci.className = "focus";
  66. }
  67. } else {
  68. if ( ci.className =="focus" ) {
  69. ci.className = "";
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * 将单个视频信息插入编辑器中
  76. */
  77. function insertSingle(){
  78. var width = $G("videoWidth"),
  79. height = $G("videoHeight"),
  80. url=$G('videoUrl').value,
  81. align = findFocus("videoFloat","name");
  82. if(!url) return false;
  83. if ( !checkNum( [width, height] ) ) return false;
  84. editor.execCommand('insertvideo', {
  85. url: convert_url(url),
  86. width: width.value,
  87. height: height.value,
  88. align: align
  89. });
  90. }
  91. /**
  92. * 将元素id下的所有代表视频的图片插入编辑器中
  93. * @param id
  94. */
  95. function insertSearch(id){
  96. var imgs = domUtils.getElementsByTagName($G(id),"img"),
  97. videoObjs=[];
  98. for(var i=0,img; img=imgs[i++];){
  99. if(img.getAttribute("selected")){
  100. videoObjs.push({
  101. url:img.getAttribute("ue_video_url"),
  102. width:420,
  103. height:280,
  104. align:"none"
  105. });
  106. }
  107. }
  108. editor.execCommand('insertvideo',videoObjs);
  109. }
  110. /**
  111. * 找到id下具有focus类的节点并返回该节点下的某个属性
  112. * @param id
  113. * @param returnProperty
  114. */
  115. function findFocus( id, returnProperty ) {
  116. var tabs = $G( id ).children,
  117. property;
  118. for ( var i = 0, ci; ci = tabs[i++]; ) {
  119. if ( ci.className=="focus" ) {
  120. property = ci.getAttribute( returnProperty );
  121. break;
  122. }
  123. }
  124. return property;
  125. }
  126. function convert_url(s){
  127. return s.replace(/http:\/\/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i,"http://www.tudou.com/v/$1")
  128. .replace(/http:\/\/www\.youtube\.com\/watch\?v=([\w\-]+)/i,"http://www.youtube.com/v/$1")
  129. .replace(/http:\/\/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i,"http://player.youku.com/player.php/sid/$1")
  130. .replace(/http:\/\/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "http://player.56.com/v_$1.swf")
  131. .replace(/http:\/\/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "http://player.56.com/v_$1.swf")
  132. .replace(/http:\/\/v\.ku6\.com\/.+\/([^.]+)\.html/i, "http://player.ku6.com/refer/$1/v.swf");
  133. }
  134. /**
  135. * 检测传入的所有input框中输入的长宽是否是正数
  136. * @param nodes input框集合,
  137. */
  138. function checkNum( nodes ) {
  139. for ( var i = 0, ci; ci = nodes[i++]; ) {
  140. var value = ci.value;
  141. if ( !isNumber( value ) && value) {
  142. alert( lang.numError );
  143. ci.value = "";
  144. ci.focus();
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. /**
  151. * 数字判断
  152. * @param value
  153. */
  154. function isNumber( value ) {
  155. return /(0|^[1-9]\d*$)/.test( value );
  156. }
  157. /**
  158. * tab切换
  159. * @param tabParentId
  160. * @param keepFocus 当此值为真时,切换按钮上会保留focus的样式
  161. */
  162. function switchTab( tabParentId,keepFocus ) {
  163. var tabElements = $G( tabParentId ).children,
  164. tabHeads = tabElements[0].children,
  165. tabBodys = tabElements[1].children;
  166. for ( var i = 0, length = tabHeads.length; i < length; i++ ) {
  167. var head = tabHeads[i];
  168. domUtils.on( head, "click", function () {
  169. //head样式更改
  170. for ( var k = 0, len = tabHeads.length; k < len; k++ ) {
  171. if(!keepFocus)tabHeads[k].className = "";
  172. }
  173. this.className = "focus";
  174. //body显隐
  175. var tabSrc = this.getAttribute( "tabSrc" );
  176. for ( var j = 0, length = tabBodys.length; j < length; j++ ) {
  177. var body = tabBodys[j],
  178. id = body.getAttribute( "id" );
  179. if ( id == tabSrc ) {
  180. body.style.display = "";
  181. if(id=="videoSearch"){
  182. selectTxt($G("videoSearchTxt"));
  183. }
  184. if(id=="video"){
  185. selectTxt($G("videoUrl"));
  186. }
  187. } else {
  188. body.style.display = "none";
  189. }
  190. }
  191. } );
  192. }
  193. }
  194. /**
  195. * 创建图片浮动选择按钮
  196. * @param ids
  197. */
  198. function createAlignButton( ids ) {
  199. for ( var i = 0, ci; ci = ids[i++]; ) {
  200. var floatContainer = $G( ci ),
  201. nameMaps = {"none":lang['default'], "left":lang.floatLeft, "right":lang.floatRight, "center":lang.block};
  202. for ( var j in nameMaps ) {
  203. var div = document.createElement( "div" );
  204. div.setAttribute( "name", j );
  205. if ( j == "none" ) div.className="focus";
  206. div.style.cssText = "background:url(../../themes/default/images/" + j + "_focus.jpg);";
  207. div.setAttribute( "title", nameMaps[j] );
  208. floatContainer.appendChild( div );
  209. }
  210. switchSelect( ci );
  211. }
  212. }
  213. /**
  214. * 选择切换
  215. * @param selectParentId
  216. */
  217. function switchSelect( selectParentId ) {
  218. var selects = $G( selectParentId ).children;
  219. for ( var i = 0, ci; ci = selects[i++]; ) {
  220. domUtils.on( ci, "click", function () {
  221. for ( var j = 0, cj; cj = selects[j++]; ) {
  222. cj.className = "";
  223. cj.removeAttribute && cj.removeAttribute( "class" );
  224. }
  225. this.className = "focus";
  226. } )
  227. }
  228. }
  229. /**
  230. * 监听url改变事件
  231. * @param url
  232. */
  233. function addUrlChangeListener(url){
  234. if (browser.ie) {
  235. url.onpropertychange = function () {
  236. createPreviewVideo( this.value );
  237. }
  238. } else {
  239. url.addEventListener( "input", function () {
  240. createPreviewVideo( this.value );
  241. }, false );
  242. }
  243. }
  244. /**
  245. * 根据url生成视频预览
  246. * @param url
  247. */
  248. function createPreviewVideo(url){
  249. if ( !url )return;
  250. var matches = url.match(/youtu.be\/(\w+)$/) || url.match(/youtube\.com\/watch\?v=(\w+)/) || url.match(/youtube.com\/v\/(\w+)/),
  251. youku = url.match(/youku\.com\/v_show\/id_(\w+)/);
  252. if (matches){
  253. url = "https://www.youtube.com/v/" + matches[1] + "?version=3&feature=player_embedded";
  254. }else if(youku){
  255. url = "http://player.youku.com/player.php/sid/"+youku[1]+"/v.swf"
  256. }else if(!endWith(url,[".swf",".flv",".wmv"])){
  257. $G("preview").innerHTML = lang.urlError;
  258. return;
  259. }
  260. $G("preview").innerHTML = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  261. ' src="' + url + '"' +
  262. ' width="' + 420 + '"' +
  263. ' height="' + 280 + '"' +
  264. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" ></embed>';
  265. }
  266. /**
  267. * 末尾字符检测
  268. * @param str
  269. * @param endStrArr
  270. */
  271. function endWith(str,endStrArr){
  272. for(var i=0,len = endStrArr.length;i<len;i++){
  273. var tmp = endStrArr[i];
  274. if(str.length - tmp.length<0) return false;
  275. if(str.substring(str.length-tmp.length)==tmp){
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. /**
  282. * ajax获取视频信息
  283. */
  284. function getMovie(){
  285. var keywordInput = $G("videoSearchTxt");
  286. if(!keywordInput.getAttribute("hasClick") ||!keywordInput.value){
  287. selectTxt(keywordInput);
  288. return;
  289. }
  290. $G( "searchList" ).innerHTML = lang.loading;
  291. var keyword = keywordInput.value,
  292. type = $G("videoType").value,
  293. str="";
  294. ajax.request(editor.options.getMovieUrl,{
  295. searchKey:keyword,
  296. videoType:type,
  297. onsuccess:function(xhr){
  298. try{
  299. var info = eval("("+xhr.responseText+")");
  300. }catch(e){
  301. return;
  302. }
  303. var videos = info.multiPageResult.results;
  304. var html=["<table width='530'>"];
  305. for(var i=0,ci;ci = videos[i++];){
  306. html.push(
  307. "<tr>" +
  308. "<td><img title='"+lang.clickToSelect+"' ue_video_url='"+ci.outerPlayerUrl+"' alt='"+ci.tags+"' width='106' height='80' src='"+ci.picUrl+"' /> </td>" +
  309. "<td>" +
  310. "<p><a target='_blank' title='"+lang.goToSource+"' href='"+ci.itemUrl+"'>"+ci.title.substr(0,30)+"</a></p>" +
  311. "<p style='height: 62px;line-height: 20px' title='"+ci.description+"'> "+ ci.description.substr(0,95) +" </p>" +
  312. "</td>" +
  313. "</tr>"
  314. );
  315. }
  316. html.push("</table>");
  317. $G("searchList").innerHTML = str = html.length ==2 ?lang.noVideo : html.join("");
  318. var imgs = domUtils.getElementsByTagName($G("searchList"),"img");
  319. if(!imgs)return;
  320. for(var i=0,img;img = imgs[i++];){
  321. domUtils.on(img,"click",function(){
  322. changeSelected(this);
  323. })
  324. }
  325. }
  326. });
  327. }
  328. /**
  329. * 改变对象o的选中状态
  330. * @param o
  331. */
  332. function changeSelected(o){
  333. if ( o.getAttribute( "selected" ) ) {
  334. o.removeAttribute( "selected" );
  335. o.style.cssText = "filter:alpha(Opacity=100);-moz-opacity:1;opacity: 1;border: 2px solid #fff";
  336. } else {
  337. o.setAttribute( "selected", "true" );
  338. o.style.cssText = "filter:alpha(Opacity=50);-moz-opacity:0.5;opacity: 0.5;border:2px solid blue;";
  339. }
  340. }
  341. /**
  342. * 视频搜索相关注册事件
  343. */
  344. function addSearchListener(){
  345. domUtils.on($G("videoSearchBtn"),"click",getMovie);
  346. domUtils.on($G( "videoSearchTxt" ),"click",function () {
  347. if ( this.value == lang.static.videoSearchTxt.value ) {
  348. this.value = "";
  349. }
  350. this.setAttribute("hasClick","true");
  351. selectTxt(this);
  352. });
  353. $G( "videoSearchTxt" ).onkeyup = function(){
  354. this.setAttribute("hasClick","true");
  355. this.onkeyup = null;
  356. };
  357. domUtils.on($G( "videoSearchReset" ),"click",function () {
  358. var txt = $G( "videoSearchTxt" );
  359. txt.value = "";
  360. selectTxt(txt);
  361. $G( "searchList" ).innerHTML = "";
  362. });
  363. domUtils.on($G( "videoType" ),"change", getMovie);
  364. domUtils.on($G( "videoSearchTxt" ), "keyup", function ( evt ) {
  365. if ( evt.keyCode == 13 ) {
  366. getMovie();
  367. }
  368. } )
  369. }
  370. })();