deduct-steps-ui.js
10.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
jQuery(document).ready(function ($) {
/**
* Hide our Triggers metabox if unnecessary
*/
$("#_badgeos_earned_by")
.change(function () {
if ("triggers" == $(this).val()) {
$("#badgeos_steps_ui").show();
} else {
$("#badgeos_steps_ui").hide();
}
})
.change();
/**
* Make our Triggers list sortable
*/
$("#deduct_steps_list").sortable({
/**
* When the list order is updated
*/
update: function () {
/**
* Loop through each element
*/
$("#deduct_steps_list li").each(function (index, value) {
/**
* Write it's current position to our hidden input value
*/
$(this)
.children('input[name="order"]')
.val(index);
});
}
});
/**
* Listen for our change to our trigger type selectors
*/
$("#deduct_steps_list").on("change", ".select-trigger-type", function () {
/**
* Grab our selected trigger type and achievement selector
*/
var trigger_type = $(this).val();
var achievement_selector = $(this).siblings(".select-achievement-type");
var visit_post_selector = $(this).siblings(".badgeos-select-visit-post");
var visit_page_selector = $(this).siblings(".badgeos-select-visit-page");
var num_of_years = $(this).siblings(".badgeos-num-of-years");
var x_number_of_users = $(this).siblings(".badgeos-x-number-of-users");
var remove_rank = $(this).siblings(".badgeos-select-remove-rank");
var remove_achivement = $(this).siblings(".badgeos-select-remove-achivement");
var num_of_days = $(this).siblings(".badgeos-num-of-days");
var num_of_months = $(this).siblings(".badgeos-num-of-months");
/**
* If we're working with dedpoint, show the achievement selecter (otherwise, hide it)
*/
if ("badgeos_remove_rank_on_point_deduct" == trigger_type) {
remove_rank.show();
} else {
remove_rank.hide();
}
if ("badgeos_remove_achievment_on_point_deduct" == trigger_type) {
remove_achivement.show();
} else {
remove_achivement.hide();
}
if (
"any-achievement" == trigger_type ||
"all-achievements" == trigger_type ||
"specific-achievement" == trigger_type
) {
achievement_selector.show();
} else {
achievement_selector.hide();
}
if ("badgeos_wp_not_login" == trigger_type || "badgeos_on_completing_num_of_day" == trigger_type) {
num_of_days.show();
} else {
num_of_days.hide();
}
if ("badgeos_on_completing_num_of_year" == trigger_type) {
num_of_years.show();
} else {
num_of_years.hide();
}
if ("badgeos_on_the_first_x_users" == trigger_type) {
x_number_of_users.show();
} else {
x_number_of_users.hide();
}
if ("badgeos_on_completing_num_of_month" == trigger_type) {
num_of_months.show();
} else {
num_of_months.hide();
}
if ("badgeos_visit_a_page" == trigger_type || "badgeos_award_author_on_visit_page" == trigger_type) {
visit_page_selector.show();
} else {
visit_page_selector.hide();
}
if ("badgeos_visit_a_post" == trigger_type || "badgeos_award_author_on_visit_post" == trigger_type) {
visit_post_selector.show();
} else {
visit_post_selector.hide();
}
$(".badgeos_dedpoint_step_fields").hide();
$(".badgeos_dedpoint_step_ddl_dynamic").hide();
$("#badgeos_dedpoint_step_ddl_dynamic_" + trigger_type)
.show()
.trigger("change");
/**
* Trigger a change for our achievement type post selector to determine if it should show
*/
achievement_selector.change();
});
$(".badgeos_dedpoint_step_ddl_dynamic").on("change", function () {
$(".badgeos_dedpoint_step_subddl_dynamic").hide();
$(".badgeos_dedpoint_step_subtxt_dynamic").hide();
main_trigger = $(this).data("trigger");
curr_trigger = $(this).val();
$(".badgeos_dedpoint_step_subddl_" + curr_trigger).show();
$(".badgeos_dedpoint_step_subtxt_" + curr_trigger).show();
});
/**
* Trigger a change for our trigger type post selector to determine if it should show
*/
$(".select-trigger-type").change();
/**
* Listen for a change to our achievement type selectors
*/
$("#deduct_steps_list").on("change", ".select-achievement-type", function () {
/**
* Setup our necessary variables
*/
var achievement_selector = $(this);
var achievement_type = achievement_selector.val();
var step_id = achievement_selector.parent("li").attr("data-step-id");
var excluded_posts = [
achievement_selector.siblings('input[name="post_id"]').val()
];
var trigger_type = achievement_selector
.siblings(".select-trigger-type")
.val();
/**
* If we've selected a *specific* achievement type, show our post selector
* and populate it w/ the corresponding achievement posts
*/
if ("" !== achievement_type && "specific-achievement" == trigger_type) {
$.post(
ajaxurl,
{
action: "post_deduct_select_ajax",
achievement_type: achievement_type,
step_id: step_id,
excluded_posts: excluded_posts
},
function (response) {
achievement_selector
.siblings("select.select-achievement-post")
.html(response);
achievement_selector
.siblings("select.select-achievement-post")
.show();
}
);
/**
* Otherwise, keep our post selector hidden
*/
} else {
achievement_selector.siblings(".select-achievement-post").hide();
if ("badgeos_specific_new_comment" == trigger_type) {
achievement_selector.siblings("input.select-achievement-post").show();
}
}
});
/**
* Trigger a change for our achievement type post selector to determine if it should show
*/
$(".select-achievement-type").change();
});
/**
* Add a new deduct step
*
* @param achievement_id
*/
function badgeos_add_new_deduct_step(achievement_id) {
jQuery.post(
ajaxurl,
{
action: "add_deduct_step",
achievement_id: achievement_id
},
function (response) {
jQuery(response).appendTo("#deduct_steps_list");
/**
* Dynamically add the menu order for the new step to be one higher
* than the last in line
*/
new_step_menu_order =
Number(
jQuery("#deduct_steps_list li.step-row")
.eq(-2)
.children('input[name="order"]')
.val()
) + 1;
jQuery("#deduct_steps_list li.step-row:last")
.children('input[name="order"]')
.val(new_step_menu_order);
/**
* Trigger a change for the new trigger type <select> element
*/
jQuery("#deduct_steps_list li.step-row:last")
.children(".select-trigger-type")
.change();
}
);
}
/**
* Delete a step
*/
function badgeos_delete_deduct_step(step_id) {
jQuery.post(
ajaxurl,
{
action: "delete_deduct_step",
step_id: step_id
},
function (response) {
jQuery(".step-" + step_id).remove();
}
);
}
/**
* Update all steps
*
* @param e
*/
function badgeos_update_deduct_steps(e) {
jQuery(".save-deduct-steps-spinner").show();
step_data = {
action: "update_deduct_steps",
steps: []
};
var total_steps = 0;
/**
* Loop through each step and collect its data
*/
jQuery("#deduct_steps_list .step-row").each(function () {
/**
* Cache our step object
*/
var step = jQuery(this);
var trigger_type = step.find(".select-trigger-type").val();
var visit_post_selector = step.find(".badgeos-select-visit-post").val();
var visit_page_selector = step.find(".badgeos-select-visit-page").val();
var selected_subtrigger = step
.find(
"#badgeos_dedpoint_step_dynamic_section_" +
trigger_type +
" .badgeos_dedpoint_step_ddl_dynamic"
)
.val();
var selected_subtrigger_id = step
.find(
"#badgeos_dedpoint_step_dynamic_section_" +
trigger_type +
" .badgeos_dedpoint_step_ddl_dynamic"
)
.attr("id");
var serialize_data = step
.find(".badgeos_dedpoint_step_fields_" + selected_subtrigger + ":input")
.serialize();
/**
* Setup our step object
*/
var step_details = {
step_id: step.attr("data-step-id"),
order: step.find('input[name="order"]').val(),
required_count: step.find(".required-count").val(),
point_value: step.find(".point-value").val(),
trigger_type: trigger_type,
achievement_type: step.find(".select-achievement-type").val(),
remove_rank: step.find(".badgeos-select-remove-rank").val(),
remove_achivement: step.find(".badgeos-select-remove-achivement").val(),
x_number_of_users: step.find(".badgeos-x-number-of-users").val(),
num_of_years: step.find(".badgeos-num-of-years").val(),
num_of_days: step.find(".badgeos-num-of-days").val(),
num_of_months: step.find(".badgeos-num-of-months").val(),
badgeos_subtrigger_id: selected_subtrigger_id,
badgeos_subtrigger_value: selected_subtrigger,
badgeos_fields_data: serialize_data,
visit_post: visit_post_selector,
visit_page: visit_page_selector,
achievement_post:
"badgeos_specific_new_comment" === trigger_type
? step.find("input.select-achievement-post").val()
: step.find("select.select-achievement-post").val(),
title: step.find(".step-title .title").val()
};
/**
* Allow external functions to add their own data to the array
*/
step.trigger("update_step_data", [step_details, step]);
/**
* Add our relevant data to the array
*/
step_data.steps.push(step_details);
total_steps++;
});
if (total_steps == 0) {
jQuery(".save-deduct-steps-spinner").hide();
}
jQuery.post(ajaxurl, step_data, function (response) {
/**
* Parse our response and update our step titles
*/
var titles = jQuery.parseJSON(response);
jQuery.each(titles, function (index, value) {
jQuery("#step-" + index + "-title").val(value);
});
/**
* Hide our save spinner
*/
jQuery(".save-deduct-steps-spinner").hide();
});
}