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.

86 lines
4.1KB

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