popin.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * @constructor
  3. **/
  4. function Popin() {
  5. this.wrapper = document.createElement("div");
  6. var overlay = document.createElement("div");
  7. overlay.classList.add(htmlClasses.popin.overlay);
  8. this.wrapper.appendChild(overlay);
  9. var container = document.createElement("div");
  10. container.classList.add(htmlClasses.popin.container);
  11. this.wrapper.appendChild(container);
  12. this.contentWrapper = document.createElement("div");
  13. this.contentWrapper.className = htmlClasses.popin.imgContent;
  14. this.img = document.createElement("img");
  15. this.img.src = "/images/loading.svg";
  16. this.contentWrapper.appendChild(this.img);
  17. container.appendChild(this.contentWrapper);
  18. container.addEventListener("click", (() => this.hide()).bind(this));
  19. this.contentWrapper.addEventListener("click", e => e.stopPropagation());
  20. this.query = new XMLHttpRequest();
  21. this.query.responseType = 'blob';
  22. this.query.onreadystatechange = (() => {
  23. if (this.query.readyState === 4) {
  24. if (Math.floor(this.query.status / 100) === 2 && this.query.response) {
  25. this.img.src = window.URL.createObjectURL(/** @type {Blob!} */ (this.query.response));
  26. } else {
  27. // Http error
  28. console.error("Cannot get resource", this.query.status);
  29. }
  30. }
  31. }).bind(this);
  32. }
  33. Popin.prototype.show = function() {
  34. document.body.appendChild(this.wrapper);
  35. };
  36. Popin.prototype.hide = function() {
  37. this.wrapper.remove();
  38. };
  39. Popin.prototype.setImage = function(src) {
  40. if (this.query.readyState !== 0 && this.query.readyState !== 4)
  41. this.query.abort();
  42. this.query.open("GET", src, true);
  43. this.query.send();
  44. };
  45. Popin.prototype.setContent = function(fragment) {
  46. this.img.remove();
  47. this.contentWrapper.className = htmlClasses.popin.domContent;
  48. this.contentWrapper.appendChild(fragment);
  49. };