adblocker-image-data.js
1 year ago
admin-global.js
1 year ago
admin.js
1 year ago
admin.min.js
1 year ago
advertisement.js
9 years ago
conditions.js
1 year ago
dialog-advads-modal.js
1 year ago
groups.js
1 year ago
termination.js
1 year ago
ui.js
3 years ago
wizard.js
1 year ago
wizard.js
214 lines
| 1 | /* eslint-disable camelcase */ |
| 2 | |
| 3 | const advads_wizard = { |
| 4 | box_order: [ |
| 5 | // selectors of elements in the wizard in the correct order |
| 6 | '#post-body-content, #ad-types-box', // show title and type together |
| 7 | '#ad-parameters-box', |
| 8 | '#ad-targeting-box', // display and visitor conditions |
| 9 | ], |
| 10 | current_box: '#post-body-content, #ad-types-box', // current active box |
| 11 | one_column: false, // whether the edit screen is in one-column mode |
| 12 | status: false, // what is the current status? true if running, else false |
| 13 | init() { |
| 14 | // status can be "start" to start wizard or nothing to not start it |
| 15 | const _this = this; |
| 16 | jQuery('#advads-wizard-controls-next').on('click', function () { |
| 17 | _this.next(); |
| 18 | }); |
| 19 | |
| 20 | jQuery('#advads-wizard-controls-prev').on('click', function () { |
| 21 | _this.prev(); |
| 22 | }); |
| 23 | |
| 24 | jQuery('#advads-wizard-controls-save').on('click', function (e) { |
| 25 | e.preventDefault(); |
| 26 | jQuery('#publish').trigger('click'); |
| 27 | }); // save ad |
| 28 | |
| 29 | jQuery('#advads-wizard-display-conditions-show').on( |
| 30 | 'click', |
| 31 | function () { |
| 32 | _this.show_conditions('#ad-targeting-box'); |
| 33 | } |
| 34 | ); |
| 35 | |
| 36 | jQuery('#advads-wizard-visitor-conditions-show').on( |
| 37 | 'click', |
| 38 | function () { |
| 39 | _this.show_conditions('#ad-targeting-box'); |
| 40 | } |
| 41 | ); |
| 42 | |
| 43 | jQuery('.advads-show-in-wizard').hide(); |
| 44 | jQuery('#advads-start-wizard').on('click', function () { |
| 45 | _this.start(); |
| 46 | }); |
| 47 | |
| 48 | // end wizard when the button was clicked |
| 49 | jQuery('.advads-stop-wizard').on('click', function () { |
| 50 | _this.close(); |
| 51 | }); |
| 52 | |
| 53 | // jump to next box when ad type is selected |
| 54 | jQuery('#advanced-ad-type input').on('change', function () { |
| 55 | _this.next(); |
| 56 | }); |
| 57 | }, |
| 58 | show_current_box() { |
| 59 | jQuery(this.current_box).removeClass('advads-hide'); |
| 60 | }, |
| 61 | start() { |
| 62 | // do stuff when wizard is started |
| 63 | if (!this.is_form_valid()) { |
| 64 | return; |
| 65 | } |
| 66 | // show page in 1-column stype |
| 67 | this.status = true; |
| 68 | if (jQuery('#post-body').hasClass('columns-1')) { |
| 69 | this.one_column = true; |
| 70 | } else { |
| 71 | jQuery('#post-body').addClass('columns-1').removeClass('columns-2'); |
| 72 | } |
| 73 | // hide all boxes, messages and the headline by adding a hide css class |
| 74 | jQuery( |
| 75 | '#post-body-content, .postbox-container .postbox, h1 ~ div:not(.advads-admin-notice):not(#message.updated), h1' |
| 76 | ).addClass('advads-hide'); |
| 77 | // display first box |
| 78 | this.show_current_box(); |
| 79 | // display close button and controls |
| 80 | jQuery('#advads-stop-wizard, #advads-wizard-controls').removeClass( |
| 81 | 'hidden' |
| 82 | ); |
| 83 | this.update_nav(); |
| 84 | // initially hide some elemente |
| 85 | jQuery('#advads-ad-description').addClass('advads-hide'); // ad description |
| 86 | jQuery('#advads-ad-info').addClass('advads-hide'); // shortcode and php function info |
| 87 | // hide all elements with 'advads-hide-for-wizard' class |
| 88 | jQuery('.advads-hide-in-wizard').hide(); |
| 89 | jQuery('.advads-show-in-wizard').show(); |
| 90 | jQuery('#advads-start-wizard').hide(); |
| 91 | // remove close-class from ad type box |
| 92 | jQuery('#ad-types-box').removeClass('closed'); |
| 93 | this.save_hide_wizard(false); |
| 94 | }, |
| 95 | close() { |
| 96 | // close the wizard by showing all elements again |
| 97 | this.status = false; |
| 98 | jQuery('.advads-hide').removeClass('advads-hide'); |
| 99 | jQuery('#advads-stop-wizard, #advads-wizard-controls').addClass( |
| 100 | 'hidden' |
| 101 | ); |
| 102 | if (this.one_column !== true) { |
| 103 | jQuery('#post-body').addClass('columns-2').removeClass('columns-1'); |
| 104 | } |
| 105 | // reset current box |
| 106 | this.current_box = this.box_order[0]; |
| 107 | jQuery('#advads-wizard-welcome').remove(); // close wizard welcome message |
| 108 | // show all elements with 'advads-hide-for-wizard' class |
| 109 | jQuery('.advads-hide-in-wizard').show(); |
| 110 | jQuery('.advads-show-in-wizard').hide(); |
| 111 | jQuery('#advads-start-wizard').show(); |
| 112 | this.save_hide_wizard(true); |
| 113 | }, |
| 114 | update_nav() { |
| 115 | // update navigation, display only valid buttons |
| 116 | // display all buttons |
| 117 | jQuery('#advads-wizard-controls button').removeClass('hidden'); |
| 118 | // hide next button if there is no next widget |
| 119 | const i = this.box_order.indexOf(this.current_box); |
| 120 | if (i === this.box_order.length - 1) { |
| 121 | jQuery('#advads-wizard-controls-next').addClass('hidden'); |
| 122 | } |
| 123 | if (i === 0) { |
| 124 | jQuery('#advads-wizard-controls-prev').addClass('hidden'); |
| 125 | } |
| 126 | // hide save button for first boxes |
| 127 | if (i <= 1) { |
| 128 | jQuery('#advads-wizard-controls-save').addClass('hidden'); |
| 129 | } else { |
| 130 | jQuery('#advads-wizard-controls-save').removeClass('hidden'); |
| 131 | } |
| 132 | }, |
| 133 | |
| 134 | /** |
| 135 | * Check the form just before some of its fields become hidden. |
| 136 | */ |
| 137 | is_form_valid() { |
| 138 | const form = jQuery('form#post')[0]; |
| 139 | |
| 140 | if (form.checkValidity && form.reportValidity) { |
| 141 | if (!form.checkValidity()) { |
| 142 | // Highlight invalid fields. |
| 143 | form.reportValidity(); |
| 144 | return false; |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | // Disable validation so that hidden invalid fields do not prevent the form from being sent. |
| 149 | form.setAttribute('novalidate', true); |
| 150 | return true; |
| 151 | }, |
| 152 | next() { |
| 153 | // show next box |
| 154 | if (!this.is_form_valid()) { |
| 155 | return; |
| 156 | } |
| 157 | if (!this.status) { |
| 158 | return; |
| 159 | } |
| 160 | // get index of current item in box-array |
| 161 | const i = this.box_order.indexOf(this.current_box); |
| 162 | // check if there is a next index |
| 163 | if (this.box_order[i + 1] === undefined) { |
| 164 | return; |
| 165 | } |
| 166 | // hide current element |
| 167 | jQuery(this.current_box).addClass('advads-hide'); |
| 168 | // load next element into current |
| 169 | this.current_box = this.box_order[i + 1]; |
| 170 | this.show_current_box(); |
| 171 | this.update_nav(); |
| 172 | }, |
| 173 | prev() { |
| 174 | // show previous box |
| 175 | if (!this.is_form_valid()) { |
| 176 | return; |
| 177 | } |
| 178 | // get index of current item in box-array |
| 179 | const i = this.box_order.indexOf(this.current_box); |
| 180 | // check if there is a previous index |
| 181 | if (this.box_order[i - 1] === undefined) { |
| 182 | return; |
| 183 | } |
| 184 | // hide current element |
| 185 | jQuery(this.current_box).addClass('advads-hide'); |
| 186 | // load next element into current |
| 187 | this.current_box = this.box_order[i - 1]; |
| 188 | this.show_current_box(); |
| 189 | this.update_nav(); |
| 190 | }, |
| 191 | save_hide_wizard(hideWizard) { |
| 192 | // update wizard state (started by default or not?) |
| 193 | |
| 194 | jQuery.ajax({ |
| 195 | type: 'POST', |
| 196 | url: ajaxurl, |
| 197 | data: { |
| 198 | action: 'advads-save-hide-wizard-state', |
| 199 | hideWizard, |
| 200 | nonce: advadsglobal.ajax_nonce, |
| 201 | }, |
| 202 | }); |
| 203 | }, |
| 204 | show_conditions(box) { |
| 205 | // show the conditions options in display and visitor conditions |
| 206 | jQuery(box).find('.advads-show-in-wizard').hide(); |
| 207 | jQuery(box).find('.advads-hide-in-wizard').show(); |
| 208 | }, |
| 209 | }; |
| 210 | |
| 211 | jQuery(document).ready(function () { |
| 212 | advads_wizard.init(); |
| 213 | }); |
| 214 |