content-ui.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, requestAnimationFrame */
  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. requestAnimationFrame(() => {
  29. maskElement = createElement(MASK_TAGNAME, document.body);
  30. maskElement.style.setProperty("opacity", 0, "important");
  31. maskElement.style.setProperty("background-color", "transparent", "important");
  32. maskElement.offsetWidth;
  33. maskElement.style.setProperty("position", "fixed", "important");
  34. maskElement.style.setProperty("top", "0", "important");
  35. maskElement.style.setProperty("left", "0", "important");
  36. maskElement.style.setProperty("width", "100%", "important");
  37. maskElement.style.setProperty("height", "100%", "important");
  38. maskElement.style.setProperty("z-index", 2147483647, "important");
  39. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  40. maskElement.style.setProperty("will-change", "opacity, background-color", "important");
  41. const progressBarElement = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  42. progressBarElement.style.setProperty("background-color", "white", "important");
  43. progressBarElement.style.setProperty("position", "fixed", "important");
  44. progressBarElement.style.setProperty("top", "0", "important");
  45. progressBarElement.style.setProperty("left", "0", "important");
  46. progressBarElement.style.setProperty("width", "0", "important");
  47. progressBarElement.style.setProperty("height", "8px", "important");
  48. progressBarElement.style.setProperty("transition", "width 100ms", "important");
  49. progressBarElement.style.setProperty("will-change", "width", "important");
  50. maskElement.offsetWidth;
  51. maskElement.style.setProperty("background-color", "black", "important");
  52. maskElement.style.setProperty("opacity", .3, "important");
  53. document.body.offsetWidth;
  54. });
  55. }
  56. },
  57. onprogress(index, maxIndex) {
  58. const progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  59. if (progressBarElement && maxIndex) {
  60. const width = Math.floor((index / maxIndex) * 100) + "%";
  61. if (progressBarElement.style.width != width) {
  62. requestAnimationFrame(() => progressBarElement.style.setProperty("width", Math.floor((index / maxIndex) * 100) + "%", "important"));
  63. }
  64. }
  65. },
  66. end() {
  67. const maskElement = document.querySelector(MASK_TAGNAME);
  68. if (maskElement) {
  69. requestAnimationFrame(() => maskElement.remove());
  70. }
  71. }
  72. };
  73. function createElement(tagName, parentElement) {
  74. const element = document.createElement(tagName);
  75. parentElement.appendChild(element);
  76. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  77. return element;
  78. }
  79. })();