Skip to content

Commit

Permalink
Move inline JavaScript to Webpack entry points
Browse files Browse the repository at this point in the history
Issue #111
  • Loading branch information
TomaszGasior committed Sep 27, 2020
1 parent 690f065 commit c545e92
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 121 deletions.
71 changes: 71 additions & 0 deletions assets/js/radio-station-edit-add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Enforce leading zeros in typed values ("87.6" -> "87.60").
// Do not use "input" event. It looks better but makes manual typing hard.
// Works only in Webkit/Blink. "input.valueAsNumber" condition needed for MSEdge.
// See: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6367416/
function setupDecimalInputs()
{
let decimalInputs = document.querySelectorAll('input.decimal');

decimalInputs.forEach(input => {
let decimals = (input.step.match(/\..*/) || [''])[0].length - 1;

if (decimals && input.valueAsNumber != undefined) {
let updateFixed = () => {
input.value = input.valueAsNumber.toFixed(decimals);
};

input.addEventListener('change', updateFixed);
updateFixed();
}
});
}

// Keep original "0.01" step for values smaller than 74, otherwise use "0.05".
// This way entering usual frequencies is simpler while OIRT frequencies are
// available too. See https://github.com/TomaszGasior/RadioLista-v3/issues/35
function setupFrequencyInput()
{
let frequencyInput = document.querySelector('.frequency-input');

const REPLACED_STEP = '0.05';
const ORIGINAL_STEP = frequencyInput.step;

let updateStep = () => {
if (frequencyInput.value >= 74) {
if (frequencyInput.step != REPLACED_STEP) {
frequencyInput.step = REPLACED_STEP;
frequencyInput.min = REPLACED_STEP;
}
}
else if (frequencyInput.step != ORIGINAL_STEP) {
frequencyInput.step = ORIGINAL_STEP;
frequencyInput.min = ORIGINAL_STEP;
}
};

updateStep();
frequencyInput.addEventListener('input', updateStep);
frequencyInput.addEventListener('blur', updateStep);
}

function setupLocalityInput()
{
let localityTypeInput = document.querySelector('.locality-type-input');
let localityCityWrapper = document.querySelector('.locality-city-wrapper');

const LOCALITY_COUNTRY = localityTypeInput.dataset.localityCountryValue;

let updateFieldVisibility = () => {
localityCityWrapper.hidden = (localityTypeInput.value == LOCALITY_COUNTRY);
};

updateFieldVisibility();
localityTypeInput.addEventListener('change', updateFieldVisibility);
localityTypeInput.addEventListener('blur', updateFieldVisibility);
}

document.addEventListener('DOMContentLoaded', () => {
setupDecimalInputs();
setupFrequencyInput();
setupLocalityInput();
});
19 changes: 19 additions & 0 deletions assets/js/radio-table-remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function setupRemoveConfirmDialog()
{
let input = document.querySelector('.radio-table-remove-confirm');

let confirmDialog = () => {
if (input.checked && false === window.confirm(input.dataset.confirmMessage)) {
input.checked = false;
input.blur();
}

input.removeEventListener('change', confirmDialog);
};

input.addEventListener('change', confirmDialog);
}

document.addEventListener('DOMContentLoaded', () => {
setupRemoveConfirmDialog();
});
41 changes: 38 additions & 3 deletions assets/js/radio-table-settings.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
import { RadioTableColumnsUI } from './src/RadioTableColumnsUI.js';

function setupCustomThemeInput()
{
let themeSelectorInput = document.querySelector('.radio-table-theme');
let customThemeFieldsWrapper = document.querySelector('.radio-table-custom-theme-wrapper');

const CUSTOM_THEME_NAME = themeSelectorInput.dataset.customThemeName;

let updateFieldVisibility = () => {
customThemeFieldsWrapper.hidden = (themeSelectorInput.value != CUSTOM_THEME_NAME);
};

updateFieldVisibility();
themeSelectorInput.addEventListener('change', updateFieldVisibility);
themeSelectorInput.addEventListener('blur', updateFieldVisibility);
}

function setupCustomWidthInput()
{
let widthTypeInput = document.querySelector('.radio-table-width-type');
let customWidthFieldWrapper = document.querySelector('.radio-table-custom-width-wrapper');

const WIDTH_CUSTOM = widthTypeInput.dataset.customWidthValue;

let updateFieldVisibility = () => {
customWidthFieldWrapper.hidden = (widthTypeInput.value != WIDTH_CUSTOM);
};

updateFieldVisibility();
widthTypeInput.addEventListener('change', updateFieldVisibility);
widthTypeInput.addEventListener('blur', updateFieldVisibility);
}

document.addEventListener('DOMContentLoaded', () => {
let container = document.querySelector('.radio-table-columns');
let radioTableColumnsContainer = document.querySelector('.radio-table-columns');

if (container) {
new RadioTableColumnsUI(container);
if (radioTableColumnsContainer) {
new RadioTableColumnsUI(radioTableColumnsContainer);
}

setupCustomThemeInput();
setupCustomWidthInput();
});
20 changes: 0 additions & 20 deletions templates/common/form/custom_types.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@
'min': step,
}) %}
{{ block('integer_unit_widget') }}

{# Enforce leading zeros in typed values ("87.6" -> "87.60"). #}
{# Do not use "input" event, it looks better but makes manual typing hard. #}
{# Works only in Webkit/Blink. "input.valueAsNumber" condition needed for MSEdge. #}
{# See: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6367416/ #}
<script>
(function(){
let input = document.getElementById('{{ id }}');
let decimals = (input.step.match(/\..*/) || [''])[0].length - 1;
if (decimals && input.valueAsNumber != undefined) {
let updateFixed = () => {
input.value = input.valueAsNumber.toFixed(decimals);
};
input.addEventListener('change', updateFixed);
updateFixed();
}
})();
</script>
{% endblock decimal_unit_widget %}

{% block radio_table_columns_row %}
Expand Down
27 changes: 10 additions & 17 deletions templates/radio_station/_radio_station_tabs.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
<div>
<h2>{{ 'radio_station.edit.heading.general'|trans }}</h2>

{{ form_row(form.frequency, {'unit_label': get_frequency_label(radio_station.radioTable.frequencyUnit)}) }}
{{ form_row(form.frequency, {
attr: { 'class': 'frequency-input' },
'unit_label': get_frequency_label(radio_station.radioTable.frequencyUnit),
}) }}
{{ form_row(form.name) }}
{{ form_row(form.radioGroup) }}
{{ form_row(form.country) }}
Expand All @@ -16,25 +19,15 @@
{{ form_row(form.power, {'unit_label': 'kW'}) }}
{{ form_row(form.polarization) }}
{{ form_row(form.type) }}
{{ form_row(form.localityType, {'attr': {'class': 'locality-type-input'}}) }}

{{ form_row(form.localityType, {'attr': {
'class': 'locality-type-input',
'data-locality-country-value': constant('TYPE_COUNTRY', radio_station.locality),
}}) }}

<div class="locality-city-wrapper">
{{ form_row(form.localityCity) }}
</div>

<script>
(function(){
const LOCALITY_COUNTRY = '{{ constant('TYPE_COUNTRY', radio_station.locality) }}';
let localityTypeInput = document.querySelector('.locality-type-input');
let localityCityWrapper = document.querySelector('.locality-city-wrapper');
let updateFieldVisibility = () => {
localityCityWrapper.hidden = (localityTypeInput.value == LOCALITY_COUNTRY);
};
updateFieldVisibility();
localityTypeInput.addEventListener('change', updateFieldVisibility);
localityTypeInput.addEventListener('blur', updateFieldVisibility);
})();
</script>
</div>
<div>
<h2>{{ 'radio_station.edit.heading.reception'|trans }}</h2>
Expand Down
32 changes: 0 additions & 32 deletions templates/radio_station/_radio_station_tabs_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,3 @@
</div>
{% endif %}
{% endblock %}

{% block _radio_station_edit_frequency_widget %}
{{ block('decimal_unit_widget') }}

{# Keep original "0.01" step for values smaller than 74, otherwise use "0.05". #}
{# This way entering usual frequencies is simpler while OIRT frequencies are #}
{# available too. See https://github.com/TomaszGasior/RadioLista-v3/issues/35 #}
<script>
(function(){
let frequencyInput = document.getElementById('{{ id }}');
const REPLACED_STEP = '0.05';
const ORIGINAL_STEP = frequencyInput.step;
let updateStep = () => {
if (frequencyInput.value >= 74) {
if (frequencyInput.step != REPLACED_STEP) {
frequencyInput.step = REPLACED_STEP;
frequencyInput.min = REPLACED_STEP;
}
}
else if (frequencyInput.step != ORIGINAL_STEP) {
frequencyInput.step = ORIGINAL_STEP;
frequencyInput.min = ORIGINAL_STEP;
}
};
updateStep();
frequencyInput.addEventListener('input', updateStep);
frequencyInput.addEventListener('blur', updateStep);
})();
</script>
{% endblock %}
4 changes: 4 additions & 0 deletions templates/radio_station/add.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

{% block page_title %}{{ 'radio_station.add.title'|trans }}{% endblock %}

{% block head_closing %}
{{ encore_entry_script_tags('radio-station-edit-add') }}
{% endblock %}

{% block context_menu %}
<ul>
<li>
Expand Down
4 changes: 4 additions & 0 deletions templates/radio_station/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

{% block page_title %}{{ 'radio_station.edit.title'|trans }}{% endblock %}

{% block head_closing %}
{{ encore_entry_script_tags('radio-station-edit-add') }}
{% endblock %}

{% block context_menu %}
<ul>
<li>
Expand Down
31 changes: 11 additions & 20 deletions templates/radio_table/remove.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

{% block page_title %}{{ 'radio_table.remove.title'|trans }}{% endblock %}

{% block head_closing %}
{{ encore_entry_script_tags('radio-table-remove') }}
{% endblock %}

{% block context_menu %}
<ul>
<li>
Expand Down Expand Up @@ -65,30 +69,17 @@

{{ form_start(form_radio_table) }}

<div class="radio-table-remove-confirm">
{{ form_row(form_radio_table.confirm, {label_translation_parameters: {
'%name%': radio_table.name,
}}) }}
</div>
{{ form_row(form_radio_table.confirm, {
label_translation_parameters: { '%name%': radio_table.name },
attr: {
'class': 'radio-table-remove-confirm',
'data-confirm-message': 'radio_table.remove.text.confirm_alert'|trans,
},
}) }}

<button>{{ 'radio_table.remove.action.radio_table'|trans }}</button>

{{ form_end(form_radio_table) }}

<script>
(function(){
let input = document.querySelector('.radio-table-remove-confirm input');
let confirmDialog = () => {
if (input.checked && false === window.confirm("{{ 'radio_table.remove.text.confirm_alert'|trans }}")) {
input.checked = false;
input.blur();
}
input.removeEventListener('change', confirmDialog);
};
input.addEventListener('change', confirmDialog);
})();
</script>
</div>
</div>
{% endblock %}
37 changes: 8 additions & 29 deletions templates/radio_table/settings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@

{% set jscolor_settings = '{mode:\'HSV\', required:false, hash:true, borderRadius:0, borderWidth:0, shadowBlur:2}' %}

{{ form_row(form.appearanceTheme, {attr: {'class': 'radio-table-theme'}}) }}
{{ form_row(form.appearanceTheme, {attr: {
'class': 'radio-table-theme',
'data-custom-theme-name': '',
}}) }}

{# style="" is used as dirty hack for box-shadow removing. #}
<div class="radio-table-custom-theme-wrapper">
Expand All @@ -100,39 +103,15 @@
{{ form_row(form.appearanceBackgroundImage) }}
</div>

{{ form_row(form.appearanceWidthType, {attr: {'class': 'radio-table-width-type'}}) }}
{{ form_row(form.appearanceWidthType, {attr: {
'class': 'radio-table-width-type',
'data-custom-width-value': constant('WIDTH_CUSTOM', radio_table.appearance),
}}) }}

<div class="radio-table-custom-width-wrapper">
{{ form_row(form.appearanceCustomWidth, {'unit_label': 'px'}) }}
</div>

<script>
(function(){
const CUSTOM_THEME_NAME = '';
let themeSelectorField = document.querySelector('.radio-table-theme');
let customThemeFieldsWrapper = document.querySelector('.radio-table-custom-theme-wrapper');
let updateFieldVisibility = () => {
customThemeFieldsWrapper.hidden = (themeSelectorField.value != CUSTOM_THEME_NAME);
};
updateFieldVisibility();
themeSelectorField.addEventListener('change', updateFieldVisibility);
themeSelectorField.addEventListener('blur', updateFieldVisibility);
})();
(function(){
const WIDTH_CUSTOM = '{{ constant('WIDTH_CUSTOM', radio_table.appearance) }}';
let localityTypeInput = document.querySelector('.radio-table-width-type');
let localityCityWrapper = document.querySelector('.radio-table-custom-width-wrapper');
let updateFieldVisibility = () => {
localityCityWrapper.hidden = (localityTypeInput.value != WIDTH_CUSTOM);
};
updateFieldVisibility();
localityTypeInput.addEventListener('change', updateFieldVisibility);
localityTypeInput.addEventListener('blur', updateFieldVisibility);
})();
</script>

{{ form_row(form.appearanceCollapsedComments) }}
</div>

Expand Down
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Encore
.addEntry('common', './assets/js/common.js')
.addEntry('radio-table-show', './assets/js/radio-table-show.js')
.addEntry('radio-table-settings', './assets/js/radio-table-settings.js')
.addEntry('radio-table-remove', './assets/js/radio-table-remove.js')
.addEntry('radio-station-edit-add', './assets/js/radio-station-edit-add.js')
.addStyleEntry('admin', './assets/css/admin.css')
.addStyleEntry('error', './assets/css/error.css')

Expand Down

0 comments on commit c545e92

Please sign in to comment.