Current File : /home/virtualki/22346/oldwp/wp-content/themes/megiw/js/edit-realization.js
jQuery(document).ready(function($){
 	
 	var $ids = '';
 	
 	if($('#wi_realization_meta_gallery_list .attachment_id').size() > 0){
 		$('#wi_realization_meta_gallery_list .attachment_id').each(function(index){
 			if(index > 0){
 				$ids += ','; 
 			}
 			$ids += $(this).val();
 		});
 	}
 	
 	var shortcode_string = '[gallery ids="'+$ids+'"]';
 	var	shortcode = wp.shortcode.next( 'gallery', shortcode_string );
 	var attachments = wp.media.gallery.attachments( shortcode.shortcode );
    var selection = new wp.media.model.Selection( attachments.models, {
        props:    attachments.props.toJSON(),
        multiple: true
    }); 	
 	 	
 	var gallery_file_frame = wp.media({	
 			title: "Wstaw zdjęcia",
 			library: {
				type: 'image'
 			},
 			button: {            
				text: "Wstaw zdjęcia"
 			},         
 			multiple: true,
 			state: 'gallery-edit',
 			frame: 'post',
 			editing: true, 			
 			selection: selection
      	});
      	
   	$('#wi_realization_meta_gallery_add_file').click(function(){
 		gallery_file_frame.open();
 		return false;
	});
	
	var $gallery_list = $('#wi_realization_meta_gallery_list');
	var $gallery_list_item = $('<li><span class="icon"><img class="thumb" src="" alt="" /></span><span class="input"><input type="hidden" name="_wi_realization_meta_gallery[]" value="" class="attachment_id" /></span></li>');
	
	gallery_file_frame.on('update', function() {
		
		$gallery_list.find('li').remove();
		
		gallery_file_frame.states.get('gallery-edit').get('library').each(function(obj){
			var attachment = obj.toJSON();
			
			var $clone = $gallery_list_item.clone();
			$clone.find('.thumb').attr('src',attachment.sizes.thumbnail.url);
			$clone.find('.attachment_id').val(attachment.id);
			$clone.appendTo($gallery_list);
		});
		
	});
	
 	$gallery_list.on('click','.remove', function(){
 		$(this).closest('li').remove();
 	});
});