Le repo des sources pour le site web des JM2L
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.
 
 
 
 
 

120 lines
3.2 KiB

  1. /*
  2. * jQuery Repeatable Fields v1.3.1
  3. * http://www.rhyzz.com/repeatable-fields.html
  4. *
  5. * Copyright (c) 2014-2015 Rhyzz
  6. * License MIT
  7. */
  8. (function($) {
  9. $.fn.repeatable_fields = function(custom_settings) {
  10. var default_settings = {
  11. wrapper: '.wrapper',
  12. container: '.container',
  13. row: '.row',
  14. add: '.add',
  15. remove: '.remove',
  16. move: '.move',
  17. template: '.template',
  18. is_sortable: true,
  19. before_add: null,
  20. after_add: after_add,
  21. is_ready: null,
  22. before_remove: null,
  23. after_remove: null,
  24. sortable_options: null,
  25. }
  26. var settings = $.extend(default_settings, custom_settings);
  27. // Initialize all repeatable field wrappers
  28. initialize(this);
  29. function initialize(parent) {
  30. $(settings.wrapper, parent).each(function(index, element) {
  31. var wrapper = this;
  32. var container = $(wrapper).children(settings.container);
  33. // Disable all form elements inside the row template
  34. $(container).children(settings.template).hide().find(':input').each(function() {
  35. $(this).prop('disabled', true);
  36. });
  37. var row_count = $(container).children(settings.row).filter(function() {
  38. return !$(this).hasClass(settings.template.replace('.', ''));
  39. }).length;
  40. $(container).attr('data-rf-row-count', row_count);
  41. $(wrapper).on('click', settings.add, function(event) {
  42. event.stopImmediatePropagation();
  43. var row_template = $($(container).children(settings.template).clone().removeClass(settings.template.replace('.', ''))[0].outerHTML);
  44. // Enable all form elements inside the row template
  45. $(row_template).find(':input').each(function() {
  46. $(this).prop('disabled', false);
  47. });
  48. if(typeof settings.before_add === 'function') {
  49. settings.before_add(container);
  50. }
  51. var new_row = $(row_template).show().appendTo(container);
  52. if(typeof settings.after_add === 'function') {
  53. settings.after_add(container, new_row);
  54. }
  55. // The new row might have it's own repeatable field wrappers so initialize them too
  56. initialize(new_row);
  57. if(typeof settings.is_ready === 'function') {
  58. settings.is_ready(container, new_row);
  59. }
  60. });
  61. $(wrapper).on('click', settings.remove, function(event) {
  62. event.stopImmediatePropagation();
  63. var row = $(this).parents(settings.row).first();
  64. if(typeof settings.before_remove === 'function') {
  65. settings.before_remove(container, row);
  66. }
  67. row.remove();
  68. if(typeof settings.after_remove === 'function') {
  69. settings.after_remove(container);
  70. }
  71. });
  72. if(settings.is_sortable === true && typeof $.ui !== 'undefined' && typeof $.ui.sortable !== 'undefined') {
  73. var sortable_options = settings.sortable_options !== null ? settings.sortable_options : {};
  74. sortable_options.handle = settings.move;
  75. $(wrapper).find(settings.container).sortable(sortable_options);
  76. }
  77. });
  78. }
  79. function after_add(container, new_row) {
  80. var row_count = $(container).attr('data-rf-row-count');
  81. row_count++;
  82. $('*', new_row).each(function() {
  83. $.each(this.attributes, function(index, element) {
  84. this.value = this.value.replace(/{{row-count-placeholder}}/, row_count - 1);
  85. });
  86. });
  87. $(container).attr('data-rf-row-count', row_count);
  88. }
  89. }
  90. })(jQuery);