Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

85 lines
4.0KB

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