stimulus-library 0.9.6 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controllers/index.js +1 -2
- package/dist/controllers/tables/table_sort_controller.js +2 -2
- package/dist/controllers/turbo/index.js +3 -0
- package/dist/controllers/turbo/turbo_frame_history_controller.js +27 -0
- package/dist/controllers/turbo/turbo_frame_rc_controller.js +65 -0
- package/dist/controllers/turbo/turbo_frame_refresh_controller.js +33 -0
- package/dist/controllers/visual/countdown_controller.js +13 -7
- package/dist/mixins/index.js +1 -0
- package/dist/mixins/use_resize_observer.js +9 -0
- package/package.json +6 -6
|
@@ -5,6 +5,7 @@ export * from './media';
|
|
|
5
5
|
export * from './scroll';
|
|
6
6
|
export * from './signal';
|
|
7
7
|
export * from './tables';
|
|
8
|
+
export * from './turbo';
|
|
8
9
|
export * from './utility';
|
|
9
10
|
export * from './visual';
|
|
10
11
|
// Controllers
|
|
@@ -30,5 +31,3 @@ export * from './teleport_controller';
|
|
|
30
31
|
export * from './temporary_state_controller';
|
|
31
32
|
export * from './toggle_class_controller';
|
|
32
33
|
export * from './trix_modifier_controller';
|
|
33
|
-
export * from './turbo_frame_rc_controller';
|
|
34
|
-
export * from './turbo_frame_refresh_controller';
|
|
@@ -78,8 +78,8 @@ export class TableSortController extends BaseController {
|
|
|
78
78
|
let cells = Array.from(row.cells);
|
|
79
79
|
let otherCells = Array.from(otherRow.cells);
|
|
80
80
|
// TODO: Handle colspans?
|
|
81
|
-
let x = cells[index]?.innerText || "";
|
|
82
|
-
let y = otherCells[index]?.innerText || "";
|
|
81
|
+
let x = cells[index]?.dataset?.sortValue || cells[index]?.innerText || "";
|
|
82
|
+
let y = otherCells[index]?.dataset?.sortValue || otherCells[index]?.innerText || "";
|
|
83
83
|
let sortVal = x.localeCompare(y, "en", { sensitivity: "base", numeric: true, caseFirst: "upper" });
|
|
84
84
|
if (row.dataset.sortTop || otherRow.dataset.sortBottom) {
|
|
85
85
|
if (row.dataset.sortTop && otherRow.dataset.sortTop) {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BaseController } from "../../utilities";
|
|
2
|
+
import { useMutationObserver } from "../../mixins";
|
|
3
|
+
export default class TurboFrameHistoryController extends BaseController {
|
|
4
|
+
initialize() {
|
|
5
|
+
this.mutate = this.mutate.bind(this);
|
|
6
|
+
}
|
|
7
|
+
connect() {
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
if (!window.Turbo) {
|
|
10
|
+
throw new Error('Expected Turbo to be defined on the window.');
|
|
11
|
+
}
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
const { navigator } = window.Turbo;
|
|
14
|
+
this.navigator = navigator;
|
|
15
|
+
useMutationObserver(this, this.el, this.mutate, { attributes: true });
|
|
16
|
+
}
|
|
17
|
+
mutate(entries) {
|
|
18
|
+
entries.forEach((mutation) => {
|
|
19
|
+
if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
|
|
20
|
+
const src = this.element.getAttribute('src');
|
|
21
|
+
if (src != null) {
|
|
22
|
+
this.navigator.history.push(new URL(src));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { BaseController } from "../../utilities/base_controller";
|
|
2
|
+
import { isHTMLAnchorElement } from "../../utilities/elements";
|
|
3
|
+
import { isTurboFrame } from "../../utilities/turbo";
|
|
4
|
+
export class TurboFrameRCController extends BaseController {
|
|
5
|
+
static values = {
|
|
6
|
+
frameId: String,
|
|
7
|
+
src: String,
|
|
8
|
+
loadingMessage: String,
|
|
9
|
+
};
|
|
10
|
+
toggle(event) {
|
|
11
|
+
event?.preventDefault();
|
|
12
|
+
let frame = this._getFrame();
|
|
13
|
+
let frameSrc = frame.src;
|
|
14
|
+
if (frameSrc == null || frameSrc !== this._getSrc()) {
|
|
15
|
+
this._setSrc();
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this._clear();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
setSrc(event) {
|
|
22
|
+
event?.preventDefault();
|
|
23
|
+
this._setSrc();
|
|
24
|
+
}
|
|
25
|
+
clear(event) {
|
|
26
|
+
event?.preventDefault();
|
|
27
|
+
this._clear();
|
|
28
|
+
}
|
|
29
|
+
_setSrc() {
|
|
30
|
+
let frame = this._getFrame();
|
|
31
|
+
if (this.hasLoadingMessageValue) {
|
|
32
|
+
frame.innerHTML = this.loadingMessageValue;
|
|
33
|
+
}
|
|
34
|
+
frame.src = this._getSrc();
|
|
35
|
+
}
|
|
36
|
+
_clear() {
|
|
37
|
+
let frame = this._getFrame();
|
|
38
|
+
frame.src = "";
|
|
39
|
+
frame.innerHTML = "";
|
|
40
|
+
}
|
|
41
|
+
_getFrame() {
|
|
42
|
+
let frame = document.getElementById(`${this.frameIdValue}`);
|
|
43
|
+
if (frame == null) {
|
|
44
|
+
throw new Error(`Could not find frame with ID '${this.frameIdValue}'`);
|
|
45
|
+
}
|
|
46
|
+
if (!isTurboFrame(frame)) {
|
|
47
|
+
throw new Error(`Element targeted by ID '${this.frameIdValue}'`);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return frame;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
_getSrc() {
|
|
54
|
+
let element = this.el;
|
|
55
|
+
if (this.hasSrcValue) {
|
|
56
|
+
return this.srcValue;
|
|
57
|
+
}
|
|
58
|
+
else if (isHTMLAnchorElement(element)) {
|
|
59
|
+
return element.href;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw new Error("No link given to drive frame to");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BaseController } from "../../utilities/base_controller";
|
|
2
|
+
import { isTurboFrame } from "../../utilities/turbo";
|
|
3
|
+
import { useTimeout } from "../../mixins/use_timeout";
|
|
4
|
+
// noinspection SillyAssignmentJS
|
|
5
|
+
export class TurboFrameRefreshController extends BaseController {
|
|
6
|
+
static values = {
|
|
7
|
+
interval: Number,
|
|
8
|
+
poll: Boolean,
|
|
9
|
+
};
|
|
10
|
+
get _poll() {
|
|
11
|
+
return this.hasPollValue ? this.pollValue : false;
|
|
12
|
+
}
|
|
13
|
+
connect() {
|
|
14
|
+
let element = this.el;
|
|
15
|
+
if (isTurboFrame(element)) {
|
|
16
|
+
if (!!element.src) {
|
|
17
|
+
throw new Error('The provided <turbo-frame> element has no `src` attribute.');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw new Error('Expected controller to be mounted on a <turbo-frame> element.');
|
|
22
|
+
}
|
|
23
|
+
if (this._poll) {
|
|
24
|
+
requestAnimationFrame(() => useTimeout(this, this.refresh, this.intervalValue));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
refresh(event) {
|
|
28
|
+
event?.preventDefault();
|
|
29
|
+
let element = this.el;
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
element.reload();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -7,15 +7,19 @@ export class CountdownController extends BaseController {
|
|
|
7
7
|
static targets = ["years", "months", "days", "hours", "minutes", "seconds"];
|
|
8
8
|
static classes = ["countingDown", "ended"];
|
|
9
9
|
// Instance Data
|
|
10
|
-
|
|
10
|
+
_timeout = null;
|
|
11
11
|
get _removeUnused() {
|
|
12
12
|
return this.hasRemoveUnusedValue ? this.removeUnusedValue : false;
|
|
13
13
|
}
|
|
14
14
|
get _deadlineDate() {
|
|
15
15
|
return new Date(this.deadlineValue);
|
|
16
16
|
}
|
|
17
|
+
initialize() {
|
|
18
|
+
this._tick = this._tick.bind(this);
|
|
19
|
+
}
|
|
17
20
|
connect() {
|
|
18
|
-
this.
|
|
21
|
+
this._timeout = setTimeout(this._tick, 1000);
|
|
22
|
+
console.log(this._timeout);
|
|
19
23
|
installClassMethods(this);
|
|
20
24
|
this.addCountingDownClasses();
|
|
21
25
|
}
|
|
@@ -26,8 +30,8 @@ export class CountdownController extends BaseController {
|
|
|
26
30
|
}
|
|
27
31
|
deadlineValueChanged() {
|
|
28
32
|
// Countdown had previously ended, restart ticking. Updating mid-tick will just work.
|
|
29
|
-
if (this.
|
|
30
|
-
this.
|
|
33
|
+
if (this._timeout == null) {
|
|
34
|
+
this._timeout = setTimeout(this._tick, 1000);
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
_tick() {
|
|
@@ -41,9 +45,11 @@ export class CountdownController extends BaseController {
|
|
|
41
45
|
this.removeCountingDownClasses();
|
|
42
46
|
this.addEndedClasses();
|
|
43
47
|
this.dispatchEvent(this.el, "countdown:ended");
|
|
48
|
+
return;
|
|
44
49
|
}
|
|
45
50
|
else {
|
|
46
51
|
distance = intervalToDuration({ start: this._deadlineDate, end: now });
|
|
52
|
+
this._timeout = setTimeout(this._tick, 1000);
|
|
47
53
|
}
|
|
48
54
|
if (this.hasYearsTarget) {
|
|
49
55
|
this._updateTarget(this.yearsTarget, this._years(distance));
|
|
@@ -70,9 +76,9 @@ export class CountdownController extends BaseController {
|
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
_clearTick() {
|
|
73
|
-
if (this.
|
|
74
|
-
|
|
75
|
-
this.
|
|
79
|
+
if (this._timeout) {
|
|
80
|
+
clearTimeout(this._timeout);
|
|
81
|
+
this._timeout = null;
|
|
76
82
|
}
|
|
77
83
|
}
|
|
78
84
|
_updateTarget(target, value) {
|
package/dist/mixins/index.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useMixin } from "./create_mixin";
|
|
2
|
+
export function useResizeObserver(controller, element, handler, options) {
|
|
3
|
+
handler = handler.bind(controller);
|
|
4
|
+
let observer = new ResizeObserver(handler);
|
|
5
|
+
let setup = () => observer.observe(element, options);
|
|
6
|
+
let teardown = () => observer.disconnect();
|
|
7
|
+
useMixin(controller, setup, teardown);
|
|
8
|
+
return teardown;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"ruby on rails",
|
|
10
10
|
"ruby-on-rails"
|
|
11
11
|
],
|
|
12
|
-
"version": "0.9.
|
|
12
|
+
"version": "0.9.8",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": {
|
|
15
15
|
"name": "Sub-Xaero",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"test:treeshake": "agadoo dist"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@babel/runtime": "^7.20.
|
|
37
|
+
"@babel/runtime": "^7.20.13",
|
|
38
38
|
"@hotwired/stimulus": "^3.2.1",
|
|
39
39
|
"date-fns": "^2.29.3",
|
|
40
40
|
"dialog-polyfill": "^0.5.6",
|
|
@@ -46,11 +46,11 @@
|
|
|
46
46
|
"@types/lodash-es": "^4.17.6",
|
|
47
47
|
"@types/smoothscroll-polyfill": "^0.3.1",
|
|
48
48
|
"agadoo": "^3.0.0",
|
|
49
|
-
"cypress": "^12.1
|
|
49
|
+
"cypress": "^12.5.1",
|
|
50
50
|
"fast-glob": "^3.2.12",
|
|
51
|
-
"rimraf": "^
|
|
51
|
+
"rimraf": "^4.1.2",
|
|
52
52
|
"standard-version": "^9.5.0",
|
|
53
|
-
"typescript": "^4.
|
|
54
|
-
"vite": "^4.
|
|
53
|
+
"typescript": "^4.9.5",
|
|
54
|
+
"vite": "^4.1.1"
|
|
55
55
|
}
|
|
56
56
|
}
|