resizer.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS002: Fix invalid constructor
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS206: Consider reworking classes to avoid initClass
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  9. */
  10. (function () {
  11. let MIN = undefined;
  12. let MAX = undefined;
  13. app.views.Resizer = class Resizer extends app.View {
  14. constructor(...args) {
  15. this.onDragStart = this.onDragStart.bind(this);
  16. this.onDrag = this.onDrag.bind(this);
  17. this.onDragEnd = this.onDragEnd.bind(this);
  18. super(...args);
  19. }
  20. static initClass() {
  21. this.className = "_resizer";
  22. this.events = {
  23. dragstart: "onDragStart",
  24. dragend: "onDragEnd",
  25. };
  26. MIN = 260;
  27. MAX = 600;
  28. }
  29. static isSupported() {
  30. return "ondragstart" in document.createElement("div") && !app.isMobile();
  31. }
  32. init() {
  33. this.el.setAttribute("draggable", "true");
  34. this.appendTo($("._app"));
  35. }
  36. resize(value, save) {
  37. value -= app.el.offsetLeft;
  38. if (!(value > 0)) {
  39. return;
  40. }
  41. value = Math.min(Math.max(Math.round(value), MIN), MAX);
  42. const newSize = `${value}px`;
  43. document.documentElement.style.setProperty("--sidebarWidth", newSize);
  44. if (save) {
  45. app.settings.setSize(value);
  46. }
  47. }
  48. onDragStart(event) {
  49. event.dataTransfer.effectAllowed = "link";
  50. event.dataTransfer.setData("Text", "");
  51. $.on(window, "dragover", this.onDrag);
  52. }
  53. onDrag(event) {
  54. const value = event.pageX;
  55. if (!(value > 0)) {
  56. return;
  57. }
  58. this.lastDragValue = value;
  59. if (this.lastDrag && this.lastDrag > Date.now() - 50) {
  60. return;
  61. }
  62. this.lastDrag = Date.now();
  63. this.resize(value, false);
  64. }
  65. onDragEnd(event) {
  66. $.off(window, "dragover", this.onDrag);
  67. let value = event.pageX || event.screenX - window.screenX;
  68. if (
  69. this.lastDragValue &&
  70. !(this.lastDragValue - 5 < value && value < this.lastDragValue + 5)
  71. ) {
  72. // https://github.com/freeCodeCamp/devdocs/issues/265
  73. value = this.lastDragValue;
  74. }
  75. this.resize(value, true);
  76. }
  77. };
  78. app.views.Resizer.initClass();
  79. return app.views.Resizer;
  80. })();