PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.74
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.74
1.2.74 1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 All 174 releases
userswp / admin / assets / js / uwp-form-builder.js

uwp-form-builder.js in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.74, at admin/assets/js/uwp-form-builder.js

487 lines 20.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 'use strict';
3
4 window.UWP = window.UWP || {};
5
6 UWP.Form_Builder = {
7
8 actionMap: {
9 register: "uwp_ajax_register_action",
10 profile_tabs: "uwp_ajax_profile_tabs_action",
11 profile_tab: "uwp_ajax_profile_tabs_action",
12 user_sorting: "uwp_ajax_user_sorting_action",
13 },
14
15 /**
16 * Initialize the admin functionality
17 */
18 init: function () {
19 this.initFieldAddition();
20 this.initFieldSorting();
21 },
22
23 /**
24 * Initialize the field addition functionality
25 */
26 initFieldAddition: function () {
27 const self = this;
28 const $tabs = $("#uwp-form-builder-tab-existing, #uwp-form-builder-tab, #uwp-form-builder-tab-predefined, #uwp-form-builder-tab-custom");
29
30 $tabs.find("ul li a").on('click', (e) => {
31 e.preventDefault();
32 const $element = $(e.currentTarget);
33 if (!$element.attr('id')) {
34 return;
35 }
36
37 let htmlvarName = $element.attr('id').replace('uwp-', '');
38 const fieldType = $element.data('field-type');
39 const typeKey = $element.data("field-type-key");
40 const formType = $element.closest('#uwp-form-builder-tab, #uwp-form-builder-tab-predefined').find('#form_type').val();
41 const id = 'new' + $(".field_row_main ul.core").children('li:last-child').index() + 1;
42 const manageFieldType = $element.closest('#uwp-available-fields').find(".manage_field_type").val();
43 const fieldDataType = $element.data('data_type');
44 const customType = $element.data("field-custom-type");
45 const formId = $('[name="manage_field_form_id"]').val();
46 const nonce = $('[name="_wpnonce"]').val();
47
48 let data = {
49 'htmlvar_name': htmlvarName,
50 'field_type': fieldType,
51 'field_type_key': typeKey,
52 'form_type': formType,
53 'field_id': id,
54 'field_ins_upd': 'new',
55 'manage_field_type': manageFieldType,
56 'field_data_type': fieldDataType,
57 'custom_type': customType,
58 'form_id': formId,
59 '_wpnonce': nonce
60 };
61
62 if (manageFieldType === 'profile_tabs') {
63 data = {
64 ...data,
65 'tab_layout': $element.data('tab_layout'),
66 'tab_level': $element.data('tab_level'),
67 'tab_parent': $element.data('tab_parent'),
68 'tab_name': $element.data('tab_name'),
69 'tab_type': $element.data('tab_type'),
70 'tab_icon': $element.data('tab_icon'),
71 'tab_key': $element.data('tab_key'),
72 'tab_content': $element.data('tab_content'),
73 'tab_privacy': $element.data('tab_privacy'),
74 'user_decided': $element.data('user_decided'),
75 };
76 } else if (manageFieldType === 'user_sorting') {
77 data = {
78 ...data,
79 'data_type': $element.data('data_type'),
80 'tab_level': $element.data('tab_level'),
81 'tab_parent': $element.data('tab_parent'),
82 'field_icon': $element.data('field_icon'),
83 'site_title': $element.data('site_title'),
84 'sort': $element.data('sort'),
85 };
86 }
87
88 const actionType = self.getActionType(manageFieldType);
89 const action = actionType.action;
90
91 self.addNewField($element, action, data, htmlvarName, manageFieldType);
92 });
93 },
94
95 /**
96 * Get the action type based on the manage field type
97 *
98 * @param {string} type - The manage field type
99 * @returns {string} The action type
100 */
101 getActionType: function (type) {
102 if (this.actionMap[type]) {
103 return {
104 fieldType: type,
105 action: this.actionMap[type]
106 };
107 }
108
109 var formBuilderEvent = $.Event('uwp_resolve_form_builder_action');
110 formBuilderEvent.fieldType = type;
111 formBuilderEvent.manageFieldType = "custom_fields";
112 formBuilderEvent.actionType = "uwp_ajax_action";
113
114 $(document).trigger(formBuilderEvent);
115
116 return {
117 fieldType: formBuilderEvent.manageFieldType,
118 action: formBuilderEvent.actionType
119 };
120 },
121
122 /**
123 * Add a new field to the form
124 *
125 * @param {HTMLElement} element - The clicked element
126 * @param {string} action - The action type
127 * @param {Object} data - The field data
128 * @param {string} htmlvarName - The HTML variable name
129 * @param {string} manageFieldType - The manage field type
130 */
131 addNewField: function (element, action, data, htmlvarName, manageFieldType) {
132 $(window).trigger('uwp_form_builder_before_add_field', [htmlvarName, data]);
133
134 $.get(uwp_admin_ajax.url + '?action=' + action + '&create_field=true', data,
135 function (response) {
136 $('.field_row_main ul.core').append(response);
137 $(`#licontainer_${htmlvarName}`).find('#sort_order').val(parseInt($(`#licontainer_${htmlvarName}`).index()) + 1);
138
139 let $liContainer = $(`#licontainer_${htmlvarName}`);
140
141 if (!$liContainer.length) {
142 $liContainer = $(`#licontainer_${data.field_id}`);
143 }
144
145 if ($liContainer.length) {
146 if (manageFieldType !== 'register') {
147 UWP.Form_Builder.initTabSettings($liContainer.find('.uwp-fieldset'));
148 }
149
150 $('html, body').animate({
151 scrollTop: $liContainer.offset().top,
152 }, 1000);
153 }
154
155 if (manageFieldType === 'register') {
156 UWP.Form_Builder.saveField(htmlvarName, 'register');
157 }
158
159 $(window).trigger('uwp_form_builder_after_add_field', [manageFieldType, htmlvarName, data]);
160 });
161
162 if ($('#uwp-form-builder-tab-existing #uwp-' + htmlvarName).length > 0) {
163 $('#uwp-form-builder-tab-existing #uwp-' + htmlvarName).closest('li').hide();
164 }
165
166 if (htmlvarName !== 'fieldset' && (manageFieldType === 'register' || manageFieldType === 'search')) {
167 element.closest('li').hide();
168 }
169 },
170
171 /**
172 * Initialize the field sorting functionality
173 */
174 initFieldSorting: function () {
175 $("ul.uwp-tabs-selected").sortable({
176 opacity: 0.8,
177 cursor: 'move',
178 placeholder: "ui-state-highlight",
179 cancel: "input,label,select",
180 update: function () {
181 UWP.Form_Builder.updateFieldOrder($(this));
182 console.log('Fields have been ordered.');
183 aui_toast('uwp_tabs_reorder_tab_success', 'success', uwp_admin_ajax.txt_saved);
184 }
185 });
186
187 $('ul.uwp-profile-tabs-selected').nestedSortable({
188 maxLevels: 2,
189 handle: '.uwp-fieldset',
190 items: 'li',
191 disableNestingClass: 'mjs-nestedSortable-no-nesting',
192 helper: 'clone',
193 placeholder: 'ui-state-highlight',
194 forcePlaceholderSize: true,
195 listType: 'ul',
196 update: function () {
197 UWP.Form_Builder.updateTabOrder($(this));
198 }
199 });
200 },
201
202 /**
203 * Update the field order after sorting
204 *
205 * @param {jQuery} $sortable - The sortable element
206 */
207 updateFieldOrder: function ($sortable) {
208 const manageFieldType = $sortable.closest('#uwp-selected-fields').find(".manage_field_type").val();
209 const nonce = $sortable.closest('#uwp-selected-fields').find(".uwp_create_field_nonce").val();
210 const order = $sortable.sortable("serialize") + '&update=update&manage_field_type=' + manageFieldType;
211 const formId = $('[name="manage_field_form_id"]').val();
212 const formIdParam = '&form_id=' + formId + '&_wpnonce=' + nonce;
213 const actionType = UWP.Form_Builder.getActionType(manageFieldType);
214 const action = actionType.action;
215
216 $.get(uwp_admin_ajax.url + '?action=' + action + '&create_field=true', order + formIdParam, function () { });
217 },
218
219 /**
220 * Update the tab order after sorting
221 *
222 * @param {jQuery} $sortable - The sortable element
223 */
224 updateTabOrder: function ($sortable) {
225 const manageFieldType = $sortable.closest('#uwp-selected-fields').find(".manage_field_type").val();
226 const $tabs = $('.field_row_main ul.core').nestedSortable('toArray', {
227 startDepthCount: 0
228 });
229 const $order = {};
230 const formId = $('[name="manage_field_form_id"]').val();
231
232 $.each($tabs, function (index, tab) {
233 if (tab.id) {
234 $order[index] = {
235 id: tab.id,
236 tab_level: tab.depth,
237 tab_parent: tab.parent_id
238 };
239 }
240 });
241
242 const actionType = UWP.Form_Builder.getActionType(manageFieldType);
243 const action = actionType.action;
244
245 const data = {
246 'tabs': $order,
247 'form_id': formId,
248 '_wpnonce': $('[name="_wpnonce"]').val()
249 };
250
251 $.get(uwp_admin_ajax.url + '?action=' + action + '&create_field=true&update=update&manage_field_type=' + manageFieldType, data, function () { });
252 },
253
254 /**
255 * Save a field
256 *
257 * @param {string} id - The field ID
258 * @param {string} type - The field type
259 */
260 saveField: function (id, type) {
261 const formId = $('[name="manage_field_form_id"]').val();
262 const formIdParam = '&form_id=' + formId;
263 const actionType = UWP.Form_Builder.getActionType(type);
264 const action = actionType.action;
265 const manageFieldType = actionType.fieldType;
266
267 if ($('.uwp-form-settings-form #htmlvar_name').length > 0) {
268 const htmlvarName = $('.uwp-form-settings-form #htmlvar_name').val();
269 if (htmlvarName !== '') {
270 const iChars = "!`@#$%^&*()+=-[]\\\';,./{}|\":<>?~ ";
271 for (let i = 0; i < htmlvarName.length; i++) {
272 if (iChars.indexOf(htmlvarName.charAt(i)) !== -1) {
273 alert(uwp_admin_ajax.custom_field_not_special_char);
274 return false;
275 }
276 }
277 }
278 const optionValInput = $('.uwp-form-settings-form #option_values');
279 if (optionValInput.length === 1 && optionValInput.val() === '') {
280 alert(uwp_admin_ajax.custom_field_options_not_blank_var);
281 return false;
282 }
283 }
284
285 const requestData = type === 'register' ?
286 $(`#licontainer_${id} form`).serializeObject() :
287 $('.uwp-form-settings-form').serializeObject();
288
289 requestData['create_field'] = true;
290 requestData['field_ins_upd'] = 'submit';
291
292 $.post({
293 'url': `${uwp_admin_ajax.url}?action=${action}&manage_field_type=${manageFieldType}${formIdParam}`,
294 'data': requestData,
295 'beforeSend': function () {
296 var $loader = $('.uwp-form-settings-form #save').length ? $('.uwp-form-settings-form #save') : $('.uwp-form-settings-form').parent().find('#save');
297 $loader.html('<span class="spinner-border spinner-border-sm" role="status"></span> ' + uwp_admin_ajax.txt_saving).addClass('disabled');
298 },
299 'success': function (result) {
300 if ($.trim(result) === 'invalid_key') {
301 $('.uwp-form-settings-form #save').html(uwp_admin_ajax.txt_save).removeClass('disabled');
302 alert(uwp_admin_ajax.custom_field_unique_name);
303 } else {
304 $(`#licontainer_${id}`).replaceWith($.trim(result));
305 aui_toast('uwp_tabs_save_tab_success', 'success', uwp_admin_ajax.txt_saved);
306
307 if (type === 'profile_tab') {
308 UWP.Form_Builder.updateTabOrder($('.field_row_main ul.core'));
309 } else {
310 UWP.Form_Builder.updateFieldOrder($(".field_row_main ul.core"));
311 }
312
313 UWP.Form_Builder.closeTabSettings();
314 aui_init();
315 }
316 }
317 });
318 },
319
320 /**
321 * Delete a field
322 *
323 * @param {string} id - The field ID
324 * @param {string} nonce - The security nonce
325 * @param {string} deleteId - The ID of the element to show after deletion
326 * @param {string} type - The field type
327 */
328 deleteField: function (id, nonce, deleteId, type) {
329 const formId = $('[name="manage_field_form_id"]').val();
330 const actionType = UWP.Form_Builder.getActionType(type);
331
332 aui_confirm(uwp_admin_ajax.custom_field_delete, uwp_admin_ajax.txt_delete, uwp_admin_ajax.txt_cancel, true).then(function (confirmed) {
333 if (confirmed) {
334 if (id.substring(0, 3) === "new") {
335 $(`#licontainer_${id}`).remove();
336 } else {
337 $.get(`${uwp_admin_ajax.url}`, {
338 action: actionType.action,
339 create_field: true,
340 field_ins_upd: 'delete',
341 manage_field_type: actionType.fieldType,
342 field_id: id,
343 form_id: formId,
344 _wpnonce: nonce
345 },
346 function () {
347 $(`#licontainer_${id}`).remove();
348 });
349 $(`#uwp-${deleteId}`).closest('li').show();
350 }
351
352 aui_toast('uwp_tabs_delete_success', 'success', uwp_admin_ajax.txt_deleted);
353
354 if ($('#uwp-field-settings:visible button.btn-close').length) {
355 $('#uwp-field-settings:visible button.btn-close').trigger("click");
356 }
357
358 if ($(`#uwp-form-builder-tab-existing #uwp-${deleteId}`).length > 0) {
359 $(`#uwp-form-builder-tab-existing #uwp-${deleteId}`).closest('li').hide();
360 }
361 }
362 });
363 },
364
365 /**
366 * Initialize the tab settings
367 *
368 * @param {jQuery} $element - The element to initialize settings for
369 */
370 initTabSettings: function ($element) {
371 // Close any open settings first.
372 if ($('#licontainer_').length && $($element).parent().attr("id") != 'licontainer_') {
373 $('#licontainer_').remove();
374 } else if ($('#licontainer_new-1').length && $($element).parent().attr("id") != 'licontainer_new-1') {
375 $('#licontainer_new-1').remove();
376 }
377
378 let $settings = $($element).parent().find('.dd-setting').first().html();
379 $settings = jQuery('<div class="dd-setting">' + $settings + '</div>');
380 $settings.removeClass('d-none');
381
382 const $id = $settings.find('[name="id"]').val();
383 const $type = $settings.find('[name="tab_type"]').val();
384
385 if ($($element).closest('ul').hasClass('dd-list') || $type == 'fieldset') {
386 $settings.find('.alert-info').addClass('d-none');
387 } else {
388 $settings.find(`[data-argument="gd-tab-name-${$id}"],[data-argument="gd-tab-icon-${$id}"]`).addClass('d-none');
389 }
390
391 $('#uwp-form-builder-tab-selected .dd-form').removeClass('border-width-2 border-primary');
392 $($element).parent().find('.dd-form').first().addClass('border-width-2 border-primary');
393 $('#uwp-field-settings .card-body').html($settings);
394 $('#uwp-field-settings .card-body').find('.iconpicker-input').removeClass('iconpicker-input');
395 $('#uwp-field-settings-tab').tab('show');
396 $('#uwp-field-settings .card-footer').html('');
397 $('#uwp-field-settings .uwp-tab-actions').detach().appendTo('#uwp-field-settings .card-footer');
398
399 UWP.Admin.initAdvancedSettings();
400 aui_init();
401
402 // Conditional Fields on change
403 $(".uwp-form-settings-form").off('change').on("change", function () {
404 try {
405 aui_conditional_fields('.uwp-form-settings-form');
406 } catch (err) {
407 console.log(err.message);
408 }
409 });
410
411 // Conditional Fields on load
412 try {
413 aui_conditional_fields(".uwp-form-settings-form");
414 } catch (err) {
415 console.log(err.message);
416 }
417 },
418
419 /**
420 * Close the tab settings
421 */
422 closeTabSettings: function () {
423 $('#uwp-fields-tab').tab('show');
424 $('#uwp-selected-fields .dd-form').removeClass('border-width-2 border-primary');
425
426 // If not saved then remove
427 const $id = $('#uwp-field-settings').find('[name="id"],[name="field_id"]').val();
428 if (!$id || !$.isNumeric($id)) {
429 $(`#licontainer_,#licontainer_${$id}`).remove();
430 }
431 },
432
433 /**
434 * Handle changes in the data type
435 *
436 * @param {HTMLElement} obj - The changed element
437 * @param {string} cont - The container ID
438 */
439 dataTypeChanged: function (obj, cont) {
440 if (obj && cont) {
441 $(`#licontainer_${cont}`).find('.decimal-point-wrapper').hide();
442 if ($(obj).val() == 'FLOAT') {
443 $(`#licontainer_${cont}`).find('.decimal-point-wrapper').show();
444 }
445 if ($(obj).val() == 'FLOAT' || $(obj).val() == 'INT') {
446 $(`#licontainer_${cont}`).find('.uwp-price-extra-set').show();
447 if ($(`#licontainer_${cont}`).find(".uwp-price-extra-set input[name='extra[is_price]']:checked").val() == '1') {
448 $(`#licontainer_${cont}`).find('.uwp-price-extra').show();
449 }
450 } else {
451 $(`#licontainer_${cont}`).find('.uwp-price-extra-set').hide();
452 $(`#licontainer_${cont}`).find('.uwp-price-extra').hide();
453 }
454 }
455 },
456 };
457
458 // Global function mappings for backward compatibility
459 window.uwp_data_type_changed = UWP.Form_Builder.dataTypeChanged;
460 window.save_field = UWP.Form_Builder.saveField;
461 window.delete_field = UWP.Form_Builder.deleteField;
462 window.uwp_tabs_close_settings = UWP.Form_Builder.closeTabSettings;
463 window.uwp_tabs_item_settings = UWP.Form_Builder.initTabSettings;
464
465 // Initialize when document is ready
466 $(document).ready(function () {
467 UWP.Form_Builder.init();
468 });
469
470 $.fn.serializeObject = function () {
471 var o = {};
472 var a = this.serializeArray();
473 $.each(a, function () {
474 if (o[this.name]) {
475 if (!o[this.name].push) {
476 o[this.name] = [o[this.name]];
477 }
478
479 o[this.name].push(this.value || '');
480 } else {
481 o[this.name] = this.value || '';
482 }
483 });
484 return o;
485 };
486
487 })(jQuery);