adblocker-image-data.js
1 year ago
admin-global.js
1 year ago
admin.js
2 days ago
advertisement.js
3 months ago
conditions.js
1 year ago
dialog-advads-modal.js
1 year ago
termination.js
1 year ago
ui.js
3 years ago
conditions.js
535 lines
| 1 | /** |
| 2 | * Logic for Display and Visitor Conditions forms |
| 3 | */ |
| 4 | |
| 5 | jQuery(document).ready(function ($) { |
| 6 | /** |
| 7 | * Pressing the button to add a new condition to the list of conditions |
| 8 | */ |
| 9 | $('.advads-conditions-new select').on('change', function () { |
| 10 | // get the form fieldset and values. |
| 11 | const condition_form_container = $(this).parents('fieldset'); |
| 12 | const condition_type = condition_form_container |
| 13 | .find('.advads-conditions-new select') |
| 14 | .val(); |
| 15 | const condition_title = condition_form_container |
| 16 | .find('.advads-conditions-new select option:selected') |
| 17 | .text(); |
| 18 | let condition_index = parseInt( |
| 19 | condition_form_container.find('.advads-conditions-index').val() |
| 20 | ); |
| 21 | const condition_list_target_ID = $.escapeSelector( |
| 22 | condition_form_container.data('condition-list-target') |
| 23 | ); // ID of the container into which the new condition is loaded. |
| 24 | const condition_list_target = $('#' + condition_list_target_ID); // container into which the new condition is loaded. |
| 25 | const conditions_form_name = condition_form_container.data( |
| 26 | 'condition-form-name' |
| 27 | ); // name prefix for the form. |
| 28 | const conditions_connector_default = condition_form_container.data( |
| 29 | 'condition-connector-default' |
| 30 | ); // default connector option. |
| 31 | const conditions_action = |
| 32 | condition_form_container.data('condition-action'); // action to which to send the AJAX call to. |
| 33 | if (!condition_type || '' == condition_type) { |
| 34 | return; |
| 35 | } |
| 36 | condition_form_container.find('.advads-loader').show(); // show loader. |
| 37 | $.ajax({ |
| 38 | type: 'POST', |
| 39 | url: ajaxurl, |
| 40 | data: { |
| 41 | action: conditions_action, |
| 42 | type: condition_type, |
| 43 | index: condition_index, |
| 44 | form_name: conditions_form_name, |
| 45 | nonce: advadsglobal.ajax_nonce, |
| 46 | }, |
| 47 | success(r, textStatus, XMLHttpRequest) { |
| 48 | // add. |
| 49 | if (r) { |
| 50 | if ('or' === conditions_connector_default) { |
| 51 | // as used for display conditions. |
| 52 | var connector = |
| 53 | '<input style="display:none;" type="checkbox" name="' + |
| 54 | conditions_form_name + |
| 55 | '[' + |
| 56 | condition_index + |
| 57 | '][connector]" checked="checked" value="or" id="advads-conditions-' + |
| 58 | condition_list_target_ID + |
| 59 | '-' + |
| 60 | condition_index + |
| 61 | '-connector"><label for="advads-conditions-' + |
| 62 | condition_list_target_ID + |
| 63 | '-' + |
| 64 | condition_index + |
| 65 | '-connector">' + |
| 66 | advadstxt.condition_or + |
| 67 | '</label>'; |
| 68 | var newline = |
| 69 | '<tr class="advads-conditions-connector advads-conditions-connector-or"><td colspan="3">' + |
| 70 | connector + |
| 71 | '</td></tr><tr><td class="advads-conditions-type" data-condition-type="' + |
| 72 | condition_type + |
| 73 | '">' + |
| 74 | condition_title + |
| 75 | '</td><td>' + |
| 76 | r + |
| 77 | '</td><td><button type="button" class="advads-conditions-remove button">x</button></td></tr>'; |
| 78 | } else { |
| 79 | // as used for visitor conditions. |
| 80 | var connector = |
| 81 | '<input type="checkbox" name="' + |
| 82 | conditions_form_name + |
| 83 | '[' + |
| 84 | condition_index + |
| 85 | '][connector]" value="or" id="advads-conditions-' + |
| 86 | condition_list_target_ID + |
| 87 | '-' + |
| 88 | condition_index + |
| 89 | '-connector"><label for="advads-conditions-' + |
| 90 | condition_list_target_ID + |
| 91 | '-' + |
| 92 | condition_index + |
| 93 | '-connector">' + |
| 94 | advadstxt.condition_and + |
| 95 | '</label>'; |
| 96 | var newline = |
| 97 | '<tr class="advads-conditions-connector advads-conditions-connector-and"><td colspan="3">' + |
| 98 | connector + |
| 99 | '</td></tr><tr><td class="advads-conditions-type" data-condition-type="' + |
| 100 | condition_type + |
| 101 | '">' + |
| 102 | condition_title + |
| 103 | '</td><td>' + |
| 104 | r + |
| 105 | '</td><td><button type="button" class="advads-conditions-remove button">x</button></td></tr>'; |
| 106 | } |
| 107 | condition_list_target.find('tbody').append(newline); |
| 108 | condition_list_target |
| 109 | .find( |
| 110 | 'tbody .advads-conditions-single.advads-buttonset' |
| 111 | ) |
| 112 | .advads_buttonset(); |
| 113 | condition_list_target |
| 114 | .find('tbody .advads-conditions-connector input') |
| 115 | .advads_button(); |
| 116 | // increase count. |
| 117 | condition_index++; |
| 118 | condition_form_container |
| 119 | .find('.advads-conditions-index') |
| 120 | .val(condition_index); |
| 121 | // reset select. |
| 122 | condition_form_container.find( |
| 123 | '.advads-conditions-new select' |
| 124 | )[0].selectedIndex = 0; |
| 125 | advads_display_condition_option_not_selected(); |
| 126 | const tr = $(this).parents('.advads-conditions-table tr'); |
| 127 | |
| 128 | condition_list_target.next('input').remove(); |
| 129 | } |
| 130 | }, |
| 131 | error(MLHttpRequest, textStatus, errorThrown) { |
| 132 | condition_form_container |
| 133 | .find('.advads-conditions-new') |
| 134 | .append(errorThrown); |
| 135 | }, |
| 136 | complete(MLHttpRequest, textStatus) { |
| 137 | condition_form_container |
| 138 | .find('.advads-conditions-new .advads-loader') |
| 139 | .hide(); // hide loader. |
| 140 | }, |
| 141 | }); |
| 142 | }); |
| 143 | // disable term in the term list of the appropriate condition by just clicking on it. |
| 144 | $(document).on( |
| 145 | 'click', |
| 146 | '.advads-conditions-terms-buttons .button', |
| 147 | function (e) { |
| 148 | $(this).remove(); |
| 149 | } |
| 150 | ); |
| 151 | // display input field to search for terms. |
| 152 | $(document).on( |
| 153 | 'click', |
| 154 | '.advads-conditions-terms-show-search', |
| 155 | function (e) { |
| 156 | e.preventDefault(); |
| 157 | // display input field. |
| 158 | $(this).siblings('.advads-conditions-terms-search').show().focus(); |
| 159 | // register autocomplete. |
| 160 | advads_register_terms_autocomplete( |
| 161 | $(this).siblings('.advads-conditions-terms-search') |
| 162 | ); |
| 163 | $(this).next('br').show(); |
| 164 | $(this).hide(); |
| 165 | } |
| 166 | ); |
| 167 | |
| 168 | // function for autocomplete. |
| 169 | function advads_register_terms_autocomplete(self) { |
| 170 | self.autocomplete({ |
| 171 | classes: { |
| 172 | 'ui-autocomplete': 'advads-ui-autocomplete', |
| 173 | }, |
| 174 | source(request, callback) { |
| 175 | // var searchField = request.term; |
| 176 | advads_term_search(self, callback); |
| 177 | }, |
| 178 | minLength: 1, |
| 179 | select(event, ui) { |
| 180 | // append new line with input fields. |
| 181 | $( |
| 182 | '<label class="button advads-button advads-ui-state-active"><span class="advads-button-text">' + |
| 183 | ui.item.label + |
| 184 | '<input type="hidden" name="' + |
| 185 | self.data('inputName') + |
| 186 | '" value="' + |
| 187 | ui.item.value + |
| 188 | '"></span></label>' |
| 189 | ).appendTo(self.siblings('.advads-conditions-terms-buttons')); |
| 190 | |
| 191 | // show / hide other elements |
| 192 | // $( '.advads-display-conditions-individual-post' ).hide(); |
| 193 | // $( '.advads-conditions-postids-list .show-search a' ).show(); |
| 194 | }, |
| 195 | // eslint-disable-next-line no-unused-vars |
| 196 | close(event, ui) { |
| 197 | self.val(''); |
| 198 | }, |
| 199 | }); |
| 200 | } |
| 201 | |
| 202 | // remove author from list by clicking on it. |
| 203 | $(document).on( |
| 204 | 'click', |
| 205 | '.advads-conditions-authors-buttons .button', |
| 206 | function () { |
| 207 | $(this).remove(); |
| 208 | } |
| 209 | ); |
| 210 | // display input field to search for author. |
| 211 | $(document).on( |
| 212 | 'click', |
| 213 | '.advads-conditions-authors-show-search', |
| 214 | function (e) { |
| 215 | e.preventDefault(); |
| 216 | // display input field. |
| 217 | $(this) |
| 218 | .siblings('.advads-conditions-authors-search') |
| 219 | .show() |
| 220 | .focus(); |
| 221 | // register autocomplete. |
| 222 | advadsRegisterAuthorAutocomplete( |
| 223 | $(this).siblings('.advads-conditions-authors-search') |
| 224 | ); |
| 225 | $(this).next('br').show(); |
| 226 | $(this).hide(); |
| 227 | } |
| 228 | ); |
| 229 | |
| 230 | // author search box autocomplete. |
| 231 | function advadsRegisterAuthorAutocomplete(self) { |
| 232 | self.autocomplete({ |
| 233 | classes: { |
| 234 | 'ui-autocomplete': 'advads-ui-autocomplete', |
| 235 | }, |
| 236 | source(request, callback) { |
| 237 | advadsAuthorSearch(self, callback); |
| 238 | }, |
| 239 | minLength: 1, |
| 240 | select(event, ui) { |
| 241 | // append new line with input fields. |
| 242 | $( |
| 243 | '<label class="button advads-button advads-ui-state-active"><span class="advads-button-text">' + |
| 244 | ui.item.label + |
| 245 | '<input type="hidden" name="' + |
| 246 | self.data('inputName') + |
| 247 | '" value="' + |
| 248 | ui.item.value + |
| 249 | '"></span></label>' |
| 250 | ).appendTo(self.siblings('.advads-conditions-authors-buttons')); |
| 251 | }, |
| 252 | close() { |
| 253 | self.val(''); |
| 254 | }, |
| 255 | }); |
| 256 | } |
| 257 | |
| 258 | // display input field to search for post, page, etc. |
| 259 | $(document).on( |
| 260 | 'click', |
| 261 | '.advads-conditions-postids-show-search', |
| 262 | function (e) { |
| 263 | e.preventDefault(); |
| 264 | // display input field. |
| 265 | $(this) |
| 266 | .next() |
| 267 | .find('.advads-display-conditions-individual-post') |
| 268 | .show(); |
| 269 | //$( '.advads-conditions-postids-search-line .description' ).hide(); |
| 270 | $(this).hide(); |
| 271 | } |
| 272 | ); |
| 273 | // register autocomplete to display condition individual posts. |
| 274 | // eslint-disable-next-line no-unused-vars |
| 275 | $(document).on( |
| 276 | 'focus', |
| 277 | '.advads-display-conditions-individual-post', |
| 278 | // eslint-disable-next-line no-unused-vars |
| 279 | function (e) { |
| 280 | const self = this; |
| 281 | if (!$(this).data('autocomplete')) { |
| 282 | // If the autocomplete wasn't called yet: |
| 283 | $(this) |
| 284 | .autocomplete({ |
| 285 | classes: { |
| 286 | 'ui-autocomplete': 'advads-ui-autocomplete', |
| 287 | }, |
| 288 | source(request, callback) { |
| 289 | const searchParam = request.term; |
| 290 | advads_post_search(searchParam, callback); |
| 291 | }, |
| 292 | minLength: 1, |
| 293 | select(event, ui) { |
| 294 | // append new line with input fields |
| 295 | const newline = $( |
| 296 | `<label class="button advads-button advads-ui-state-active"><span class="advads-button-text">${ui.item.label}</span></label>` |
| 297 | ); |
| 298 | $( |
| 299 | `<input type="hidden" name="${self.dataset.fieldName}[value][]" value="${ui.item.value}"/>` |
| 300 | ).appendTo(newline); |
| 301 | newline.insertBefore( |
| 302 | $(self).parent( |
| 303 | '.advads-conditions-postids-search-line' |
| 304 | ) |
| 305 | ); |
| 306 | }, |
| 307 | // eslint-disable-next-line no-unused-vars |
| 308 | close(event, ui) { |
| 309 | $(self).val(''); |
| 310 | }, |
| 311 | }) |
| 312 | .autocomplete() |
| 313 | .data('ui-autocomplete')._renderItem = function (ul, item) { |
| 314 | ul.addClass( |
| 315 | 'advads-conditions-postids-autocomplete-suggestions' |
| 316 | ); |
| 317 | return $('<li></li>') |
| 318 | .append( |
| 319 | "<span class='left'>" + |
| 320 | item.label + |
| 321 | "</span> <span class='right'>" + |
| 322 | item.info + |
| 323 | '</span>' |
| 324 | ) |
| 325 | .appendTo(ul); |
| 326 | }; |
| 327 | } |
| 328 | } |
| 329 | ); |
| 330 | |
| 331 | // remove individual posts from the display conditions post list. |
| 332 | $(document).on( |
| 333 | 'click', |
| 334 | '.advads-conditions-postid-buttons .button', |
| 335 | // eslint-disable-next-line no-unused-vars |
| 336 | function (e) { |
| 337 | $(this).remove(); |
| 338 | } |
| 339 | ); |
| 340 | // display/hide error message if no option was selected |
| 341 | // is also called on every click. |
| 342 | function advads_display_condition_option_not_selected() { |
| 343 | $('.advads-conditions-not-selected').each(function () { |
| 344 | if ($(this).siblings('input:checked').length) { |
| 345 | $(this).hide(); |
| 346 | } else { |
| 347 | $(this).show(); |
| 348 | } |
| 349 | }); |
| 350 | } |
| 351 | |
| 352 | advads_display_condition_option_not_selected(); |
| 353 | |
| 354 | // update error messages when an item is clicked. |
| 355 | $(document).on( |
| 356 | 'click', |
| 357 | '.advads-conditions-terms-buttons input[type="checkbox"], .advads-conditions-single input[type="checkbox"]', |
| 358 | function () { |
| 359 | // needs a slight delay until the buttons are updated. |
| 360 | window.setTimeout( |
| 361 | advads_display_condition_option_not_selected, |
| 362 | 200 |
| 363 | ); |
| 364 | } |
| 365 | ); |
| 366 | // activate and toggle conditions connector option. |
| 367 | $('.advads-conditions-connector input').advads_button(); |
| 368 | |
| 369 | // dynamically change label. |
| 370 | jQuery(document).on( |
| 371 | 'click', |
| 372 | '.advads-conditions-connector input', |
| 373 | function () { |
| 374 | if (jQuery(this).is(':checked')) { |
| 375 | jQuery(this) |
| 376 | .next('label') |
| 377 | .find('span') |
| 378 | .html(advadstxt.condition_or); |
| 379 | jQuery(this) |
| 380 | .parents('.advads-conditions-connector') |
| 381 | .addClass('advads-conditions-connector-or') |
| 382 | .removeClass('advads-conditions-connector-and'); |
| 383 | } else { |
| 384 | jQuery(this) |
| 385 | .next('label') |
| 386 | .find('span') |
| 387 | .html(advadstxt.condition_and); |
| 388 | jQuery(this) |
| 389 | .parents('.advads-conditions-connector') |
| 390 | .addClass('advads-conditions-connector-and') |
| 391 | .removeClass('advads-conditions-connector-or'); |
| 392 | } |
| 393 | } |
| 394 | ); |
| 395 | // remove a line with a display or visitor condition. |
| 396 | $(document).on('click', '.advads-conditions-remove', function () { |
| 397 | const tr = $(this).parents('.advads-conditions-table tr'); |
| 398 | const table = $(this).closest('.advads-conditions-table'); |
| 399 | tr.prev('tr').remove(); |
| 400 | tr.remove(); |
| 401 | |
| 402 | if (table.find('tr').length === 0) { |
| 403 | const fieldset = table.next('fieldset'); |
| 404 | table.after( |
| 405 | '<input type="hidden" name="' + |
| 406 | fieldset.data('condition-form-name') + |
| 407 | '[0][type]" value="unknown">' |
| 408 | ); |
| 409 | } |
| 410 | }); |
| 411 | }); |
| 412 | |
| 413 | /** |
| 414 | * Callback for term search autocomplete |
| 415 | * |
| 416 | * @param {type} search term |
| 417 | * @param field |
| 418 | * @param {type} callback |
| 419 | * @return {obj} json object with labels and values |
| 420 | */ |
| 421 | function advads_term_search(field, callback) { |
| 422 | // return ['post', 'poster']; |
| 423 | const query = { |
| 424 | action: 'advads-terms-search', |
| 425 | nonce: advadsglobal.ajax_nonce, |
| 426 | }; |
| 427 | |
| 428 | query.search = field.val(); |
| 429 | query.tax = field.data('tagName'); |
| 430 | |
| 431 | let querying = true; |
| 432 | |
| 433 | const results = {}; |
| 434 | jQuery.post( |
| 435 | ajaxurl, |
| 436 | query, |
| 437 | function (r) { |
| 438 | querying = false; |
| 439 | const results = []; |
| 440 | if (r) { |
| 441 | r.map(function (element, index) { |
| 442 | results[index] = { |
| 443 | value: element.term_id, |
| 444 | label: element.name, |
| 445 | }; |
| 446 | }); |
| 447 | } |
| 448 | callback(results); |
| 449 | }, |
| 450 | 'json' |
| 451 | ); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Callback for author search autocomplete |
| 456 | * |
| 457 | * @param {string} search author |
| 458 | * @param {HTMLInputElement} field html input field |
| 459 | * @param {Function} callback Callback function |
| 460 | * @return {Object} JSON object with labels and values |
| 461 | */ |
| 462 | |
| 463 | function advadsAuthorSearch(field, callback) { |
| 464 | const query = { |
| 465 | action: 'advads-authors-search', |
| 466 | nonce: advadsglobal.ajax_nonce, |
| 467 | }; |
| 468 | |
| 469 | query.search = field.val(); |
| 470 | |
| 471 | let querying = true; |
| 472 | |
| 473 | const results = []; |
| 474 | // eslint-disable-next-line no-undef |
| 475 | jQuery.post( |
| 476 | // eslint-disable-next-line no-undef |
| 477 | ajaxurl, |
| 478 | query, |
| 479 | function (r) { |
| 480 | querying = false; |
| 481 | if (r) { |
| 482 | r.map(function (element, index) { |
| 483 | results[index] = { |
| 484 | value: element.data.ID, |
| 485 | label: element.data.display_name, |
| 486 | }; |
| 487 | }); |
| 488 | } |
| 489 | callback(results); |
| 490 | }, |
| 491 | 'json' |
| 492 | ); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Callback for post search autocomplete |
| 497 | * |
| 498 | * @param {str} searchParam |
| 499 | * @param {type} callback |
| 500 | * @return {obj} json object with labels and values |
| 501 | */ |
| 502 | function advads_post_search(searchParam, callback) { |
| 503 | // return ['post', 'poster']; |
| 504 | const query = { |
| 505 | action: 'advads-post-search', |
| 506 | _ajax_linking_nonce: jQuery('#_ajax_linking_nonce').val(), |
| 507 | search: searchParam, |
| 508 | nonce: advadsglobal.ajax_nonce, |
| 509 | }; |
| 510 | |
| 511 | let querying = true; |
| 512 | |
| 513 | const results = {}; |
| 514 | jQuery.post( |
| 515 | ajaxurl, |
| 516 | query, |
| 517 | function (r) { |
| 518 | querying = false; |
| 519 | const results = []; |
| 520 | if (r) { |
| 521 | // eslint-disable-next-line array-callback-return |
| 522 | r.map(function (element, index) { |
| 523 | results[index] = { |
| 524 | label: element.title, |
| 525 | value: element.ID, |
| 526 | info: element.info, |
| 527 | }; |
| 528 | }); |
| 529 | } |
| 530 | callback(results); |
| 531 | }, |
| 532 | 'json' |
| 533 | ); |
| 534 | } |
| 535 |