admin-global.js
8 years ago
admin.js
8 years ago
advertisement.js
9 years ago
shortcode.js
10 years ago
wizard.js
8 years ago
admin.js
963 lines
| 1 | jQuery( document ).ready(function ($) { |
| 2 | if ( ! $.fn.accordion || ! $.fn.tooltip || ! advads_use_ui_buttonset() ) { |
| 3 | $( '.advads-jqueryui-error').show(); |
| 4 | } |
| 5 | |
| 6 | function advads_load_ad_type_parameter_metabox(ad_type) { |
| 7 | jQuery( '#advanced-ad-type input' ).prop( 'disabled', true ); |
| 8 | $( '#advanced-ads-tinymce-wrapper' ).hide(); |
| 9 | $( '#advanced-ads-ad-parameters' ).html( '<span class="spinner advads-ad-parameters-spinner advads-spinner"></span>' ); |
| 10 | $.ajax({ |
| 11 | type: 'POST', |
| 12 | url: ajaxurl, |
| 13 | data: { |
| 14 | 'action': 'load_ad_parameters_metabox', |
| 15 | 'ad_type': ad_type, |
| 16 | 'ad_id': $( '#post_ID' ).val(), |
| 17 | 'nonce': advadsglobal.ajax_nonce |
| 18 | }, |
| 19 | success: function (data, textStatus, XMLHttpRequest) { |
| 20 | // toggle main content field |
| 21 | if (data) { |
| 22 | $( '#advanced-ads-ad-parameters' ).html( data ).trigger( 'paramloaded' ); |
| 23 | advads_maybe_textarea_to_tinymce( ad_type ); |
| 24 | } |
| 25 | }, |
| 26 | error: function (MLHttpRequest, textStatus, errorThrown) { |
| 27 | $( '#advanced-ads-ad-parameters' ).html( errorThrown ); |
| 28 | } |
| 29 | }).always( function ( MLHttpRequest, textStatus, errorThrown ) { |
| 30 | jQuery( '#advanced-ad-type input').prop( 'disabled', false ); |
| 31 | }); |
| 32 | }; |
| 33 | |
| 34 | $( document ).on('change', '#advanced-ad-type input', function () { |
| 35 | var ad_type = $( this ).val() |
| 36 | advads_load_ad_type_parameter_metabox( ad_type ); |
| 37 | }); |
| 38 | |
| 39 | // trigger for ad injection after ad creation |
| 40 | $( '#advads-ad-injection-box .advads-ad-injection-button' ).on( 'click', function(){ |
| 41 | var placement_type = this.dataset.placementType, // create new placement |
| 42 | placement_slug = this.dataset.placementSlug, // use existing placement |
| 43 | options = {}; |
| 44 | |
| 45 | if ( ! placement_type && ! placement_slug ) { return; } |
| 46 | |
| 47 | // create new placement |
| 48 | if ( placement_type ) { |
| 49 | // for content injection |
| 50 | if ( 'post_content' === placement_type ) { |
| 51 | var paragraph = prompt( advadstxt.after_paragraph_promt, 1); |
| 52 | if ( paragraph !== null ) { |
| 53 | options.index = parseInt( paragraph, 10 ); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | $( '#advads-ad-injection-box .advads-loader' ).show(); |
| 58 | $( '#advads-ad-injection-box-placements' ).hide(); |
| 59 | $( 'body').animate({ scrollTop: $( '#advads-ad-injection-box' ).offset().top -40 }, 1, 'linear' ); |
| 60 | |
| 61 | $.ajax({ |
| 62 | type: 'POST', |
| 63 | url: ajaxurl, |
| 64 | data: { |
| 65 | action: 'advads-ad-injection-content', |
| 66 | placement_type: placement_type, |
| 67 | placement_slug: placement_slug, |
| 68 | ad_id: $( '#post_ID' ).val(), |
| 69 | options: options, |
| 70 | nonce: advadsglobal.ajax_nonce |
| 71 | }, |
| 72 | success: function (r, textStatus, XMLHttpRequest) { |
| 73 | $( '#advads-ad-injection-box *' ).hide(); |
| 74 | // append anchor to placement message |
| 75 | $( '#advads-ad-injection-message-placement-created .advads-placement-link' ).attr( 'href', $( '#advads-ad-injection-message-placement-created a' ).attr( 'href' ) + r ); |
| 76 | $( '#advads-ad-injection-message-placement-created, #advads-ad-injection-message-placement-created *' ).show(); |
| 77 | }, |
| 78 | error: function (MLHttpRequest, textStatus, errorThrown) { |
| 79 | $( '#advads-ad-injection-box' ).html( errorThrown ); |
| 80 | } |
| 81 | }).always( function ( MLHttpRequest, textStatus, errorThrown ) { |
| 82 | // jQuery( '#advanced-ad-type input').prop( 'disabled', false ); |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | // activate general buttons |
| 87 | if ( advads_use_ui_buttonset() ) { |
| 88 | $( '.advads-buttonset' ).buttonset(); |
| 89 | } |
| 90 | // activate accordions |
| 91 | if ( $.fn.accordion ) { |
| 92 | $( ".advads-accordion" ).accordion({ |
| 93 | active: false, |
| 94 | collapsible: true, |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | $( document ).on('click', '.advads-conditions-terms-buttons .button', function (e) { |
| 99 | $( this ).remove(); |
| 100 | }); |
| 101 | // display input field to search for terms |
| 102 | $( document ).on('click', '.advads-conditions-terms-show-search', function (e) { |
| 103 | e.preventDefault(); |
| 104 | // display input field |
| 105 | $( this ).siblings( '.advads-conditions-terms-search' ).show().focus(); |
| 106 | // register autocomplete |
| 107 | advads_register_terms_autocomplete( $( this ).siblings( '.advads-conditions-terms-search' ) ); |
| 108 | $( this ).next( 'br' ).show(); |
| 109 | $( this ).hide(); |
| 110 | }); |
| 111 | // function for autocomplete |
| 112 | function advads_register_terms_autocomplete( self ){ |
| 113 | self.autocomplete({ |
| 114 | source: function(request, callback){ |
| 115 | // var searchField = request.term; |
| 116 | advads_term_search( self, callback ); |
| 117 | }, |
| 118 | minLength: 1, |
| 119 | select: function( event, ui ) { |
| 120 | // append new line with input fields |
| 121 | $( '<label class="button ui-state-active">' + ui.item.label + '<input type="hidden" name="' + self.data('inputName') + '" value="' + ui.item.value + '"></label>' ).appendTo( self.siblings( '.advads-conditions-terms-buttons' ) ); |
| 122 | |
| 123 | // show / hide other elements |
| 124 | // $( '.advads-display-conditions-individual-post' ).hide(); |
| 125 | // $( '.advads-conditions-postids-list .show-search a' ).show(); |
| 126 | }, |
| 127 | close: function( event, ui ) { |
| 128 | self.val( '' ); |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | // display input field to search for post, page, etc. |
| 133 | $( document ).on( 'click', '.advads-conditions-postids-show-search', function (e) { |
| 134 | e.preventDefault(); |
| 135 | // display input field |
| 136 | $( this).next().find( '.advads-display-conditions-individual-post' ).show(); |
| 137 | //$( '.advads-conditions-postids-search-line .description' ).hide(); |
| 138 | $( this ).hide(); |
| 139 | }); |
| 140 | // register autocomplete to display condition individual posts |
| 141 | $( document ).on( "focus", ".advads-display-conditions-individual-post", function(e) { |
| 142 | var self = this; |
| 143 | if ( !$(this).data("autocomplete") ) { // If the autocomplete wasn't called yet: |
| 144 | $( this ).autocomplete({ |
| 145 | source: function(request, callback){ |
| 146 | var searchParam = request.term; |
| 147 | advads_post_search( searchParam, callback ); |
| 148 | }, |
| 149 | minLength: 1, |
| 150 | select: function( event, ui ) { |
| 151 | // append new line with input fields |
| 152 | var newline = $( '<label class="button ui-state-active">' + ui.item.label + '</label>' ); |
| 153 | $( '<input type="hidden" name="' + self.dataset.fieldName + '[value][]" value="' + ui.item.value + '"/>' ).appendTo( newline ); |
| 154 | newline.insertBefore( $( self ).parent( '.advads-conditions-postids-search-line' ) ); |
| 155 | }, |
| 156 | close: function( event, ui ) { |
| 157 | $( self ).val( '' ); |
| 158 | }, |
| 159 | }) |
| 160 | .autocomplete().data("ui-autocomplete")._renderItem = function( ul, item ) { |
| 161 | ul.addClass( "advads-conditions-postids-autocomplete-suggestions" ); |
| 162 | return $( "<li></li>" ) |
| 163 | .append( "<span class='left'>" + item.label + "</span> <span class='right'>" + item.info + "</span>" ) |
| 164 | .appendTo( ul ); |
| 165 | }; |
| 166 | }; |
| 167 | }); |
| 168 | |
| 169 | // remove individual posts from the display conditions post list |
| 170 | $( document ).on('click', '.advads-conditions-postid-buttons .button', function(e){ |
| 171 | $( this ).remove(); |
| 172 | }); |
| 173 | // display/hide error message if no option was selected |
| 174 | // is also called on every click |
| 175 | function advads_display_condition_option_not_selected(){ |
| 176 | $( '.advads-conditions-not-selected' ).each(function(){ |
| 177 | if( $( this ).siblings('input:checked').length ){ |
| 178 | $( this ).hide(); |
| 179 | } else { |
| 180 | $( this ).show(); |
| 181 | } |
| 182 | }); |
| 183 | } |
| 184 | advads_display_condition_option_not_selected(); |
| 185 | // update error messages when an item is clicked |
| 186 | $( document ).on( 'click', '.advads-conditions-terms-buttons input[type="checkbox"], .advads-conditions-single input[type="checkbox"]', function(){ |
| 187 | // needs a slight delay until the buttons are updated |
| 188 | window.setTimeout( advads_display_condition_option_not_selected, 200 ); |
| 189 | }); |
| 190 | // activate and toggle conditions connector option |
| 191 | $('.advads-conditions-connector input').advads_button(); |
| 192 | // dynamically change label |
| 193 | $(document).on('click', '.advads-conditions-connector input', function(){ |
| 194 | if( $( this ).is(':checked' ) ){ |
| 195 | $( this ).button( "option", "label", advadstxt.condition_or ); |
| 196 | $( this ).parents( '.advads-conditions-connector' ).addClass('advads-conditions-connector-or').removeClass('advads-conditions-connector-and'); |
| 197 | } else { |
| 198 | $( this ).button( "option", "label", advadstxt.condition_and ); |
| 199 | $( this ).parents( '.advads-conditions-connector' ).addClass('advads-conditions-connector-and').removeClass('advads-conditions-connector-or'); |
| 200 | } |
| 201 | }); |
| 202 | // remove a line with a display or visitor condition |
| 203 | $(document).on('click', '.advads-conditions-remove', function(){ |
| 204 | $(this).parents('.advads-conditions-table tr').prev('tr').remove(); |
| 205 | $(this).parents('.advads-conditions-table tr').remove(); |
| 206 | }); |
| 207 | |
| 208 | // display new ad group form |
| 209 | $( '#advads-new-ad-group-link' ).click(function(e){ |
| 210 | e.preventDefault(); |
| 211 | $( '#advads-new-group-form' ).show().find('input[type="text"]').focus(); |
| 212 | }); |
| 213 | |
| 214 | // display ad groups form |
| 215 | $( '#advads-ad-group-list a.edit, #advads-ad-group-list a.row-title' ).click(function(e){ |
| 216 | e.preventDefault(); |
| 217 | var advadsgroupformrow = $( this ).parents( '.advads-group-row' ).next( '.advads-ad-group-form' ); |
| 218 | if(advadsgroupformrow.is( ':visible' )){ |
| 219 | advadsgroupformrow.hide(); |
| 220 | // clear last edited id |
| 221 | $('#advads-last-edited-group').val(''); |
| 222 | } else { |
| 223 | advadsgroupformrow.show(); |
| 224 | var group_id = $( this ).parents( '.advads-group-row' ).find('.advads-group-id').val(); |
| 225 | $('#advads-last-edited-group').val( group_id ); |
| 226 | |
| 227 | } |
| 228 | }); |
| 229 | // display ad groups usage |
| 230 | $( '#advads-ad-group-list a.usage' ).click(function(e){ |
| 231 | e.preventDefault(); |
| 232 | var usagediv = $( this ).parents( '.advads-group-row' ).find( '.advads-usage' ); |
| 233 | if(usagediv.is( ':visible' )){ |
| 234 | usagediv.hide(); |
| 235 | } else { |
| 236 | usagediv.show(); |
| 237 | } |
| 238 | }); |
| 239 | // display placement settings form |
| 240 | $( '.advads-placements-table a.advads-placement-options-link' ).click(function(e){ |
| 241 | e.preventDefault(); |
| 242 | var advadsplacementformrow = $( this ).next( '.advads-placements-advanced-options' ); |
| 243 | if( advadsplacementformrow.is( ':visible' ) ){ |
| 244 | advadsplacementformrow.hide(); |
| 245 | // clear last edited id |
| 246 | $('#advads-last-edited-placement').val(''); |
| 247 | } else { |
| 248 | advadsplacementformrow.show(); |
| 249 | var placement_id = $( this ).parents( '.advads-placements-table-options' ).find('.advads-placement-slug').val(); |
| 250 | $('#advads-last-edited-placement').val( placement_id ); |
| 251 | |
| 252 | } |
| 253 | }); |
| 254 | // display manual placement usage |
| 255 | $( '.advads-placements-table .usage-link' ).click(function(e){ |
| 256 | e.preventDefault(); |
| 257 | var usagediv = $( this ).parents( 'tr' ).find( '.advads-usage' ); |
| 258 | if(usagediv.is( ':visible' )){ |
| 259 | usagediv.hide(); |
| 260 | } else { |
| 261 | usagediv.show(); |
| 262 | } |
| 263 | }); |
| 264 | /** |
| 265 | * automatically open all options and show usage link when this is the placement linked in the URL |
| 266 | * also highlight the box with an effect for a short time |
| 267 | */ |
| 268 | if( jQuery( window.location.hash ).length ){ |
| 269 | jQuery( window.location.hash ).toggleClass( 'advads-placement-background-blink' ).find( '.advads-toggle-link + div, .advads-usage' ).show(); |
| 270 | |
| 271 | } |
| 272 | |
| 273 | // group page: add ad to group |
| 274 | $( '.advads-group-add-ad button' ).click( function() { |
| 275 | var $settings_row = $( this ).closest( '.advads-ad-group-form' ), |
| 276 | $ad = $settings_row.find( '.advads-group-add-ad-list-ads option:selected' ) |
| 277 | $weight_selector = $settings_row.find( '.advads-group-add-ad-list-weights' ).last(), |
| 278 | $ad_table = $settings_row.find( '.advads-group-ads tbody' ); |
| 279 | // add new row if does not already exist |
| 280 | if ( $ad.length && $weight_selector.length && ! $ad_table.find( '[name="' + $ad.val() + '"]' ).length ) { |
| 281 | $ad_table.append( |
| 282 | $( '<tr></tr>' ).append( |
| 283 | $( '<td></td>' ).html( $ad.text() ), |
| 284 | $( '<td></td>' ).append( $weight_selector.clone().val( $weight_selector.val() ).prop( 'name', $ad.val() ) ), |
| 285 | '<td><button type="button" class="advads-remove-ad-from-group button">x</button></td>' |
| 286 | ) |
| 287 | ); |
| 288 | } |
| 289 | }); |
| 290 | // group page: remove ad from group |
| 291 | $( '#advads-ad-group-list' ).on( 'click', '.advads-remove-ad-from-group', function() { |
| 292 | var $ad_row = $( this ).closest( 'tr' ); |
| 293 | |
| 294 | if ( $ad_row.data( 'ad-id' ) ) { |
| 295 | // save the ad id, it is needed when this ad is not included in any other group |
| 296 | $( '#advads-ad-group-list form' ).append( |
| 297 | '<input type="hidden" name="advads-groups-removed-ads[]" value="' + $ad_row.data( 'ad-id' ) + '">' |
| 298 | ); |
| 299 | } |
| 300 | $ad_row.remove(); |
| 301 | }); |
| 302 | // group page: handle switching of group types based on a class derrived from that type |
| 303 | $('.advads-ad-group-type input').click(function(){ |
| 304 | advads_show_group_options( $( this ) ); |
| 305 | }); |
| 306 | function advads_show_group_options( el ){ |
| 307 | // first, hide all options except title and type |
| 308 | // iterate through all elements |
| 309 | el.each( function(){ |
| 310 | var _this = jQuery( this ); |
| 311 | _this.parents('.advads-ad-group-form').find('.advads-option:not(.static)').hide(); |
| 312 | var current_type = _this.val(); |
| 313 | |
| 314 | // now, show only the ones corresponding with the group type |
| 315 | _this.parents('.advads-ad-group-form').find( '.advads-group-type-' + current_type ).show(); |
| 316 | }); |
| 317 | } |
| 318 | // set default group options for earch group |
| 319 | |
| 320 | advads_show_group_options( $( '.advads-ad-group-type input:checked' ) ); |
| 321 | // group page: hide ads if more than 4 – than only show 3 |
| 322 | $('.advads-ad-group-list-ads').each( function(){ |
| 323 | if( 5 <= $(this).find('li').length ){ |
| 324 | $(this).find('li:gt(2)').hide(); |
| 325 | }; |
| 326 | }); |
| 327 | // show more than 3 ads when clicked on a link |
| 328 | $('.advads-group-ads-list-show-more').click( function(){ |
| 329 | jQuery( this ).hide().parents('.advads-ad-group-list-ads').find('li').show(); |
| 330 | }); |
| 331 | |
| 332 | /** |
| 333 | * SETTINGS PAGE |
| 334 | */ |
| 335 | |
| 336 | // activate licenses |
| 337 | $('.advads-license-activate').click(function(){ |
| 338 | |
| 339 | var button = $(this); |
| 340 | |
| 341 | if( ! this.dataset.addon ) { return } |
| 342 | |
| 343 | // hide button to prevent issues with activation when people click twice |
| 344 | button.hide(); |
| 345 | |
| 346 | var query = { |
| 347 | action: 'advads-activate-license', |
| 348 | addon: this.dataset.addon, |
| 349 | pluginname: this.dataset.pluginname, |
| 350 | optionslug: this.dataset.optionslug, |
| 351 | license: $(this).parents('td').find('.advads-license-key').val(), |
| 352 | security: $('#advads-licenses-ajax-referrer').val() |
| 353 | }; |
| 354 | |
| 355 | // show loader |
| 356 | $( '<span class="spinner advads-spinner"></span>' ).insertAfter( button ); |
| 357 | |
| 358 | // send and close message |
| 359 | $.post(ajaxurl, query, function (r) { |
| 360 | // remove spinner |
| 361 | $('span.spinner').remove(); |
| 362 | var parent = button.parents('td'); |
| 363 | |
| 364 | if( r === '1' ){ |
| 365 | parent.find('.advads-license-activate-error').remove(); |
| 366 | parent.find('.advads-license-deactivate').show(); |
| 367 | button.fadeOut(); |
| 368 | parent.find('.advads-license-activate-active').fadeIn(); |
| 369 | parent.find('input').prop('readonly', 'readonly'); |
| 370 | } else if( r === 'ex' ){ |
| 371 | parent.find('.advads-license-activate-error').remove(); |
| 372 | parent.find('.advads-license-expired-error').show(); |
| 373 | button.show(); |
| 374 | } else { |
| 375 | parent.find('.advads-license-activate-error').show().text( r ); |
| 376 | button.show(); |
| 377 | } |
| 378 | }); |
| 379 | }); |
| 380 | |
| 381 | // deactivate licenses |
| 382 | $('.advads-license-deactivate').click(function(){ |
| 383 | |
| 384 | var button = $(this); |
| 385 | |
| 386 | if( ! this.dataset.addon ) { return } |
| 387 | |
| 388 | // hide button to prevent issues with double clicking |
| 389 | button.hide(); |
| 390 | |
| 391 | var query = { |
| 392 | action: 'advads-deactivate-license', |
| 393 | addon: this.dataset.addon, |
| 394 | pluginname: this.dataset.pluginname, |
| 395 | optionslug: this.dataset.optionslug, |
| 396 | security: $('#advads-licenses-ajax-referrer').val() |
| 397 | }; |
| 398 | |
| 399 | // show loader |
| 400 | $( '<span class="spinner advads-spinner"></span>' ).insertAfter( button ); |
| 401 | |
| 402 | // send and close message |
| 403 | $.post(ajaxurl, query, function (r) { |
| 404 | // remove spinner |
| 405 | $('span.spinner').remove(); |
| 406 | |
| 407 | if( r === '1' ){ |
| 408 | button.siblings('.advads-license-activate-error').hide(); |
| 409 | button.siblings('.advads-license-activate-active').hide(); |
| 410 | button.siblings('.advads-license-activate').show(); |
| 411 | button.siblings('input').prop('readonly', false); |
| 412 | button.fadeOut(); |
| 413 | } else if( r === 'ex' ){ |
| 414 | button.siblings('.advads-license-activate-error').hide(); |
| 415 | button.siblings('.advads-license-activate-active').hide(); |
| 416 | button.siblings('.advads-license-expired-error').show(); |
| 417 | button.siblings('input').prop('readonly', false); |
| 418 | button.fadeOut(); |
| 419 | } else { |
| 420 | button.siblings('.advads-license-activate-error').show().text( r ); |
| 421 | button.siblings('.advads-license-activate-active').hide(); |
| 422 | button.show(); |
| 423 | } |
| 424 | }); |
| 425 | }); |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | /** |
| 431 | * PLACEMENTS |
| 432 | */ |
| 433 | // show image tooltips |
| 434 | if ( $.fn.tooltip ) { |
| 435 | $( ".advads-placements-new-form .advads-placement-type" ).tooltip({ |
| 436 | items: "span", |
| 437 | content: function() { |
| 438 | return $( this ).parents('.advads-placement-type').find( '.advads-placement-description' ).html(); |
| 439 | } |
| 440 | }); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Image ad uploader |
| 445 | */ |
| 446 | $('body').on('click', '.advads_image_upload', function(e) { |
| 447 | |
| 448 | e.preventDefault(); |
| 449 | |
| 450 | var button = $(this); |
| 451 | |
| 452 | // If the media frame already exists, reopen it. |
| 453 | if ( file_frame ) { |
| 454 | // file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); |
| 455 | file_frame.open(); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | // Create the media frame. |
| 460 | file_frame = wp.media.frames.file_frame = wp.media( { |
| 461 | id: 'advads_type_image_wp_media', |
| 462 | title: button.data( 'uploaderTitle' ), |
| 463 | button: { |
| 464 | text: button.data( 'uploaderButtonText' ) |
| 465 | }, |
| 466 | library: { |
| 467 | type: 'image' |
| 468 | }, |
| 469 | multiple: false // only allow one file to be selected |
| 470 | } ); |
| 471 | |
| 472 | // When an image is selected, run a callback. |
| 473 | file_frame.on( 'select', function() { |
| 474 | |
| 475 | var selection = file_frame.state().get('selection'); |
| 476 | selection.each( function( attachment, index ) { |
| 477 | attachment = attachment.toJSON(); |
| 478 | if ( 0 === index ) { |
| 479 | // place first attachment in field |
| 480 | $( '#advads-image-id' ).val( attachment.id ); |
| 481 | $( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( attachment.width ); |
| 482 | $( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( attachment.height ); |
| 483 | // update image preview |
| 484 | var new_image = '<img width="'+ attachment.width +'" height="'+ attachment.height + |
| 485 | '" title="'+ attachment.title +'" alt="'+ attachment.alt +'" src="'+ attachment.url +'"/>'; |
| 486 | $('#advads-image-preview').html( new_image ); |
| 487 | $('#advads-image-edit-link').attr( 'href', attachment.editLink ); |
| 488 | // process "reserve this space" checkbox |
| 489 | $( '#advanced-ads-ad-parameters-size input[type=number]:first' ).change(); |
| 490 | } |
| 491 | }); |
| 492 | }); |
| 493 | |
| 494 | // Finally, open the modal |
| 495 | file_frame.open(); |
| 496 | }); |
| 497 | |
| 498 | // WP 3.5+ uploader |
| 499 | var file_frame; |
| 500 | window.formfield = ''; |
| 501 | |
| 502 | advads_ad_list_build_filters(); |
| 503 | |
| 504 | // adblocker related code |
| 505 | $( '#advanced-ads-use-adblocker' ).change( function() { |
| 506 | advads_toggle_box( this, '#advads-adblocker-wrapper' ); |
| 507 | }); |
| 508 | |
| 509 | // processing of the rebuild asset form and the FTP/SSH credentials form |
| 510 | var $advads_adblocker_wrapper = $( '#advads-adblocker-wrapper' ); |
| 511 | $advads_adblocker_wrapper.find( 'input[type="submit"]' ).prop( 'disabled', false ); |
| 512 | $advads_adblocker_wrapper.on( 'submit', 'form', function( event ) { |
| 513 | event.preventDefault(); |
| 514 | var $form = $( '#advanced-ads-rebuild-assets-form' ); |
| 515 | $form.prev( '.error' ).remove(); |
| 516 | $form.find( 'input[type="submit"]' ).prop( 'disabled', true ).after( '<span class="spinner advads-spinner"></span>' ); |
| 517 | |
| 518 | var args = { |
| 519 | data: { |
| 520 | action: 'advads-adblock-rebuild-assets', |
| 521 | nonce: advadsglobal.ajax_nonce, |
| 522 | }, |
| 523 | done: function( data, textStatus, jqXHR ) { |
| 524 | var $advads_adblocker_wrapper = $( '#advads-adblocker-wrapper' ); |
| 525 | $advads_adblocker_wrapper.html( data ); |
| 526 | }, |
| 527 | fail: function( jqXHR, textStatus, errorThrown ) { |
| 528 | $form.before( '<div class="error"><p>' + textStatus + ': ' + errorThrown + '</p></div>' ); |
| 529 | $form.find( 'input[type="submit"]' ).prop( 'disabled', false ).next( '.advads-spinner' ).remove(); |
| 530 | }, |
| 531 | on_modal_close: function() { |
| 532 | var $form = $( '#advanced-ads-rebuild-assets-form' ); |
| 533 | $form.find( 'input[type="submit"]' ).prop( 'disabled', false ).next( '.advads-spinner' ).remove(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | $.each( $form.serializeArray(), function( i, o ) { |
| 538 | args.data[ o.name ] = o.value; |
| 539 | }); |
| 540 | |
| 541 | advanced_ads_admin.filesystem.ajax( args ); |
| 542 | }); |
| 543 | |
| 544 | // process "reserve this space" checkbox |
| 545 | $( '#advanced-ads-ad-parameters' ).on( 'change', '#advanced-ads-ad-parameters-size input[type=number]', function() { |
| 546 | if ( $( '#advanced-ads-ad-parameters-size input[type=number]' ).filter( function() { |
| 547 | return parseInt( this.value, 10 ) > 0; |
| 548 | }).length === 2 ) { |
| 549 | $( '#advads-wrapper-add-sizes' ).prop( 'disabled', false ); |
| 550 | } else { |
| 551 | $( '#advads-wrapper-add-sizes' ).prop( 'disabled', true ).prop( 'checked', false ); |
| 552 | } |
| 553 | }); |
| 554 | // process "reserve this space" checkbox - ad type changed |
| 555 | $( '#advanced-ads-ad-parameters' ).on( 'paramloaded', function() { |
| 556 | $( '#advanced-ads-ad-parameters-size input[type=number]:first' ).change(); |
| 557 | }) |
| 558 | // process "reserve this space" checkbox - on load |
| 559 | $( '#advanced-ads-ad-parameters-size input[type=number]:first' ).change(); |
| 560 | |
| 561 | // move meta box markup to hndle headline |
| 562 | $('.advads-hndlelinks').each(function(){ |
| 563 | $(this).removeClass('hidden'); |
| 564 | $(this).appendTo( $(this).parent('.inside').siblings('h2.hndle') ); |
| 565 | }); |
| 566 | // open tutorial link when clicked on it |
| 567 | $('.advads-video-link').click(function( el ){ |
| 568 | el.preventDefault(); |
| 569 | var video_container = $(this).parents('h2').siblings('.inside').find('.advads-video-link-container'); |
| 570 | video_container.html( video_container.data('videolink') ); |
| 571 | }); |
| 572 | // open inline tutorial link when clicked on it |
| 573 | $('.advads-video-link-inline').click(function( el ){ |
| 574 | el.preventDefault(); |
| 575 | var video_container = $(this).parents('div').siblings('.advads-video-link-container'); |
| 576 | video_container.html( video_container.data('videolink') ); |
| 577 | }); |
| 578 | // switch import type |
| 579 | jQuery( '.advads_import_type' ).change( function() { |
| 580 | if ( this.value === 'xml_content' ) { |
| 581 | jQuery( '#advads_xml_file' ).hide(); |
| 582 | jQuery( '#advads_xml_content' ).show(); |
| 583 | } else { |
| 584 | jQuery( '#advads_xml_file' ).show(); |
| 585 | jQuery( '#advads_xml_content' ).hide(); |
| 586 | } |
| 587 | }); |
| 588 | }); |
| 589 | |
| 590 | /** |
| 591 | * store the action hash in settings form action |
| 592 | * thanks for Yoast SEO for this idea |
| 593 | */ |
| 594 | function advads_set_tab_hashes() { |
| 595 | // iterate through forms |
| 596 | jQuery( '#advads-tabs' ).find( 'a' ).each(function () { |
| 597 | var id = jQuery( this ).attr( 'id' ).replace( '-tab', '' ); |
| 598 | var optiontab = jQuery( '#' + id ); |
| 599 | |
| 600 | var form = optiontab.children( '.advads-settings-tab-main-form' ); |
| 601 | if ( form.length ) { |
| 602 | var currentUrl = form.attr( 'action' ).split( '#' )[ 0 ]; |
| 603 | form.attr( 'action', currentUrl + jQuery( this ).attr( 'href' ) ); |
| 604 | } |
| 605 | }); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * callback for term search autocomplete |
| 610 | * |
| 611 | * @param {type} search term |
| 612 | * @param {type} callback |
| 613 | * @returns {obj} json object with labels and values |
| 614 | */ |
| 615 | function advads_term_search(field, callback) { |
| 616 | |
| 617 | // return ['post', 'poster']; |
| 618 | var query = { |
| 619 | action: 'advads-terms-search', |
| 620 | nonce: advadsglobal.ajax_nonce |
| 621 | }; |
| 622 | |
| 623 | query.search = field.val(); |
| 624 | query.tax = field.data('tagName'); |
| 625 | |
| 626 | var querying = true; |
| 627 | |
| 628 | var results = {}; |
| 629 | jQuery.post(ajaxurl, query, function (r) { |
| 630 | querying = false; |
| 631 | var results = []; |
| 632 | if(r){ |
| 633 | r.map(function(element, index){ |
| 634 | results[index] = { |
| 635 | value: element.term_id, |
| 636 | label: element.name |
| 637 | }; |
| 638 | }); |
| 639 | } |
| 640 | callback( results ); |
| 641 | }, 'json'); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * callback for post search autocomplete |
| 646 | * |
| 647 | * @param {str} searchParam |
| 648 | * @param {type} callback |
| 649 | * @returns {obj} json object with labels and values |
| 650 | */ |
| 651 | function advads_post_search( searchParam, callback ) { |
| 652 | |
| 653 | // return ['post', 'poster']; |
| 654 | var query = { |
| 655 | action: 'advads-post-search', |
| 656 | _ajax_linking_nonce: jQuery( '#_ajax_linking_nonce' ).val(), |
| 657 | 'search': searchParam, |
| 658 | nonce: advadsglobal.ajax_nonce |
| 659 | }; |
| 660 | |
| 661 | var querying = true; |
| 662 | |
| 663 | var results = {}; |
| 664 | jQuery.post(ajaxurl, query, function (r) { |
| 665 | querying = false; |
| 666 | var results = []; |
| 667 | if(r){ |
| 668 | r.map(function(element, index){ |
| 669 | results[index] = { |
| 670 | label: element.title, |
| 671 | value: element.ID, |
| 672 | info: element.info |
| 673 | }; |
| 674 | }); |
| 675 | } |
| 676 | callback( results ); |
| 677 | }, 'json'); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * toggle content elements (hide/show) |
| 682 | * |
| 683 | * @param selector jquery selector |
| 684 | */ |
| 685 | function advads_toggle(selector) { |
| 686 | jQuery( selector ).slideToggle(); |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * toggle content elements with a checkbox (hide/show) |
| 691 | * |
| 692 | * @param selector jquery selector |
| 693 | */ |
| 694 | function advads_toggle_box(e, selector) { |
| 695 | if (jQuery( e ).is( ':checked' )) { |
| 696 | jQuery( selector ).slideDown(); |
| 697 | } else { |
| 698 | jQuery( selector ).slideUp(); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * disable content of one box when selecting another |
| 704 | * only grey/disable it, don’t hide it |
| 705 | * |
| 706 | * @param selector jquery selector |
| 707 | */ |
| 708 | function advads_toggle_box_enable(e, selector) { |
| 709 | if (jQuery( e ).is( ':checked' )) { |
| 710 | jQuery( selector ).find( 'input' ).removeAttr( 'disabled', '' ); |
| 711 | } else { |
| 712 | jQuery( selector ).find( 'input' ).attr( 'disabled', 'disabled' ); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * validate placement form on submit |
| 718 | */ |
| 719 | function advads_validate_placement_form(){ |
| 720 | // check if placement type was selected |
| 721 | if( ! jQuery('.advads-placement-type input:checked').length){ |
| 722 | jQuery('.advads-placement-type-error').show(); |
| 723 | return false; |
| 724 | } else { |
| 725 | jQuery('.advads-placement-type-error').hide(); |
| 726 | } |
| 727 | // check if placement name was entered |
| 728 | if( jQuery('.advads-new-placement-name').val() == '' ){ |
| 729 | jQuery('.advads-placement-name-error').show(); |
| 730 | return false; |
| 731 | } else { |
| 732 | jQuery('.advads-placement-name-error').hide(); |
| 733 | } |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * replace textarea with TinyMCE editor for Rich Content ad type |
| 739 | */ |
| 740 | function advads_maybe_textarea_to_tinymce( ad_type ) { |
| 741 | var textarea = jQuery( '#advads-ad-content-plain' ), |
| 742 | textarea_html = textarea.val(), |
| 743 | tinymce_id = 'advanced-ads-tinymce', |
| 744 | tinymce_id_ws = jQuery( '#' + tinymce_id ), |
| 745 | tinymce_wrapper_div = jQuery ( '#advanced-ads-tinymce-wrapper' ); |
| 746 | |
| 747 | if ( ad_type !== 'content' ) { |
| 748 | tinymce_id_ws.prop('name', tinymce_id ); |
| 749 | tinymce_wrapper_div.hide(); |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | if ( typeof tinyMCE === 'object' && tinyMCE.get( tinymce_id ) !== null ) { |
| 754 | // visual mode |
| 755 | if ( textarea_html ) { |
| 756 | // see BeforeSetContent in the wp-includes\js\tinymce\plugins\wordpress\plugin.js |
| 757 | var wp = window.wp, |
| 758 | hasWpautop = ( wp && wp.editor && wp.editor.autop && tinyMCE.get( tinymce_id ).getParam( 'wpautop', true ) ); |
| 759 | if ( hasWpautop ) { |
| 760 | textarea_html = wp.editor.autop( textarea_html ); |
| 761 | } |
| 762 | tinyMCE.get( tinymce_id ).setContent( textarea_html ); |
| 763 | } |
| 764 | textarea.remove(); |
| 765 | tinymce_id_ws.prop('name', textarea.prop( 'name' ) ); |
| 766 | tinymce_wrapper_div.show(); |
| 767 | } else if ( tinymce_id_ws.length ) { |
| 768 | // text mode |
| 769 | tinymce_id_ws.val( textarea_html ); |
| 770 | textarea.remove(); |
| 771 | tinymce_id_ws.prop('name', textarea.prop( 'name' ) ); |
| 772 | tinymce_wrapper_div.show(); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * adds <option> tags to <select> dropdowns before the 'Filter' button on the ad list table |
| 778 | * @param {string} jQuery wrapped set where to find value/text for <option> |
| 779 | * @param {string} jQuery wrapped set with <select> tag |
| 780 | */ |
| 781 | function advads_ad_list_add_items_to_dropdowns( $input, $select ) { |
| 782 | var all_unique_rows = []; |
| 783 | |
| 784 | $input.each( function() { |
| 785 | var one_row = jQuery( this ).text(); |
| 786 | |
| 787 | if ( jQuery.inArray( one_row, all_unique_rows ) === -1 ) { |
| 788 | all_unique_rows.push( one_row ); |
| 789 | $select.append( |
| 790 | jQuery("<option/>", { |
| 791 | value: one_row, |
| 792 | text: one_row |
| 793 | }) |
| 794 | ); |
| 795 | } |
| 796 | }); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * adds filter dropdowns before the 'Filter' button on the ad list table |
| 801 | */ |
| 802 | function advads_ad_list_build_filters() { |
| 803 | var $filter_type = jQuery( '#advads-filter-type' ), |
| 804 | $filter_size = jQuery( '#advads-filter-size' ), |
| 805 | $filter_group = jQuery( '#advads-filter-group' ), |
| 806 | $filter_date = jQuery( '#advads-filter-date' ), |
| 807 | $ad_planning_col = jQuery( '.advads-filter-timing' ); |
| 808 | |
| 809 | advads_ad_list_add_items_to_dropdowns( jQuery( '.advads-ad-type' ), $filter_type ); |
| 810 | advads_ad_list_add_items_to_dropdowns( jQuery( '.advads-ad-size' ), $filter_size ); |
| 811 | advads_ad_list_add_items_to_dropdowns( jQuery( '.taxonomy-advanced_ads_groups a' ), $filter_group ); |
| 812 | // if such classes exist on the page - show related <option>s |
| 813 | jQuery( '#advads-filter-date option:gt(0)' ).each( function() { |
| 814 | var v = jQuery( this ).val(); |
| 815 | if ( $ad_planning_col.hasClass( v ) ) { |
| 816 | $filter_date.children( 'option[value="' + v + '"] ' ).show(); |
| 817 | } |
| 818 | }); |
| 819 | |
| 820 | jQuery( "#advads-filter-type, #advads-filter-size, #advads-filter-group, #advads-filter-date" ).change( function() { |
| 821 | var $the_list_tr = jQuery( '#the-list tr' ).removeClass( 'advads-hidden' ); |
| 822 | |
| 823 | if ( $filter_type.val() ) { |
| 824 | $the_list_tr.filter( function() { |
| 825 | return jQuery( this ).find( '.advads-ad-type' ).text() !== $filter_type.val(); |
| 826 | }).addClass( 'advads-hidden' ); |
| 827 | } |
| 828 | if ( $filter_size.val() ) { |
| 829 | $the_list_tr.not( '.advads-hidden' ).filter ( function() { |
| 830 | return jQuery( this ).find( '.advads-ad-size' ).text() !== $filter_size.val(); |
| 831 | }).addClass( 'advads-hidden' ); |
| 832 | } |
| 833 | if ( $filter_date.val() ) { |
| 834 | $the_list_tr.not( '.advads-hidden' ).filter( function() { |
| 835 | return jQuery( this ).find( '.' + $filter_date.val() ).length === 0; |
| 836 | }).addClass( 'advads-hidden' ); |
| 837 | } |
| 838 | if ( $filter_group.val() ) { |
| 839 | $the_list_tr.not( '.advads-hidden' ).filter( function() { |
| 840 | var ret = false; |
| 841 | //iterate through each group within current tr |
| 842 | jQuery( this ).find( '.taxonomy-advanced_ads_groups a' ).each( function() { |
| 843 | if ( jQuery( this ).text() === $filter_group.val() ) { |
| 844 | ret = true; |
| 845 | return false; //break the loop |
| 846 | } |
| 847 | }); |
| 848 | return ret === false; |
| 849 | }).addClass( 'advads-hidden' ); |
| 850 | } |
| 851 | // create stripped table, because css nth-child does not counts hidden rows |
| 852 | $the_list_tr.not( '.advads-hidden' ).filter( ':odd' ).addClass( 'advads-ad-list-odd' ).removeClass( 'advads-ad-list-even' ).end() |
| 853 | .filter( ':even' ).addClass( 'advads-ad-list-even' ).removeClass( 'advads-ad-list-odd' ); |
| 854 | }); |
| 855 | } |
| 856 | |
| 857 | // Change JQueryUI names to fix name collision with other libraries, eg. Bootstrap |
| 858 | jQuery.fn.advads_button = jQuery.fn.button; |
| 859 | |
| 860 | /** |
| 861 | * check if jQueryUI button/buttonset can be used |
| 862 | */ |
| 863 | var advads_use_ui_buttonset = ( function() { |
| 864 | var ret = null; |
| 865 | return function () { |
| 866 | if ( null === ret ) { |
| 867 | var needle = 'var g="string"==typeof f,h=c.call(arguments,1)'; //string from jquery-ui source code |
| 868 | ret = jQuery.fn.advads_button && jQuery.fn.buttonset && jQuery.fn.button.toString().indexOf( needle ) !== -1; |
| 869 | } |
| 870 | return ret; |
| 871 | }; |
| 872 | })(); |
| 873 | |
| 874 | window.advanced_ads_admin = window.advanced_ads_admin || {}; |
| 875 | advanced_ads_admin.filesystem = { |
| 876 | /** |
| 877 | * Holds the current job while the user writes data in the 'Connection Information' modal. |
| 878 | * |
| 879 | * @type {obj} |
| 880 | */ |
| 881 | _locked_job: false, |
| 882 | |
| 883 | /** |
| 884 | * Toggle the 'Connection Information' modal. |
| 885 | */ |
| 886 | _requestForCredentialsModalToggle: function() { |
| 887 | this.$filesystemModal.toggle(); |
| 888 | jQuery( 'body' ).toggleClass( 'modal-open' ); |
| 889 | }, |
| 890 | |
| 891 | _init: function() { |
| 892 | this._init = function() {} |
| 893 | var self = this; |
| 894 | |
| 895 | self.$filesystemModal = jQuery( '#advanced-ads-rfc-dialog' ); |
| 896 | /** |
| 897 | * Sends saved job. |
| 898 | */ |
| 899 | self.$filesystemModal.on( 'submit', 'form', function( event ) { |
| 900 | event.preventDefault(); |
| 901 | |
| 902 | self.ajax( self._locked_job, true ); |
| 903 | self._requestForCredentialsModalToggle() |
| 904 | } ); |
| 905 | |
| 906 | /** |
| 907 | * Closes the request credentials modal when clicking the 'Cancel' button. |
| 908 | */ |
| 909 | self.$filesystemModal.on( 'click', '[data-js-action="close"]', function() { |
| 910 | if ( jQuery.isPlainObject( self._locked_job ) && self._locked_job.on_modal_close ) { |
| 911 | self._locked_job.on_modal_close(); |
| 912 | } |
| 913 | |
| 914 | self._locked_job = false; |
| 915 | self._requestForCredentialsModalToggle(); |
| 916 | } ); |
| 917 | }, |
| 918 | |
| 919 | /** |
| 920 | * Sends AJAX request. Shows 'Connection Information' modal if needed. |
| 921 | * |
| 922 | * @param {object} args |
| 923 | * @param {bool} skip_modal |
| 924 | */ |
| 925 | ajax: function( args, skip_modal ) { |
| 926 | this._init(); |
| 927 | |
| 928 | if ( ! skip_modal && this.$filesystemModal.length > 0 ) { |
| 929 | this._requestForCredentialsModalToggle(); |
| 930 | this.$filesystemModal.find( 'input:enabled:first' ).focus(); |
| 931 | |
| 932 | // Do not send request. |
| 933 | this._locked_job = args; |
| 934 | return; |
| 935 | } |
| 936 | |
| 937 | var options = { |
| 938 | method: 'POST', |
| 939 | url: window.ajaxurl, |
| 940 | data: { |
| 941 | username: jQuery( '#username' ).val(), |
| 942 | password: jQuery( '#password' ).val(), |
| 943 | hostname: jQuery( '#hostname' ).val(), |
| 944 | connection_type: jQuery( 'input[name="connection_type"]:checked' ).val(), |
| 945 | public_key: jQuery( '#public_key' ).val(), |
| 946 | private_key: jQuery( '#private_key' ).val() |
| 947 | } |
| 948 | }; |
| 949 | |
| 950 | options.data = jQuery.extend( options.data, args.data ); |
| 951 | var request = jQuery.ajax( options ); |
| 952 | |
| 953 | if ( args.done ) { |
| 954 | request.done( args.done ); |
| 955 | } |
| 956 | |
| 957 | if ( args.fail ) { |
| 958 | request.fail( args.fail ); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 |