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 2, 2021
1 parent 0f0cf90 commit dfb4980
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 103 deletions.
57 changes: 57 additions & 0 deletions assets/js/admin-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*!
* 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;
let translations;

/**
* Reads configuration placed on the html with the
* data attribute 'data-sonata-admin'
*
* @returns {void}
*/
const read = () => {
const adminConfiguration = document.querySelector('[data-sonata-admin]');

config = null;
translations = null;

if (adminConfiguration !== null) {
const data = JSON.parse(adminConfiguration.dataset.sonataAdmin);

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

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

return config !== null ? config[key] : undefined;
};

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

return translations !== null ? translations[key] : undefined;
};

export { getConfig, getTranslation, read };
31 changes: 31 additions & 0 deletions assets/js/admin-flash-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* 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.
*/

/**
* @returns {void}
*/
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;
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.debug(message);
};

export default log;
39 changes: 39 additions & 0 deletions assets/js/admin-setup-icheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* 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';

/**
* @param {HTMLElement} subject
* @returns {void}
*/
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;
Loading

0 comments on commit dfb4980

Please sign in to comment.