-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract some javascript from main Admin and base script
- Loading branch information
1 parent
0f0cf90
commit dfb4980
Showing
8 changed files
with
188 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.