You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.collection.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import "jquery-ui"
  2. import "./form.scss"
  3. $(document).ready(function () {
  4. initCollectionWidget();
  5. });
  6. function initCollectionWidget() {
  7. $('.field-collection[data-prototype]').each(function (i, collectionWidget) {
  8. setCollectionWidgetSortable($(collectionWidget));
  9. reindexKeyCollectionWidget($(collectionWidget));
  10. setCollectionWidgetAdd($(collectionWidget));
  11. setCollectionWidgetDelete($(collectionWidget));
  12. });
  13. }
  14. function setCollectionWidgetAdd($collectionWidget) {
  15. if ($collectionWidget.data('allow-add')) {
  16. $collectionWidget.find('.field-collection-add').on('click', function (e) {
  17. // grab the prototype template
  18. var newWidget = $collectionWidget.attr('data-prototype');
  19. // replace the "__name__" used in the id and name of the prototype
  20. // with a number that's unique to your emails
  21. // end name attribute looks like name="contact[emails][2]"
  22. newWidget = newWidget.replace(/__name__/g, getNumItems($collectionWidget));
  23. // create a new list element and add it to the list
  24. $collectionWidget.find('.form-widget-compound .field-collection-group').append(newWidget);
  25. $collectionWidget.find('.field-collection-item:last').find('.field-position').val(getNumItems($collectionWidget));
  26. reindexKeyCollectionWidget($collectionWidget);
  27. setCollectionWidgetDelete($collectionWidget);
  28. $collectionWidget.trigger('collection-add-item');
  29. $collectionWidget.data('num-items', $collectionWidget.data('num-items') + 1);
  30. $collectionWidget.find('.collection-empty').hide();
  31. });
  32. }
  33. }
  34. function setCollectionWidgetDelete($collectionWidget) {
  35. if ($collectionWidget.data('allow-delete')) {
  36. $collectionWidget.find('.field-collection-delete').off('click');
  37. $collectionWidget.find('.field-collection-delete').on('click', function () {
  38. $(this).parents('.form-group:first').remove();
  39. reindexKeyCollectionWidget($collectionWidget);
  40. if(getNumItems($collectionWidget)==0)$collectionWidget.find('.collection-empty').show();
  41. });
  42. }
  43. }
  44. function getNumItems($collectionWidget) {
  45. if ($collectionWidget.data('reindex-key')) {
  46. return $collectionWidget.find('.field-collection-item').length;
  47. } else {
  48. return $collectionWidget.data('num-items');
  49. }
  50. }
  51. function reindexKeyCollectionWidget($collectionWidget) {
  52. if ($collectionWidget.data('reindex-key')) {
  53. $collectionWidget.find('.field-collection-item').each(function (i, item) {
  54. $(item).find('input,textarea').each(function (y, field) {
  55. let $field = $(field);
  56. //Chanegment ID
  57. let posId = Tools.indexOfLastDigit($field.prop('id'));
  58. let idPrefix = $field.prop('id').substr(0, posId);
  59. let idSuffix = $field.prop('id').substr(posId + 1);
  60. $field.prop('id', idPrefix + i + idSuffix);
  61. //Chanegment Name
  62. let posName = Tools.indexOfLastDigit($field.prop('name'));
  63. let namePrefix = $field.prop('name').substr(0, posName);
  64. let nameSuffix = $field.prop('name').substr(posName + 1);
  65. $field.prop('name', namePrefix + i + nameSuffix);
  66. });
  67. });
  68. }
  69. }
  70. function setCollectionWidgetSortable($collectionWidget) {
  71. if ($collectionWidget.data('sortable')) {
  72. $collectionWidget.find('.field-collection-group').sortable({
  73. "handle" : '.lc-btn-sortable',
  74. cancel: ''
  75. });
  76. $collectionWidget.find('.field-collection-group').on("sortupdate", function (event, ui) {
  77. $collectionWidget.find('.field-collection-group>div').each(function (index, item) {
  78. $(item).find('.field-position').val(index);
  79. });
  80. });
  81. }
  82. }