input.js 10.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
(function($){
	
	/*
	*  Gallery
	*
	*  static model for this field
	*
	*  @type	event
	*  @date	28/07/13
	*
	*/
	
	
	// reference
	var _media = acf.media;
	
	
	// field
	acf.fields.gallery = {
		
		$el : null,
		
		o : {},
		
		set : function( o ){
			
			// merge in new option
			$.extend( this, o );
			
			
			// find input
			this.$input = this.$el.children('input[type="hidden"]');
			
			
			// get options
			this.o = acf.helpers.get_atts( this.$el );
			
			
			// wp library query
			this.o.query = {};
			
			
			// library
			if( this.o.library == 'uploadedTo' )
			{
				this.o.query.uploadedTo = acf.o.post_id;
			}
			
			
			// view
			this.o.view = 'grid';
			if( this.$el.hasClass('view-list') )
			{
				this.o.view = 'list';
			}
			
			
			// return this for chaining
			return this;
			
		},
		init : function(){

			// is clone field?
			if( acf.helpers.is_clone_field(this.$input) )
			{
				return;
			}
			
			
			// update count
			this.render();
			
			
			// sortable
			this.$el.find('> .thumbnails > .inner').sortable({
				items					:	'> .thumbnail',
				forceHelperSize			:	true,
				forcePlaceholderSize	:	true,
				scroll					:	true,
				start					:	function (event, ui) {
				
					// alter width / height to allow for 2px border
					ui.placeholder.width( ui.placeholder.width() - 4 );
					ui.placeholder.height( ui.placeholder.height() - 4 );
					
	   			}
			});
					
		},
		view : function( view ){
			
			this.o.view = view;
			
			this.render();
			
		},
		render : function(){
			
			// vars
			var count	=	this.$el.find('.thumbnails .thumbnail').length,
				s		=	acf.l10n.gallery[ 'count_' + Math.min( count, 2) ].replace('%d', count),
				$span	=	this.$el.find('.toolbar .count');
			
			
			// update span text
			$span.html( s );
			
			
			// toolbar
			if( this.o.view == 'list' )
			{
				this.$el.addClass('view-list');
				this.$el.find('.toolbar .view-grid-li').removeClass('active');
				this.$el.find('.toolbar .view-list-li').addClass('active');
			}
			else
			{
				this.$el.removeClass('view-list');
				this.$el.find('.toolbar .view-grid-li').addClass('active');
				this.$el.find('.toolbar .view-list-li').removeClass('active');
			}
			
			
			
		},
		add : function( image ){
			
			// IMPORTANT: this may not be the field we think it is. 
			// Opening a popup will cause the acf/setup_fields action to run, and this may be updated with the fields found in the popup! 
			// Reset this using the _media.div
			
			
			// reset
			this.set({ $el : _media.div });
			
			
			// vars
			var tmpl = this.$el.find('.tmpl-thumbnail').html();
			
			
			// update tmpl
			$.each(image, function( k, v ){
				var regex = new RegExp('{' + k + '}', 'g');
				tmpl = tmpl.replace(regex, v);
			});
		
		
			// add div
		    this.$el.find('.thumbnails > .inner').append( tmpl );
			
			
			// update gallery count
			this.render();
			
				 	
		 	// validation
			this.$el.closest('.field').removeClass('error');
	
		},
		edit : function( id ){
			
			// set global var
			_media.div = this.$el;
			

			// clear the frame
			_media.clear_frame();
			
			
			// create the media frame
			_media.frame = wp.media({
				title		:	acf.l10n.gallery.edit,
				multiple	:	false,
				button		:	{ text : acf.l10n.gallery.update }
			});
			
			
			// open
			_media.frame.on('open',function() {
				
				// set to browse
				if( _media.frame.content._mode != 'browse' )
				{
					_media.frame.content.mode('browse');
				}
				
				
				// add class
				_media.frame.$el.closest('.media-modal').addClass('acf-media-modal acf-expanded');
			
				// set selection
				var selection	=	_media.frame.state().get('selection'),
					attachment	=	wp.media.attachment( id );
				
				
				attachment.fetch();
				selection.add( attachment );
				
				
				// change events for list view
				_media.frame.$el.on('change', '.setting input, .setting textarea', function(){
					
					// vars
					var $el = $(this),
						setting = $el.closest('.setting').attr('data-setting');
					
					
					// update galleries
					$('.acf-gallery .thumbnail[data-id="' + id + '"] .td-' + setting).html( $el.val() );
					
				});
							
			});
			
			
			// close
			_media.frame.on('close',function(){
			
				// remove class
				_media.frame.$el.closest('.media-modal').removeClass('acf-media-modal');
				
			});
			
							
			// Finally, open the modal
			_media.frame.open();
			
		},
		remove : function( id ){
			
			// reference
			var _this = this;
			
			
			// vars
			$thumb = this.$el.find('.thumbnails .thumbnail[data-id="' + id + '"]');
			
			
			// fade and remove
			$thumb.animate({ opacity : 0 }, 250, function(){
				
				$thumb.remove();
				
				_this.render();
				
			});
			
		},
		render_collection : function(){
			
			// Note: Need to find a differen 'on' event. Now that attachments load custom fields, this function can't rely on a timeout. Instead, hook into a render function foreach item
			
			// set timeout for 0, then it will always run last after the add event
			setTimeout(function(){
			
			
			// vars
			var $gallery	=	_media.div,
				$content	=	_media.frame.content.get().$el

				collection	=	_media.frame.content.get().collection || null;
				
			
			if( collection )
			{
				var i = -1;
				
				collection.each(function( item ){
					
					i++;
					
					var $li = $content.find('.attachments > .attachment:eq(' + i + ')');
					
					
					// if image is already inside the gallery, disable it!
					if( $gallery.find('.thumbnails .thumbnail[data-id="' + item.id + '"]').exists() )
					{
						item.off('selection:single');
						$li.addClass('acf-selected');
					}
					
				});
			}
			
			
			}, 0);
				
		},
		popup : function()
		{
			// reference
			var _this = this;
			
			
			// set global var
			_media.div = this.$el;
			

			// clear the frame
			_media.clear_frame();
			
			
			 // Create the media frame
			 _media.frame = wp.media({
				states : [
					new wp.media.controller.Library({
						library		:	wp.media.query( this.o.query ),
						multiple	:	true,
						title		:	acf.l10n.gallery.select,
						priority	:	20,
						filterable	:	'all'
					})
				]
			});
			
			
			// customize model / view
			_media.frame.on('content:activate', function(){
				
				// vars
				var toolbar = null,
					filters = null;
					
				
				// populate above vars making sure to allow for failure
				try
				{
					toolbar = _media.frame.content.get().toolbar;
					filters = toolbar.get('filters');
				} 
				catch(e)
				{
					// one of the objects was 'undefined'... perhaps the frame open is Upload Files
					//console.log( e );
				}
				
				
				// validate
				if( !filters )
				{
					return false;
				}
				
				
				// no need for 'uploaded' filter
				if( _this.o.library == 'uploadedTo' )
				{
					filters.$el.find('option[value="uploaded"]').remove();
					filters.$el.after('<span>' + acf.l10n.gallery.uploadedTo + '</span>')
					
					$.each( filters.filters, function( k, v ){
						
						v.props.uploadedTo = acf.o.post_id;
						
					});
				}
				
				
				// hide selected items from the library
				_this.render_collection();
				 
				_media.frame.content.get().collection.on( 'reset add', function(){
				    
					_this.render_collection();
				    
			    });
			    
								
			});
			
			
			// When an image is selected, run a callback.
			_media.frame.on( 'select', function() {
				
				// get selected images
				selection = _media.frame.state().get('selection');
				
				if( selection )
				{
					selection.each(function(attachment){
	
						
						// is image already in gallery?
						if( _this.$el.find('.thumbnails .thumbnail[data-id="' + attachment.id + '"]').exists() )
						{
							return;
						}
											
						
				    	// vars
				    	var image = {
					    	id			:	attachment.id,
					    	title		:	attachment.attributes.title,
					    	name		:	attachment.attributes.filename,
					    	url			:	attachment.attributes.url,
					    	caption 	:	attachment.attributes.caption,
						    alt			:	attachment.attributes.alt,
						    description	:	attachment.attributes.description
				    	};
				    	
				    	
				    	// file?
					    if( attachment.attributes.type != 'image' )
					    {
						    image.url = attachment.attributes.icon;
					    }
					    
					    
					    // is preview size available?
				    	if( attachment.attributes.sizes && attachment.attributes.sizes[ _this.o.preview_size ] )
				    	{
					    	image.url = attachment.attributes.sizes[ _this.o.preview_size ].url;
				    	}
					    
					    
				    	// add file to field
				        _this.add( image );
				        
						
				    });
				    // selection.each(function(attachment){
				}
				// if( selection )
				
				
			});
			// _media.frame.on( 'select', function() {
					 
			
			// Finally, open the modal
			_media.frame.open();
				
			
			return false;
		}
		
	};
	
	
	/*
	*  Events
	*
	*  jQuery events for this field
	*
	*  @type	function
	*  @date	1/03/2011
	*
	*  @param	N/A
	*  @return	N/A
	*/
	
	$(document).on('click', '.acf-gallery .acf-button-edit', function( e ){
		
		e.preventDefault();
		
		// vars
		var id = $(this).closest('.thumbnail').attr('data-id');
		
		acf.fields.gallery.set({ $el : $(this).closest('.acf-gallery') }).edit( id );
		
		$(this).blur();
			
	});
	
	$(document).on('click', '.acf-gallery .acf-button-delete', function( e ){
		
		e.preventDefault();
		
		// vars
		var id = $(this).closest('.thumbnail').attr('data-id');
		
		acf.fields.gallery.set({ $el : $(this).closest('.acf-gallery') }).remove( id );
		
		$(this).blur();
			
	});
	
	$(document).on('click', '.acf-gallery .add-image', function( e ){
		
		e.preventDefault();
		
		acf.fields.gallery.set({ $el : $(this).closest('.acf-gallery') }).popup();
		
		$(this).blur();
		
	});
	
	$(document).on('click', '.acf-gallery .view-grid', function( e ){
		
		e.preventDefault();
		
		acf.fields.gallery.set({ $el : $(this).closest('.acf-gallery') }).view( 'grid' );
		
		$(this).blur();
		
	});
	
	$(document).on('click', '.acf-gallery .view-list', function( e ){
		
		e.preventDefault();
		
		acf.fields.gallery.set({ $el : $(this).closest('.acf-gallery') }).view( 'list' );
		
		$(this).blur();
		
	});
	
	
	/*
	*  acf/setup_fields
	*
	*  run init function on all elements for this field
	*
	*  @type	event
	*  @date	20/07/13
	*
	*  @param	{object}	e		event object
	*  @param	{object}	el		DOM object which may contain new ACF elements
	*  @return	N/A
	*/
	
	$(document).on('acf/setup_fields', function(e, el){
		
		$(el).find('.acf-gallery').each(function(){
			
			acf.fields.gallery.set({ $el : $(this) }).init();
			
		});
		
	});
	
	

})(jQuery);