Current File : /home/virtualki/22346/oldwp/wp-content/themes/megiw/js/edit-product.js
jQuery(document).ready(function($){
    $('.wi_product_color_field').wpColorPicker({
    	palettes: ['#d9a218','#d98818','#d1670a','#fae158','#c1cf71','#bd3e3e','#2e556e','#927aa6']
    });

 	var $upload_list = $('#wi_product_meta_download_list');
 	var $upload_list_item = $('<li><span class="label"></span><span class="input"><input type="hidden" name="_wi_product_meta_download[]" value="" class="attachment_id" /><input type="button" class="button" value="Usuń" title="Usuń" /></span></li>');
 	var download_file_frame = wp.media({	
 			title: "Wstaw pliki",
 			button: {            
				text: "Wstaw pliki"
 			},         
 			multiple: true
      	});
 	
 	$('#wi_product_meta_download_add_file').click(function(){
 		download_file_frame.open();
 		return false;
 	});
 	
 	download_file_frame.on('select', function() {
 		
 		download_file_frame.state().get('selection').each(function(obj){
 			var attachment = obj.toJSON();
 			var $clone = $upload_list_item.clone(true);
			var title = (attachment.title == '')?attachment.filename:attachment.title;
			$clone.find('.label').text(title);
			$clone.find('.attachment_id').val(attachment.id);
			$clone.appendTo($upload_list);
 		});
 		 		
 	});	
 	
 	$upload_list.on('click','li', function(){
 		$(this).closest('li').remove();
 	});
 	
 	$('#wi_product_meta_download_remove_all').click(function(){
 		$upload_list.find('li').remove();
 	});
	
	var $addon_list = $('#wi_product_meta_addon_list');
	var $addon_list_item = $('<li><span class="label"></span><span class="input"><textarea class="addon_description" name="_wi_product_meta_addon_description[]"></textarea><input type="hidden" class="addon_name" name="_wi_product_meta_addon_name[]" /><input type="hidden" class="addon_price" name="_wi_product_meta_addon_price[]" /><input type="button" class="button remove" value="Usuń" title="Usuń" /><input type="button" class="button edit" value="Edytuj" title="Edytuj" /></span></li>');
	
 	$('#wi_product_addon_remove_all').click(function(){
 		$addon_list.find('li').remove();
 	});
 	
 	$addon_list.on('click','.remove', function(){
 		$(this).closest('li').remove();
 	});
 	
 	var $toReplace = null;
 	
 	$addon_list.on('click','.edit', function(){
		var editor = tinyMCE.get('wi_product_addon_description');
		$toReplace = $(this).closest('li');		
		
		editor.setContent($toReplace.find('.addon_description').val());
		$('#wi_product_addon_name').val($toReplace.find('.addon_name').val());
		$('#wi_product_addon_price').val($toReplace.find('.addon_price').val());		
 	});
	
 	$('#wi_product_addon_save').click(function(){
 		var editor = tinyMCE.get('wi_product_addon_description');
		
		if(editor){
			content = editor.getContent();
		}else{
			content = $('#wi_product_addon_description').val();
		}
		
		var $clone = $addon_list_item.clone(true);
		var title = $('#wi_product_addon_name').val();
		var price = $('#wi_product_addon_price').val();
		
		$clone.find('.label').text(title);
		$clone.find('.addon_name').val(title);
		$clone.find('.addon_price').val(price);
		$clone.find('.addon_description').val(content);
		
		if($toReplace == null){
			$clone.appendTo($addon_list);	
		}else if($toReplace.size() > 0){
			$toReplace.replaceWith($clone);
			$toReplace = null;
		}
		
		$('#wi_product_addon_name').val('');
		$('#wi_product_addon_price').val('');
		editor.setContent('');
 	});
 	
 	var $ids = '';
 	
 	if($('#wi_product_meta_gallery_list .attachment_id').size() > 0){
 		$('#wi_product_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_product_meta_gallery_add_file').click(function(){
 		gallery_file_frame.open();
 		return false;
	});
	
	var $gallery_list = $('#wi_product_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_product_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();
 	});
});