webapp.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: taoqili
  4. * Date: 12-5-7
  5. * Time: 下午2:37
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. UE.plugins['webapp'] = function () {
  9. var me = this;
  10. function createInsertStr( obj, toIframe, addParagraph ) {
  11. return !toIframe ?
  12. (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  13. ' 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 + '" />' +
  14. (addParagraph ? '</p>' : '')
  15. :
  16. '<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>';
  17. }
  18. function switchImgAndIframe( img2frame ) {
  19. var tmpdiv,
  20. nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  21. for ( var i = 0, node; node = nodes[i++]; ) {
  22. if ( node.className != "edui-faked-webapp" )continue;
  23. tmpdiv = me.document.createElement( "div" );
  24. 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 );
  25. node.parentNode.replaceChild( tmpdiv.firstChild, node );
  26. }
  27. }
  28. me.addListener( "beforegetcontent", function () {
  29. switchImgAndIframe( true );
  30. } );
  31. me.addListener( 'aftersetcontent', function () {
  32. switchImgAndIframe( false );
  33. } );
  34. me.addListener( 'aftergetcontent', function ( cmdName ) {
  35. if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) )
  36. return;
  37. switchImgAndIframe( false );
  38. } );
  39. UE.commands['webapp'] = {
  40. execCommand:function ( cmd, obj ) {
  41. me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  42. },
  43. queryCommandState:function () {
  44. return me.highlight ? -1 : 0;
  45. }
  46. };
  47. };