music.js 3.8 KB

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