├── .gitignore
├── .gitattributes
├── README.md
├── jscripts
├── duplicateFields.min.js
└── duplicateFields.js
└── index.html
/.gitignore:
--------------------------------------------------------------------------------
1 | nbproject/
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Installation
2 |
3 | Include script *after* the jQuery library:
4 |
5 |
6 |
7 | ## Usage
8 | Basic:
9 |
19 |
20 | With callback:
21 |
40 |
41 |
42 |
43 | ## Description
44 | Very simple script to dublicate and remove your elements in HTML. Can be used in form fieldsets.
45 |
46 |
47 |
48 |
49 | ## [ Demo ](http://fire1.github.io/DuplicateElement/)
50 |
51 |
52 |
--------------------------------------------------------------------------------
/jscripts/duplicateFields.min.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * Copyright (C) 2015 Angel Zaprianov
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | * Duplicate Fields version: 1.1.2
17 | */
18 | !function(e){e.fn.duplicateElement=function(n){function t(e){e=e.parent(),e.find(n.class_remove).show(),e.find(n.class_remove).last().hide(),e.find(n.class_create).hide(),e.find(n.class_create).last().show()}function i(e,n){a(n),e.parent().append(n)}function a(a){console.log(a),a.find(n.class_create).unbind(),a.on("click",n.class_create,function(c){var l,o=e(this).parent(".dinamic-field"),r=e(this).parent();return o.length>0?l=o.clone():r.length>0&&(l=a.clone().addClass("dinamic-field")),i(a,l),s(a),"function"==typeof n.onCreate&&n.onCreate(l,e(this),c),t(a),c.preventDefault(),!1})}function s(i){i.on("click",n.class_remove,function(a){var s=e(this).parents(".dinamic-field"),c=e(this).parents(i);return s.length>0?s.remove():c.length>0&&(i.empty(),i.hide(),i.remove()),"function"==typeof n.onRemove&&n.onRemove(e(this)),t(i),a.preventDefault(),!1})}n=e.extend(e.fn.duplicateElement.defaults,n);var c=this;e(c).parent();return this.each(function(){var i=e(this);t(i),a(i),i.find(n.class_remove).first().hide(),s(i)})},e.fn.duplicateElement.defaults={tag_name:"div",tag_id:"#dinamic-fields",clone_model:"#clone-field-model",class_remove:".remove-this-fields",class_create:".create-new-fields",onCreate:"",onRemove:""}}(jQuery);
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | Duplicate element
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
37 |
38 |
39 |
40 |
41 |
42 |
Duplicate Element
43 |
44 |
Simple jQuery pugin for duplicating elements.
45 |
46 |
92 |
93 |
94 |
99 |
100 |
101 |
--------------------------------------------------------------------------------
/jscripts/duplicateFields.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * Copyright (C) 2015 Angel Zaprianov
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | * Duplicate Fields version: 1.1.2
17 | */
18 |
19 | (function ($) {
20 | $.fn.duplicateElement = function (options) {
21 | options = $.extend($.fn.duplicateElement.defaults, options);
22 | var main = this;
23 | var $main = $(main).parent();
24 |
25 | function handleNavigationButtons(ElementTarget) {
26 | ElementTarget = ElementTarget.parent();
27 | ElementTarget.find(options.class_remove).show();
28 | ElementTarget.find(options.class_remove).last().hide();
29 | ElementTarget.find(options.class_create).hide();
30 | ElementTarget.find(options.class_create).last().show();
31 | }
32 |
33 | /**
34 | * Generate new element
35 | * @param target
36 | * @param newElement
37 | */
38 | function createElement(target, newElement) {
39 | clickNewExecutor(newElement);
40 | target.parent().append(newElement);
41 | }
42 |
43 |
44 | /**
45 | * Generate new fild on click
46 | * @param ElementTarget
47 | */
48 | function clickNewExecutor(ElementTarget) {
49 | console.log(ElementTarget);
50 | ElementTarget.find(options.class_create).unbind();
51 | ElementTarget.on("click", options.class_create, function (event) {
52 | var newElement,
53 | isGenerate = $(this).parent(".dinamic-field"),
54 | isStatic = $(this).parent();
55 | if (isGenerate.length > 0) {
56 | //console.log('generated');
57 | newElement = isGenerate.clone();
58 | } else if (isStatic.length > 0) {
59 | //console.log($(main));
60 | newElement = ElementTarget.clone().addClass("dinamic-field");
61 | }
62 | //
63 | // Handle view of buttons
64 | createElement(ElementTarget, newElement);
65 | //
66 | // Add remove listener
67 | clickRemoveExecutor(ElementTarget);
68 | //
69 | // Callback function on create
70 | if (typeof options.onCreate === "function") {
71 | options.onCreate(newElement, $(this), event);
72 | }
73 | //
74 | // Manage buttons
75 | handleNavigationButtons(ElementTarget);
76 | //
77 | // Prevent Default
78 | event.preventDefault();
79 | return false;
80 | });
81 | }
82 |
83 | function clickRemoveExecutor(ElementTarget) {
84 | ElementTarget.on("click", options.class_remove, function (event) {
85 | var isGenerate = $(this).parents(".dinamic-field");
86 | var isStatic = $(this).parents(ElementTarget);
87 | if (isGenerate.length > 0) {
88 | isGenerate.remove();
89 | } else if (isStatic.length > 0) {
90 | ElementTarget.empty();
91 | ElementTarget.hide();
92 | ElementTarget.remove();
93 | }
94 | //
95 | // Callback function on remove
96 | if (typeof options.onRemove === "function") {
97 | options.onRemove($(this));
98 | }
99 | //
100 | // Manage buttons
101 | handleNavigationButtons(ElementTarget);
102 | //
103 | // Prevent Default
104 | event.preventDefault();
105 | return false;
106 | });
107 | }
108 |
109 | return this.each(function () {
110 | var target = $(this);
111 | handleNavigationButtons(target);
112 | //
113 | // Generate new field on click
114 | clickNewExecutor(target);
115 | //
116 | // Hide remove button
117 | target.find(options.class_remove).first().hide();
118 | //
119 | // Remove operation
120 | clickRemoveExecutor(target)
121 |
122 | });
123 | };
124 | //
125 | // Set up the default options.
126 | $.fn.duplicateElement.defaults = {
127 | tag_name: 'div',
128 | tag_id: "#dinamic-fields",
129 | clone_model: "#clone-field-model",
130 | class_remove: ".remove-this-fields",
131 | class_create: ".create-new-fields",
132 | onCreate: "",
133 | onRemove: ""
134 | };
135 | })(jQuery);
--------------------------------------------------------------------------------