Browse Source

Form fields: added callbacks ('equals' and 'get_field_value') to allow the implementation of enhanced form fields

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3356 a333f486-631f-4898-b8df-5754b55c2be0
romainq 10 years ago
parent
commit
82e3e0db5a
2 changed files with 40 additions and 9 deletions
  1. 13 1
      application/forms.class.inc.php
  2. 27 8
      js/property_field.js

+ 13 - 1
application/forms.class.inc.php

@@ -234,9 +234,11 @@ class DesignerForm
 					}
 					$sNotifyParentSelectorJS = is_null($sNotifyParentSelector) ? 'null' : "'".addslashes($sNotifyParentSelector)."'";
 					$sAutoApply = $oField->IsAutoApply() ? 'true' : 'false';
+					$sHandlerEquals = $oField->GetHandlerEquals();
+					$sHandlerGetValue = $oField->GetHandlerGetValue();
 					$this->AddReadyScript(
 <<<EOF
-$('#row_$sFieldId').property_field({parent_selector: $sNotifyParentSelectorJS, field_id: '$sFieldId', auto_apply: $sAutoApply, value: '', submit_to: '$sActionUrl', submit_parameters: $sJSSubmitParams });
+$('#row_$sFieldId').property_field({parent_selector: $sNotifyParentSelectorJS, field_id: '$sFieldId', equals: $sHandlerEquals, get_field_value: $sHandlerGetValue, auto_apply: $sAutoApply, value: '', submit_to: '$sActionUrl', submit_parameters: $sJSSubmitParams });
 EOF
 					);
 				}
@@ -670,6 +672,16 @@ class DesignerFormField
 	{
 		$this->aCSSClasses[] = $sCSSClass;
 	}
+
+	public function GetHandlerEquals()
+	{
+		return 'null';
+	}
+
+	public function GetHandlerGetValue()
+	{
+		return 'null';
+	}
 }
 
 class DesignerLabelField extends DesignerFormField

+ 27 - 8
js/property_field.js

@@ -10,12 +10,13 @@ $(function()
 		{
 			parent_selector: null,
 			field_id: '',
+			get_field_value: null,
+			equals: null,
 			submit_to: 'index.php',
 			submit_parameters: {operation: 'async_action'},
 			do_apply: null,
 			do_cancel: null,
 			auto_apply: false
-			
 		},
 	
 		// the constructor
@@ -83,7 +84,11 @@ $(function()
 		_on_change: function()
 		{
 			var new_value = this._get_field_value();
-			if (new_value != this.value)
+			if (this._equals(new_value, this.value))
+			{
+				this.bModified = false;
+			}
+			else
 			{
 				this.bModified = true;
 				if (this.options.auto_apply)
@@ -91,22 +96,36 @@ $(function()
 					this._do_apply();
 				}
 			}
+			this._refresh();
+		},
+		_equals: function( value1, value2 )
+		{
+			if (this.options.equals === null)
+			{
+				return value1 == value2;
+			}
 			else
 			{
-				this.bModified = false;
+				return this.options.equals(value1, value2);
 			}
-			this._refresh();
 		},
 		_get_field_value: function()
 		{
-			var oField = $('#'+this.options.field_id);
-			if (oField.attr('type') == 'checkbox')
+			if (this.options.get_field_value === null)
 			{
-				return (oField.attr('checked') == 'checked');
+				var oField = $('#'+this.options.field_id);
+				if (oField.attr('type') == 'checkbox')
+				{
+					return (oField.attr('checked') == 'checked');
+				}
+				else
+				{
+					return oField.val();				
+				}
 			}
 			else
 			{
-				return oField.val();				
+				return this.options.get_field_value();
 			}			
 		},
 		_get_committed_value: function()