Skip to content

Commit be66f27

Browse files
committed
refactor: Move Sprocket JS files to Webpack
wip
1 parent 6cc642c commit be66f27

File tree

15 files changed

+109
-18
lines changed

15 files changed

+109
-18
lines changed

app/assets/javascripts/application.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,16 @@
1313
//= require rails-timeago
1414
//= require locales/jquery.timeago.de.js
1515
//
16-
// lib/assets
17-
//= require flash
18-
//= require color_mode_picker
19-
//
2016
// app/assets
2117
// --> Include some assets first, as they are used by other assets.
2218
// --> Hence, the order specified here is important.
2319
//
24-
// 1. Some common base functions and monkey patches
25-
//= require base
26-
// 2. Programming groups are required by "channels/synchronized_editor_channel.js"
20+
// 1. Programming groups are required by "channels/synchronized_editor_channel.js"
2721
//= require programming_groups
28-
// 3. The turtle library is required by "editor/turtle.js"
22+
// 2. The turtle library is required by "editor/turtle.js"
2923
//= require turtle
30-
// 4. Some channels are required by "editor/editor.js.erb"
31-
//= require_tree ./channels
32-
// 5. Require the editor components, as needed by "./editor.js" and "./community_solution.js"
24+
// 3. Some channels are required by "editor/editor.js.erb"
25+
// 4. Require the editor components, as needed by "./editor.js" and "./community_solution.js"
3326
//= require_tree ./editor
3427
//
3528
// All remaining assets are loaded in alphabetical order

app/assets/javascripts/base.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
Array.prototype.includes = function(element) {
2-
return this.indexOf(element) !== -1;
3-
};
4-
51
window.CodeOcean = {
62
refresh: function() {
73
Turbo.visit(window.location.pathname);

app/javascript/application.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,10 @@ window.ace = ace; // Publish ace in global namespace
8888
// Turbo
8989
import '@hotwired/turbo-rails';
9090
import './turbo-migration';
91+
92+
// ActionCable
93+
import "@rails/actioncable"
94+
import "./channels"
95+
96+
// Import of Rails sprocket assets with minimal changes.
97+
import 'sprocket_assets'

app/javascript/base.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Array.prototype.includes = function(element) {
2+
return this.indexOf(element) !== -1;
3+
};
4+
5+
window.CodeOcean = {
6+
refresh: function() {
7+
Turbo.visit(window.location.pathname);
8+
}
9+
};
10+
11+
const ANIMATION_DURATION = 500;
12+
13+
$.isController = function(name) {
14+
return $('div[data-controller="' + name + '"]').isPresent();
15+
};
16+
17+
$.fn.isPresent = function() {
18+
return this.length > 0;
19+
};
20+
21+
$.fn.scrollTo = function(selector) {
22+
$(this).animate({
23+
scrollTop: $(document.querySelector(selector)).offset().top - $(this).offset().top + $(this).scrollTop()
24+
}, ANIMATION_DURATION);
25+
};
26+
27+
$(document).on('turbo-migration:load', function() {
28+
// Update all CSRF tokens on the page to reduce InvalidAuthenticityToken errors
29+
// See https://github.com/rails/jquery-ujs/issues/456 for details
30+
$.rails.refreshCSRFTokens();
31+
$('.reloadCurrentPage').on('click', function() {
32+
window.location.reload();
33+
});
34+
35+
// Set current user and current contributor
36+
window.current_user = JSON.parse($('meta[name="current-user"]')?.attr('content') || null);
37+
window.current_contributor = JSON.parse($('meta[name="current-contributor"]')?.attr('content') || null);
38+
39+
// Set locale for all JavaScript functions
40+
const htmlTag = $('html')
41+
I18n.defaultLocale = htmlTag.data('default-locale');
42+
I18n.locale = htmlTag.attr('lang');
43+
jQuery.timeago.settings.lang = I18n.locale;
44+
45+
// Initialize Sentry
46+
const sentrySettings = $('meta[name="sentry"]')
47+
48+
// Workaround for Turbo: We must not re-initialize the Relay object when visiting another page
49+
if (sentrySettings && sentrySettings.data() && sentrySettings.data()['enabled'] && Sentry.getReplay() === undefined) {
50+
Sentry.init({
51+
dsn: sentrySettings.data('dsn'),
52+
attachStacktrace: true,
53+
release: sentrySettings.data('release'),
54+
environment: sentrySettings.data('environment'),
55+
tracesSampleRate: 1.0,
56+
replaysSessionSampleRate: 0.0,
57+
replaysOnErrorSampleRate: 1.0,
58+
integrations: window.SentryIntegrations(),
59+
profilesSampleRate: 1.0,
60+
initialScope: scope =>{
61+
if (current_user) {
62+
scope.setUser(_.omit(current_user, 'displayname'));
63+
}
64+
return scope;
65+
}
66+
});
67+
}
68+
69+
// Enable sorttable again, as it is disabled otherwise by Turbo
70+
if (sorttable) {
71+
sorttable.init.done = false;
72+
sorttable.init();
73+
}
74+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Action Cable provides the framework to deal with WebSockets in Rails.
2+
// You can generate new channels where WebSocket features live using the `bin/rails generate channel` command.
3+
4+
import { createConsumer } from "@rails/actioncable"
5+
6+
export default createConsumer()

app/javascript/channels/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Import all the channels to be used by Action Cable
2+
const channels = require.context(".", true, /_channel\.js$/)
3+
channels.keys().forEach(channels)

app/assets/javascripts/channels/la_exercises.js renamed to app/javascript/channels/la_exercises_channel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import consumer from "./consumer"
2+
13
$(document).on('turbo-migration:load', function() {
24
if ($.isController('exercises') && $('.teacher_dashboard').isPresent()) {
35

@@ -18,7 +20,7 @@ $(document).on('turbo-migration:load', function() {
1820
const specific_channel = { channel: "LaExercisesChannel", exercise_id: exercise_id, study_group_id: study_group_id };
1921

2022

21-
App.la_exercise = App.cable.subscriptions.create(specific_channel, {
23+
consumer.subscriptions.create(specific_channel, {
2224
connected: function () {
2325
// Called when the subscription is ready for use on the server
2426
},

app/assets/javascripts/channels/pg_matching_channel.js renamed to app/javascript/channels/pg_matching_channel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import consumer from "./consumer";
2+
13
$(document).on('turbo-migration:load', function () {
24

35
if ($.isController('programming_groups') && window.location.pathname.includes('programming_groups/new')) {
46
const matching_page = $('#matching');
57
const exercise_id = matching_page.data('exercise-id');
68
const specific_channel = { channel: "PgMatchingChannel", exercise_id: exercise_id};
79

8-
App.pg_matching = App.cable.subscriptions.create(specific_channel, {
10+
consumer.subscriptions.create(specific_channel, {
911
connected() {
1012
// Called when the subscription is ready for use on the server
1113
},
File renamed without changes.

0 commit comments

Comments
 (0)