video.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * video插件, 为UEditor提供视频插入支持
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. UE.plugins['video'] = function (){
  7. var me =this;
  8. /**
  9. * 创建插入视频字符窜
  10. * @param url 视频地址
  11. * @param width 视频宽度
  12. * @param height 视频高度
  13. * @param align 视频对齐
  14. * @param toEmbed 是否以flash代替显示
  15. * @param addParagraph 是否需要添加P 标签
  16. */
  17. function creatInsertStr(url,width,height,id,align,classname,type){
  18. var str;
  19. switch (type){
  20. case 'image':
  21. str = '<img ' + (id ? 'id="' + id+'"' : '') + ' width="'+ width +'" height="' + height + '" _url="'+url+'" class="' + classname.replace(/\bvideo-js\b/, '') + '"' +
  22. ' src="' + me.options.UEDITOR_HOME_URL+'themes/default/images/spacer.gif" style="background:url('+me.options.UEDITOR_HOME_URL+'themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;'+(align ? 'float:' + align + ';': '')+'" />'
  23. break;
  24. case 'embed':
  25. var ext = url.substr(url.lastIndexOf('.') + 1);
  26. if(ext == 'mp4' || ext == 'flv'){
  27. str = '<object id="f4Player" width="' + width + '" height="' + height + '"' + (align ? ' style="float:' + align + '"': '') + ' type="application/x-shockwave-flash" class="' + classname + '" data="/statics/js/ckeditor/plugins/flashplayer/player/player.swf"> <param name="movie" value="/statics/js/ckeditor/plugins/flashplayer/player/player.swf" /> <param name="quality" value="high" /> <param name="menu" value="false" /> <param name="scale" value="noscale" /> <param name="allowfullscreen" value="true"> <param name="allowscriptaccess" value="always"> <param name="swlivevonnect" value="true" /> <param name="cachebusting" value="false"><param name="flashvars" value="autoplay=1&skin=/statics/js/ckeditor/plugins/flashplayer/player/skin.swf&video=' + utils.html(url) + '&thumbnail="/></object>';
  28. }else{
  29. str = '<embed type="application/x-shockwave-flash" class="' + classname + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  30. ' src="' + utils.html(url) + '" width="' + width + '" height="' + height + '"' + (align ? ' style="float:' + align + '"': '') +
  31. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  32. }
  33. break;
  34. case 'video':
  35. var ext = url.substr(url.lastIndexOf('.') + 1);
  36. if(ext == 'ogv') ext = 'ogg';
  37. str = '<video' + (id ? ' id="' + id + '"' : '') + ' class="' + classname + ' video-js" ' + (align ? ' style="float:' + align + '"': '') +
  38. ' controls preload="none" width="' + width + '" height="' + height + '" src="' + url + '" data-setup="{}">' +
  39. '<source src="' + url + '" type="video/' + ext + '" /></video>';
  40. break;
  41. }
  42. return str;
  43. }
  44. function switchImgAndVideo(root,img2video){
  45. utils.each(root.getNodesByTagName(img2video ? 'img' : 'embed video'),function(node){
  46. var className = node.getAttr('class');
  47. if(className && className.indexOf('edui-faked-video') != -1){
  48. var html = creatInsertStr( img2video ? node.getAttr('_url') : node.getAttr('src'),node.getAttr('width'),node.getAttr('height'),null,node.getStyle('float') || '',className,img2video ? 'embed':'image');
  49. node.parentNode.replaceChild(UE.uNode.createElement(html),node);
  50. }
  51. if(className && className.indexOf('edui-upload-video') != -1){
  52. var html = creatInsertStr( img2video ? node.getAttr('_url') : node.getAttr('src'),node.getAttr('width'),node.getAttr('height'),null,node.getStyle('float') || '',className,img2video ? 'video':'image');
  53. node.parentNode.replaceChild(UE.uNode.createElement(html),node);
  54. }
  55. })
  56. }
  57. me.addOutputRule(function(root){
  58. switchImgAndVideo(root,true)
  59. });
  60. me.addInputRule(function(root){
  61. switchImgAndVideo(root)
  62. });
  63. /**
  64. * 插入视频
  65. * @command insertvideo
  66. * @method execCommand
  67. * @param { String } cmd 命令字符串
  68. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  69. * @example
  70. * ```javascript
  71. *
  72. * var videoAttr = {
  73. * //视频地址
  74. * url: 'http://www.youku.com/xxx',
  75. * //视频宽高值, 单位px
  76. * width: 200,
  77. * height: 100
  78. * };
  79. *
  80. * //editor 是编辑器实例
  81. * //向编辑器插入单个视频
  82. * editor.execCommand( 'insertvideo', videoAttr );
  83. * ```
  84. */
  85. /**
  86. * 插入视频
  87. * @command insertvideo
  88. * @method execCommand
  89. * @param { String } cmd 命令字符串
  90. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  91. * @example
  92. * ```javascript
  93. *
  94. * var videoAttr1 = {
  95. * //视频地址
  96. * url: 'http://www.youku.com/xxx',
  97. * //视频宽高值, 单位px
  98. * width: 200,
  99. * height: 100
  100. * },
  101. * videoAttr2 = {
  102. * //视频地址
  103. * url: 'http://www.youku.com/xxx',
  104. * //视频宽高值, 单位px
  105. * width: 200,
  106. * height: 100
  107. * }
  108. *
  109. * //editor 是编辑器实例
  110. * //该方法将会向编辑器内插入两个视频
  111. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  112. * ```
  113. */
  114. /**
  115. * 查询当前光标所在处是否是一个视频
  116. * @command insertvideo
  117. * @method queryCommandState
  118. * @param { String } cmd 需要查询的命令字符串
  119. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  120. * @example
  121. * ```javascript
  122. *
  123. * //editor 是编辑器实例
  124. * editor.queryCommandState( 'insertvideo' );
  125. * ```
  126. */
  127. me.commands["insertvideo"] = {
  128. execCommand: function (cmd, videoObjs, type){
  129. videoObjs = utils.isArray(videoObjs)?videoObjs:[videoObjs];
  130. if(me.fireEvent('beforeinsertvideo', videoObjs) === true){
  131. return;
  132. }
  133. var html = [],id = 'tmpVedio', cl;
  134. for(var i=0,vi,len = videoObjs.length;i<len;i++){
  135. vi = videoObjs[i];
  136. cl = (type == 'upload' ? 'edui-upload-video video-js vjs-default-skin':'edui-faked-video');
  137. html.push(creatInsertStr( vi.url, vi.width || 420, vi.height || 280, id + i, null, cl, 'image'));
  138. }
  139. me.execCommand("inserthtml",html.join(""),true);
  140. var rng = this.selection.getRange();
  141. for(var i= 0,len=videoObjs.length;i<len;i++){
  142. var img = this.document.getElementById('tmpVedio'+i);
  143. domUtils.removeAttributes(img,'id');
  144. rng.selectNode(img).select();
  145. me.execCommand('imagefloat',videoObjs[i].align)
  146. }
  147. me.fireEvent('afterinsertvideo', videoObjs);
  148. },
  149. queryCommandState : function(){
  150. var img = me.selection.getRange().getClosedNode(),
  151. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video")!=-1);
  152. return flag ? 1 : 0;
  153. }
  154. };
  155. };