music.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * 插入音乐命令
  3. * @file
  4. */
  5. UE.plugin.register('music', function (){
  6. var me = this;
  7. function creatInsertStr(url,width,height,align,cssfloat,toEmbed){
  8. return !toEmbed ?
  9. '<img ' +
  10. (align && !cssfloat? 'align="' + align + '"' : '') +
  11. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  12. ' width="'+ width +'" height="' + height + '" _url="'+url+'" class="edui-faked-music"' +
  13. ' src="'+me.options.langPath+me.options.lang+'/images/music.png" />'
  14. :
  15. '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  16. ' src="' + url + '" width="' + width + '" height="' + height + '" '+ (align && !cssfloat? 'align="' + align + '"' : '') +
  17. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  18. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  19. }
  20. return {
  21. outputRule: function(root){
  22. utils.each(root.getNodesByTagName('img'),function(node){
  23. var html;
  24. if(node.getAttr('class') == 'edui-faked-music'){
  25. var cssfloat = node.getStyle('float');
  26. var align = node.getAttr('align');
  27. html = creatInsertStr(node.getAttr("_url"), node.getAttr('width'), node.getAttr('height'), align, cssfloat, true);
  28. var embed = UE.uNode.createElement(html);
  29. node.parentNode.replaceChild(embed,node);
  30. }
  31. })
  32. },
  33. inputRule:function(root){
  34. utils.each(root.getNodesByTagName('embed'),function(node){
  35. if(node.getAttr('class') == 'edui-faked-music'){
  36. var cssfloat = node.getStyle('float');
  37. var align = node.getAttr('align');
  38. html = creatInsertStr(node.getAttr("src"), node.getAttr('width'), node.getAttr('height'), align, cssfloat,false);
  39. var img = UE.uNode.createElement(html);
  40. node.parentNode.replaceChild(img,node);
  41. }
  42. })
  43. },
  44. commands:{
  45. /**
  46. * 插入音乐
  47. * @command music
  48. * @method execCommand
  49. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  50. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  51. * @example
  52. * ```javascript
  53. * //editor是编辑器实例
  54. * //在编辑器里插入一个“植物大战僵尸”的APP
  55. * editor.execCommand( 'music' , {
  56. * width: 400,
  57. * height: 95,
  58. * align: "center",
  59. * url: "音乐地址"
  60. * } );
  61. * ```
  62. */
  63. 'music':{
  64. execCommand:function (cmd, musicObj) {
  65. var me = this,
  66. str = creatInsertStr(musicObj.url, musicObj.width || 400, musicObj.height || 95, "none", false);
  67. me.execCommand("inserthtml",str);
  68. },
  69. queryCommandState:function () {
  70. var me = this,
  71. img = me.selection.getRange().getClosedNode(),
  72. flag = img && (img.className == "edui-faked-music");
  73. return flag ? 1 : 0;
  74. }
  75. }
  76. }
  77. }
  78. });