video.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. (function(){
  2. var domUtils = UM.dom.domUtils;
  3. var widgetName = 'video';
  4. UM.registerWidget( widgetName,{
  5. tpl: "<link rel=\"stylesheet\" type=\"text/css\" href=\"<%=video_url%>video.css\" />" +
  6. "<div class=\"edui-video-wrapper\">" +
  7. "<div id=\"eduiVideoTab\">" +
  8. "<div id=\"eduiVideoTabHeads\" class=\"edui-video-tabhead\">" +
  9. "<span tabSrc=\"video\" class=\"edui-video-focus\"><%=lang_tab_insertV%></span>" +
  10. "</div>" +
  11. "<div id=\"eduiVideoTabBodys\" class=\"edui-video-tabbody\">" +
  12. "<div id=\"eduiVideoPanel\" class=\"edui-video-panel\">" +
  13. "<table>" +
  14. "<tr>" +
  15. " <td>" +
  16. " <label for=\"eduiVideoUrl\" class=\"edui-video-url\"><%=lang_video_url%></label>" +
  17. " </td>" +
  18. " <td>" +
  19. " <input id=\"eduiVideoUrl\" type=\"text\" placeholder=\"请输入视频url(必填)\">" +
  20. " </td>" +
  21. "</tr>" +
  22. "<tr>" +
  23. " <td>" +
  24. " <label for=\"eduiVideoPoster\" class=\"edui-video-poster\">视频封面</label>" +
  25. " </td>" +
  26. " <td><input id=\"eduiVideoPoster\" type=\"text\" placeholder=\"请输入封面图url(非必填)\"></td>" +
  27. "</tr>" +
  28. // "<tr>" +
  29. // " <td>" +
  30. // " <label for=\"eduiVideoPoster\" class=\"edui-video-poster\">自动播放</label>" +
  31. // " </td>" +
  32. // " <td>" +
  33. // " <label>" +
  34. // " <input class='eduiVideoAutoPlay' name=\"auto_play\" type=\"radio\" checked>" +
  35. // " <span> 否 </span>" +
  36. // " <label>" +
  37. // " <label>" +
  38. // " <input class='eduiVideoAutoPlay' name=\"auto_play\" type=\"radio\">" +
  39. // " <span> 是 </span>" +
  40. // " <label>" +
  41. // " </td>" +
  42. // "</tr>" +
  43. "</table>" +
  44. "<div id=\"eduiVideoPreview\"></div>" +
  45. "<div id=\"eduiVideoInfo\">" +
  46. "<fieldset>" +
  47. "<legend><%=lang_video_size%></legend>" +
  48. "<table>" +
  49. // "<tr><td><label for=\"eduiVideoWidth\"><%=lang_videoW%></label></td><td><input class=\"edui-video-txt\" id=\"eduiVideoWidth\" type=\"text\"/></td></tr>" +
  50. "<tr>" +
  51. " <td><label for=\"eduiVideoHeight\"><%=lang_videoH%></label></td>" +
  52. " <td><input class=\"edui-video-txt\" id=\"eduiVideoHeight\" value=\"190\" type=\"text\"/></td>" +
  53. "</tr>" +
  54. "</table>" +
  55. "</fieldset>" +
  56. "<fieldset>" +
  57. "<legend><%=lang_alignment%></legend>" +
  58. "<div id=\"eduiVideoFloat\"></div>" +
  59. "</fieldset>" +
  60. "</div>" +
  61. "</div>" +
  62. "</div>" +
  63. "</div>" +
  64. "</div>",
  65. initContent:function( editor, $widget ){
  66. var me = this,
  67. lang = editor.getLang( widgetName),
  68. video_url = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/video/';
  69. me.lang = lang;
  70. me.editor = editor;
  71. me.$widget = $widget;
  72. me.root().html( $.parseTmpl( me.tpl, $.extend( { video_url: video_url }, lang['static'] ) ) );
  73. me.initController( lang );
  74. },
  75. initEvent:function(){
  76. var me = this,
  77. url = $("#eduiVideoUrl", me.$widget)[0];
  78. if( 'oninput' in url ) {
  79. url.oninput = function(){
  80. me.createPreviewVideo( this.value );
  81. };
  82. } else {
  83. url.onpropertychange = function () {
  84. me.createPreviewVideo( this.value );
  85. }
  86. }
  87. },
  88. initController: function( lang ){
  89. var me = this,
  90. img = me.editor.selection.getRange().getClosedNode(),
  91. url;
  92. me.createAlignButton( ["eduiVideoFloat"] );
  93. //编辑视频时初始化相关信息
  94. if(img && img.className == "edui-faked-video"){
  95. $("#eduiVideoUrl", me.$widget)[0].value = url = img.getAttribute("_url");
  96. $("#eduiVideoWidth", me.$widget)[0].value = img.width;
  97. $("#eduiVideoHeight", me.$widget)[0].value = img.height;
  98. var align = domUtils.getComputedStyle(img,"float"),
  99. parentAlign = domUtils.getComputedStyle(img.parentNode,"text-align");
  100. me.updateAlignButton(parentAlign==="center"?"center":align);
  101. }
  102. me.createPreviewVideo(url);
  103. },
  104. /**
  105. * 根据url生成视频预览
  106. */
  107. createPreviewVideo: function(url){
  108. if ( !url )return;
  109. var me = this;
  110. // lang = me.lang,
  111. // conUrl = me.convert_url(url);
  112. // if(!me.endWith(conUrl,[".swf",".flv",".wmv"])){
  113. // $("#eduiVideoPreview", me.$widget).html( lang.urlError );
  114. // return;
  115. // }
  116. $("#eduiVideoPreview", me.$widget)[0].innerHTML = '<video src="' + url + '" controls></video>';
  117. },
  118. /**
  119. * 将单个视频信息插入编辑器中
  120. */
  121. insertSingle: function(){
  122. var me = this,
  123. // width = $("#eduiVideoWidth", me.$widget)[0],
  124. height = $("#eduiVideoHeight", me.$widget)[0],
  125. url = $('#eduiVideoUrl', me.$widget)[0].value,
  126. poster = $('#eduiVideoPoster', me.$widget)[0].value,
  127. align = this.findFocus("eduiVideoFloat","name");
  128. if(!url) return false;
  129. if ( !me.checkNum( [ height] ) ) return false;
  130. this.editor.execCommand('insertvideo', {
  131. url: me.convert_url(url),
  132. // width: width.value,
  133. poster: poster,
  134. height: height.value,
  135. align: align
  136. });
  137. },
  138. /**
  139. * URL转换
  140. */
  141. convert_url: function(url){
  142. if ( !url ) return '';
  143. var matches = url.match(/youtu.be\/(\w+)$/) ||
  144. url.match(/youtube\.com\/watch\?v=(\w+)/) ||
  145. url.match(/youtube.com\/v\/(\w+)/),
  146. youku = url.match(/youku\.com\/v_show\/id_(\w+)/),
  147. youkuPlay = /player\.youku\.com/ig.test(url);
  148. if(youkuPlay){
  149. url = url.replace(/\?f=.*/, "");
  150. } else if (matches){
  151. url = "https://www.youtube.com/v/" + matches[1] + "?version=3&feature=player_embedded";
  152. }else if(youku){
  153. url = "http://player.youku.com/player.php/sid/"+youku[1]+"/v.swf"
  154. } else {
  155. url = url.replace(/http:\/\/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i, "http://www.tudou.com/v/$1")
  156. .replace(/http:\/\/www\.youtube\.com\/watch\?v=([\w\-]+)/i, "http://www.youtube.com/v/$1")
  157. .replace(/http:\/\/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i, "http://player.youku.com/player.php/sid/$1")
  158. .replace(/http:\/\/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "http://player.56.com/v_$1.swf")
  159. .replace(/http:\/\/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "http://player.56.com/v_$1.swf")
  160. .replace(/http:\/\/v\.ku6\.com\/.+\/([^.]+)\.html/i, "http://player.ku6.com/refer/$1/v.swf")
  161. .replace(/\?f=.*/, "");
  162. }
  163. return url;
  164. },
  165. /**
  166. * 检测传入的所有input框中输入的长宽是否是正数
  167. */
  168. checkNum: function checkNum( nodes ) {
  169. var me = this;
  170. for ( var i = 0, ci; ci = nodes[i++]; ) {
  171. var value = ci.value;
  172. if ( !me.isNumber( value ) && value) {
  173. alert( me.lang.numError );
  174. ci.value = "";
  175. ci.focus();
  176. return false;
  177. }
  178. }
  179. return true;
  180. },
  181. /**
  182. * 数字判断
  183. * @param value
  184. */
  185. isNumber: function( value ) {
  186. return /(0|^[1-9]\d*$)/.test( value );
  187. },
  188. updateAlignButton: function( align ) {
  189. var aligns = $( "#eduiVideoFloat", this.$widget )[0].children;
  190. for ( var i = 0, ci; ci = aligns[i++]; ) {
  191. if ( ci.getAttribute( "name" ) == align ) {
  192. if ( ci.className !="edui-video-focus" ) {
  193. ci.className = "edui-video-focus";
  194. }
  195. } else {
  196. if ( ci.className =="edui-video-focus" ) {
  197. ci.className = "";
  198. }
  199. }
  200. }
  201. },
  202. /**
  203. * 创建图片浮动选择按钮
  204. * @param ids
  205. */
  206. createAlignButton: function( ids ) {
  207. var lang = this.lang,
  208. vidoe_home = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/video/';
  209. for ( var i = 0, ci; ci = ids[i++]; ) {
  210. var floatContainer = $( "#" + ci, this.$widget ) [0],
  211. nameMaps = {"none":lang['default'], "left":lang.floatLeft, "right":lang.floatRight};
  212. for ( var j in nameMaps ) {
  213. var div = document.createElement( "div" );
  214. div.setAttribute( "name", j );
  215. if ( j == "none" ) div.className="edui-video-focus";
  216. div.style.cssText = "background:url("+ vidoe_home +"images/" + j + "_focus.jpg);";
  217. div.setAttribute( "title", nameMaps[j] );
  218. floatContainer.appendChild( div );
  219. }
  220. this.switchSelect( ci );
  221. }
  222. },
  223. /**
  224. * 选择切换
  225. */
  226. switchSelect: function( selectParentId ) {
  227. var selects = $( "#" + selectParentId, this.$widget )[0].children;
  228. for ( var i = 0, ci; ci = selects[i++]; ) {
  229. $(ci).on("click", function () {
  230. for ( var j = 0, cj; cj = selects[j++]; ) {
  231. cj.className = "";
  232. cj.removeAttribute && cj.removeAttribute( "class" );
  233. }
  234. this.className = "edui-video-focus";
  235. } )
  236. }
  237. },
  238. /**
  239. * 找到id下具有focus类的节点并返回该节点下的某个属性
  240. * @param id
  241. * @param returnProperty
  242. */
  243. findFocus: function( id, returnProperty ) {
  244. var tabs = $( "#" + id , this.$widget)[0].children,
  245. property;
  246. for ( var i = 0, ci; ci = tabs[i++]; ) {
  247. if ( ci.className=="edui-video-focus" ) {
  248. property = ci.getAttribute( returnProperty );
  249. break;
  250. }
  251. }
  252. return property;
  253. },
  254. /**
  255. * 末尾字符检测
  256. */
  257. endWith: function(str,endStrArr){
  258. for(var i=0,len = endStrArr.length;i<len;i++){
  259. var tmp = endStrArr[i];
  260. if(str.length - tmp.length<0) return false;
  261. if(str.substring(str.length-tmp.length)==tmp){
  262. return true;
  263. }
  264. }
  265. return false;
  266. },
  267. width:610,
  268. height:498,
  269. buttons: {
  270. ok: {
  271. exec: function( editor, $w ){
  272. $("#eduiVideoPreview", $w).html("");
  273. editor.getWidgetData(widgetName).insertSingle();
  274. }
  275. },
  276. cancel: {
  277. exec: function(){
  278. //清除视频
  279. $("#eduiVideoPreview").html("");
  280. }
  281. }
  282. }
  283. });
  284. })();