-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simpleClone.js
109 lines (88 loc) · 2.95 KB
/
jquery.simpleClone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*!
* jQuery Simple Clone.
*
* @category Plugins
* @author KUCKLU
* @license Licensed under MIT (http://www.opensource.org/licenses/mit-license.php)
* @copyright (c) 2022 KUCKLU
* @version 1.2.0
*/
(function ($) {
'use strict';
const defaults = {
copyValue : true,
cloneLimit : false,
limitMessageClass : 'simpleClone-clnLmt',
limitMessageText : 'Maximum count has been reached.',
targetClass : 'simpleClone-clnElem',
addButtonWrapClass: 'simpleClone-btnWrap',
removeButtonClass : 'simpleClone-rmvBtn',
removeButtonText : 'Remove',
filterCloneElement: null,
onClone : null,
onComplete : null,
onRemove : null,
onCompleteRemove : null
};
$.fn.simpleClone = function (options) {
const opts = $.extend({}, defaults, options);
$(document).on('click', '.' + opts.removeButtonClass, function (e) {
e.preventDefault();
const $self = $(this);
const $parent = $self.parent();
if ($.isFunction(opts.onRemove)) {
opts.onRemove.call(this, opts);
}
$parent.remove();
if ($.isFunction(opts.onCompleteRemove)) {
opts.onCompleteRemove.call(this, opts);
}
});
this.on('click', function (e) {
e.preventDefault();
const $self = $(this);
const $wrap = $self.closest('.' + opts.addButtonWrapClass);
const $targetWrap = $wrap.prev();
const $targets = $targetWrap.find('.' + opts.targetClass);
const $target = $targets.first();
const count = $target.parent().children('.' + opts.targetClass).length + 1;
if (opts.cloneLimit === false || (typeof opts.cloneLimit === 'number' && count <= opts.cloneLimit)) {
let $clone = $target.clone(true);
const rmvButton = $('<button>')
.attr('type', 'button')
.addClass(opts.removeButtonClass === null ? defaults.removeButtonClass : defaults.removeButtonClass + ' ' + opts.removeButtonClass)
.text(opts.removeButtonText);
$clone.append(rmvButton);
if ($.isFunction(opts.filterCloneElement)) {
$clone = opts.filterCloneElement.call(this, $clone, opts);
}
$clone.find('select').each(function (index, item) {
$(item).val($target.find('select').eq(index).val());
});
if (opts.copyValue === false) {
$clone.find('input:not("input[type=radio], input[type=button], input[type=submit]"), textarea, select').each(function (index, item) {
$(item).val('');
});
}
if ($.isFunction(opts.onClone)) {
opts.onClone.call(this, $clone, opts);
}
$target.parent().append($clone);
if ($.isFunction(opts.onComplete)) {
opts.onComplete.call(this, $clone, opts);
}
} else {
const message = $('<span>')
.addClass(opts.limitMessageClass)
.text(opts.limitMessageText);
if ($self.next('.' + opts.limitMessageClass).length === 0) {
$self.after(message);
setTimeout(function () {
$self.next('.' + opts.limitMessageClass).fadeOut().remove();
}, 4000);
}
}
});
return this;
};
})(jQuery);