wpdiscuz-custom-form.js
9.75 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var $ = jQuery.noConflict();
$(document).ready(function ($) {
var currentContainer;
var currentRowId;
var currentColumnType;
$('.icp-auto').iconpicker();
$('body').on('click', '#wpdiscuz_form_add_row', function () {
wpdiscuzForm.addRow('#wpd-form-sortable-rows');
wpdFormFieldSortable();
});
$('body').on('click', '.wpd-form-row-wrap .wpd-form-row-actions .fa-columns', function () {
wpdiscuzForm.formTwoColumn($(this));
wpdFormFieldSortable();
});
$('body').on('click', '.wpd-form-row-wrap .wpd-form-row-actions .fa-trash-alt', function () {
if($(this).parents('.wpd-form-row-wrap').find('.wpd-default-field').length){
alert(wpdFormAdminOptions.can_not_delete_field);
return;
}
if (confirm(wpdFormAdminOptions.confirm_delete_message)) {
$(this).parents('.wpd-form-row-wrap').remove();
}
});
$('body').on('click', '.wpd-form-add-filed', function () {
currentRowId = $(this).parents('.wpd-form-row-wrap').attr('id');
currentColumnType = 'full';
if ($(this).parents('.wpd-form-col').hasClass('left-col')) {
currentColumnType = 'left';
} else if ($(this).parents('.wpd-form-col').hasClass('right-col')) {
currentColumnType = 'right';
}
currentContainer = $(this).parents('.wpd-form-col').find('.col-body');
tb_show(wpdFormAdminOptions.wpd_form_fields, ajaxurl + "?action=wpdiscuzCustomFields&width=700&height=400");
return false;
});
$('body').on('click', '.wpd-field .fa-trash-alt', function () {
if (confirm(wpdFormAdminOptions.confirm_delete_message)) {
$(this).parents('.wpd-field').remove();
}
});
$('body').on('click', '.wpd-field .fa-pencil-alt', function () {
$(this).parents('.wpd-field').find('.wpd-field-body').toggle(500);
});
$('body').on('click', '.wpd-field-button', function () {
var fieldType = $(this).attr('id');
var fieldTitle = $(this).text();
var defaultField = '0';
if ($(this).hasClass('wpdDefaultField')) {
defaultField = 1;
}
wpdFieldLoad();
$.ajax({
type: 'POST',
url: ajaxurl,
data: {
action: 'adminFieldForm',
fieldType: fieldType,
row: currentRowId,
col: currentColumnType,
defaultField: defaultField
}
}).done(function (response) {
$('#TB_ajaxWindowTitle').text(fieldTitle);
$('#TB_ajaxContent').html(response);
$('.icp-auto').iconpicker();
});
});
$('body').on('click', '#wpd-add-field-button', function () {
var tbForm = $('#TB_ajaxContent_form');
tbForm.submit(function (event) {
event.preventDefault();
});
if (tbForm[0].checkValidity()) {
var fieldName = $(this).parents('#TB_ajaxContent').find('.wpd-field-body .wpd-field-name').val();
var dialogFieldHtml = $(this).parents('#TB_ajaxContent').find('.wpd-field-body');
var dynamicFiledCont = $('<div class="wpd-col-full wpd-field">' +
'<div class="wpd-field-head">' + fieldName +
'<div class="wpd-field-actions">' +
'<i class="fas fa-pencil-alt" title="' + wpdFormAdminOptions.edit_field + '"></i>|' +
'<i class="fas fa-trash-alt" title="' + wpdFormAdminOptions.delete + '"></i>' +
'</div></div></div>');
dialogFieldHtml.hide();
dialogFieldHtml.appendTo(dynamicFiledCont);
dynamicFiledCont.appendTo(currentContainer);
wpdFormFieldSortable();
tb_remove();
}
});
wpdFormRowSortable();
wpdFormFieldSortable();
/*========================= FUNCTIONS ==================================== */
function wpdFormRowSortable() {
$('#wpd-form-sortable-rows').sortable({
containment: '.wpd-form',
cancel: '.wpd-form-row-body,.fa-columns, .fa-trash-alt',
update: function (event, ui) {
ui.item.find('.row_order').val(ui.item.index());
wpdiscuzForm.rebuildRowsData('#wpd-form-sortable-rows', '.wpd-form-row-wrap', 'wpd-form-row-wrap');
}
});
}
function wpdFormFieldSortable() {
$('.col-body').sortable({
connectWith: '.col-body',
containment: '.wpd-form',
cancel: '.fas, .wpd-field-body',
update: function (event, ui) {
wpdiscuzForm.afterFieldDrag(ui.item);
}
});
}
function wpdFieldLoad() {
$('#TB_ajaxContent').html('<img class="wpdFieldLoad" src="' + wpdFormAdminOptions.loaderImg + '">');
}
$('body').on('click', '.wpd-advaced-options-title', function(){
$(this).next('.wpd-advaced-options-cont').toggle();
});
});
//===================== OBJECT ========================== //
var wpdiscuzForm = {
/**
* @param {string} selector Container where should be added row
*/
addRow: function (selector) {
$(selector).append('<div class="wpd-form-row-wrap">' +
'<input class="column_type" type="hidden" value="full"/>' +
'<input class="row_order" type="hidden" name=""/>' +
'<div class="wpd-form-row-head">' +
'<div class="wpd-form-row-actions">' +
'<i class="fas fa-columns" title="' + wpdFormAdminOptions.two_column + '"></i>|' +
'<i class="fas fa-trash-alt" title="' + wpdFormAdminOptions.delete + '"></i>|' +
'<i class="fas fa-arrows-alt" title="' + wpdFormAdminOptions.move + '"></i>' +
'</div></div>' +
'<div class="wpd-form-row">' +
'<div class="wpd-form-row-body">' +
'<div class="full-col wpd-form-col">' +
'<div class="col-body"></div>' +
'<div class="wpd-form-add-filed">' +
'<i class="fas fa-plus" title="' + wpdFormAdminOptions.add_field + '">' +
'</div></div>' +
'</div></div></div>');
wpdiscuzForm.rebuildRowsData(selector, '.wpd-form-row-wrap', 'wpd-form-row-wrap');
},
rebuildRowsData: function (container, selector, idPrefix) {
$(container + ' ' + selector).each(function (index, element) {
var dynamicId = idPrefix + '_' + index;
$(this).attr('id', dynamicId);
$(this).find('.column_type').attr('name', wpdFormAdminOptions.wpdiscuz_form_structure + '[' + dynamicId + '][column_type]');
$(this).find('.row_order').attr('name', wpdFormAdminOptions.wpdiscuz_form_structure + '[' + dynamicId + '][row_order]');
$(this).find('.row_order').attr('value', index);
$('input , textarea', $(this)).each(function () {
var oldRowName = $(this).attr('name');
if (oldRowName) {
var newRowName = oldRowName.replace(new RegExp(wpdFormAdminOptions.wpdiscuz_form_structure + '\\[[^\\]]+\\]' ,'g'), wpdFormAdminOptions.wpdiscuz_form_structure + '[' + dynamicId + ']');
$(this).attr('name', newRowName);
}
});
});
},
formTwoColumn: function (obj) {
var rowWrap = obj.parents('.wpd-form-row-wrap');
var row = rowWrap.find('.wpd-form-row>.wpd-form-row-body');
var colNewName = 'full';
var colNewType = 'full';
if (row.hasClass('two-col')) {
var rigthColumnHtml = $('.right-col>.col-body', row).html();
var leftColumn = $('.left-col', row);
$('.right-col', row).remove();
$('.col-body', leftColumn).append(rigthColumnHtml);
leftColumn.removeClass('left-col');
leftColumn.addClass('full-col');
} else {
var fullColumn = $('.full-col', row);
fullColumn.removeClass('full-col');
fullColumn.addClass('left-col');
fullColumn.after('<div class="right-col wpd-form-col">' +
'<div class="col-body"></div>' +
'<div class="wpd-form-add-filed">' +
'<i class="fas fa-plus" title="' + wpdFormAdminOptions.add_field + '">' +
'</div></div>');
colNewName = 'left';
colNewType = 'two';
}
obj.toggleClass("wpd-form-columns-two");
row.toggleClass("two-col");
rowWrap.find('.column_type').val(colNewType);
wpdiscuzForm.replaceColName(colNewName, rowWrap.attr('id'), row);
},
replaceColName: function (colNewName, parentRowId, obj) {
$('input , textarea', obj).each(function () {
var oldRowName = $(this).attr('name');
if (oldRowName) {
var newRowName = oldRowName.replace(new RegExp(wpdFormAdminOptions.wpdiscuz_form_structure + '\\[[^\\]]+\\]\\[[^\\]]+\\]' ,'g'), wpdFormAdminOptions.wpdiscuz_form_structure + '[' + parentRowId + '][' + colNewName + ']');
$(this).attr('name', newRowName);
}
});
},
afterFieldDrag: function (obj) {
var parentRowId = obj.parents('.wpd-form-row-wrap').attr('id');
var parentColName = 'full';
if (obj.parents('.wpd-form-col').hasClass('left-col')) {
parentColName = 'left';
} else if (obj.parents('.wpd-form-col').hasClass('right-col')) {
parentColName = 'right';
}
wpdiscuzForm.replaceColName(parentColName, parentRowId, obj);
}
};