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