balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

main.js (9729B)


      1 jQuery( function ( $ ) {
      2 	'use strict';
      3 
      4 	/**
      5 	 * ---------------------------------------
      6 	 * ------------- Events ------------------
      7 	 * ---------------------------------------
      8 	 */
      9 
     10 	/**
     11 	 * No or Single predefined demo import button click.
     12 	 */
     13 	$( '.js-ocdi-import-data' ).on( 'click', function () {
     14 
     15 		// Reset response div content.
     16 		$( '.js-ocdi-ajax-response' ).empty();
     17 
     18 		// Prepare data for the AJAX call
     19 		var data = new FormData();
     20 		data.append( 'action', 'ocdi_import_demo_data' );
     21 		data.append( 'security', ocdi.ajax_nonce );
     22 		data.append( 'selected', $( '#ocdi__demo-import-files' ).val() );
     23 		if ( $('#ocdi__content-file-upload').length ) {
     24 			data.append( 'content_file', $('#ocdi__content-file-upload')[0].files[0] );
     25 		}
     26 		if ( $('#ocdi__widget-file-upload').length ) {
     27 			data.append( 'widget_file', $('#ocdi__widget-file-upload')[0].files[0] );
     28 		}
     29 		if ( $('#ocdi__customizer-file-upload').length ) {
     30 			data.append( 'customizer_file', $('#ocdi__customizer-file-upload')[0].files[0] );
     31 		}
     32 		if ( $('#ocdi__redux-file-upload').length ) {
     33 			data.append( 'redux_file', $('#ocdi__redux-file-upload')[0].files[0] );
     34 			data.append( 'redux_option_name', $('#ocdi__redux-option-name').val() );
     35 		}
     36 
     37 		// AJAX call to import everything (content, widgets, before/after setup)
     38 		ajaxCall( data );
     39 
     40 	});
     41 
     42 
     43 	/**
     44 	 * Grid Layout import button click.
     45 	 */
     46 	$( '.js-ocdi-gl-import-data' ).on( 'click', function () {
     47 		var selectedImportID = $( this ).val();
     48 		var $itemContainer   = $( this ).closest( '.js-ocdi-gl-item' );
     49 
     50 		// If the import confirmation is enabled, then do that, else import straight away.
     51 		if ( ocdi.import_popup ) {
     52 			displayConfirmationPopup( selectedImportID, $itemContainer );
     53 		}
     54 		else {
     55 			gridLayoutImport( selectedImportID, $itemContainer );
     56 		}
     57 	});
     58 
     59 
     60 	/**
     61 	 * Grid Layout categories navigation.
     62 	 */
     63 	(function () {
     64 		// Cache selector to all items
     65 		var $items = $( '.js-ocdi-gl-item-container' ).find( '.js-ocdi-gl-item' ),
     66 			fadeoutClass = 'ocdi-is-fadeout',
     67 			fadeinClass = 'ocdi-is-fadein',
     68 			animationDuration = 200;
     69 
     70 		// Hide all items.
     71 		var fadeOut = function () {
     72 			var dfd = jQuery.Deferred();
     73 
     74 			$items
     75 				.addClass( fadeoutClass );
     76 
     77 			setTimeout( function() {
     78 				$items
     79 					.removeClass( fadeoutClass )
     80 					.hide();
     81 
     82 				dfd.resolve();
     83 			}, animationDuration );
     84 
     85 			return dfd.promise();
     86 		};
     87 
     88 		var fadeIn = function ( category, dfd ) {
     89 			var filter = category ? '[data-categories*="' + category + '"]' : 'div';
     90 
     91 			if ( 'all' === category ) {
     92 				filter = 'div';
     93 			}
     94 
     95 			$items
     96 				.filter( filter )
     97 				.show()
     98 				.addClass( 'ocdi-is-fadein' );
     99 
    100 			setTimeout( function() {
    101 				$items
    102 					.removeClass( fadeinClass );
    103 
    104 				dfd.resolve();
    105 			}, animationDuration );
    106 		};
    107 
    108 		var animate = function ( category ) {
    109 			var dfd = jQuery.Deferred();
    110 
    111 			var promise = fadeOut();
    112 
    113 			promise.done( function () {
    114 				fadeIn( category, dfd );
    115 			} );
    116 
    117 			return dfd;
    118 		};
    119 
    120 		$( '.js-ocdi-nav-link' ).on( 'click', function( event ) {
    121 			event.preventDefault();
    122 
    123 			// Remove 'active' class from the previous nav list items.
    124 			$( this ).parent().siblings().removeClass( 'active' );
    125 
    126 			// Add the 'active' class to this nav list item.
    127 			$( this ).parent().addClass( 'active' );
    128 
    129 			var category = this.hash.slice(1);
    130 
    131 			// show/hide the right items, based on category selected
    132 			var $container = $( '.js-ocdi-gl-item-container' );
    133 			$container.css( 'min-width', $container.outerHeight() );
    134 
    135 			var promise = animate( category );
    136 
    137 			promise.done( function () {
    138 				$container.removeAttr( 'style' );
    139 			} );
    140 		} );
    141 	}());
    142 
    143 
    144 	/**
    145 	 * Grid Layout search functionality.
    146 	 */
    147 	$( '.js-ocdi-gl-search' ).on( 'keyup', function( event ) {
    148 		if ( 0 < $(this).val().length ) {
    149 			// Hide all items.
    150 			$( '.js-ocdi-gl-item-container' ).find( '.js-ocdi-gl-item' ).hide();
    151 
    152 			// Show just the ones that have a match on the import name.
    153 			$( '.js-ocdi-gl-item-container' ).find( '.js-ocdi-gl-item[data-name*="' + $(this).val().toLowerCase() + '"]' ).show();
    154 		}
    155 		else {
    156 			$( '.js-ocdi-gl-item-container' ).find( '.js-ocdi-gl-item' ).show();
    157 		}
    158 	} );
    159 
    160 	/**
    161 	 * ---------------------------------------
    162 	 * --------Helper functions --------------
    163 	 * ---------------------------------------
    164 	 */
    165 
    166 	/**
    167 	 * Prepare grid layout import data and execute the AJAX call.
    168 	 *
    169 	 * @param int selectedImportID The selected import ID.
    170 	 * @param obj $itemContainer The jQuery selected item container object.
    171 	 */
    172 	function gridLayoutImport( selectedImportID, $itemContainer ) {
    173 		// Reset response div content.
    174 		$( '.js-ocdi-ajax-response' ).empty();
    175 
    176 		// Hide all other import items.
    177 		$itemContainer.siblings( '.js-ocdi-gl-item' ).fadeOut( 500 );
    178 
    179 		$itemContainer.animate({
    180 			opacity: 0
    181 		}, 500, 'swing', function () {
    182 			$itemContainer.animate({
    183 				opacity: 1
    184 			}, 500 )
    185 		});
    186 
    187 		// Hide the header with category navigation and search box.
    188 		$itemContainer.closest( '.js-ocdi-gl' ).find( '.js-ocdi-gl-header' ).fadeOut( 500 );
    189 
    190 		// Append a title for the selected demo import.
    191 		$itemContainer.parent().prepend( '<h3>' + ocdi.texts.selected_import_title + '</h3>' );
    192 
    193 		// Remove the import button of the selected item.
    194 		$itemContainer.find( '.js-ocdi-gl-import-data' ).remove();
    195 
    196 		// Prepare data for the AJAX call
    197 		var data = new FormData();
    198 		data.append( 'action', 'ocdi_import_demo_data' );
    199 		data.append( 'security', ocdi.ajax_nonce );
    200 		data.append( 'selected', selectedImportID );
    201 
    202 		// AJAX call to import everything (content, widgets, before/after setup)
    203 		ajaxCall( data );
    204 	}
    205 
    206 	/**
    207 	 * Display the confirmation popup.
    208 	 *
    209 	 * @param int selectedImportID The selected import ID.
    210 	 * @param obj $itemContainer The jQuery selected item container object.
    211 	 */
    212 	function displayConfirmationPopup( selectedImportID, $itemContainer ) {
    213 		var $dialogContiner         = $( '#js-ocdi-modal-content' );
    214 		var currentFilePreviewImage = ocdi.import_files[ selectedImportID ]['import_preview_image_url'] || ocdi.theme_screenshot;
    215 		var previewImageContent     = '';
    216 		var importNotice            = ocdi.import_files[ selectedImportID ]['import_notice'] || '';
    217 		var importNoticeContent     = '';
    218 		var dialogOptions           = $.extend(
    219 			{
    220 				'dialogClass': 'wp-dialog',
    221 				'resizable':   false,
    222 				'height':      'auto',
    223 				'modal':       true
    224 			},
    225 			ocdi.dialog_options,
    226 			{
    227 				'buttons':
    228 				[
    229 					{
    230 						text: ocdi.texts.dialog_no,
    231 						click: function() {
    232 							$(this).dialog('close');
    233 						}
    234 					},
    235 					{
    236 						text: ocdi.texts.dialog_yes,
    237 						class: 'button  button-primary',
    238 						click: function() {
    239 							$(this).dialog('close');
    240 							gridLayoutImport( selectedImportID, $itemContainer );
    241 						}
    242 					}
    243 				]
    244 			});
    245 
    246 		if ( '' === currentFilePreviewImage ) {
    247 			previewImageContent = '<p>' + ocdi.texts.missing_preview_image + '</p>';
    248 		}
    249 		else {
    250 			previewImageContent = '<div class="ocdi__modal-image-container"><img src="' + currentFilePreviewImage + '" alt="' + ocdi.import_files[ selectedImportID ]['import_file_name'] + '"></div>'
    251 		}
    252 
    253 		// Prepare notice output.
    254 		if( '' !== importNotice ) {
    255 			importNoticeContent = '<div class="ocdi__modal-notice  ocdi__demo-import-notice">' + importNotice + '</div>';
    256 		}
    257 
    258 		// Populate the dialog content.
    259 		$dialogContiner.prop( 'title', ocdi.texts.dialog_title );
    260 		$dialogContiner.html(
    261 			'<p class="ocdi__modal-item-title">' + ocdi.import_files[ selectedImportID ]['import_file_name'] + '</p>' +
    262 			previewImageContent +
    263 			importNoticeContent
    264 		);
    265 
    266 		// Display the confirmation popup.
    267 		$dialogContiner.dialog( dialogOptions );
    268 	}
    269 
    270 	/**
    271 	 * The main AJAX call, which executes the import process.
    272 	 *
    273 	 * @param FormData data The data to be passed to the AJAX call.
    274 	 */
    275 	function ajaxCall( data ) {
    276 		$.ajax({
    277 			method:      'POST',
    278 			url:         ocdi.ajax_url,
    279 			data:        data,
    280 			contentType: false,
    281 			processData: false,
    282 			beforeSend:  function() {
    283 				$( '.js-ocdi-ajax-loader' ).show();
    284 			}
    285 		})
    286 		.done( function( response ) {
    287 			if ( 'undefined' !== typeof response.status && 'newAJAX' === response.status ) {
    288 				ajaxCall( data );
    289 			}
    290 			else if ( 'undefined' !== typeof response.status && 'customizerAJAX' === response.status ) {
    291 				// Fix for data.set and data.delete, which they are not supported in some browsers.
    292 				var newData = new FormData();
    293 				newData.append( 'action', 'ocdi_import_customizer_data' );
    294 				newData.append( 'security', ocdi.ajax_nonce );
    295 
    296 				// Set the wp_customize=on only if the plugin filter is set to true.
    297 				if ( true === ocdi.wp_customize_on ) {
    298 					newData.append( 'wp_customize', 'on' );
    299 				}
    300 
    301 				ajaxCall( newData );
    302 			}
    303 			else if ( 'undefined' !== typeof response.status && 'afterAllImportAJAX' === response.status ) {
    304 				// Fix for data.set and data.delete, which they are not supported in some browsers.
    305 				var newData = new FormData();
    306 				newData.append( 'action', 'ocdi_after_import_data' );
    307 				newData.append( 'security', ocdi.ajax_nonce );
    308 				ajaxCall( newData );
    309 			}
    310 			else if ( 'undefined' !== typeof response.message ) {
    311 				$( '.js-ocdi-ajax-response' ).append( '<p>' + response.message + '</p>' );
    312 				$( '.js-ocdi-ajax-loader' ).hide();
    313 
    314 				// Trigger custom event, when OCDI import is complete.
    315 				$( document ).trigger( 'ocdiImportComplete' );
    316 			}
    317 			else {
    318 				$( '.js-ocdi-ajax-response' ).append( '<div class="notice  notice-error  is-dismissible"><p>' + response + '</p></div>' );
    319 				$( '.js-ocdi-ajax-loader' ).hide();
    320 			}
    321 		})
    322 		.fail( function( error ) {
    323 			$( '.js-ocdi-ajax-response' ).append( '<div class="notice  notice-error  is-dismissible"><p>Error: ' + error.statusText + ' (' + error.status + ')' + '</p></div>' );
    324 			$( '.js-ocdi-ajax-loader' ).hide();
    325 		});
    326 	}
    327 } );