1
0

content-ui.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global document, getComputedStyle */
  21. this.singlefile.ui = this.singlefile.ui || (() => {
  22. const MASK_TAGNAME = "singlefile-mask";
  23. const PROGRESS_BAR_TAGNAME = "singlefile-progress-var";
  24. return {
  25. init() {
  26. let maskElement = document.querySelector(MASK_TAGNAME);
  27. if (!maskElement) {
  28. maskElement = createElement(MASK_TAGNAME, document.body);
  29. maskElement.style.setProperty("opacity", 0, "important");
  30. maskElement.style.setProperty("background-color", "transparent", "important");
  31. maskElement.offsetWidth;
  32. maskElement.style.setProperty("position", "fixed", "important");
  33. maskElement.style.setProperty("top", "0", "important");
  34. maskElement.style.setProperty("left", "0", "important");
  35. maskElement.style.setProperty("width", "100%", "important");
  36. maskElement.style.setProperty("height", "100%", "important");
  37. maskElement.style.setProperty("z-index", 2147483647, "important");
  38. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  39. maskElement.style.setProperty("will-change", "opacity, background-color", "important");
  40. const progressBarElement = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  41. progressBarElement.style.setProperty("background-color", "white", "important");
  42. progressBarElement.style.setProperty("position", "fixed", "important");
  43. progressBarElement.style.setProperty("top", "0", "important");
  44. progressBarElement.style.setProperty("left", "0", "important");
  45. progressBarElement.style.setProperty("width", "0", "important");
  46. progressBarElement.style.setProperty("height", "8px", "important");
  47. progressBarElement.style.setProperty("transition", "width 10ms", "important");
  48. progressBarElement.style.setProperty("will-change", "width", "important");
  49. maskElement.offsetWidth;
  50. maskElement.style.setProperty("background-color", "black", "important");
  51. maskElement.style.setProperty("opacity", .3, "important");
  52. document.body.offsetWidth;
  53. }
  54. },
  55. onprogress(event) {
  56. const progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  57. if (progressBarElement && event.details.max) {
  58. const width = Math.floor((event.details.index / event.details.max) * 100) + "%";
  59. if (progressBarElement.style.width != width) {
  60. progressBarElement.style.setProperty("width", Math.floor((event.details.index / event.details.max) * 100) + "%", "important");
  61. progressBarElement.offsetWidth;
  62. }
  63. }
  64. },
  65. end() {
  66. const maskElement = document.querySelector(MASK_TAGNAME);
  67. if (maskElement) {
  68. maskElement.remove();
  69. }
  70. }
  71. };
  72. function createElement(tagName, parentElement) {
  73. const element = document.createElement(tagName);
  74. parentElement.appendChild(element);
  75. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  76. return element;
  77. }
  78. })();