iframe.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. export default function () {
  2. Ext.define('Ext.ux.IFrame', {
  3. extend: 'Ext.Component',
  4. alias: 'widget.uxiframe',
  5. loadMask: 'Loading...',
  6. src: 'about:blank',
  7. renderTpl: [
  8. '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0"></iframe>'
  9. ],
  10. childEls: ['iframeEl'],
  11. initComponent: function () {
  12. this.superclass.initComponent.call(this)
  13. this.superclass.initEvents.call(this)
  14. this.frameName = this.frameName || this.id + '-frame';
  15. },
  16. initEvents: function () {
  17. var me = this;
  18. this.superclass.initEvents.call(this)
  19. me.iframeEl.on('load', me.onLoad, me);
  20. },
  21. initRenderData: function () {
  22. return Ext.apply(this.superclass.initRenderData.call(this), {
  23. src: this.src,
  24. frameName: this.frameName
  25. });
  26. },
  27. getBody: function () {
  28. var doc = this.getDoc();
  29. return doc.body || doc.documentElement;
  30. },
  31. getDoc: function () {
  32. try {
  33. return this.getWin().document;
  34. } catch (ex) {
  35. return null;
  36. }
  37. },
  38. getWin: function () {
  39. var me = this,
  40. name = me.frameName,
  41. win = Ext.isIE ? me.iframeEl.dom.contentWindow : window.frames[name];
  42. return win;
  43. },
  44. getFrame: function () {
  45. var me = this;
  46. return me.iframeEl.dom;
  47. },
  48. onLoad: function () {
  49. var me = this,
  50. doc = me.getDoc();
  51. if (doc) {
  52. this.el.unmask();
  53. this.fireEvent('load', this);
  54. } else if (me.src) {
  55. this.el.unmask();
  56. this.fireEvent('error', this);
  57. }
  58. },
  59. load: function (src) {
  60. var me = this,
  61. text = me.loadMask,
  62. frame = me.getFrame();
  63. if (me.fireEvent('beforeload', me, src) !== false) {
  64. if (text && me.el) {
  65. me.el.mask(text);
  66. }
  67. frame.src = me.src = (src || me.src);
  68. }
  69. }
  70. });
  71. }