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

	var Anc_Text_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-text > 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_Text_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-text-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-text-draggable');
					},											
				}
				param_dropzone_element.droppable(droppable_opt);						
			}

		}		
		
		var draggableFile = function() {
			var draggable_opt = {
				revert: "invalid",							// im zweifel gehts wieder zurück zum ursprung
				cursor: "move",								// mauszeiger .... 
				containment: "#ancsubbundleproduct",		// element darf nur innerhalb von containment bewegt werden
				opacity: .5,
				snap:true,									// einrasten
				snapMode:"inner",
				grid: [10,10],
				stack: '.anc-text-draggable',	//	letzter drop  liegt vorne
				helper: 'clone',							// nicht moven sondern kopieren
				//	snapTolerance:50,
			}
			jQuery(' .anc-text-draggable').draggable(draggable_opt);		
		}		
		/**
		 * fnc löscht falls vorhanden dropzone elemente
		 * lädt bild in div.preview-database-text und setzt id ins input
		 * bereitet bild für crop vor (resizeTextForTextEditor)
		 * 
		 * @param int param_anc_text_id
		 * @param element element_input
		 * @param string param_anc_text_url
		 * @param element element_text_container
		 * @returns {undefined}
		 */
		var loadTextInto = function(
				param_anc_text_id, 
				param_anc_text_url,
				param_anc_admin_id,
				param_ncalbum_id,
				param_anc_customer_id,
				param_used_byitem_id,
				param_ordered ,
				param_anc_text_content, 
				
//				element_text_container, 
				element_input,
				element_input_text_id,
				element_input_admin_id,
				element_input_album_id,
				element_input_customer_id,
				element_input_used_byitem_id,
				element_input_ordered, 
				element_input_dropzone) {
			console.log('loadtextinto');			
			//			
//			console.log('loadTextInto'+'param_anc_text_id'+param_anc_text_id+' element_input '+element_input + ' param_anc_text_url: '+param_anc_text_url+', element_text_container: '+element_text_container) ;
//			jQuery('textarea', element_text_container).remove();
//			console.log(element_input);
//			console.log(element_text_container);
//			console.log('param_anc_admin_id'+param_anc_admin_id);
//			console.log('param_anc_customer_id'+param_anc_customer_id);
//			console.log(param_anc_text_content);
//			jQuery('.dz-message ', element_text_container).remove();
//			jQuery('.dz-text-preview ', element_text_container).remove();
			element_input.attr('value', param_anc_text_id);
			element_input_text_id.attr('value', param_anc_text_id);
			element_input_admin_id.attr('value', param_anc_admin_id);
			element_input_customer_id.attr('value',param_anc_customer_id);
			element_input_album_id.attr('value',param_ncalbum_id);
//			jQuery('#anctext-texteditorarea-id').html(param_anc_text_content);
			tinyMCE.activeEditor.setContent(param_anc_text_content);
			
//			resizeTextForTextEditor(param_anc_text_url);
		}
		
		
		var customerGallerysimplePrettyPhoto = function() {
			jQuery("a[rel^='prettyPhoto']").prettyPhoto({});
//			jQuery(".anctext-showNcTextForm").append('<img class="zoom" src="/skin/frontend/base/default/texts/anc/text/zoom.png" />');
			jQuery(".anc_text_wrapper > .anctext-showNcTextForm").remove();
			var css_zoom = {
				'height': '20px',
				'margin-right': '-20px',
				'position': 'relative',
				'z-index': '500',
				'opacity':'1'
			};
			jQuery(".AncGalleryTexts > .anctext-showNcTextForm > .zoom").css(css_zoom);		
		}
		var placeholder_width;
		var placeholder_height;
		var TextEditorFactor;
		var element_textEditorArea;		
		var manipulateTextEditor = function() {
			console.log('manipulateTextEditor');
			element_textEditorArea = jQuery("#anctext-texteditorarea-id");
			jQuery(document).ready(function($) {
				var textEditorArea_width = element_textEditorArea.width();
				var textEditorArea_height = element_textEditorArea.height();
				
				placeholder_width = jQuery('> .dropzone ',element_textEditorArea).data('pxwidth');
				placeholder_height = jQuery('> .dropzone ',element_textEditorArea).data('pxheight');
				
				var maxfilesizemb = jQuery('> .dropzone ',element_textEditorArea).data('maxfilesizemb');
				jQuery('.anctext-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 = textEditorArea_width / placeholder_width;
				TextEditorFactor = factor;
				console.log('factor: '+factor);
				
				var css_new_size = {
					'width': textEditorArea_width,
					'height': placeholder_height * factor,
				}

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

				resizeTextForTextEditor(
					jQuery('> .dropzone > .preview-database-text  ',element_textEditorArea).data('textsrc'), 
					jQuery('> .dropzone > .preview-database-text  ',element_textEditorArea).data()
				);
			});

		}
		var Croppic;
		/**
		 * bereitet bild für crop bearbgeitung vor
		 * 
		 * benötigt object variable @var Anc_Text_Type.TextEditorFactor und @var Anc_Text_Type.element_textEditorArea, 
		 *	diese werden durch @see Anc_Text_Type.manipulateTextEditor() initialisiert
		 * 
		 * @param string param_textsrc
		 * @returns {undefined}
		 */
		var resizeTextForTextEditor = function(param_textsrc, param_cropdata) {
			var newImg2 = new Text();
			newImg2.src = param_textsrc;
			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>';
				}
				
				var new_img_width = newImg2.width * TextEditorFactor;
				var new_img_height = newImg2.height * TextEditorFactor;
				jQuery('.anctext-info > .text').html('Ihr Bild ist '+newImg2.width+'px mal '+newImg2.height+'px gro&szlig;!' + warning);				
				var cropperOptions = {
					textUrl: param_textsrc,
					textHeight: Math.round(new_img_height),
					textWidth: 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_textEditorArea).remove();			
				Croppic = new Anc_Lib_Croppic(jQuery(' > .dropzone', element_textEditorArea), cropperOptions);	
				if (typeof param_cropdata !== 'undefined') {
					var cropdata = {
						'cropH' :	Math.round(param_cropdata.croph*TextEditorFactor),
						'cropW' :	Math.round(param_cropdata.cropw*TextEditorFactor),
						'imgH' :	Math.round(param_cropdata.imgh*TextEditorFactor),
						'imgW' :	Math.round(param_cropdata.imgw*TextEditorFactor),
						'imgInitH' :Math.round(param_cropdata.imginith*TextEditorFactor),
						'imgInitW' :Math.round(param_cropdata.imginitw*TextEditorFactor),
						'imgX1' :	Math.round(param_cropdata.imgx1*TextEditorFactor),
						'imgY1' :	Math.round(param_cropdata.imgy1*TextEditorFactor),						
					}
					Croppic.initialWithValues(cropdata);
				}
			};
		}

		var crop = function() {
			cropData = Croppic.crop();
			jQuery('#product_addtocart_form * .ancSPText_KEY_cropH	> .input-box > input').attr('value', Math.round(cropData.cropH / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_cropW	> .input-box > input').attr('value', Math.round(cropData.cropW / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_imgH	> .input-box > input').attr('value', Math.round(cropData.imgH / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_imgInitH > .input-box > input').attr('value', Math.round(cropData.imgInitH / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_imgInitW > .input-box > input').attr('value', Math.round(cropData.imgInitW / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_imgW	> .input-box > input').attr('value', Math.round(cropData.imgW / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_imgX1 > .input-box > input').attr('value', Math.round(cropData.imgX1 / TextEditorFactor));
			jQuery('#product_addtocart_form * .ancSPText_KEY_imgY1 > .input-box > input').attr('value', Math.round(cropData.imgY1	/ TextEditorFactor));			
		}
		
		
	return {
		manipulateTextEditor:manipulateTextEditor,
		draggableFile:draggableFile,
		droppableFile:droppableFile,
		loadTextInto:loadTextInto,
		customerGallerysimplePrettyPhoto:customerGallerysimplePrettyPhoto,
		crop:crop
	}	
})();