Skip to content

Commit 9f37401

Browse files
move javascript in template to external file
1 parent 968957c commit 9f37401

File tree

7 files changed

+230
-243
lines changed

7 files changed

+230
-243
lines changed

assets/js/app-database.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
$(document).on('click', '.back', function(event) {
2+
event.preventDefault();
3+
4+
$(this).parents('.step').addClass('d-none');
5+
6+
var href= $(this).attr('href');
7+
$(href).removeClass('d-none');
8+
});
9+
10+
$(document).on('submit', 'form', function(event) {
11+
event.preventDefault();
12+
13+
var form = $(this);
14+
15+
var body = $('#form-connect').serialize();
16+
body = body + '&' + $('#form-mappings').serialize();
17+
18+
if ('form-mappings' == form.attr('id')) {
19+
$('#step-mappings').addClass('d-none');
20+
$('#step-loading').removeClass('d-none');
21+
}
22+
23+
fetch(form.attr('action'), {
24+
'credentials': 'include',
25+
'headers': {
26+
'Content-Type': 'application/x-www-form-urlencoded',
27+
},
28+
'method': 'post',
29+
'body': body
30+
}).then(function(response) {
31+
return response.json();
32+
}).then(function(json) {
33+
if (true == json.exception) {
34+
createToast(json.message);
35+
36+
if ('form-mappings' == form.attr('id')) {
37+
$('#step-loading').addClass('d-none');
38+
$('#step-mappings').removeClass('d-none');
39+
}
40+
41+
} else {
42+
if ('form-connect' == form.attr('id')) {
43+
var columns = '<option value="">-</option>';
44+
for (var key in json.columns) {
45+
var column = json.columns[key];
46+
columns += '<option value="' + column + '">' + column + '</option>';
47+
}
48+
49+
$(document).find('.select-column').each(function() {
50+
$(this).html(columns);
51+
var field = $(this).data('field');
52+
if (json.columns.includes(field)) {
53+
$(this).val(field);
54+
}
55+
});
56+
57+
$('#step-connect').addClass('d-none');
58+
$('#step-mappings').removeClass('d-none');
59+
}
60+
61+
if ('form-mappings' == form.attr('id')) {
62+
$('#step-completed h3 .badge').text(json.documents);
63+
64+
$('#step-mappings').addClass('d-none');
65+
$('#step-loading').addClass('d-none');
66+
$('#step-completed').removeClass('d-none');
67+
68+
$('#errors').addClass('d-none');
69+
$('#step-completed tbody').html('');
70+
71+
if (0 < (json.errors).length) {
72+
for (var key in json.errors) {
73+
var error = json.errors[key];
74+
$('#step-completed tbody').append(`<tr><td>${error['_id']}</td><td>${error['status']}</td><td>${error['message']}</td></tr>`);
75+
}
76+
77+
$('#errors').removeClass('d-none');
78+
}
79+
}
80+
}
81+
}).catch(function() {
82+
});
83+
});

assets/js/app-enrich.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function refresh() {
2+
var indices = $('#data_indices').val();
3+
if (0 < indices.length) {
4+
indices = indices.join(',');
5+
6+
fetch(app_base_url + 'admin/indices/' + indices + '/mappings/fetch', {
7+
credentials: 'include',
8+
method: 'get'
9+
}).then(function(response) {
10+
return response.json();
11+
}).then(function(json) {
12+
var options = [];
13+
for (var k in json) {
14+
if (false == options.includes(json[k])) {
15+
options.push(json[k]);
16+
}
17+
}
18+
options.sort();
19+
20+
$(document).find('.update-fields').each(function() {
21+
var select = $(this);
22+
var selectedOptions = [];
23+
select.find('option:selected').each(function() {
24+
selectedOptions.push($(this).val());
25+
});
26+
27+
select.find('option').remove();
28+
29+
if (typeof select.attr('multiple') == 'undefined') {
30+
var newOption = new Option('-', '', false, false);
31+
select.append(newOption).trigger('change');
32+
}
33+
34+
for (var k in options) {
35+
var selected = false;
36+
if (true == selectedOptions.includes(options[k])) {
37+
selected = true;
38+
}
39+
var newOption = new Option(options[k], options[k], selected, selected);
40+
select.append(newOption).trigger('change');
41+
}
42+
});
43+
}).catch(function() {
44+
});
45+
} else {
46+
$(document).find('.update-fields').each(function() {
47+
$(this).find('option').remove();
48+
});
49+
}
50+
}
51+
52+
refresh();
53+
54+
$(document).on('change', '#data_indices', function(event) {
55+
refresh();
56+
});

assets/js/app-subscription.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
function deleteSubscription(url) {
2+
window.location.href = url;
3+
}
4+
5+
function testNotification(url) {
6+
fetch(url, {
7+
credentials: 'include',
8+
method: 'GET',
9+
mode: 'cors',
10+
}).then(function(response) {
11+
return response.json();
12+
}).then(function(json) {
13+
if ('undefined' !== typeof json.message) {
14+
createToast(json.message);
15+
} else {
16+
createToast('Sent');
17+
}
18+
})
19+
.catch(function(error) {
20+
console.log(error);
21+
});
22+
}
23+
24+
function getSubscription() {
25+
if (true == serviceWorkerEnabled) {
26+
navigator.serviceWorker.ready.then(function(ServiceWorkerRegistration) {
27+
if ('pushManager' in ServiceWorkerRegistration) {
28+
ServiceWorkerRegistration.pushManager.getSubscription()
29+
.then(function(PushSubscription) {
30+
if (PushSubscription && 'object' === typeof PushSubscription) {
31+
$(document).find('.actions').each(function() {
32+
var deleteButton = $(this).find('button');
33+
var unsubscribeButton = $(this).find('.unsubscribe-button');
34+
if (PushSubscription.endpoint == unsubscribeButton.data('endpoint')) {
35+
unsubscribeButton.removeClass('d-none');
36+
deleteButton.addClass('d-none');
37+
}
38+
});
39+
}
40+
});
41+
}
42+
});
43+
}
44+
}
45+
46+
function pushManagerUnsubscribe(url) {
47+
if (true == serviceWorkerEnabled) {
48+
navigator.serviceWorker.ready.then(function(ServiceWorkerRegistration) {
49+
if ('pushManager' in ServiceWorkerRegistration) {
50+
ServiceWorkerRegistration.pushManager.getSubscription()
51+
.then(function(PushSubscription) {
52+
if (PushSubscription && 'object' === typeof PushSubscription) {
53+
PushSubscription.unsubscribe()
54+
.then(function() {
55+
deleteSubscription(url);
56+
})
57+
.catch(function(error) {
58+
console.log(error);
59+
});
60+
}
61+
});
62+
}
63+
});
64+
}
65+
}
66+
67+
$(document).ready(function() {
68+
getSubscription();
69+
70+
$(document).on('click', '.unsubscribe-button', function(event) {
71+
event.preventDefault();
72+
pushManagerUnsubscribe($(this).attr('href'));
73+
});
74+
75+
$(document).on('click', '.test-notification-push', function(event) {
76+
event.preventDefault();
77+
testNotification($(this).attr('href'));
78+
});
79+
});

templates/Modules/app_index_database_import/app_index_database_import_index.html.twig

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818

1919
{% block main_content %}
20+
{{ encore_entry_script_tags('app-database-js') }}
21+
2022
{% embed 'Embed/card_embed.html.twig' %}
2123
{% import 'Import/app_import.html.twig' as appImport %}
2224
{% block content %}
@@ -179,95 +181,3 @@
179181
{% endblock %}
180182
{% endembed %}
181183
{% endblock %}
182-
183-
{% block scripts %}
184-
{{ parent() }}
185-
186-
<script type="text/javascript">
187-
var index = '{{ index.name }}';
188-
189-
$(document).on('click', '.back', function(event) {
190-
event.preventDefault();
191-
192-
$(this).parents('.step').addClass('d-none');
193-
194-
var href= $(this).attr('href');
195-
$(href).removeClass('d-none');
196-
});
197-
198-
$(document).on('submit', 'form', function(event) {
199-
event.preventDefault();
200-
201-
var form = $(this);
202-
203-
var body = $('#form-connect').serialize();
204-
body = body + '&' + $('#form-mappings').serialize();
205-
206-
if ('form-mappings' == form.attr('id')) {
207-
$('#step-mappings').addClass('d-none');
208-
$('#step-loading').removeClass('d-none');
209-
}
210-
211-
fetch(form.attr('action'), {
212-
'credentials': 'include',
213-
'headers': {
214-
'Content-Type': 'application/x-www-form-urlencoded',
215-
},
216-
'method': 'post',
217-
'body': body
218-
}).then(function(response) {
219-
return response.json();
220-
}).then(function(json) {
221-
if (true == json.exception) {
222-
createToast(json.message);
223-
224-
if ('form-mappings' == form.attr('id')) {
225-
$('#step-loading').addClass('d-none');
226-
$('#step-mappings').removeClass('d-none');
227-
}
228-
229-
} else {
230-
if ('form-connect' == form.attr('id')) {
231-
var columns = '<option value="">-</option>';
232-
for (var key in json.columns) {
233-
var column = json.columns[key];
234-
columns += '<option value="' + column + '">' + column + '</option>';
235-
}
236-
237-
$(document).find('.select-column').each(function() {
238-
$(this).html(columns);
239-
var field = $(this).data('field');
240-
if (json.columns.includes(field)) {
241-
$(this).val(field);
242-
}
243-
});
244-
245-
$('#step-connect').addClass('d-none');
246-
$('#step-mappings').removeClass('d-none');
247-
}
248-
249-
if ('form-mappings' == form.attr('id')) {
250-
$('#step-completed h3 .badge').text(json.documents);
251-
252-
$('#step-mappings').addClass('d-none');
253-
$('#step-loading').addClass('d-none');
254-
$('#step-completed').removeClass('d-none');
255-
256-
$('#errors').addClass('d-none');
257-
$('#step-completed tbody').html('');
258-
259-
if (0 < (json.errors).length) {
260-
for (var key in json.errors) {
261-
var error = json.errors[key];
262-
$('#step-completed tbody').append(`<tr><td>${error['_id']}</td><td>${error['status']}</td><td>${error['message']}</td></tr>`);
263-
}
264-
265-
$('#errors').removeClass('d-none');
266-
}
267-
}
268-
}
269-
}).catch(function() {
270-
});
271-
});
272-
</script>
273-
{% endblock %}

0 commit comments

Comments
 (0)