PageRenderTime 23ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 1ms

/ext-4.1.0_b3/examples/ux/IFrame.js

https://bitbucket.org/srogerf/javascript
JavaScript | 173 lines | 116 code | 35 blank | 22 comment | 15 complexity | 120dd66c9977d4e28045d2a0a55e40d0 MD5 | raw file
  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 = window.frames[name];
  60. return win;
  61. },
  62. getFrame: function() {
  63. var me = this;
  64. return me.iframeEl.dom;
  65. },
  66. beforeDestroy: function () {
  67. var me = this,
  68. doc, prop;
  69. if (me.rendered) {
  70. try {
  71. doc = me.getDoc();
  72. if (doc) {
  73. Ext.EventManager.removeAll(doc);
  74. for (prop in doc) {
  75. if (doc.hasOwnProperty(prop)) {
  76. delete doc[prop];
  77. }
  78. }
  79. }
  80. } catch(e) { }
  81. }
  82. me.callParent();
  83. },
  84. onLoad: function() {
  85. var me = this,
  86. doc = me.getDoc(),
  87. fn = me.onRelayedEvent;
  88. if (doc) {
  89. try {
  90. Ext.EventManager.removeAll(doc);
  91. // These events need to be relayed from the inner document (where they stop
  92. // bubbling) up to the outer document. This has to be done at the DOM level so
  93. // the event reaches listeners on elements like the document body. The effected
  94. // mechanisms that depend on this bubbling behavior are listed to the right
  95. // of the event.
  96. Ext.EventManager.on(doc, {
  97. mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
  98. mousemove: fn, // window resize drag detection
  99. mouseup: fn, // window resize termination
  100. click: fn, // not sure, but just to be safe
  101. dblclick: fn, // not sure again
  102. scope: me
  103. });
  104. } catch(e) {
  105. // cannot do this xss
  106. }
  107. // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
  108. Ext.EventManager.on(window, 'unload', me.beforeDestroy, me);
  109. this.el.unmask();
  110. this.fireEvent('load', this);
  111. } else if(me.src && me.src != '') {
  112. this.el.unmask();
  113. this.fireEvent('error', this);
  114. }
  115. },
  116. onRelayedEvent: function (event) {
  117. // relay event from the iframe's document to the document that owns the iframe...
  118. var iframeEl = this.iframeEl,
  119. iframeXY = iframeEl.getXY(),
  120. eventXY = event.getXY();
  121. // the event from the inner document has XY relative to that document's origin,
  122. // so adjust it to use the origin of the iframe in the outer document:
  123. event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
  124. event.injectEvent(iframeEl); // blame the iframe for the event...
  125. event.xy = eventXY; // restore the original XY (just for safety)
  126. },
  127. load: function (src) {
  128. var me = this,
  129. text = me.loadMask,
  130. frame = me.getFrame();
  131. if (me.fireEvent('beforeload', me, src) !== false) {
  132. if (text && me.el) {
  133. me.el.mask(text);
  134. }
  135. frame.src = me.src = (src || me.src);
  136. }
  137. }
  138. });