Parcourir la source

- New implementation of the n-n link edition widget... in progress.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@656 a333f486-631f-4898-b8df-5754b55c2be0
dflaven il y a 15 ans
Parent
commit
de0b29cde1
3 fichiers modifiés avec 169 ajouts et 132 suppressions
  1. 14 0
      js/jquery.popupmenu.js
  2. 134 124
      js/linkswidget.js
  3. 21 8
      js/utils.js

+ 14 - 0
js/jquery.popupmenu.js

@@ -34,6 +34,20 @@ jQuery.fn.popupmenu = function ()
 										elementPos: 0,
 										hideAfterPosition: true
 										});
+				// In links containing a hash, replace what's after the hash by the current hash
+				// In order to navigate to the same tab as the current one when editing an object
+				currentHash = '';
+				aMatches = /#(.*)$/.exec(window.location.href);
+				if (aMatches != null)
+				{
+					currentHash = aMatches[1];
+					popupmenu.find('a').each( function() {
+						if ( /#(.*)$/.test(this.href))
+						{
+							this.href = this.href.replace(/#(.*)$/, '#'+currentHash);
+						}
+					});
+				}
 				popupmenu.css('display', 'block');
 			}
 			else

+ 134 - 124
js/linkswidget.js

@@ -1,153 +1,163 @@
 // JavaScript Document
-
-function LinksWidget(id, sLinkedClass, sExtKeyToMe, sExtKeyToRemote, aAttributes)
+function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix)
 {
 	this.id = id;
-	this.sLinkedClass = sLinkedClass;
-	this.sExtKeyToMe = sExtKeyToMe;
-	this.sExtKeyToRemote = sExtKeyToRemote;
-	this.aAttributes = aAttributes;
-	this.aLinks = new Array();
-
+	this.iInputId = iInputId;
+	this.sClass = sClass;
+	this.sAttCode = sAttCode;
+	this.sSuffix = sSuffix;
+	var me = this;
 	this.Init = function()
 	{
-		sLinks = $('#'+this.id).val();
-		if (sLinks.length > 0)
-		{
-			this.aLinks = JSON.parse(sLinks);
-		}
-		this.Refresh();
+		// make sure that the form is clean
+		$('#linkedset_'+this.id+' .selection').each( function() { this.checked = false; });
+		$('#'+this.id+'_btnRemove').attr('disabled','disabled');
+		$('#'+this.id+'_linksToRemove').val('');
 	}
+	
+	this.RemoveSelected = function()
+	{
+		var my_id = '#'+me.id;
+		$('#linkedset_'+me.id+' .selection:checked').each(
+			function()
+			{
+				$linksToRemove = $(my_id+'_linksToRemove');
+				prevValue = $linksToRemove.val();
+				if (prevValue != '')
+				{
+					$linksToRemove.val(prevValue + ',' + this.value);
+				}
+				else
+				{
+					$linksToRemove.val(this.value);
+				}
+				$(my_id+'_row_'+this.value).remove();
+			}
+		);
+		// Disable the button since all the selected items have been removed
+		$(my_id+'_btnRemove').attr('disabled','disabled');
+		// Re-run the zebra plugin to properly highlight the remaining lines
+		$('#linkset_'+this.id+' .listResults').trigger('update');
 		
-	this.Refresh = function ()
+	}
+
+	this.OnSelectChange = function()
 	{
-		$('#v_'+this.id).html('<img src="../images/indicator.gif" />');
-		sLinks = JSON.stringify(this.aLinks);
-		if (this.aLinks.length == 0)
+		var nbChecked = $('#linkedset_'+me.id+' .selection:checked').length;
+		if (nbChecked > 0)
 		{
-			$('#'+this.id+'_values').empty();		
-			$('#'+this.id).val(sLinks);
-			$('#'+this.id).trigger('validate');
+			$('#'+me.id+'_btnRemove').attr('disabled','');
 		}
 		else
 		{
-			$('#'+this.id).val(sLinks);
-			$('#'+this.id+'_values').load('ajax.render.php?operation=ui.linkswidget.linkedset&sclass='+this.sLinkedClass+'&sextkeytome='+this.sExtKeyToMe+'&sextkeytoremote='+this.sExtKeyToRemote+'&myid='+this.id,
-			{'sset' : sLinks}, function()
-				{
-					// Refresh the style of the loaded table
-					$('#'+this.id+' table.listResults').tableHover();	
-		 			$('#'+this.id+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra', 'truncatedList']} ); // sortable and zebra tables
-				}
-			);
+			$('#'+me.id+'_btnRemove').attr('disabled','disabled');
 		}
 	}
 	
-	this.OnOk = function()
+	this.AddObjects  = function()
 	{
-		this.aObjectBeingLinked = new Array();
-		sSelected = 'selected_objects_'+this.id;
-		oSelected = document.getElementById(sSelected);
-		for(i=0; i<oSelected.length; i++)
-		{
-			this.aObjectBeingLinked[i] = oSelected.options[i].value;
-		}
-		this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
-		this.aLinks = new Array(); // rebuild the list of links from scratch
-		if (this.aAttributes.length == 0)
-		{
-			// No attributes on the link, no need for the extra dialog box
-			// Process the results directly
-			for(i=0; i<this.aObjectBeingLinked.length; i++)
+		//$('#dlg_'+this.id).hide();
+		$('#dlg_'+me.id).dialog('open');
+		//alert('Not Yet Implemented !');
+	}
+	
+	this.SearchObjectsToAdd = function()
+	{
+		var theMap = { sAttCode: me.sAttCode,
+					   iInputId: me.iInputId,
+					   sSuffix: me.sSuffix
+					 }
+		
+		// Gather the parameters from the search form
+		$('#SearchFormToAdd_'+me.id+' :input').each(
+			function(i)
 			{
-				oLink = {};
-				oLink[this.sExtKeyToRemote] = this.aObjectBeingLinked[i];
-				this.aLinks.push(oLink);
+				if (this.name != '')
+				{
+					theMap[this.name] = this.value;
+				}
 			}
-			this.Refresh();
-			// Grey out the 'Add...' button
-			$('#ac_add_'+this.id).attr('disabled', 'disabled');
-		}
-		else
-		{
-			if (oSelected.length > 0)
+		);
+		
+		// Gather the already linked target objects
+		theMap.aAlreadyLinked = new Array();
+		$('#linkedset_'+me.id+' .selection:input').each(
+			function(i)
 			{
-				$('#LinkDlg_'+this.id).dialog('open');
+				theMap.aAlreadyLinked.push(this.value);
 			}
-			else
+		);
+		theMap['sRemoteClass'] = theMap['class'];  // swap 'class' (defined in the form) and 'remoteClass'
+		theMap['class'] = me.sClass;
+		theMap.operation = 'searchObjectsToAdd'; // Override what is defined in the form itself
+		
+		sSearchAreaId = '#SearchResultsToAdd_'+me.id;
+		
+		// Run the query and display the results
+		$.post( 'ajax.render.php', theMap, 
+			function(data)
 			{
-				this.Refresh();
-				$('#ac_add_'+this.id).attr('disabled', 'disabled');
-			}
-		}
-	}
+				$(sSearchAreaId).html(data);
+				$(sSearchAreaId+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
+				
+			},
+			'html'
+		);
 
-	this.OnCancel = function()
-	{
-		// Restore the links to their previous value (just in case)
-		this.aLinks = this.aPreviousLinks;
+		return false; // Don't submit the form, stay in the current page !
 	}
-	
-	this.OnLinkOk = function()
+
+	this.DoAddObjects = function()
 	{
-		$('#LinkDlg_'+this.id).dialog('close');
-		for(i=0; i<this.aObjectBeingLinked.length; i++)
-		{
-			oLink = {};
-			oLink[this.sExtKeyToRemote] = this.aObjectBeingLinked[i];
-			for(j=0; j<this.aAttributes.length; j++)
+		var theMap = { sAttCode: me.sAttCode,
+				   	   iInputId: me.iInputId,
+				   	   sSuffix: me.sSuffix,
+				   	   'class': me.sClass
+				 	 }
+		
+		// Gather the parameters from the search form
+		$('#SearchResultsToAdd_'+me.id+' :checked').each(
+			function(i)
 			{
-				oLink[aAttributes[j]] = $('#'+this.id+'_'+j).val();
+				if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) ) 
+				{
+					//console.log(this.type);
+					arrayExpr = /\[\]$/;
+					if (arrayExpr.test(this.name))
+					{
+						// Array
+						if (theMap[this.name] == undefined)
+						{
+							theMap[this.name] = new Array();
+						}
+						theMap[this.name].push(this.value);
+					}
+					else
+					{
+						theMap[this.name] = this.value;
+					}
+					$(this).parents('tr:first').remove(); // Remove the whole line, so that, next time the dialog gets displayed it's no longer there
+				}
 			}
-			this.aLinks.push(oLink);
-		}
-		this.Refresh();
-		// Grey out the 'Add...' button
-		$('#ac_add_'+this.id).attr('disabled', 'disabled');
-		return false;
-	}
-	
-	this.OnLinkCancel = function()
-	{
-		$('#LinkDlg_'+this.id).dialog('close');
-		// Restore the links to their previous value (just in case)
-		this.aLinks = this.aPreviousLinks;
-		// Grey out the 'Add...' button
-		$('#ac_add_'+this.id).attr('disabled', 'disabled');
+		);
+		theMap['operation'] = 'doAddObjects';
+		
+		// Run the query and display the results
+		$.post( 'ajax.render.php', theMap, 
+			function(data)
+			{
+				//console.log('Data: ' + data);
+				if (data != '')
+				{
+					$('#'+me.id+'_empty_row').remove();
+					$('#linkedset_'+me.id+' .listResults tbody').append(data);
+					$('#linkedset_'+me.id+' .listResults').trigger('update');
+					$('#linkedset_'+me.id+' .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
+				}
+			},
+			'html'
+		);
+		$('#dlg_'+me.id).dialog('close');
 		return false;
 	}
-	
-	this.RemoveLink = function(index)
-	{
-		this.aLinks.splice(index, 1); // Remove the element at position 'index'
-		this.Refresh();
-	}
-	
-	this.AddObject = function()
-	{
-		linkedObjId = $('#id_ac_'+this.id).val();
-		// Clears the selection
-		$('#id_ac_'+this.id).val('');
-		$('#ac_'+this.id).val('');
-		// Add the object to the list
-		this.aObjectBeingLinked = new Array();
-		this.aObjectBeingLinked[0] = linkedObjId;
-		// Add the object to the list of links
-		this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
-		$('#LinkDlg_'+this.id).dialog('open');
-	}
-	
-	this.ModifyLink = function(index)
-	{
-		this.aObjectBeingLinked = new Array();
-		this.aObjectBeingLinked[0] = this.aLinks[index][this.sExtKeyToRemote];
-		this.aPreviousLinks = this.aLinks; // Save the list in case of cancellation
-		// Set the default values of the dialog to the current ones
-		for(j=0; j<this.aAttributes.length; j++)
-		{
-			$('#'+this.id+'_'+j).val(aLinks[index][aAttributes[j]]);
-		}
-		this.aLinks.splice(index, 1); // Remove the element at position 'index'
-		$('#LinkDlg_'+this.id).dialog('open'); // And add it again	
-	}
 }

+ 21 - 8
js/utils.js

@@ -59,21 +59,34 @@ function ReloadSearchForm(divId, sClassName, sBaseClass)
 	$('#'+divId).block();
 	var formEvents = $('#'+divId+' form').data('events');
 	var bSubmitHookIsUsed = false;
-	if ( (formEvents != undefined) && (SubmitHook != undefined))
+	
+	// Save the submit handlers
+	aSubmit = new Array();
+	if ( (formEvents != null) && (formEvents.submit != undefined))
 	{
-		// Assume that we're using the function submit hook...
-		bSubmitHookIsUsed = true;
+		aSubmit = formEvents.submit;
 	}
-	$('#'+divId+' form').unbind('submit');
+
 	$.post('ajax.render.php',
 	   { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId },
 	   function(data){
 		   $('#'+divId).empty();
 		   $('#'+divId).append(data);
-		   if (bSubmitHookIsUsed)
-		   {
-			   $('#'+divId+' form').bind('submit', SubmitHook);
-		   }
+			if (aSubmit.length > 0)
+			{
+				for(index = 0; index < aSubmit.length; index++)
+				{
+					// Restore the previously bound submit handlers
+					if (aSubmit[index].data != undefined)
+					{
+						$('#'+divId+' form').bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
+					}
+					else
+					{
+						$('#'+divId+' form').bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
+					}
+				}
+			}
 		   $('#'+divId).unblock();
 	   }
 	 );