webapp.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * 百度应用
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 插入百度应用
  8. * @command webapp
  9. * @method execCommand
  10. * @remind 需要百度APPKey
  11. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  12. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  13. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  14. * @example
  15. * ```javascript
  16. * //editor是编辑器实例
  17. * //在编辑器里插入一个“植物大战僵尸”的APP
  18. * editor.execCommand( 'webapp' , {
  19. * title: '植物大战僵尸',
  20. * width: 560,
  21. * height: 465,
  22. * logo: '应用展示的图片',
  23. * url: '百度应用的地址'
  24. * } );
  25. * ```
  26. */
  27. //UE.plugins['webapp'] = function () {
  28. // var me = this;
  29. // function createInsertStr( obj, toIframe, addParagraph ) {
  30. // return !toIframe ?
  31. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  32. // ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" style="background:url(' + obj.logo+') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" />' +
  33. // (addParagraph ? '</p>' : '')
  34. // :
  35. // '<iframe class="edui-faked-webapp" title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = '+obj.logo+'></iframe>';
  36. // }
  37. //
  38. // function switchImgAndIframe( img2frame ) {
  39. // var tmpdiv,
  40. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  41. // for ( var i = 0, node; node = nodes[i++]; ) {
  42. // if ( node.className != "edui-faked-webapp" ){
  43. // continue;
  44. // }
  45. // tmpdiv = me.document.createElement( "div" );
  46. // tmpdiv.innerHTML = createInsertStr( img2frame ? {url:node.getAttribute( "_url" ), width:node.width, height:node.height,title:node.title,logo:node.style.backgroundImage.replace("url(","").replace(")","")} : {url:node.getAttribute( "src", 2 ),title:node.title, width:node.width, height:node.height,logo:node.getAttribute("logo_url")}, img2frame ? true : false,false );
  47. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  48. // }
  49. // }
  50. //
  51. // me.addListener( "beforegetcontent", function () {
  52. // switchImgAndIframe( true );
  53. // } );
  54. // me.addListener( 'aftersetcontent', function () {
  55. // switchImgAndIframe( false );
  56. // } );
  57. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  58. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  59. // return;
  60. // }
  61. // switchImgAndIframe( false );
  62. // } );
  63. //
  64. // me.commands['webapp'] = {
  65. // execCommand:function ( cmd, obj ) {
  66. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  67. // }
  68. // };
  69. //};
  70. UE.plugin.register("webapp", function() {
  71. var me = this;
  72. function createInsertStr(obj, toEmbed) {
  73. return !toEmbed
  74. ? '<img title="' +
  75. obj.title +
  76. '" width="' +
  77. obj.width +
  78. '" height="' +
  79. obj.height +
  80. '"' +
  81. ' src="' +
  82. me.options.UEDITOR_HOME_URL +
  83. 'themes/default/images/spacer.gif" _logo_url="' +
  84. obj.logo +
  85. '" style="background:url(' +
  86. obj.logo +
  87. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' +
  88. obj.url +
  89. '" ' +
  90. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : "") +
  91. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : "") +
  92. "/>"
  93. : '<iframe class="edui-faked-webapp" title="' +
  94. obj.title +
  95. '" ' +
  96. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : "") +
  97. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : "") +
  98. 'width="' +
  99. obj.width +
  100. '" height="' +
  101. obj.height +
  102. '" scrolling="no" frameborder="0" src="' +
  103. obj.url +
  104. '" logo_url = "' +
  105. obj.logo +
  106. '"></iframe>';
  107. }
  108. return {
  109. outputRule: function(root) {
  110. utils.each(root.getNodesByTagName("img"), function(node) {
  111. var html;
  112. if (node.getAttr("class") == "edui-faked-webapp") {
  113. html = createInsertStr(
  114. {
  115. title: node.getAttr("title"),
  116. width: node.getAttr("width"),
  117. height: node.getAttr("height"),
  118. align: node.getAttr("align"),
  119. cssfloat: node.getStyle("float"),
  120. url: node.getAttr("_url"),
  121. logo: node.getAttr("_logo_url")
  122. },
  123. true
  124. );
  125. var embed = UE.uNode.createElement(html);
  126. node.parentNode.replaceChild(embed, node);
  127. }
  128. });
  129. },
  130. inputRule: function(root) {
  131. utils.each(root.getNodesByTagName("iframe"), function(node) {
  132. if (node.getAttr("class") == "edui-faked-webapp") {
  133. var img = UE.uNode.createElement(
  134. createInsertStr({
  135. title: node.getAttr("title"),
  136. width: node.getAttr("width"),
  137. height: node.getAttr("height"),
  138. align: node.getAttr("align"),
  139. cssfloat: node.getStyle("float"),
  140. url: node.getAttr("src"),
  141. logo: node.getAttr("logo_url")
  142. })
  143. );
  144. node.parentNode.replaceChild(img, node);
  145. }
  146. });
  147. },
  148. commands: {
  149. /**
  150. * 插入百度应用
  151. * @command webapp
  152. * @method execCommand
  153. * @remind 需要百度APPKey
  154. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  155. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  156. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  157. * @example
  158. * ```javascript
  159. * //editor是编辑器实例
  160. * //在编辑器里插入一个“植物大战僵尸”的APP
  161. * editor.execCommand( 'webapp' , {
  162. * title: '植物大战僵尸',
  163. * width: 560,
  164. * height: 465,
  165. * logo: '应用展示的图片',
  166. * url: '百度应用的地址'
  167. * } );
  168. * ```
  169. */
  170. webapp: {
  171. execCommand: function(cmd, obj) {
  172. var me = this,
  173. str = createInsertStr(
  174. utils.extend(obj, {
  175. align: "none"
  176. }),
  177. false
  178. );
  179. me.execCommand("inserthtml", str);
  180. },
  181. queryCommandState: function() {
  182. var me = this,
  183. img = me.selection.getRange().getClosedNode(),
  184. flag = img && img.className == "edui-faked-webapp";
  185. return flag ? 1 : 0;
  186. }
  187. }
  188. }
  189. };
  190. });