Your IP : 216.73.216.147


Current Path : /home/balossw/www/temp/common/cms/media/js/
Upload File :
Current File : /home/balossw/www/temp/common/cms/media/js/module.inc.js

/*---------------------------------------------------------------------------
		File name ............ content/js/module.inc.js
		Project .............. Gestion
		Author ............... Antoine Lemoine
		Creation date ........ 18.01.2008
		Modification date .... 02.10.2008
---------------------------------------------------------------------------*/


var ModuleHelper = function ()
{
	this.images_cache = new Array();

	this.submitForm = function (form_id)
	{
		$('#'+form_id).submit();
	}

	this.selectAll = function ()
	{
		$('#elems-table :checkbox.row-checkbox').attr('checked','checked').mouseup();
	}

	this.unselectAll = function ()
	{
		$('#elems-table :checkbox.row-checkbox').removeAttr('checked').mouseup();
	}

	this.multiAction = function (owner, action)
	{
		app.submit($(owner).parents('form'),'&'+action+'=true');
	}

	this.confirm = function (owner, message)
	{
		app.confirm(message,null,
			new DialogButton(
				'Oui',
				function ()
				{
					location = $(owner).attr('href')+'&confirm=true';
				},
				'ok-button'
			)
		);
	}
	
	this.confirmMultiDelete = function (owner)
	{
		app.confirm('Êtes-vous sûr de vouloir supprimer les enregistrements sélectionnés ?',null,
			new DialogButton(
				'Oui',
				function ()
				{
					app.submit($(owner).parents('form'),'&delete=true&confirm=true');
				},
				'ok-button'
			)
		);
	}
	
	this.getImage = function (path)
	{
		if (this.images_cache[path]==undefined)
		{
			this.images_cache[path] = new Image();
			this.images_cache[path].src = path;
		}
		return this.images_cache[path];
	}

	this.showImagePreview = function (index, path)
	{
		var jimage_selection = $('#image_selection_'+index);
		var jimage_preview = $('#image_preview_'+index);
		var jimage_preview_loading = $('#image_preview_loading_'+index);
		var value = jimage_selection.val();
			
		if (jimage_selection[0].selectedIndex==-1 || jimage_selection.val()=='')
		{
			jimage_preview.hide();
			jimage_preview_loading.hide();
		}
		else
		{
			jimage_preview.hide();
			jimage_preview_loading.show();
	
			var src = path+'/'+value;
			var img = this.getImage(src);
			jimage_preview.attr('src',src);
	
			if (!img.complete)
			{
				var _self = this;
				setTimeout(function () { _self.showImagePreview(index,path); },20);
				return;
			}
	
			var ih = img.naturalHeight ? img.naturalHeight : img.height;
			var iw = img.naturalWidth ? img.naturalWidth : img.width;
	
			var mh = 200, mw = 300;
			var h = ih, w = iw;
	
			if (ih>mh && ih/mh>=iw/mw)
			{
				h = mh;
				w = (iw*h)/ih;
			}
			else if (iw>mw && ih/mh<iw/mw)
			{
				w = mw;
				h = (ih*w)/iw;
			}
			
			jimage_preview.width(w).height(h);
			jimage_preview.css({'margin-left':parseInt((mw-w)/2)+'px','margin-top':parseInt((mh-h)/2)+'px'});
	
			jimage_preview_loading.hide();
			jimage_preview.show();
		}
	}
	
	this.addItem = function (index, separator)
	{
		var input_select = $('#allowed-items-'+index);
		var output_select = $('#selected-items-'+index);
		
		var jselected_item = input_select.find('option:selected');

		if (jselected_item.size()>0)
		{
			var item_value = jselected_item.val();
			var item_label = jselected_item.text();
			var joptions = output_select.find('option');
			var exist = false;
			for (i=0; i<joptions.size(); i++)
			{
				if (joptions.eq(i).val()==item_value)
				{
					exist = true;
					break;
				}
			}
			if (!exist)
			{
				output_select[0].options[output_select[0].length] = new Option(item_label,item_value);
				this._updateMultiItemsValue(index,separator);
			}
			else { app.alert("L'&eacute;l&eacute;ment est d&eacute;j&agrave; dans la liste."); }
		}
		else { app.alert("Vous devez s&eacute;lectionner un &eacute;l&eacute;ment &agrave; ajouter."); }
	}

	this.delItem = function (index, separator)
	{
		var output_select = $('#selected-items-'+index);
		var jselected_item = output_select.find('option:selected');

		if (jselected_item.size()>0)
		{
			jselected_item.remove();
			this._updateMultiItemsValue(index,separator);
		}
		else { app.alert("Vous devez s&eacute;lectionner un &eacute;l&eacute;ment &agrave; supprimer."); }
	}

	this.moveItemUp = function (index, separator)
	{
		var output_select = $('#selected-items-'+index);
		var jselected_item = output_select.find('option:selected');

		if (jselected_item.size()>0)
		{
			var jprevious = jselected_item.prev();
			if (jselected_item.size()>0)
			{
				jprevious.before(jselected_item);
			}
			this._updateMultiItemsValue(index,separator);
		}
		else { app.alert("Vous devez s&eacute;lectionner un &eacute;l&eacute;ment &agrave; d&eacute;placer."); }
	}
	
	this.moveItemDown = function (index, separator)
	{
		var output_select = $('#selected-items-'+index);
		var jselected_item = output_select.find('option:selected');

		if (jselected_item.size()>0)
		{
			var jnext = jselected_item.next();
			if (jselected_item.size()>0)
			{
				jnext.after(jselected_item);
			}
			this._updateMultiItemsValue(index,separator);
		}
		else { app.alert("Vous devez s&eacute;lectionner un &eacute;l&eacute;ment &agrave; d&eacute;placer."); }
	}
	
	this._updateMultiItemsValue = function (index, separator)
	{
		var jtextarea = $('#hidden-items-list-'+index);
		var output_select = $('#selected-items-'+index);
		var joptions = output_select.find('option');
		var value = '';
		for (i=0; i<joptions.size(); i++)
		{
			value += (i!=0 ? separator : '')+joptions.eq(i).val();
		}
		jtextarea.val(value);
	}

	this.changeButtonValue = function (id, value)
	{
		$('#'+id).val(value);
	}

	this.changeColor = function changeColor(id, increment)
	{
		var jinput = $('#'+id);

		var value = jinput.val();
		if (value.length>=6)
		{
			jinput.val(value.substr(0,6));
			value = jinput.val();

			if (!value.match(/[0-9A-F]+/)) { jinput.val('000000'); }
	
			var r = parseInt(value.substr(0,2),16);
			var g = parseInt(value.substr(2,2),16);
			var b = parseInt(value.substr(4,2),16);
			var r2 = r;
			var g2 = g;
			var b2 = b;
	
			if (Math.abs(increment)>=parseInt('FFFF',16))
			{
				r2 += increment/parseInt('FFFF',16);
				if (r2<=parseInt('FF',16) && r2>=0) { r = r2; }
			}
			else if (Math.abs(increment)>=parseInt('FF',16))
			{
				g2 += increment/parseInt('FF',16);
				if (g2<=parseInt('FF',16) && g2>=0) { g = g2; }
			}
			else
			{
				b2 += increment;
				if (b2<=parseInt('FF',16) && b2>=0) { b = b2; }
			}
	
			var r_string = r.toString(16).toUpperCase();
			while (r_string.length<2)
			{
				r_string = '0' + r_string;
			}
	
			var g_string = g.toString(16).toUpperCase();
			while (g_string.length<2)
			{
				g_string = '0' + g_string;
			}
	
			var b_string = b.toString(16).toUpperCase();
			while (b_string.length<2)
			{
				b_string = '0' + b_string;
			}
			value = r_string + g_string + b_string;
			jinput.val(value);
	
			jinput.css({'background-color':'#'+value,'color':(r<parseInt('7F',16) && g<parseInt('7F',16) && b<parseInt('7F',16) ? '#FFF' : '#000')});
		}
		else
		{
			jinput.css({'background-color':'#FFF','color':'#000'});
		}
	}

	this.setOrder = function (select, location)
	{
		var order = $(select).val();
		if (order!='') { document.location = location+order; }
	}
	
	this.gotoSelectedValue = function (select)
	{
		var url = $(select).val();
		if (url!='') { document.location = url; }
	}

}

var module = new ModuleHelper();