jquery.liveFilter.js
9 years ago
jquery.liveFilter.min.js
8 years ago
jquery.widgetopts.beaver.js
8 years ago
jquery.widgetopts.beaver.min.js
7 years ago
select2-settings.js
8 years ago
select2-settings.min.js
7 years ago
select2.min.js
8 years ago
settings.js
8 years ago
settings.min.js
7 years ago
widgets.js
3 years ago
widgets.min.js
5 years ago
wpWidgetOpts.js
3 years ago
wpWidgetOpts.min.js
5 years ago
settings.js
353 lines
| 1 | "use strict"; |
| 2 | |
| 3 | var widgetoptsSettingsModule = { |
| 4 | init: function() { |
| 5 | var self = this; |
| 6 | jQuery( '.widgetopts-module-settings-container' ).hide(); |
| 7 | |
| 8 | self.bindEvents(); |
| 9 | }, |
| 10 | |
| 11 | bindEvents: function() { |
| 12 | var self = this; |
| 13 | var $wpcontent = jQuery( '#wpcontent' ); |
| 14 | |
| 15 | $wpcontent.on( 'click', '.widgetopts-toggle-settings, .widgetopts-module-settings-cancel', self.openModal ); |
| 16 | $wpcontent.on( 'click', '.widgetopts-close-modal, .widgetopts-modal-background', self.closeModal ); |
| 17 | $wpcontent.on( 'keyup', self.closeModal ); |
| 18 | $wpcontent.on( 'click', '.widgetopts-toggle-activation', self.moduleToggle ); |
| 19 | $wpcontent.on( 'click', '.widgetopts-module-settings-save', self.saveSettings ); |
| 20 | // $wpcontent.on( 'click', '.widgetopts-license-button', self.licenseHandler ); |
| 21 | $wpcontent.on( 'click', '.opts-add-class-btn', self.toggleCustomClass ); |
| 22 | $wpcontent.on( 'click', '.opts-remove-class-btn', self.removeCustomClass ); |
| 23 | $wpcontent.on( 'click', '.widgetopts-delete-cache', self.clearWidgetCache ); |
| 24 | $wpcontent.on( 'click', '.widgetopts-license_deactivate', self.deactivationHandler ); |
| 25 | |
| 26 | }, |
| 27 | |
| 28 | openModal: function( e ) { |
| 29 | e.preventDefault(); |
| 30 | |
| 31 | var $container = jQuery(this).parents( '.widgetopts-module-card' ).find( '.widgetopts-module-settings-container' ), |
| 32 | $modalBg = jQuery( '.widgetopts-modal-background' ); |
| 33 | |
| 34 | $modalBg.show(); |
| 35 | $container |
| 36 | .show(); |
| 37 | |
| 38 | jQuery( 'body' ).addClass( 'widgetopts-modal-open' ); |
| 39 | }, |
| 40 | |
| 41 | closeModal: function( e ) { |
| 42 | if ( 'undefined' !== typeof e ) { |
| 43 | e.preventDefault(); |
| 44 | |
| 45 | // For keyup events, only process esc |
| 46 | if ( 'keyup' === e.type && 27 !== e.which ) { |
| 47 | return; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | jQuery( '.widgetopts-modal-background' ).hide(); |
| 52 | jQuery( '.widgetopts-module-settings-container' ).hide(); |
| 53 | jQuery( 'body' ).removeClass( 'widgetopts-modal-open' ); |
| 54 | }, |
| 55 | |
| 56 | moduleToggle: function( e ) { |
| 57 | e.preventDefault(); |
| 58 | e.stopPropagation(); |
| 59 | |
| 60 | var $button = jQuery(this), |
| 61 | $card = $button.parents( '.widgetopts-module-card' ), |
| 62 | $buttons = $card.find( '.widgetopts-toggle-activation' ), |
| 63 | module = $card.attr( 'data-module-id' ); |
| 64 | |
| 65 | $buttons.prop( 'disabled', true ); |
| 66 | |
| 67 | if ( $button.html() == widgetopts.translation.activate ) { |
| 68 | var method = 'activate'; |
| 69 | } else { |
| 70 | var method = 'deactivate'; |
| 71 | } |
| 72 | |
| 73 | widgetoptsSettingsModule.ajaxRequest( module, method, {}, widgetoptsSettingsModule.moduleCallback ); |
| 74 | }, |
| 75 | |
| 76 | moduleCallback: function( results ) { |
| 77 | var module = results.module; |
| 78 | var method = results.method; |
| 79 | |
| 80 | var $card = jQuery( '#widgetopts-module-card-' + module ), |
| 81 | $buttons = $card.find( '.widgetopts-toggle-activation' ); |
| 82 | |
| 83 | if ( results.errors.length > 0 ) { |
| 84 | $buttons |
| 85 | .html( widgetopts.translations.error ) |
| 86 | .addClass( 'button-secondary' ) |
| 87 | .removeClass( 'button-primary' ); |
| 88 | |
| 89 | setTimeout( function() { |
| 90 | widgetoptsSettingsModule.isModuleActive( module ); |
| 91 | }, 1000 ); |
| 92 | |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if ( 'activate' === method ) { |
| 97 | $buttons |
| 98 | .html( widgetopts.translation.deactivate ) |
| 99 | .addClass( 'button-secondary' ) |
| 100 | .removeClass( 'button-primary' ) |
| 101 | .prop( 'disabled', false ); |
| 102 | |
| 103 | $card |
| 104 | .addClass( 'widgetopts-module-type-enabled' ) |
| 105 | .removeClass( 'widgetopts-module-type-disabled' ); |
| 106 | |
| 107 | var newToggleSettingsLabel = widgetopts.translation.show_settings; |
| 108 | } else { |
| 109 | $buttons |
| 110 | .html( widgetopts.translation.activate ) |
| 111 | .addClass( 'button-primary' ) |
| 112 | .removeClass( 'button-secondary' ) |
| 113 | .prop( 'disabled', false ); |
| 114 | |
| 115 | $card |
| 116 | .addClass( 'widgetopts-module-type-disabled' ) |
| 117 | .removeClass( 'widgetopts-module-type-enabled' ); |
| 118 | |
| 119 | var newToggleSettingsLabel = widgetopts.translation.show_description; |
| 120 | } |
| 121 | |
| 122 | // if( !$card.hasClass('widgetopts-module-card-no-settings') ){ |
| 123 | $card.find( '.widgetopts-toggle-settings' ).html( newToggleSettingsLabel ); |
| 124 | // } |
| 125 | }, |
| 126 | |
| 127 | saveSettings: function( e ) { |
| 128 | e.preventDefault(); |
| 129 | |
| 130 | var $button = jQuery(this); |
| 131 | |
| 132 | if ( $button.hasClass( 'widgetopts-module-settings-save' ) ) { |
| 133 | var module = $button.parents( '.widgetopts-module-card' ).attr( 'data-module-id' ); |
| 134 | } else { |
| 135 | var module = ''; |
| 136 | } |
| 137 | |
| 138 | $button.prop( 'disabled', true ); |
| 139 | |
| 140 | var data = { |
| 141 | '--widgetopts-form-serialized-data': jQuery( '#widgetopts-module-settings-form' ).serialize() |
| 142 | }; |
| 143 | |
| 144 | widgetoptsSettingsModule.ajaxRequest( module, 'save', data, widgetoptsSettingsModule.savingCallback ); |
| 145 | }, |
| 146 | |
| 147 | savingCallback: function( results ) { |
| 148 | if ( '' === results.module ) { |
| 149 | jQuery( '#widgetopts-save' ).prop( 'disabled', false ); |
| 150 | } else { |
| 151 | jQuery( '#widgetopts-module-card-' + results.module + ' button.widgetopts-module-settings-save' ).prop( 'disabled', false ); |
| 152 | } |
| 153 | |
| 154 | var $container = jQuery( '.widgetopts-module-cards-container' ); |
| 155 | var view = 'grid'; |
| 156 | |
| 157 | // console.log( results ); |
| 158 | widgetoptsSettingsModule.clearMessages(); |
| 159 | if ( results.errors.length > 0 || ! results.closeModal ) { |
| 160 | widgetoptsSettingsModule.showMessages( results.messages, results.module, 'open' ); |
| 161 | $container.find( '.widgetopts-module-settings-content-container:visible' ).animate( {'scrollTop': 0}, 'fast' ); |
| 162 | |
| 163 | } else { |
| 164 | widgetoptsSettingsModule.showMessages( results.messages, results.module, 'closed' ); |
| 165 | $container.find( '.widgetopts-module-settings-content-container:visible' ).scrollTop( 0 ); |
| 166 | widgetoptsSettingsModule.closeModal(); |
| 167 | } |
| 168 | }, |
| 169 | |
| 170 | clearMessages: function() { |
| 171 | jQuery( '#widgetopts-settings-messages-container, .widgetopts-module-messages-container' ).empty(); |
| 172 | }, |
| 173 | |
| 174 | showMessages: function( messages, module, containerStatus ) { |
| 175 | jQuery.each( messages, function( index, message ) { |
| 176 | widgetoptsSettingsModule.showMessage( message, module, containerStatus ); |
| 177 | } ); |
| 178 | }, |
| 179 | |
| 180 | showMessage: function( message, module, containerStatus ) { |
| 181 | var view = 'grid'; |
| 182 | |
| 183 | if ( 'closed' !== containerStatus && 'open' !== containerStatus ) { |
| 184 | containerStatus = 'closed'; |
| 185 | } |
| 186 | |
| 187 | if ( 'string' !== typeof module ) { |
| 188 | module = ''; |
| 189 | } |
| 190 | |
| 191 | if ( 'closed' === containerStatus || '' === module ) { |
| 192 | var container = jQuery( '#widgetopts-settings-messages-container' ); |
| 193 | |
| 194 | setTimeout( function() { |
| 195 | container.removeClass( 'visible' ); |
| 196 | setTimeout( function() { |
| 197 | container.find( 'div' ).remove(); |
| 198 | }, 500 ); |
| 199 | }, 4000 ); |
| 200 | } else { |
| 201 | var container = jQuery( '#widgetopts-module-card-' + module + ' .widgetopts-module-messages-container' ); |
| 202 | } |
| 203 | |
| 204 | container.append( '<div class="updated fade"><p><strong>' + message + '</strong></p></div>' ).addClass( 'visible' ); |
| 205 | }, |
| 206 | |
| 207 | |
| 208 | ajaxRequest: function( module, method, data, callback ) { |
| 209 | var postData = { |
| 210 | 'action': widgetopts.ajax_action, |
| 211 | 'nonce': widgetopts.ajax_nonce, |
| 212 | 'module': module, |
| 213 | 'method': method, |
| 214 | 'data': data, |
| 215 | }; |
| 216 | |
| 217 | jQuery.post( ajaxurl, postData ) |
| 218 | .always(function( a, status, b ) { |
| 219 | widgetoptsSettingsModule.processAjaxResponse( a, status, b, module, method, data, callback ); |
| 220 | }); |
| 221 | }, |
| 222 | |
| 223 | processAjaxResponse: function( a, status, b, module, method, data, callback ) { |
| 224 | var results = { |
| 225 | 'module': module, |
| 226 | 'method': method, |
| 227 | 'data': data, |
| 228 | 'status': status, |
| 229 | 'license_status': null, |
| 230 | 'jqxhr': null, |
| 231 | 'success': false, |
| 232 | 'response': null, |
| 233 | 'button': null, |
| 234 | 'errors': [], |
| 235 | 'messages': [], |
| 236 | 'functionCalls': [], |
| 237 | 'redirect': false, |
| 238 | 'closeModal': true |
| 239 | }; |
| 240 | // console.log( a ); |
| 241 | |
| 242 | a = jQuery.parseJSON( a ); |
| 243 | |
| 244 | if ( 'WIDGETOPTS_Response' === a.source && 'undefined' !== a.response ) { |
| 245 | // Successful response with a valid format. |
| 246 | results.jqxhr = b; |
| 247 | results.success = a.success; |
| 248 | results.response = a.response; |
| 249 | results.errors = a.errors; |
| 250 | results.messages = a.messages; |
| 251 | results.functionCalls = a.functionCalls; |
| 252 | results.redirect = a.redirect; |
| 253 | results.closeModal = a.closeModal; |
| 254 | results.button = a.button; |
| 255 | |
| 256 | if( typeof results.license_status != 'undefined' ){ |
| 257 | results.license_status = a.license_status; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if ( 'function' === typeof callback ) { |
| 262 | callback( results ); |
| 263 | } else if ( 'function' === typeof console.log ) { |
| 264 | console.log( 'ERROR: Unable to handle settings AJAX request due to an invalid callback:', callback, {'data': postData, 'results': results} ); |
| 265 | } |
| 266 | |
| 267 | }, |
| 268 | |
| 269 | toggleCustomClass: function(e){ |
| 270 | var getVal = jQuery('.opts-add-class-txtfld').val(); |
| 271 | var fname = 'extwopts_class_settings[classlists][]'; |
| 272 | if( jQuery(this).hasClass('widgetopts-add-class-btn') ){ |
| 273 | fname = 'classes[classlists][]'; |
| 274 | } |
| 275 | if( getVal.length > 0 ){ |
| 276 | jQuery('#opts-predefined-classes ul').append('<li><input type="hidden" name="'+ fname +'" value="'+ getVal +'" /><span class"opts-li-value">'+ getVal +'</span> <a href="#" class="opts-remove-class-btn"><span class="dashicons dashicons-dismiss"></span></a></li>'); |
| 277 | jQuery('.opts-add-class-txtfld').val(''); |
| 278 | } |
| 279 | |
| 280 | e.preventDefault(); |
| 281 | e.stopPropagation(); |
| 282 | }, |
| 283 | |
| 284 | removeCustomClass: function(e){ |
| 285 | jQuery(this).parent('li').fadeOut('fast',function(){ |
| 286 | jQuery(this).remove(); |
| 287 | }); |
| 288 | e.preventDefault(); |
| 289 | e.stopPropagation(); |
| 290 | }, |
| 291 | |
| 292 | clearWidgetCache: function( e ){ |
| 293 | var $button = jQuery(this); |
| 294 | $button.prop( 'disabled', true ); |
| 295 | |
| 296 | widgetoptsSettingsModule.ajaxRequest( 'clear_cache', 'clear_cache', '', widgetoptsSettingsModule.clearWidgetCacheCallback ); |
| 297 | return false; |
| 298 | }, |
| 299 | clearWidgetCacheCallback: function( results ){ |
| 300 | if( typeof results.response != 'undefined' ){ |
| 301 | jQuery( '.widgetopts-delete-cache' ).after( '<span class="dashicons dashicons-yes widgetopts-cache-dashicons"></span>' ); |
| 302 | jQuery( '.widgetopts-cache-dashicons' ).delay(2000).fadeOut(400); |
| 303 | jQuery( '.widgetopts-delete-cache' ).prop( 'disabled', false ); |
| 304 | } |
| 305 | }, |
| 306 | |
| 307 | deactivationHandler: function(e){ |
| 308 | e.preventDefault(); |
| 309 | |
| 310 | var fld; |
| 311 | var $button = jQuery(this); |
| 312 | $button.prop( 'disabled', true ); |
| 313 | |
| 314 | fld = jQuery( '#' + $button.attr('data-target') ); |
| 315 | if( fld.val() != '' ){ |
| 316 | var data = { |
| 317 | 'license-data': fld.val(), |
| 318 | 'license-action': 'deactivate', |
| 319 | 'shortname' : $button.attr('data-shortname'), |
| 320 | 'button': $button.attr('id') |
| 321 | }; |
| 322 | |
| 323 | widgetoptsSettingsModule.ajaxRequest( 'license_key', 'deactivate_license', data, widgetoptsSettingsModule.licenseDeactivationCallback ); |
| 324 | }else{ |
| 325 | fld.css({ 'border' : '1px solid red' }); |
| 326 | $button.prop( 'disabled', false ); |
| 327 | } |
| 328 | }, |
| 329 | |
| 330 | licenseDeactivationCallback: function( results ){ |
| 331 | // console.log( results ); widgetopts-license-extended-response |
| 332 | if( typeof results.response != 'undefined' && typeof results.messages != 'undefined' && typeof results.button != 'undefined' ){ |
| 333 | var $button = jQuery( '#' + results.button ); |
| 334 | |
| 335 | jQuery( '#' + $button.attr('data-target') ).before( '<span>' + results.messages[0] + '</span>' ); |
| 336 | if( results.success == 'deactivated' ){ |
| 337 | $button.parent('td').parent('tr').fadeOut(); |
| 338 | jQuery( '#' + $button.attr('data-target') ).val(''); |
| 339 | } |
| 340 | }else{ |
| 341 | // jQuery('.widgetopts-license-key').css({ 'border' : '1px solid red' }); |
| 342 | // jQuery('.widgetopts-license-status').fadeIn(); |
| 343 | } |
| 344 | |
| 345 | $button.prop( 'disabled', false ); |
| 346 | |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | jQuery(document).ready(function() { |
| 351 | widgetoptsSettingsModule.init(); |
| 352 | }); |
| 353 |