mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 05:44:24 +00:00
Collection: amélioration + lancement d'événements
This commit is contained in:
parent
49812d43c4
commit
d3d8f27c6d
@ -23,6 +23,12 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
|||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Available options :
|
||||||
|
*
|
||||||
|
* - `button_add_label`
|
||||||
|
* - `button_remove_label`
|
||||||
|
* - `identifier`: an identifier to identify the kind of collecton. Useful if some
|
||||||
|
* javascript should be launched associated to `add_entry`, `remove_entry` events.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
@ -34,7 +40,8 @@ class ChillCollectionType extends AbstractType
|
|||||||
$resolver
|
$resolver
|
||||||
->setDefaults([
|
->setDefaults([
|
||||||
'button_add_label' => 'Add an entry',
|
'button_add_label' => 'Add an entry',
|
||||||
'button_remove_label' => 'Remove entry'
|
'button_remove_label' => 'Remove entry',
|
||||||
|
'identifier' => ''
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +51,7 @@ class ChillCollectionType extends AbstractType
|
|||||||
$view->vars['button_remove_label'] = $options['button_remove_label'];
|
$view->vars['button_remove_label'] = $options['button_remove_label'];
|
||||||
$view->vars['allow_delete'] = (int) $options['allow_delete'];
|
$view->vars['allow_delete'] = (int) $options['allow_delete'];
|
||||||
$view->vars['allow_add'] = (int) $options['allow_add'];
|
$view->vars['allow_add'] = (int) $options['allow_add'];
|
||||||
|
$view->vars['identifier'] = $options['identifier'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getParent()
|
public function getParent()
|
||||||
|
23
Resources/public/js/collection/collection.scss
Normal file
23
Resources/public/js/collection/collection.scss
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
div.chill-collection {
|
||||||
|
ul.chill-collection__list {
|
||||||
|
list-style: none;
|
||||||
|
padding: 0;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
|
||||||
|
li.chill-collection__list__entry:nth-child(2n) {
|
||||||
|
background-color: var(--chill-light-gray);
|
||||||
|
padding: 0.5rem 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// all entries, except the last one
|
||||||
|
li.chill-collection__list__entry:nth-last-child(1n+2) {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
button.chill-collection__button--add {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,22 +1,46 @@
|
|||||||
/**
|
/**
|
||||||
* Exemple d'utilisation
|
* Javascript file which handle ChillCollectionType
|
||||||
|
*
|
||||||
|
* Two events are emitted by this module, both on window and on collection / ul.
|
||||||
|
*
|
||||||
|
* Collection (an UL element) and entry (a li element) are associated with those
|
||||||
|
* events.
|
||||||
*
|
*
|
||||||
* ```html
|
|
||||||
* <ul id="form_profile_formations">
|
|
||||||
* {% for formation in form.formations %}
|
|
||||||
* <li>
|
|
||||||
* <div data-removable data-label-remove-button="{{ 'Supprimer'|trans|e }}" >
|
|
||||||
* {{ form_row(formation) }}
|
|
||||||
* </div>
|
|
||||||
* </li>
|
|
||||||
* {% endfor %}
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <button data-add-target="form_profile_formations" data-form-prototype="{{ ('<div data-removable data-label-remove-button="' ~ 'Supprimer'|trans ~'">' ~ form_widget(form.formations.vars.prototype) ~ '</div>')|e }}" >{{ 'Ajouter une formation'|trans }}</button>
|
|
||||||
* ```
|
* ```
|
||||||
|
* window.addEventListener('collection-add-entry', function(e) {
|
||||||
|
* console.log(e.detail.collection);
|
||||||
|
* console.log(e.detail.entry);
|
||||||
|
* });
|
||||||
*
|
*
|
||||||
* Le javascript est initialisé dans `forms.js`
|
* window.addEventListener('collection-remove-entry', function(e) {
|
||||||
|
* console.log(e.detail.collection);
|
||||||
|
* console.log(e.detail.entry);
|
||||||
|
* });
|
||||||
*
|
*
|
||||||
|
* collection.addEventListener('collection-add-entry', function(e) {
|
||||||
|
* console.log(e.detail.collection);
|
||||||
|
* console.log(e.detail.entry);
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* collection.addEventListener('collection-remove-entry', function(e) {
|
||||||
|
* console.log(e.detail.collection);
|
||||||
|
* console.log(e.detail.entry);
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
require('./collection.scss');
|
||||||
|
|
||||||
|
class CollectionEvent {
|
||||||
|
constructor(collection, entry) {
|
||||||
|
this.collection = collection;
|
||||||
|
this.entry = entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {type} button
|
||||||
|
* @returns {handleAdd}
|
||||||
*/
|
*/
|
||||||
var handleAdd = function(button) {
|
var handleAdd = function(button) {
|
||||||
var
|
var
|
||||||
@ -24,32 +48,41 @@ var handleAdd = function(button) {
|
|||||||
prototype = button.dataset.formPrototype,
|
prototype = button.dataset.formPrototype,
|
||||||
collection = document.querySelector('ul[data-collection-name="'+form_name+'"]'),
|
collection = document.querySelector('ul[data-collection-name="'+form_name+'"]'),
|
||||||
entry = document.createElement('li'),
|
entry = document.createElement('li'),
|
||||||
|
event = new CustomEvent('collection-add-entry', { detail: { collection: collection, entry: entry } }),
|
||||||
counter = collection.childNodes.length,
|
counter = collection.childNodes.length,
|
||||||
content
|
content
|
||||||
;
|
;
|
||||||
content = prototype.replace(new RegExp('__name__', 'g'), counter);
|
content = prototype.replace(new RegExp('__name__', 'g'), counter);
|
||||||
entry.innerHTML = content;
|
entry.innerHTML = content;
|
||||||
|
entry.classList.add('chill-collection__list__entry');
|
||||||
initializeRemove(collection, entry);
|
initializeRemove(collection, entry);
|
||||||
collection.appendChild(entry);
|
collection.appendChild(entry);
|
||||||
|
|
||||||
|
collection.dispatchEvent(event);
|
||||||
|
window.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
var initializeRemove = function(collection, entry) {
|
var initializeRemove = function(collection, entry) {
|
||||||
var
|
var
|
||||||
button = document.createElement('button'),
|
button = document.createElement('button'),
|
||||||
|
isPersisted = entry.dataset.collectionIsPersisted,
|
||||||
content = collection.dataset.collectionButtonRemoveLabel,
|
content = collection.dataset.collectionButtonRemoveLabel,
|
||||||
allowDelete = collection.dataset.collectionAllowDelete
|
allowDelete = collection.dataset.collectionAllowDelete,
|
||||||
|
event = new CustomEvent('collection-remove-entry', { detail: { collection: collection, entry: entry } })
|
||||||
;
|
;
|
||||||
|
|
||||||
if (allowDelete == '0') {
|
if (allowDelete === '0' && isPersisted === '1') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
button.classList.add('sc-button', 'bt-delete');
|
||||||
button.textContent = content;
|
button.textContent = content;
|
||||||
|
|
||||||
button.addEventListener('click', function(e) {
|
button.addEventListener('click', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
entry.remove();
|
entry.remove();
|
||||||
console.log('click remove');
|
collection.dispatchEvent(event);
|
||||||
|
window.dispatchEvent(event);
|
||||||
});
|
});
|
||||||
|
|
||||||
entry.appendChild(button);
|
entry.appendChild(button);
|
||||||
@ -65,13 +98,12 @@ window.addEventListener('load', function() {
|
|||||||
let addButton = addButtons[i];
|
let addButton = addButtons[i];
|
||||||
addButton.addEventListener('click', function(e) {
|
addButton.addEventListener('click', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
console.log('click');
|
|
||||||
handleAdd(e.target);
|
handleAdd(e.target);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < collections.length; i ++) {
|
for (let i = 0; i < collections.length; i ++) {
|
||||||
let entries = collections[i].querySelectorAll('li'), entry;
|
let entries = collections[i].querySelectorAll('li');
|
||||||
|
|
||||||
for (let j = 0; j < entries.length; j ++) {
|
for (let j = 0; j < entries.length; j ++) {
|
||||||
initializeRemove(collections[i], entries[j]);
|
initializeRemove(collections[i], entries[j]);
|
||||||
@ -79,3 +111,5 @@ window.addEventListener('load', function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ $chill-yellow: #eec84a;
|
|||||||
$chill-orange: #e2793d;
|
$chill-orange: #e2793d;
|
||||||
$chill-red: #df4949;
|
$chill-red: #df4949;
|
||||||
$chill-gray: #ececec;
|
$chill-gray: #ececec;
|
||||||
$chill-beige :#cabb9f;
|
$chill-beige: #cabb9f;
|
||||||
$chill-pink :#dd506d;
|
$chill-pink: #dd506d;
|
||||||
$chill-dark-gray: #333333;
|
$chill-dark-gray: #333333;
|
||||||
$chill-light-gray: #b2b2b2;
|
$chill-light-gray: #b2b2b2;
|
||||||
$chill-llight-gray: $chill-light-gray;
|
$chill-llight-gray: $chill-light-gray;
|
||||||
@ -32,3 +32,34 @@ $yellow: $chill-yellow;
|
|||||||
$black: #111111;
|
$black: #111111;
|
||||||
$white: #ffffff;
|
$white: #ffffff;
|
||||||
$light-grey: $chill-light-gray;
|
$light-grey: $chill-light-gray;
|
||||||
|
|
||||||
|
/*
|
||||||
|
due to a bug in sass, we must re-declare the variable in css
|
||||||
|
(use of a sass variable after -- does not work)
|
||||||
|
*/
|
||||||
|
:root {
|
||||||
|
--chill-blue: #334d5c;
|
||||||
|
--chill-green: #43b29d;
|
||||||
|
--chill-green-dark: #328474;
|
||||||
|
--chill-yellow: #eec84a;
|
||||||
|
--chill-orange: #e2793d;
|
||||||
|
--chill-red: #df4949;
|
||||||
|
--chill-gray: #ececec;
|
||||||
|
--chill-beige: #cabb9f;
|
||||||
|
--chill-pink: #dd506d;
|
||||||
|
--chill-dark-gray: #333333;
|
||||||
|
--chill-light-gray: #b2b2b2;
|
||||||
|
--chill-llight-gray: #b2b2b2;
|
||||||
|
|
||||||
|
--dark-grey: #333333;
|
||||||
|
|
||||||
|
--orange: #e2793d;
|
||||||
|
--red: #df4949;
|
||||||
|
--green: #43b29d;
|
||||||
|
--blue: #334d5c;
|
||||||
|
--yellow: #eec84a;
|
||||||
|
|
||||||
|
--black: #111111;
|
||||||
|
--white: #ffffff;
|
||||||
|
--light-grey: #b2b2b2;
|
||||||
|
}
|
||||||
|
@ -160,17 +160,19 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block chill_collection_widget %}
|
{% block chill_collection_widget %}
|
||||||
<ul data-collection-name="{{ form.vars.name|escape('html_attr') }}" data-collection-button-remove-label="{{ form.vars.button_remove_label|trans|e }}" data-collection-allow-add="{{ form.vars.allow_add|escape('html_attr') }}" data-collection-allow-delete="{{ form.vars.allow_delete|escape('html_attr') }}">
|
<div class="chill-collection">
|
||||||
<li>
|
<ul class="chill-collection__list" data-collection-name="{{ form.vars.name|escape('html_attr') }}" data-collection-identifier="{{ form.vars.identifier|escape('html_attr') }}" data-collection-button-remove-label="{{ form.vars.button_remove_label|trans|e }}" data-collection-allow-add="{{ form.vars.allow_add|escape('html_attr') }}" data-collection-allow-delete="{{ form.vars.allow_delete|escape('html_attr') }}" >
|
||||||
<div>
|
{% for entry in form %}
|
||||||
{% for entry in form %}
|
<li class="chill-collection__list__entry" data-collection-is-persisted="1">
|
||||||
{{ form_widget(entry) }}
|
<div>
|
||||||
{% endfor %}
|
{{ form_widget(entry) }}
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
{% if form.vars.allow_add == 1 %}
|
|
||||||
<button data-collection-add-target="{{ form.vars.name|escape('html_attr') }}" data-form-prototype="{{ ('<div>' ~ form_widget(form.vars.prototype) ~ '</div>')|escape('html_attr') }}" >{{ form.vars.button_add_label|trans }}</button>
|
{% if form.vars.allow_add == 1 %}
|
||||||
|
<button class="chill-collection__button--add sc-button" data-collection-add-target="{{ form.vars.name|escape('html_attr') }}" data-form-prototype="{{ ('<div>' ~ form_widget(form.vars.prototype) ~ '</div>')|escape('html_attr') }}" >{{ form.vars.button_add_label|trans }}</button>
|
||||||
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user