您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

103 行
4.1KB

  1. import "jquery-ui-sortable"
  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 posIdPrefix = parseInt(SovTools.indexOfFirstDigit($field.prop('id')));
  58. let posIdSuffix = parseInt(SovTools.indexOfLastDigit($field.prop('id')));
  59. let idPrefix = $field.prop('id').substr(0, posIdPrefix);
  60. let idSuffix = $field.prop('id').substr(posIdSuffix + 1);
  61. $field.prop('id', idPrefix + i + idSuffix);
  62. //Chanegment Name
  63. let posNamePrefix = SovTools.indexOfFirstDigit($field.prop('name'));
  64. let posNameSuffix = SovTools.indexOfLastDigit($field.prop('name'));
  65. let namePrefix = $field.prop('name').substr(0, posNamePrefix);
  66. let nameSuffix = $field.prop('name').substr(posNameSuffix + 1);
  67. $field.prop('name', namePrefix + i + nameSuffix);
  68. });
  69. });
  70. }
  71. }
  72. function setCollectionWidgetSortable($collectionWidget) {
  73. if ($collectionWidget.data('sortable')) {
  74. $collectionWidget.find('.field-collection-group').sortable({
  75. "handle" : '.lc-btn-sortable',
  76. cancel: ''
  77. });
  78. $collectionWidget.find('.field-collection-group').on("sortupdate", function (event, ui) {
  79. $collectionWidget.find('.field-collection-group>div').each(function (index, item) {
  80. $(item).find('.field-position').val(index);
  81. });
  82. });
  83. }
  84. }