ion-img.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  5. import { d as inheritAttributes } from './helpers.js';
  6. import { b as getIonMode } from './ionic-global.js';
  7. const imgCss = ":host{display:block;-o-object-fit:contain;object-fit:contain}img{display:block;width:100%;height:100%;-o-object-fit:inherit;object-fit:inherit;-o-object-position:inherit;object-position:inherit}";
  8. const IonImgStyle0 = imgCss;
  9. const Img = /*@__PURE__*/ proxyCustomElement(class Img extends HTMLElement {
  10. constructor() {
  11. super();
  12. this.__registerHost();
  13. this.__attachShadow();
  14. this.ionImgWillLoad = createEvent(this, "ionImgWillLoad", 7);
  15. this.ionImgDidLoad = createEvent(this, "ionImgDidLoad", 7);
  16. this.ionError = createEvent(this, "ionError", 7);
  17. this.inheritedAttributes = {};
  18. this.onLoad = () => {
  19. this.ionImgDidLoad.emit();
  20. };
  21. this.onError = () => {
  22. this.ionError.emit();
  23. };
  24. this.loadSrc = undefined;
  25. this.loadError = undefined;
  26. this.alt = undefined;
  27. this.src = undefined;
  28. }
  29. srcChanged() {
  30. this.addIO();
  31. }
  32. componentWillLoad() {
  33. this.inheritedAttributes = inheritAttributes(this.el, ['draggable']);
  34. }
  35. componentDidLoad() {
  36. this.addIO();
  37. }
  38. addIO() {
  39. if (this.src === undefined) {
  40. return;
  41. }
  42. if (typeof window !== 'undefined' &&
  43. 'IntersectionObserver' in window &&
  44. 'IntersectionObserverEntry' in window &&
  45. 'isIntersecting' in window.IntersectionObserverEntry.prototype) {
  46. this.removeIO();
  47. this.io = new IntersectionObserver((data) => {
  48. /**
  49. * On slower devices, it is possible for an intersection observer entry to contain multiple
  50. * objects in the array. This happens when quickly scrolling an image into view and then out of
  51. * view. In this case, the last object represents the current state of the component.
  52. */
  53. if (data[data.length - 1].isIntersecting) {
  54. this.load();
  55. this.removeIO();
  56. }
  57. });
  58. this.io.observe(this.el);
  59. }
  60. else {
  61. // fall back to setTimeout for Safari and IE
  62. setTimeout(() => this.load(), 200);
  63. }
  64. }
  65. load() {
  66. this.loadError = this.onError;
  67. this.loadSrc = this.src;
  68. this.ionImgWillLoad.emit();
  69. }
  70. removeIO() {
  71. if (this.io) {
  72. this.io.disconnect();
  73. this.io = undefined;
  74. }
  75. }
  76. render() {
  77. const { loadSrc, alt, onLoad, loadError, inheritedAttributes } = this;
  78. const { draggable } = inheritedAttributes;
  79. return (h(Host, { key: 'da600442894427dee1974a28e545613afac69fca', class: getIonMode(this) }, h("img", { key: '16df0c7069af86c0fa7ce5af598bc0f63b4eb71a', decoding: "async", src: loadSrc, alt: alt, onLoad: onLoad, onError: loadError, part: "image", draggable: isDraggable(draggable) })));
  80. }
  81. get el() { return this; }
  82. static get watchers() { return {
  83. "src": ["srcChanged"]
  84. }; }
  85. static get style() { return IonImgStyle0; }
  86. }, [1, "ion-img", {
  87. "alt": [1],
  88. "src": [1],
  89. "loadSrc": [32],
  90. "loadError": [32]
  91. }, undefined, {
  92. "src": ["srcChanged"]
  93. }]);
  94. /**
  95. * Enumerated strings must be set as booleans
  96. * as Stencil will not render 'false' in the DOM.
  97. * The need to explicitly render draggable="true"
  98. * as only certain elements are draggable by default.
  99. * https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable.
  100. */
  101. const isDraggable = (draggable) => {
  102. switch (draggable) {
  103. case 'true':
  104. return true;
  105. case 'false':
  106. return false;
  107. default:
  108. return undefined;
  109. }
  110. };
  111. function defineCustomElement$1() {
  112. if (typeof customElements === "undefined") {
  113. return;
  114. }
  115. const components = ["ion-img"];
  116. components.forEach(tagName => { switch (tagName) {
  117. case "ion-img":
  118. if (!customElements.get(tagName)) {
  119. customElements.define(tagName, Img);
  120. }
  121. break;
  122. } });
  123. }
  124. const IonImg = Img;
  125. const defineCustomElement = defineCustomElement$1;
  126. export { IonImg, defineCustomElement };