video.js 11 KB

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