|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
-
- import "jquery-ui"
- import "./form.scss"
-
-
- $(document).ready(function () {
- initCollectionWidget();
- });
-
-
- function initCollectionWidget() {
-
- $('.field-collection[data-prototype]').each(function (i, collectionWidget) {
- setCollectionWidgetSortable($(collectionWidget));
- reindexKeyCollectionWidget($(collectionWidget));
-
- setCollectionWidgetAdd($(collectionWidget));
- setCollectionWidgetDelete($(collectionWidget));
- });
- }
-
- function setCollectionWidgetAdd($collectionWidget) {
-
- if ($collectionWidget.data('allow-add')) {
- $collectionWidget.find('.field-collection-add').on('click', function (e) {
- // grab the prototype template
- var newWidget = $collectionWidget.attr('data-prototype');
- // replace the "__name__" used in the id and name of the prototype
- // with a number that's unique to your emails
- // end name attribute looks like name="contact[emails][2]"
- newWidget = newWidget.replace(/__name__/g, getNumItems($collectionWidget));
-
- // create a new list element and add it to the list
- $collectionWidget.find('.form-widget-compound .field-collection-group').append(newWidget);
- $collectionWidget.find('.field-collection-item:last').find('.field-position').val(getNumItems($collectionWidget));
-
- reindexKeyCollectionWidget($collectionWidget);
- setCollectionWidgetDelete($collectionWidget);
- $collectionWidget.trigger('collection-add-item');
-
- $collectionWidget.data('num-items', $collectionWidget.data('num-items') + 1);
- $collectionWidget.find('.collection-empty').hide();
- });
- }
- }
-
- function setCollectionWidgetDelete($collectionWidget) {
- if ($collectionWidget.data('allow-delete')) {
- $collectionWidget.find('.field-collection-delete').off('click');
- $collectionWidget.find('.field-collection-delete').on('click', function () {
- $(this).parents('.form-group:first').remove();
- reindexKeyCollectionWidget($collectionWidget);
- if(getNumItems($collectionWidget)==0)$collectionWidget.find('.collection-empty').show();
- });
- }
- }
-
- function getNumItems($collectionWidget) {
- if ($collectionWidget.data('reindex-key')) {
- return $collectionWidget.find('.field-collection-item').length;
- } else {
- return $collectionWidget.data('num-items');
- }
- }
-
- function reindexKeyCollectionWidget($collectionWidget) {
- if ($collectionWidget.data('reindex-key')) {
- $collectionWidget.find('.field-collection-item').each(function (i, item) {
- $(item).find('input,textarea').each(function (y, field) {
- let $field = $(field);
- //Chanegment ID
- let posId = Tools.indexOfLastDigit($field.prop('id'));
- let idPrefix = $field.prop('id').substr(0, posId);
- let idSuffix = $field.prop('id').substr(posId + 1);
- $field.prop('id', idPrefix + i + idSuffix);
-
- //Chanegment Name
- let posName = Tools.indexOfLastDigit($field.prop('name'));
- let namePrefix = $field.prop('name').substr(0, posName);
- let nameSuffix = $field.prop('name').substr(posName + 1);
- $field.prop('name', namePrefix + i + nameSuffix);
- });
- });
- }
- }
-
- function setCollectionWidgetSortable($collectionWidget) {
- if ($collectionWidget.data('sortable')) {
- $collectionWidget.find('.field-collection-group').sortable({
- "handle" : '.lc-btn-sortable',
- cancel: ''
- });
- $collectionWidget.find('.field-collection-group').on("sortupdate", function (event, ui) {
- $collectionWidget.find('.field-collection-group>div').each(function (index, item) {
- $(item).find('.field-position').val(index);
- });
- });
- }
- }
|