Type.js 11.9 KB
/**
 * @package	anc_image
 * @since	20140416 
 * @copyright (c) netz.coop eG
 * @author	netz.coop eG
 */
//$.noConflict();

	var Anc_Image_Type  = (function() {
		var pastDraggable = '';

		
		var	droppableFile = function(param_dropzone_element, param_dropzone_params, param_input_element, param_note_element, param_gallery_ajaxData, param_DragAndDrop) {
			/**
			 * fnc teil der die dateien aus dem dateiexplorer entgegen nimmt
			 */			
				if(!param_dropzone_params.url) {
					alert('keine url zum hochladen angegeben');
					exit;
				}
				
				if(!param_dropzone_params.maxFiles) {
					param_dropzone_params.maxFiles = 1;
				}
			
				param_dropzone_params.paramName = "file";
				
				param_dropzone_params.processing = function(file) { 
						jQuery(" button.button ").attr("disabled", "disabled");
						param_note_element.append('<div>Bild wird hochgeladen. Bitte warten!</div>');
						jQuery(" > .preview-database-image > img ", param_dropzone_element).remove();
						jQuery(" > img ", param_dropzone_element).remove();				
						this.removeAllFiles();
				};
				param_dropzone_params.init =  function() {
						this.on("complete", function (file) {
							if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
//								alert('file.width: '+file.width+' file.height: '+file.height);
							}
						 });					
						this.on("success", function(file,rep) {
							var reponse = jQuery.parseJSON( rep );
							if(param_input_element) {
								param_input_element.attr('value', reponse.id);
							}
							if(reponse.status === 'OK') {
								var noteclass = 'anc-success';
								var param_gallery_ajaxData_success = false;
								if(jQuery.isFunction(param_gallery_ajaxData.success)) {
									param_gallery_ajaxData_success =  param_gallery_ajaxData.success;
								}

								param_gallery_ajaxData.success = function(data, textStatus,  jqXhr) {
									jQuery(this).html(data);
									
									if(jQuery.isFunction(param_gallery_ajaxData_success)) {
										param_gallery_ajaxData_success(reponse);
									}
									
									Anc_Image_Type.customerGallerysimplePrettyPhoto();	
								};
								jQuery.ajax(param_gallery_ajaxData);
							} else {
								var noteclass = 'anc-error';
							}

							jQuery(" button.button ").attr("disabled", false);
							jQuery(" div ", param_note_element).remove();
							param_note_element.append('<div class="'+noteclass+'">'+reponse.message+'</div>');
						});
				};
				param_dropzone_element.dropzone(param_dropzone_params);		
			

			/**
			 * 
			 */
			if(param_DragAndDrop) {
				alert('param_DragAndDrop:'+param_DragAndDrop);
				var droppable_opt = {
					accept:".anc-image-draggable",
					tolerance: 'touch',

					drop: function(typ, ui) {
						var currentDraggable = jQuery(ui.draggable).attr('id');
						jQuery(this).append(jQuery(ui.draggable).clone());
						if(param_input_element) {
							param_input_element.attr('value', ui.draggable.attr('id'));
						}
						jQuery(" > div ", param_dropzone_element).remove();

						//If there is an object prior to the current one
						if (this.pastDraggable != "") {
							jQuery("#" + this.pastDraggable).remove();	//Place past object into its original coordinate
						}
						this.pastDraggable = currentDraggable;
					},
					activate: function(typ, ui) {},
					deactivate: function(typ, ui) {},						
					over: function(typ, ui) {},					
					out: function(typ, ui) {
						jQuery(ui.draggable).remove();		//	jQuery(this).droppable('option', 'accept', '.anc-image-draggable');
					},											
				}
				param_dropzone_element.droppable(droppable_opt);						
			}

		}		
		
		/**
		 * fnc löscht falls vorhanden dropzone elemente
		 * lädt bild in div.preview-database-image und setzt id ins input
		 * bereitet bild für crop vor (resizeImageForImageEditor)
		 * 
		 * @param int param_anc_image_id
		 * @param element element_input
		 * @param string param_anc_image_url
		 * @param element element_image_container
		 * @returns {undefined}
		 */
		var loadImageInto = function(param_anc_image_id, element_input, param_anc_image_url, element_image_container) {
			jQuery('img ', element_image_container).remove();
			jQuery('.dz-message ', element_image_container).remove();
			jQuery('.dz-image-preview ', element_image_container).remove();
			element_input.attr('value', param_anc_image_id);
			resizeImageForImageEditor(param_anc_image_url);
		}
		
		
		var customerGallerysimplePrettyPhoto = function() {
			jQuery("a[rel^='prettyPhoto']").prettyPhoto({});
			jQuery(".ancimage-showNcImageForm").append('<img class="zoom" src="/skin/frontend/base/default/images/anc/image/zoom.png" />');
			jQuery(".anc_image_wrapper > .ancimage-showNcImageForm").remove();
			var css_zoom = {
				'height': '20px',
				'margin-right': '-20px',
				'position': 'relative',
				'z-index': '4',
				'opacity':'1'
			};
			jQuery(".AncGalleryImages > .ancimage-showNcImageForm > .zoom").css(css_zoom);		
		}
		var placeholder_width;
		var placeholder_height;
		var ImageEditorFactor;
		var element_imageEditorArea;		
		var manipulateImageEditor = function() {
			element_imageEditorArea = jQuery("#ancimage-imageeditorarea-id");
			jQuery(document).ready(function($) {
				var imageEditorArea_width = element_imageEditorArea.width();
				var imageEditorArea_height = element_imageEditorArea.height();
				
				placeholder_width = jQuery('> .dropzone ',element_imageEditorArea).data('pxwidth');
				placeholder_height = jQuery('> .dropzone ',element_imageEditorArea).data('pxheight');
				
				var maxfilesizemb = jQuery('> .dropzone ',element_imageEditorArea).data('maxfilesizemb');
				jQuery('.ancimage-info > .editor').html('Sie haben einen Bildplatzhalterraum von '+placeholder_width+'px mal '+placeholder_height+'px zu Verf&uuml;gung! Ihr Bild darf nicht mehr als '+maxfilesizemb+' mb haben.');				
				
				var factor = imageEditorArea_width / placeholder_width;
				ImageEditorFactor = factor;
				console.log('factor: '+factor);
				
				var css_new_size = {
					'width': imageEditorArea_width,
					'height': placeholder_height * factor,
				}

				jQuery('> .dropzone ',element_imageEditorArea).css(css_new_size);

				resizeImageForImageEditor(
					jQuery('> .dropzone > .preview-database-image  ',element_imageEditorArea).data('imagesrc'), 
					jQuery('> .dropzone > .preview-database-image  ',element_imageEditorArea).data()
				);
			});

		}
		var Croppic;
		/**
		 * bereitet bild für crop bearbgeitung vor
		 * 
		 * benötigt object variable @var Anc_Image_Type.ImageEditorFactor und @var Anc_Image_Type.element_imageEditorArea, 
		 *	diese werden durch @see Anc_Image_Type.manipulateImageEditor() initialisiert
		 * 
		 * @param string param_imagesrc
		 * @returns {undefined}
		 */
		var resizeImageForImageEditor = function(param_imagesrc, param_cropdata) {
			var newImg2 = new Image();
			newImg2.src = param_imagesrc;
			newImg2.onload = function() {
				
				var warning = '';
				if(placeholder_width > newImg2.width || placeholder_height > newImg2.height ) {
					warning = '<div class="anc-error">Das Bild ist zu klein, bitte nutzen Sie ein Bild mit einer größeren Auflösung!</div>';
					alert('Das Bild ist zu klein, bitte nutzen Sie ein Bild mit einer größeren Auflösung!');
				}
				
				var new_img_width = newImg2.width * ImageEditorFactor;
				var new_img_height = newImg2.height * ImageEditorFactor;
				jQuery('.ancimage-info > .image').html('Ihr Bild ist '+newImg2.width+'px mal '+newImg2.height+'px gro&szlig;!' + warning);				
				if(warning) {
					return;
				}
				var cropperOptions = {
					imageUrl: param_imagesrc,
					imageHeight: Math.round(new_img_height),
					imageWidth: Math.round(new_img_width),					
					onBeforeImgUpload: 	function(){ console.log('onBeforeImgUpload') },
					onAfterImgUpload: 	function(){ console.log('onAfterImgUpload') },
					onImgDrag:		function(){ console.log('onImgDrag') },
					onImgZoom:		function(){ console.log('onImgZoom') },
					onBeforeImgCrop: 	function(){ console.log('onBeforeImgCrop') },
					onAfterImgCrop:		function(){ console.log('onAfterImgCrop') },					
				}
				jQuery(' > .dropzone > div', element_imageEditorArea).remove();			
				Croppic = new Anc_Lib_Croppic(jQuery(' > .dropzone', element_imageEditorArea), cropperOptions);	
				if (typeof param_cropdata !== 'undefined') {
					var cropdata = {
						'cropH' :	Math.round(param_cropdata.croph*ImageEditorFactor),
						'cropW' :	Math.round(param_cropdata.cropw*ImageEditorFactor),
						'imgH' :	Math.round(param_cropdata.imgh*ImageEditorFactor),
						'imgW' :	Math.round(param_cropdata.imgw*ImageEditorFactor),
						'imgInitH' :Math.round(param_cropdata.imginith*ImageEditorFactor),
						'imgInitW' :Math.round(param_cropdata.imginitw*ImageEditorFactor),
						'imgX1' :	Math.round(param_cropdata.imgx1*ImageEditorFactor),
						'imgY1' :	Math.round(param_cropdata.imgy1*ImageEditorFactor),						
					}
					Croppic.initialWithValues(cropdata);
				}
				if(false) {
	//				@ControlRotate
					var rotate = 90;
					jQuery('div.cropControls').append('<i class="ControlRotate">X</i>');
					jQuery('.ControlRotate').unbind("click").click(function(e){
						e.preventDefault(); 		
						var css_rotate = {
							'position': 'relative',
							'transform': 'rotate('+rotate+'deg)',
							'transform-origin': '50% 50% 0px',
						}


						jQuery('.ancimage-dropzone > .cropImgWrapper > img').css(css_rotate);
						jQuery('.ancimage-dropzone > img').css(css_rotate);

						var position = jQuery('.ancimage-dropzone > img').position();

						var cropdata = {
							'cropH' :	Croppic.cropW,
							'cropW' :	Croppic.cropH,
							'imgH' :	Croppic.imgW,
							'imgW' :	Croppic.imgH,
							'imgInitH' :Croppic.imgInitW,
							'imgInitW' :Croppic.imgInitH,
							'imgX1' :	Croppic.imgX1,
							'imgY1' :	Croppic.imgY1,						
						}
						Croppic.cropH	=	cropdata.cropH;
						Croppic.cropW	=	cropdata.cropW;
						Croppic.imgH	=	cropdata.imgH;
						Croppic.imgW	=	cropdata.imgW;
						Croppic.imgInitH	=	cropdata.imgInitH;
						Croppic.imgInitW	=	cropdata.imgInitW;
						Croppic.imgX1	=	cropdata.imgX1;
						Croppic.imgY1	=	cropdata.imgY1;

						console.log('cropH',Croppic.cropH);
						console.log('cropW',Croppic.cropW);
						console.log('imgH',Croppic.imgH);
						console.log('imgW',Croppic.imgW);
						console.log('imgInitH',Croppic.imgInitH);
						console.log('imgInitW',Croppic.imgInitW);
						console.log('imgX1',Croppic.imgX1);
						console.log('imgY1',Croppic.imgY1);
	//					Croppic.initialWithValues(cropdata);					

						if(rotate ===270) {
							rotate = 0;
						} else {
							rotate = rotate+90;
						}					

					});	
				}
			};
		}

		var crop = function() {
			cropData = Croppic.crop();
			jQuery('#product_addtocart_form * .ancSPImage_KEY_cropH	> .input-box > input').attr('value', Math.round(cropData.cropH / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_cropW	> .input-box > input').attr('value', Math.round(cropData.cropW / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_imgH	> .input-box > input').attr('value', Math.round(cropData.imgH / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_imgInitH > .input-box > input').attr('value', Math.round(cropData.imgInitH / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_imgInitW > .input-box > input').attr('value', Math.round(cropData.imgInitW / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_imgW	> .input-box > input').attr('value', Math.round(cropData.imgW / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_imgX1 > .input-box > input').attr('value', Math.round(cropData.imgX1 / ImageEditorFactor));
			jQuery('#product_addtocart_form * .ancSPImage_KEY_imgY1 > .input-box > input').attr('value', Math.round(cropData.imgY1	/ ImageEditorFactor));			
		}
		
		
	return {
		manipulateImageEditor:manipulateImageEditor,
		droppableFile:droppableFile,
		loadImageInto:loadImageInto,
		customerGallerysimplePrettyPhoto:customerGallerysimplePrettyPhoto,
		crop:crop
	}	
})();