55 * @author KUCKLU
66 * @license Licensed under MIT (http://www.opensource.org/licenses/mit-license.php)
77 * @copyright (c) 2022 KUCKLU
8- * @version 1.1.2
8+ * @version 1.2.0
99 */
1010( function ( $ ) {
1111 'use strict' ;
1212
13- $ . fn . simpleClone = function ( options ) {
14- const opts = $ . extend ( { } , $ . fn . simpleClone . defaults , options ) ;
15- const elems = this ;
16-
17- return elems . each ( function ( ) {
18- $ ( this ) . on ( 'click' , function ( e ) {
19- e . preventDefault ( ) ;
20-
21- const $self = $ ( this ) ;
22- const $wrap = $self . closest ( '.' + opts . addButtonWrapClass ) ;
23- const $targetWrap = $wrap . prev ( ) ;
24- const $targets = $targetWrap . find ( '.' + opts . targetClass ) ;
25- const $target = $targets . first ( ) ;
26- const count = $target . parent ( ) . children ( '.' + opts . targetClass ) . length + 1 ;
27-
28- if ( opts . cloneLimit === false || ( typeof opts . cloneLimit === 'number' && count <= opts . cloneLimit ) ) {
29- let $clone = $target . clone ( true ) ;
30- const rmvButton = document . createElement ( 'button' ) ;
31- rmvButton . type = 'button' ;
32- rmvButton . className = opts . removeButtonClass === null ? $ . fn . simpleClone . defaults . removeButtonClass : $ . fn . simpleClone . defaults . removeButtonClass + ' ' + opts . removeButtonClass ;
33- rmvButton . innerText = opts . removeButtonText ;
34-
35- $clone . append ( rmvButton ) ;
36-
37- $clone . find ( 'select' ) . each ( function ( index , item ) {
38- $ ( item ) . val ( $target . find ( 'select' ) . eq ( index ) . val ( ) ) ;
39- } ) ;
40-
41- if ( opts . copyValue === false ) {
42- $clone . find ( 'input:not("input[type=radio], input[type=button], input[type=submit]"), textarea, select' ) . each ( function ( index , item ) {
43- $ ( item ) . val ( '' ) ;
44- } ) ;
45- }
46-
47- if ( $ . isFunction ( opts . filterCloneElement ) ) {
48- $clone = opts . filterCloneElement . call ( elems , $clone , opts ) ;
49- }
50-
51- if ( $ . isFunction ( opts . onClone ) ) {
52- opts . onClone . call ( elems , $clone , opts ) ;
53- }
54-
55- $target . parent ( ) . append ( $clone ) ;
56-
57- if ( $ . isFunction ( opts . onComplete ) ) {
58- opts . onComplete . call ( elems , $clone , opts ) ;
59- }
60- } else {
61- const message = document . createElement ( 'span' ) ;
62- message . className = opts . limitMessageClass ;
63- message . innerText = opts . limitMessageText ;
64-
65- if ( $self . next ( '.' + opts . limitMessageClass ) . length === 0 ) {
66- $self . after ( message ) ;
67-
68- setTimeout ( function ( ) {
69- $self . next ( '.' + opts . limitMessageClass ) . fadeOut ( ) . remove ( ) ;
70- } , 4000 ) ;
71- }
72- }
73- } ) ;
74-
75- $ ( document ) . on ( 'click' , '.' + $ . fn . simpleClone . defaults . removeButtonClass , function ( e ) {
76- e . preventDefault ( ) ;
77-
78- const $self = $ ( this ) ;
79-
80- if ( $ . isFunction ( opts . onRemove ) ) {
81- opts . onRemove . call ( elems , opts ) ;
82- }
83-
84- $self . parent ( ) . remove ( ) ;
85-
86- if ( $ . isFunction ( opts . onCompleteRemove ) ) {
87- opts . onCompleteRemove . call ( elems , opts ) ;
88- }
89- } ) ;
90- } ) ;
91- } ;
92-
93- $ . fn . simpleClone . defaults = {
13+ const defaults = {
9414 copyValue : true ,
9515 cloneLimit : false ,
9616 limitMessageClass : 'simpleClone-clnLmt' ,
10525 onRemove : null ,
10626 onCompleteRemove : null
10727 } ;
108- } ) ( jQuery ) ;
28+
29+ $ . fn . simpleClone = function ( options ) {
30+ const opts = $ . extend ( { } , defaults , options ) ;
31+
32+ $ ( document ) . on ( 'click' , '.' + opts . removeButtonClass , function ( e ) {
33+ e . preventDefault ( ) ;
34+
35+ const $self = $ ( this ) ;
36+ const $parent = $self . parent ( ) ;
37+
38+ if ( $ . isFunction ( opts . onRemove ) ) {
39+ opts . onRemove . call ( this , opts ) ;
40+ }
41+
42+ $parent . remove ( ) ;
43+
44+ if ( $ . isFunction ( opts . onCompleteRemove ) ) {
45+ opts . onCompleteRemove . call ( this , opts ) ;
46+ }
47+ } ) ;
48+
49+ this . on ( 'click' , function ( e ) {
50+ e . preventDefault ( ) ;
51+
52+ const $self = $ ( this ) ;
53+ const $wrap = $self . closest ( '.' + opts . addButtonWrapClass ) ;
54+ const $targetWrap = $wrap . prev ( ) ;
55+ const $targets = $targetWrap . find ( '.' + opts . targetClass ) ;
56+ const $target = $targets . first ( ) ;
57+ const count = $target . parent ( ) . children ( '.' + opts . targetClass ) . length + 1 ;
58+
59+ if ( opts . cloneLimit === false || ( typeof opts . cloneLimit === 'number' && count <= opts . cloneLimit ) ) {
60+ let $clone = $target . clone ( true ) ;
61+
62+ const rmvButton = $ ( '<button>' )
63+ . attr ( 'type' , 'button' )
64+ . addClass ( opts . removeButtonClass === null ? defaults . removeButtonClass : defaults . removeButtonClass + ' ' + opts . removeButtonClass )
65+ . text ( opts . removeButtonText ) ;
66+
67+ $clone . append ( rmvButton ) ;
68+
69+ if ( $ . isFunction ( opts . filterCloneElement ) ) {
70+ $clone = opts . filterCloneElement . call ( this , $clone , opts ) ;
71+ }
72+
73+ $clone . find ( 'select' ) . each ( function ( index , item ) {
74+ $ ( item ) . val ( $target . find ( 'select' ) . eq ( index ) . val ( ) ) ;
75+ } ) ;
76+
77+ if ( opts . copyValue === false ) {
78+ $clone . find ( 'input:not("input[type=radio], input[type=button], input[type=submit]"), textarea, select' ) . each ( function ( index , item ) {
79+ $ ( item ) . val ( '' ) ;
80+ } ) ;
81+ }
82+
83+ if ( $ . isFunction ( opts . onClone ) ) {
84+ opts . onClone . call ( this , $clone , opts ) ;
85+ }
86+
87+ $target . parent ( ) . append ( $clone ) ;
88+
89+ if ( $ . isFunction ( opts . onComplete ) ) {
90+ opts . onComplete . call ( this , $clone , opts ) ;
91+ }
92+ } else {
93+ const message = $ ( '<span>' )
94+ . addClass ( opts . limitMessageClass )
95+ . text ( opts . limitMessageText ) ;
96+
97+ if ( $self . next ( '.' + opts . limitMessageClass ) . length === 0 ) {
98+ $self . after ( message ) ;
99+
100+ setTimeout ( function ( ) {
101+ $self . next ( '.' + opts . limitMessageClass ) . fadeOut ( ) . remove ( ) ;
102+ } , 4000 ) ;
103+ }
104+ }
105+ } ) ;
106+
107+ return this ;
108+ } ;
109+ } ) ( jQuery ) ;
0 commit comments