PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.7.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.7.0
8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 8.5.2 8.5.0 All 532 releases
chatbot / js / jquery.repeatable.js

jquery.repeatable.js in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.7.0, at js/jquery.repeatable.js

259 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2
3
4
5 $.fn.repeatable = function (userSettings) {
6
7
8
9 /**
10
11 * Default settings
12
13 * @type {Object}
14
15 */
16
17 var defaults = {
18
19 addTrigger: ".add",
20
21 deleteTrigger: ".delete",
22
23 max: null,
24
25 startWith: 0,
26
27 template: null,
28
29 itemContainer: ".field-group",
30
31 onAdd: function () {},
32
33 onDelete: function () {}
34
35 };
36
37
38
39 /**
40
41 * Iterator used to make each added
42
43 * repeatable element unique
44
45 * @type {Number}
46
47 */
48
49 var i = 0;
50
51
52
53 /**
54
55 * DOM element into which repeatable
56
57 * items will be added
58
59 * @type {jQuery object}
60
61 */
62
63 var target = $(this);
64
65
66
67 /**
68
69 * Blend passed user settings with defauly settings
70
71 * @type {array}
72
73 */
74
75 var settings = $.extend({}, defaults, userSettings);
76
77
78
79 /**
80
81 * Total templated items found on the page
82
83 * at load. These may be created by server-side
84
85 * scripts.
86
87 * @return null
88
89 */
90
91 var total = function () {
92
93 return $(target).find(settings.itemContainer).length;
94
95 }();
96
97
98
99
100
101 /**
102
103 * Add an element to the target
104
105 * and call the callback function
106
107 * @return null
108
109 */
110
111 var addOne = function () {
112
113 createOne();
114
115 settings.onAdd.call(this);
116
117 };
118
119
120
121 /**
122
123 * Delete the parent element
124
125 * and call the callback function
126
127 * @return null
128
129 */
130
131 var deleteOne = function () {
132
133 $(this).parents(settings.itemContainer).first().remove();
134
135 total--;
136
137 maintainAddBtn();
138
139 settings.onDelete.call(this);
140
141 };
142
143
144
145 /**
146
147 * Add an element to the target
148
149 * @return null
150
151 */
152
153 var createOne = function() {
154
155 getUniqueTemplate().appendTo(target);
156
157 total++;
158
159 maintainAddBtn();
160
161 };
162
163
164
165 /**
166
167 * Alter the given template to make
168
169 * each form field name unique
170
171 * @return {jQuery object}
172
173 */
174
175 var getUniqueTemplate = function () {
176
177 var template = $(settings.template).html();
178
179 template = template.replace(/{\?}/g, "new" + i++); // {?} => iterated placeholder
180
181 template = template.replace(/\{[^\?\}]*\}/g, ""); // {valuePlaceholder} => ""
182
183 return $(template);
184
185 };
186
187
188
189 /**
190
191 * Determines if the add trigger
192
193 * needs to be disabled
194
195 * @return null
196
197 */
198
199 var maintainAddBtn = function () {
200
201 if (!settings.max) {
202
203 return;
204
205 }
206
207
208
209 if (total === settings.max) {
210
211 $(settings.addTrigger).attr("disabled", "disabled");
212
213 } else if (total < settings.max) {
214
215 $(settings.addTrigger).removeAttr("disabled");
216
217 }
218
219 };
220
221
222
223 /**
224
225 * Setup the repeater
226
227 * @return null
228
229 */
230
231 (function () {
232
233 $(settings.addTrigger).on("click", addOne);
234
235 $("form").on("click", ".delete", deleteOne);
236
237
238
239 if (!total) {
240
241 var toCreate = settings.startWith - total;
242
243 for (var j = 0; j < toCreate; j++) {
244
245 createOne();
246
247 }
248
249 }
250
251
252
253 })();
254
255 };
256
257
258
259 })(jQuery);