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