Skip to content

Commit

Permalink
Extract some javascript from main Admin and base script
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed May 1, 2021
1 parent 5c4ca88 commit 614ae74
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 88 deletions.
48 changes: 48 additions & 0 deletions assets/js/admin-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*!
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

let config = null;
let translations = null;

/**
* Reads configuration placed on the html with the
* data attribute 'data-sonata-admin'
*/
const read = () => {
const data = jQuery('[data-sonata-admin]').data('sonata-admin');

config = data.config;
translations = data.translations;
};

/**
* @param {string} key
* @returns {mixed}
*/
const getConfig = (key) => {
if (config === null) {
read();
}

return config[key];
};

/**
* @param {string} key
* @returns {mixed}
*/
const getTranslation = (key) => {
if (translations == null) {
read();
}

return translations[key];
};

export { getConfig, getTranslation, read };
28 changes: 28 additions & 0 deletions assets/js/admin-flash-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const addFlashMessageListener = () => {
document.querySelectorAll('.read-more-state').forEach((element) => {
element.addEventListener('change', (event) => {
const label = document.querySelector(`label[for="${element.id}"]`);
const labelMore = label.querySelector('.more');
const labelLess = label.querySelector('.less');

if (event.target.checked) {
labelMore.classList.add('hide');
labelLess.classList.remove('hide');
} else {
labelMore.classList.remove('hide');
labelLess.classList.add('hide');
}
});
});
};

export default addFlashMessageListener;
35 changes: 35 additions & 0 deletions assets/js/admin-icheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import jQuery from 'jquery';
import { getConfig } from './admin-config';
import log from './admin-log';

const setupICheck = (subject) => {
if (getConfig('USE_ICHECK')) {
log('[core|setup_icheck] configure iCheck on', subject);

const inputs = jQuery('input[type="checkbox"]:not(.read-more-state, label.btn > input, [data-sonata-icheck="false"]), input[type="radio"]:not(label.btn > input, [data-sonata-icheck="false"])', subject);

inputs.iCheck({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
});

// In case some checkboxes were already checked (for instance after moving
// back in the browser's session history) update iCheck checkboxes.
if (subject === window.document) {
setTimeout(() => {
inputs.iCheck('update');
}, 0);
}
}
};

export default setupICheck;
27 changes: 27 additions & 0 deletions assets/js/admin-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { getConfig } from './admin-config';

/**
* @param {...any} args
* @returns {void}
*/
const log = (...args) => {
if (!getConfig('DEBUG')) {
return;
}

const message = `[Sonata.Admin] ${args.join(', ')}`;

// eslint-disable-next-line no-console
console.log(message);
};

export default log;
76 changes: 27 additions & 49 deletions assets/js/Admin.js → assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* file that was distributed with this source code.
*/

import log from './admin-log';
import setupICheck from './admin-icheck';
import addFlashMessageListener from './admin-flash-message';

const Admin = {

collectionCounters: [],
Expand All @@ -21,12 +25,12 @@ const Admin = {
*/
shared_setup(subject) {
Admin.read_config();
Admin.log('[core|shared_setup] Register services on', subject);
log('[core|shared_setup] Register services on', subject);
Admin.setup_ie10_polyfill();
Admin.set_object_field_value(subject);
Admin.add_filters(subject);
Admin.setup_select2(subject);
Admin.setup_icheck(subject);
setupICheck(subject);
Admin.setup_checkbox_range_selection(subject);
Admin.setup_xeditable(subject);
Admin.setup_form_tabs_for_errors(subject);
Expand Down Expand Up @@ -68,7 +72,7 @@ const Admin = {
return this.translations[key];
},
setup_list_modal(modal) {
Admin.log('[core|setup_list_modal] configure modal on', modal);
log('[core|setup_list_modal] configure modal on', modal);
// this will force relation modal to open list of entity in a wider modal
// to improve readability
jQuery('div.modal-dialog', modal).css({
Expand All @@ -92,7 +96,7 @@ const Admin = {
},
setup_select2(subject) {
if (Admin.get_config('USE_SELECT2')) {
Admin.log('[core|setup_select2] configure Select2 on', subject);
log('[core|setup_select2] configure Select2 on', subject);

jQuery('select:not([data-sonata-select2="false"])', subject).each((index, element) => {
const select = jQuery(element);
Expand Down Expand Up @@ -138,21 +142,7 @@ const Admin = {
}
},
setup_icheck(subject) {
if (Admin.get_config('USE_ICHECK')) {
Admin.log('[core|setup_icheck] configure iCheck on', subject);

const inputs = jQuery('input[type="checkbox"]:not(label.btn > input, [data-sonata-icheck="false"]), input[type="radio"]:not(label.btn > input, [data-sonata-icheck="false"])', subject);
inputs.iCheck({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
});

// In case some checkboxes were already checked (for instance after moving
// back in the browser's session history) update iCheck checkboxes.
if (subject === window.document) {
setTimeout(() => { inputs.iCheck('update'); }, 0);
}
}
setupICheck(subject);
},
/**
* Setup checkbox range selection
Expand All @@ -163,7 +153,7 @@ const Admin = {
* @param {string|Object} subject The html selector or object on which function should be applied
*/
setup_checkbox_range_selection(subject) {
Admin.log('[core|setup_checkbox_range_selection] configure checkbox range selection on', subject);
log('[core|setup_checkbox_range_selection] configure checkbox range selection on', subject);

let previousIndex;
const useICheck = Admin.get_config('USE_ICHECK');
Expand Down Expand Up @@ -205,7 +195,7 @@ const Admin = {
},

setup_xeditable(subject) {
Admin.log('[core|setup_xeditable] configure xeditable on', subject);
log('[core|setup_xeditable] configure xeditable on', subject);
jQuery('.x-editable', subject).editable({
emptyclass: 'editable-empty btn btn-sm btn-default',
emptytext: '<i class="fas fa-pencil-alt"></i>',
Expand All @@ -229,21 +219,8 @@ const Admin = {
});
},

/**
* render log message
* @param mixed
*/
log(...args) {
if (!Admin.get_config('DEBUG')) {
return;
}

const msg = `[Sonata.Admin] ${Array.prototype.join.call(args, ', ')}`;
if (window.console && window.console.log) {
window.console.log(msg);
} else if (window.opera && window.opera.postError) {
window.opera.postError(msg);
}
log(...args);
},

stopEvent(event) {
Expand All @@ -253,7 +230,7 @@ const Admin = {
},

add_filters(subject) {
Admin.log('[core|add_filters] configure filters on', subject);
log('[core|add_filters] configure filters on', subject);

function updateCounter() {
const count = jQuery('a.sonata-toggle-filter .fa-check-square', subject).length;
Expand All @@ -269,7 +246,7 @@ const Admin = {
return;
}

Admin.log('[core|add_filters] handle filter container: ', jQuery(event.target).attr('filter-container'));
log('[core|add_filters] handle filter container: ', jQuery(event.target).attr('filter-container'));

const filtersContainer = jQuery(`#${jQuery(event.currentTarget).attr('filter-container')}`);

Expand Down Expand Up @@ -340,7 +317,7 @@ const Admin = {
* @param subject
*/
set_object_field_value(subject) {
Admin.log('[core|set_object_field_value] set value field on', subject);
log('[core|set_object_field_value] set value field on', subject);

this.log(jQuery('a.sonata-ba-edit-inline', subject));
jQuery('a.sonata-ba-edit-inline', subject).on('click', (event) => {
Expand All @@ -365,7 +342,7 @@ const Admin = {
},

setup_collection_counter(subject) {
Admin.log('[core|setup_collection_counter] setup collection counter', subject);
log('[core|setup_collection_counter] setup collection counter', subject);

// Count and save element of each collection
const highestCounterRegexp = new RegExp('_([0-9]+)[^0-9]*$');
Expand Down Expand Up @@ -419,7 +396,7 @@ const Admin = {
},

setup_per_page_switcher(subject) {
Admin.log('[core|setup_per_page_switcher] setup page switcher', subject);
log('[core|setup_per_page_switcher] setup page switcher', subject);

jQuery('select.per-page').on('change', () => {
jQuery('input[type=submit]').hide();
Expand All @@ -429,7 +406,7 @@ const Admin = {
},

setup_form_tabs_for_errors(subject) {
Admin.log('[core|setup_form_tabs_for_errors] setup form tab\'s errors', subject);
log('[core|setup_form_tabs_for_errors] setup form tab\'s errors', subject);

// Switch to first tab with server side validation errors on page load
jQuery('form', subject).each((index, element) => {
Expand All @@ -449,7 +426,7 @@ const Admin = {
},

show_form_first_tab_with_errors(form, errorSelector) {
Admin.log('[core|show_form_first_tab_with_errors] show first tab with errors', form);
log('[core|show_form_first_tab_with_errors] show first tab with errors', form);

const tabs = form.find('.nav-tabs a'); let
firstTabWithErrors;
Expand All @@ -474,7 +451,7 @@ const Admin = {
},

setup_inline_form_errors(subject) {
Admin.log('[core|setup_inline_form_errors] show first tab with errors', subject);
log('[core|setup_inline_form_errors] show first tab with errors', subject);

const deleteCheckboxSelector = '.sonata-ba-field-inline-table [id$="_delete"][type="checkbox"]';

Expand All @@ -491,7 +468,7 @@ const Admin = {
* Disable inline form errors when the row is marked for deletion
*/
switch_inline_form_errors(subject) {
Admin.log('[core|switch_inline_form_errors] switch_inline_form_errors', subject);
log('[core|switch_inline_form_errors] switch_inline_form_errors', subject);

const row = subject.closest('.sonata-ba-field-inline-table');
const errors = row.find('.sonata-ba-field-error-messages');
Expand All @@ -510,7 +487,7 @@ const Admin = {
},

setup_tree_view(subject) {
Admin.log('[core|setup_tree_view] setup tree view', subject);
log('[core|setup_tree_view] setup tree view', subject);

jQuery('ul.js-treeview', subject).treeView();
},
Expand Down Expand Up @@ -590,7 +567,7 @@ const Admin = {

setup_sticky_elements(subject) {
if (Admin.get_config('USE_STICKYFORMS')) {
Admin.log('[core|setup_sticky_elements] setup sticky elements on', subject);
log('[core|setup_sticky_elements] setup sticky elements on', subject);

const topNavbar = jQuery(subject).find('.navbar-static-top');
const wrapper = jQuery(subject).find('.content-wrapper');
Expand Down Expand Up @@ -724,7 +701,7 @@ const Admin = {
},

setup_readmore_elements(subject) {
Admin.log('[core|setup_readmore_elements] setup readmore elements on', subject);
log('[core|setup_readmore_elements] setup readmore elements on', subject);

jQuery(subject).find('.sonata-readmore').each((index, element) => {
const $element = jQuery(element);
Expand All @@ -742,7 +719,7 @@ const Admin = {
},

setup_form_submit(subject) {
Admin.log('[core|setup_form_submit] setup form submit on', subject);
log('[core|setup_form_submit] setup form submit on', subject);

jQuery(subject).find('form').on('submit', (event) => {
const form = jQuery(event.target);
Expand Down Expand Up @@ -821,6 +798,7 @@ jQuery(() => {
Admin.setup_collection_buttons(document);
Admin.setup_view_tabs_changer();
Admin.shared_setup(document);
addFlashMessageListener();
});

jQuery(window).on('resize', () => {
Expand All @@ -829,7 +807,7 @@ jQuery(window).on('resize', () => {

jQuery(document).on('sonata-admin-append-form-element', (event) => {
Admin.setup_select2(event.target);
Admin.setup_icheck(event.target);
setupICheck(event.target);
Admin.setup_collection_counter(event.target);
});

Expand Down
3 changes: 1 addition & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ import 'readmore-js';
import 'masonry-layout';

// SonataAdmin custom scripts
import './Admin';
import './admin';
import './jquery.confirmExit';
import './treeview';
import './sidebar';
import './base';

// Create global $ and jQuery variables to be used outside this script
global.$ = $;
Expand Down
Loading

0 comments on commit 614ae74

Please sign in to comment.