webapp.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  75. ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" _logo_url="'+obj.logo+'" style="background:url(' + obj.logo
  76. +') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" ' +
  77. (obj.align && !obj.cssfloat? 'align="' + obj.align + '"' : '') +
  78. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  79. '/>'
  80. :
  81. '<iframe class="edui-faked-webapp" title="'+obj.title+'" ' +
  82. (obj.align && !obj.cssfloat? 'align="' + obj.align + '"' : '') +
  83. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  84. 'width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = "'+obj.logo+'"></iframe>'
  85. }
  86. return {
  87. outputRule: function(root){
  88. utils.each(root.getNodesByTagName('img'),function(node){
  89. var html;
  90. if(node.getAttr('class') == 'edui-faked-webapp'){
  91. html = createInsertStr({
  92. title:node.getAttr('title'),
  93. 'width':node.getAttr('width'),
  94. 'height':node.getAttr('height'),
  95. 'align':node.getAttr('align'),
  96. 'cssfloat':node.getStyle('float'),
  97. 'url':node.getAttr("_url"),
  98. 'logo':node.getAttr('_logo_url')
  99. },true);
  100. var embed = UE.uNode.createElement(html);
  101. node.parentNode.replaceChild(embed,node);
  102. }
  103. })
  104. },
  105. inputRule:function(root){
  106. utils.each(root.getNodesByTagName('iframe'),function(node){
  107. if(node.getAttr('class') == 'edui-faked-webapp'){
  108. var img = UE.uNode.createElement(createInsertStr({
  109. title:node.getAttr('title'),
  110. 'width':node.getAttr('width'),
  111. 'height':node.getAttr('height'),
  112. 'align':node.getAttr('align'),
  113. 'cssfloat':node.getStyle('float'),
  114. 'url':node.getAttr("src"),
  115. 'logo':node.getAttr('logo_url')
  116. }));
  117. node.parentNode.replaceChild(img,node);
  118. }
  119. })
  120. },
  121. commands:{
  122. /**
  123. * 插入百度应用
  124. * @command webapp
  125. * @method execCommand
  126. * @remind 需要百度APPKey
  127. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  128. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  129. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  130. * @example
  131. * ```javascript
  132. * //editor是编辑器实例
  133. * //在编辑器里插入一个“植物大战僵尸”的APP
  134. * editor.execCommand( 'webapp' , {
  135. * title: '植物大战僵尸',
  136. * width: 560,
  137. * height: 465,
  138. * logo: '应用展示的图片',
  139. * url: '百度应用的地址'
  140. * } );
  141. * ```
  142. */
  143. 'webapp':{
  144. execCommand:function (cmd, obj) {
  145. var me = this,
  146. str = createInsertStr(utils.extend(obj,{
  147. align:'none'
  148. }), false);
  149. me.execCommand("inserthtml",str);
  150. },
  151. queryCommandState:function () {
  152. var me = this,
  153. img = me.selection.getRange().getClosedNode(),
  154. flag = img && (img.className == "edui-faked-webapp");
  155. return flag ? 1 : 0;
  156. }
  157. }
  158. }
  159. }
  160. });