IFrame.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*!
  2. * Ext JS Library 4.0
  3. * Copyright(c) 2006-2011 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. /**
  8. * Barebones iframe implementation. For serious iframe work, see the ManagedIFrame extension
  9. * (http://www.sencha.com/forum/showthread.php?71961).
  10. *
  11. * @class Ext.ux.IFrame
  12. */
  13. Ext.define('Ext.ux.IFrame', {
  14. extend: 'Ext.Component',
  15. alias: 'widget.uxiframe',
  16. loadMask: 'Loading...',
  17. src: 'about:blank',
  18. renderTpl: [
  19. '<iframe src="{src}" name="{frameName}" width="100%" height="100%" frameborder="0"></iframe>'
  20. ],
  21. initComponent: function () {
  22. this.callParent();
  23. this.frameName = this.frameName || this.id + '-frame';
  24. this.addEvents(
  25. 'beforeload',
  26. 'load'
  27. );
  28. Ext.apply(this.renderSelectors, {
  29. iframeEl: 'iframe'
  30. });
  31. },
  32. initEvents : function() {
  33. var me = this,
  34. iframeEl = me.iframeEl.dom,
  35. frameEl = me.getFrame();
  36. me.callParent();
  37. me.iframeEl.on('load', me.onLoad, me);
  38. },
  39. initRenderData: function() {
  40. return Ext.apply(this.callParent(), {
  41. src: this.src,
  42. frameName: this.frameName
  43. });
  44. },
  45. getBody: function() {
  46. var doc = this.getDoc();
  47. return doc.body || doc.documentElement;
  48. },
  49. getDoc: function() {
  50. try {
  51. return this.getWin().document;
  52. } catch (ex) {
  53. return null;
  54. }
  55. },
  56. getWin: function() {
  57. var me = this,
  58. name = me.frameName,
  59. win = Ext.isIE
  60. ? me.iframeEl.dom.contentWindow
  61. : window.frames[name];
  62. return win;
  63. },
  64. getFrame: function() {
  65. var me = this;
  66. return me.iframeEl.dom;
  67. },
  68. beforeDestroy: function () {
  69. var me = this,
  70. doc, prop;
  71. if (me.rendered) {
  72. try {
  73. doc = me.getDoc();
  74. if (doc) {
  75. Ext.EventManager.removeAll(doc);
  76. for (prop in doc) {
  77. if (doc.hasOwnProperty && doc.hasOwnProperty(prop)) {
  78. delete doc[prop];
  79. }
  80. }
  81. }
  82. } catch(e) { }
  83. }
  84. me.callParent();
  85. },
  86. onLoad: function() {
  87. var me = this,
  88. doc = me.getDoc(),
  89. fn = me.onRelayedEvent;
  90. if (doc) {
  91. try {
  92. Ext.EventManager.removeAll(doc);
  93. // These events need to be relayed from the inner document (where they stop
  94. // bubbling) up to the outer document. This has to be done at the DOM level so
  95. // the event reaches listeners on elements like the document body. The effected
  96. // mechanisms that depend on this bubbling behavior are listed to the right
  97. // of the event.
  98. Ext.EventManager.on(doc, {
  99. mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
  100. mousemove: fn, // window resize drag detection
  101. mouseup: fn, // window resize termination
  102. click: fn, // not sure, but just to be safe
  103. dblclick: fn, // not sure again
  104. scope: me
  105. });
  106. } catch(e) {
  107. // cannot do this xss
  108. }
  109. // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
  110. Ext.EventManager.on(window, 'unload', me.beforeDestroy, me);
  111. this.el.unmask();
  112. this.fireEvent('load', this);
  113. } else if(me.src && me.src != '') {
  114. this.el.unmask();
  115. this.fireEvent('error', this);
  116. }
  117. },
  118. onRelayedEvent: function (event) {
  119. // relay event from the iframe's document to the document that owns the iframe...
  120. var iframeEl = this.iframeEl,
  121. iframeXY = iframeEl.getXY(),
  122. eventXY = event.getXY();
  123. // the event from the inner document has XY relative to that document's origin,
  124. // so adjust it to use the origin of the iframe in the outer document:
  125. event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
  126. event.injectEvent(iframeEl); // blame the iframe for the event...
  127. event.xy = eventXY; // restore the original XY (just for safety)
  128. },
  129. load: function (src) {
  130. var me = this,
  131. text = me.loadMask,
  132. frame = me.getFrame();
  133. if (me.fireEvent('beforeload', me, src) !== false) {
  134. if (text && me.el) {
  135. me.el.mask(text);
  136. }
  137. frame.src = me.src = (src || me.src);
  138. }
  139. }
  140. });