Skip to content

Commit d38f8fa

Browse files
seanpdoylenickcharlton
authored andcommitted
Implement client-side with Stimulus
Depend on [@hotwired/stimulus][] on the client-side. Add the client-side dependency for `@hotwired/stimulus` to the `package.json`, , then scaffold out the changes to the `application.js` and `controllers/index.js` modules. Next, migrate the jQuery-powered code into controllers: the `select` controller to integrate with `selectize.js` and the `table` controller to manage `<table>` elements. Finally, render each necessary field (`belongs_to`, `has_many`, `polymorphic`, `select`) with the `[data-controller="select"]` attribute (powered by a new `Field#html_controller` method. [@hotwired/stimulus]: https://stimulus.hotwired.dev
1 parent a742521 commit d38f8fa

File tree

31 files changed

+2676
-129
lines changed

31 files changed

+2676
-129
lines changed

app/assets/builds/administrate/application.js

Lines changed: 2542 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/builds/administrate/application.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/javascripts/administrate/application.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ import "./add_jquery";
33
import {} from "jquery-ujs";
44
import "selectize/dist/js/selectize.min.js";
55

6-
import "./components/associative";
7-
import "./components/select";
8-
import "./components/table";
6+
import "./controllers";

app/assets/javascripts/administrate/components/associative.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

app/assets/javascripts/administrate/components/select.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Application } from "@hotwired/stimulus";
2+
3+
const application = Application.start();
4+
5+
// Configure Stimulus development experience
6+
application.debug = false;
7+
window.Stimulus = application;
8+
9+
export { application };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { application } from "./application";
2+
3+
import SelectController from "./select_controller";
4+
import TableController from "./table_controller";
5+
6+
application.register("select", SelectController);
7+
application.register("table", TableController);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Controller } from "@hotwired/stimulus";
2+
import $ from "jquery";
3+
4+
export default class extends Controller {
5+
connect() {
6+
$(this.element).selectize({});
7+
}
8+
};

app/assets/javascripts/administrate/components/table.js renamed to app/assets/javascripts/administrate/controllers/table_controller.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
$(function() {
2-
var keycodes = { space: 32, enter: 13 };
1+
import { Controller } from "@hotwired/stimulus";
2+
import $ from "jquery";
33

4-
var visitDataUrl = function(event) {
4+
var keycodes = { space: 32, enter: 13 };
5+
6+
export default class extends Controller {
7+
visitDataUrl(event) {
58
if (event.type == "click" ||
69
event.keyCode == keycodes.space ||
710
event.keyCode == keycodes.enter) {
@@ -16,8 +19,5 @@ $(function() {
1619
window.location = window.location.protocol + '//' + window.location.host + dataUrl;
1720
}
1821
}
19-
};
20-
21-
$("table").on("click", ".js-table-row", visitDataUrl);
22-
$("table").on("keydown", ".js-table-row", visitDataUrl);
23-
});
22+
}
23+
};

app/views/administrate/application/_collection.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ to display a collection of resources in an HTML table.
1818
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
1919
%>
2020

21-
<table aria-labelledby="<%= table_title %>">
21+
<table aria-labelledby="<%= table_title %>" data-controller="table" data-action="click->table#visitDataUrl keydown->table#visitDataUrl">
2222
<thead>
2323
<tr>
2424
<% collection_presenter.attribute_types.each do |attr_name, attr_type| %>

0 commit comments

Comments
 (0)