balmet.com

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

media.js (6651B)


      1 /*global redux_change, wp, redux */
      2 
      3 /**
      4  * Media Uploader
      5  * Dependencies        : jquery, wp media uploader
      6  * Feature added by    : Smartik - http://smartik.ws/
      7  * Date                : 05.28.2013
      8  *
      9  * Modified by         : Kevin Provance (kprovance/svl-studios)
     10  * Date                : 07.07.2021
     11  */
     12 
     13 ( function( $ ) {
     14 	'use strict';
     15 
     16 	var isFiltered;
     17 
     18 	redux.field_objects       = redux.field_objects || {};
     19 	redux.field_objects.media = redux.field_objects.media || {};
     20 
     21 	redux.field_objects.media.init = function( selector ) {
     22 		if ( ! selector ) {
     23 			selector = $( document ).find( '.redux-group-tab:visible' ).find( '.redux-container-media:visible' );
     24 		}
     25 
     26 		$( selector ).each(
     27 			function() {
     28 				var el     = $( this );
     29 				var parent = el;
     30 
     31 				if ( ! el.hasClass( 'redux-field-container' ) ) {
     32 					parent = el.parents( '.redux-field-container:first' );
     33 				}
     34 
     35 				if ( parent.is( ':hidden' ) ) {
     36 					return;
     37 				}
     38 
     39 				if ( parent.hasClass( 'redux-field-init' ) ) {
     40 					parent.removeClass( 'redux-field-init' );
     41 				} else {
     42 					return;
     43 				}
     44 
     45 				if ( undefined === redux.field_objects.pro && undefined !== redux.field_objects.image_filters ) {
     46 					redux.field_objects.image_filters.sliderInit( el, 'media' );
     47 					redux.field_objects.image_filters.checkbox( el, 'media' );
     48 				}
     49 
     50 				isFiltered = false;
     51 
     52 				// Remove the image button.
     53 				el.find( '.remove-image, .remove-file' ).off( 'click' ).on(
     54 					'click',
     55 					function() {
     56 						redux.field_objects.media.removeFile( $( this ).parents( 'fieldset.redux-field:first' ) );
     57 					}
     58 				);
     59 
     60 				// Upload media button.
     61 				el.find( '.media_upload_button' ).off().on(
     62 					'click',
     63 					function( event ) {
     64 						redux.field_objects.media.addFile( event, $( this ).parents( 'fieldset.redux-field:first' ) );
     65 					}
     66 				);
     67 			}
     68 		);
     69 	};
     70 
     71 	// Add a file via the wp.media function.
     72 	redux.field_objects.media.addFile = function( event, selector ) {
     73 		var frame;
     74 		var libFilter;
     75 		var filter;
     76 		var data;
     77 		var thumbSrc;
     78 		var height;
     79 		var key;
     80 		var object;
     81 
     82 		var jQueryel = $( this );
     83 
     84 		event.preventDefault();
     85 
     86 		// If the media frame already exists, reopen it.
     87 		if ( frame ) {
     88 			frame.open();
     89 			return;
     90 		}
     91 
     92 		// Get library filter data.
     93 		filter = $( selector ).find( '.library-filter' ).data( 'lib-filter' );
     94 
     95 		// Must exist to do decoding.
     96 		if ( undefined !== filter ) {
     97 			if ( '' !== filter ) {
     98 				libFilter  = [];
     99 				isFiltered = true;
    100 				filter     = decodeURIComponent( filter );
    101 				filter     = JSON.parse( filter );
    102 
    103 				$.each(
    104 					filter,
    105 					function( index, value ) {
    106 						index = null;
    107 						libFilter.push( value );
    108 					}
    109 				);
    110 			}
    111 		}
    112 
    113 		// Create the media frame.
    114 		frame = wp.media(
    115 			{ multiple: false,
    116 				library: { type: libFilter },  // Only allow images.
    117 
    118 				// Set the title of the modal.
    119 				title: jQueryel.data( 'choose' ),
    120 
    121 				// Customize the submit button.
    122 				button: {
    123 
    124 					// Set the text of the button.
    125 					text: jQueryel.data( 'update' )
    126 
    127 					// Tell the button not to close the modal, since we're
    128 					// going to refresh the page when the image is selected.
    129 				}
    130 			}
    131 		);
    132 
    133 		// When an image is selected, run a callback.
    134 		frame.on(
    135 			'select',
    136 			function() {
    137 
    138 				// Grab the selected attachment.
    139 				var attachment = frame.state().get( 'selection' ).first();
    140 				frame.close();
    141 
    142 				data = $( selector ).find( '.data' ).data();
    143 
    144 				if ( 'undefined' === typeof redux.field_objects.media || undefined === typeof redux.field_objects.media ) {
    145 					redux.field_objects.media = {};
    146 				}
    147 
    148 				if ( undefined === data || 'undefined' === data.mode ) {
    149 					data      = {};
    150 					data.mode = 'image';
    151 				}
    152 
    153 				if ( true === isFiltered ) {
    154 					data.mode = 0;
    155 				}
    156 
    157 				if ( 0 === data.mode ) {
    158 
    159 				} else {
    160 					if ( false !== data.mode ) {
    161 						if ( attachment.attributes.type !== data.mode ) {
    162 							if ( attachment.attributes.subtype !== data.mode ) {
    163 								return;
    164 							}
    165 						}
    166 					}
    167 				}
    168 
    169 				selector.find( '.upload' ).val( attachment.attributes.url );
    170 				selector.find( '.upload-id' ).val( attachment.attributes.id );
    171 				selector.find( '.upload-height' ).val( attachment.attributes.height );
    172 				selector.find( '.upload-width' ).val( attachment.attributes.width );
    173 
    174 				redux_change( $( selector ).find( '.upload-id' ) );
    175 
    176 				thumbSrc = attachment.attributes.url;
    177 
    178 				if ( 'undefined' !== typeof attachment.attributes.sizes && 'undefined' !== typeof attachment.attributes.sizes.thumbnail ) {
    179 					if ( 'thumbnail' === data.previewSize ) {
    180 						thumbSrc = attachment.attributes.sizes.thumbnail.url;
    181 					}
    182 				} else if ( 'undefined' !== typeof attachment.attributes.sizes ) {
    183 					height = attachment.attributes.height;
    184 
    185 					for ( key in attachment.attributes.sizes ) {
    186 						if ( attachment.attributes.sizes.hasOwnProperty( key ) ) {
    187 							object = attachment.attributes.sizes[ key ];
    188 
    189 							if ( object.height < height ) {
    190 								height   = object.height;
    191 								thumbSrc = object.url;
    192 							}
    193 						}
    194 					}
    195 				} else {
    196 					thumbSrc = attachment.attributes.icon;
    197 				}
    198 
    199 				selector.find( '.upload-thumbnail' ).val( thumbSrc );
    200 				if ( ! selector.find( '.upload' ).hasClass( 'noPreview' ) ) {
    201 					selector.find( '.screenshot' ).empty().hide().append( '<img class="redux-option-image" src="' + thumbSrc + '">' ).slideDown( 'fast' );
    202 				}
    203 
    204 				selector.find( '.remove-image' ).removeClass( 'hide' ); // Show "Remove" button.
    205 				selector.find( '.redux-background-properties' ).slideDown();
    206 			}
    207 		);
    208 
    209 		// Finally, open the modal.
    210 		frame.open();
    211 	};
    212 
    213 	// Function to remove the image on click. Still requires a save.
    214 	redux.field_objects.media.removeFile = function( selector ) {
    215 		var screenshot;
    216 
    217 		// This shouldn't have been run...
    218 		if ( ! selector.find( '.remove-image' ).addClass( 'hide' ) ) {
    219 			return;
    220 		}
    221 
    222 		selector.find( '.remove-image' ).addClass( 'hide' ); // Hide "Remove" button.
    223 		selector.find( '.upload' ).val( '' );
    224 		selector.find( '.upload-id' ).val( '' );
    225 		selector.find( '.upload-height' ).val( '' );
    226 		selector.find( '.upload-width' ).val( '' );
    227 		selector.find( '.upload-thumbnail' ).val( '' );
    228 		redux_change( $( selector ).find( '.upload-id' ) );
    229 		selector.find( '.redux-background-properties' ).hide();
    230 
    231 		screenshot = selector.find( '.screenshot' );
    232 
    233 		// Hide the screenshot.
    234 		screenshot.slideUp();
    235 
    236 		selector.find( '.remove-file' ).off();
    237 
    238 		// We don't display the upload button if .upload-notice is present.
    239 		// This means the user doesn't have the WordPress 3.5 Media Library Support.
    240 		if ( selector.find( '.section-upload .upload-notice' ).length > 0 ) {
    241 			selector.find( '.media_upload_button' ).remove();
    242 		}
    243 	};
    244 } )( jQuery );