From: Argiris K. <be...@us...> - 2005-12-08 18:15:24
|
Update of /cvsroot/magicajax/magicajax/Core/UI/Controls In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19223/Core/UI/Controls Modified Files: AjaxPanel.cs Log Message: Added "ReflectUpdatedFormValues" method that issues javascript commands for changed form elements. Index: AjaxPanel.cs =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/UI/Controls/AjaxPanel.cs,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** AjaxPanel.cs 8 Dec 2005 11:33:50 -0000 1.31 --- AjaxPanel.cs 8 Dec 2005 18:15:02 -0000 1.32 *************** *** 565,568 **** --- 565,737 ---- /// <summary> + /// Parses all form input controls from the html, and checks if + /// their values were updated (i.e. different from the current Request.Form values) + /// and sends javascript commands to update the client's form values if necessary. + /// </summary> + /// <param name="html"></param> + protected void ReflectUpdatedFormValues(string html) + { + NameValueCollection form = Context.Request.Form; + RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled; + + Regex regEx = new System.Text.RegularExpressions.Regex(@"<(?<tag>input|textarea|select)\s((?<attrname>[-\w]+)=""(?<attrvalue>.*?)""\s?)*.*?(?:/>|>(?<inner>.*?)</(?:textarea|select)>)", options); + MatchCollection matches = regEx.Matches(html); + for (int i=0; i<matches.Count; i++) + { + Match match = matches[i]; + CaptureCollection attrnames = match.Groups["attrname"].Captures; + CaptureCollection attrvalues = match.Groups["attrvalue"].Captures; + + Hashtable attrNameValues = new Hashtable(); + for (int j=0; j< attrnames.Count; j++) + { + attrNameValues.Add(attrnames[j].Value.ToLower(), attrvalues[j].Value); + } + + string tag = match.Groups["tag"].Value.ToLower(); + string name = (string)attrNameValues["name"]; + string clientID = (string)attrNameValues["id"]; + + if ( name == null ) + continue; + + switch (tag) + { + #region <input> tags + + case "input": + string type = (string)attrNameValues["type"]; + if (type != null) + { + string value = (string)attrNameValues["value"]; + bool isChecked = attrNameValues.ContainsKey("checked"); + + switch (type) + { + case "text": + case "password": + if (value != form[name]) + AjaxCallHelper.WriteSetFieldScript (name, value); + break; + case "checkbox": + if (isChecked != (form[name] != null)) + AjaxCallHelper.WriteFormat ("document.forms[0][\"{0}\"].checked={1};\r\n", name, (isChecked) ? "true" : "false"); + break; + case "radio": + if ( isChecked && form[name] != value ) + { + AjaxCallHelper.WriteFormat ("document.forms[0][\"{0}\"].checked=true;\r\n", clientID); + } + else if ( !isChecked && form[name] == value ) + { + AjaxCallHelper.WriteFormat ("document.forms[0][\"{0}\"].checked=false;\r\n", clientID); + } + break; + } + } + break; + + #endregion + + #region <textarea> tags + + case "textarea": + string text = match.Groups["inner"].Value; + if (text != form[name]) + AjaxCallHelper.WriteSetFieldScript (name, text); + break; + + #endregion + + #region <select> tags + + case "select": + Regex regExOpt = new System.Text.RegularExpressions.Regex("<option\\s((?<attrname>[-\\w]+)=\"(?<attrvalue>.*?)\"\\s?)*\\s*>.*?</option>", options); + bool multiple = attrNameValues.ContainsKey("multiple"); + + ArrayList serverOptions = new ArrayList(); + ArrayList serverSelected = new ArrayList(); + string oneSelection = null; + + //now match the options within this <select> tag + MatchCollection matchesOptions = regExOpt.Matches(match.Groups["inner"].Value); + for (int j = 0; j < matchesOptions.Count; j++) + { + Match matchOption = matchesOptions[j]; + + CaptureCollection attrnamesOption = matchOption.Groups["attrname"].Captures; + CaptureCollection attrvaluesOption = matchOption.Groups["attrvalue"].Captures; + + Hashtable attrNameValuesOption = new Hashtable(); + for (int k = 0; k < attrnamesOption.Count; k++) + { + attrNameValuesOption.Add(attrnamesOption[k].Value.ToLower(), attrvaluesOption[k].Value); + } + + string optValue = (string)attrNameValuesOption["value"]; //TODO: check value encodings etc. + bool optSelected = attrNameValuesOption.ContainsKey("selected"); + + if (optSelected && optValue != null) + { + if ( multiple ) + { + serverSelected.Add (optValue); + } + else + { + oneSelection = optValue; + break; + } + } + + serverOptions.Add (optValue); + } + + if ( ! multiple ) + { + if ( oneSelection != form[name] ) + AjaxCallHelper.WriteSetFieldScript (name, oneSelection); + } + else + { + string[] selections = form.GetValues(name); + bool elemWritten = false; + + // Make selections + for (int j=0; j < serverSelected.Count; j++) + { + if (Array.IndexOf(selections, serverSelected[j]) == -1) + { + if ( ! elemWritten ) + { + AjaxCallHelper.WriteFormat ("o=document.forms[0][\"{0}\"].options;\r\n", name); + elemWritten = true; + } + AjaxCallHelper.WriteFormat ("o[{0}].selected=true;\r\n", serverOptions.IndexOf(serverSelected[j])); + } + } + + // Make unselections + for (int j=0; j < selections.Length; j++) + { + if ( ! serverSelected.Contains(selections[j])) + { + if ( ! elemWritten ) + { + AjaxCallHelper.WriteFormat ("o=document.forms[0][\"{0}\"];\r\n", name); + elemWritten = true; + } + AjaxCallHelper.WriteFormat ("o[{0}].selected=false;\r\n", serverOptions.IndexOf(selections[j])); + } + } + } + break; + #endregion + } + + } + } + + /// <summary> /// Determines whether the AjaxPanel should treat the given control as a separate /// 'html holder' to compare and 'reflect' its html rendering. *************** *** 575,589 **** protected virtual bool IsHtmlHolder (Control control) { ! return ( control is WebControl && !(control is RenderedByScriptControl) ); } protected void ReflectContainedFormControls (Control container) { ! ArrayList elems = new ArrayList(); if ( container is IPostBackDataHandler ) elems.Add (container); elems.AddRange (Util.GetChildControlsOfType(container, typeof(IPostBackDataHandler), typeof(RenderedByScriptControl), true)); ! for (int i=0; i < elems.Count; i++) ReflectFormControl (elems[i] as Control); } --- 744,758 ---- protected virtual bool IsHtmlHolder (Control control) { ! return ( control is WebControl && !(control is RenderedByScriptControl) ); } protected void ReflectContainedFormControls (Control container) { ! ArrayList elems = new ArrayList(); if ( container is IPostBackDataHandler ) elems.Add (container); elems.AddRange (Util.GetChildControlsOfType(container, typeof(IPostBackDataHandler), typeof(RenderedByScriptControl), true)); ! for (int i=0; i < elems.Count; i++) ReflectFormControl (elems[i] as Control); } *************** *** 618,622 **** { RadioButton radio = control as RadioButton; ! int index = radio.UniqueID.LastIndexOf(':'); string uniqueGroupName = radio.UniqueID.Substring(0, index + 1) + radio.GroupName; if ( radio.Checked && form[uniqueGroupName] != radio.ID ) --- 787,791 ---- { RadioButton radio = control as RadioButton; ! int index = radio.UniqueID.LastIndexOf(':'); string uniqueGroupName = radio.UniqueID.Substring(0, index + 1) + radio.GroupName; if ( radio.Checked && form[uniqueGroupName] != radio.ID ) *************** *** 653,657 **** if ( (ddlist.SelectedIndex == -1 && form[id] != null) || ddlist.SelectedValue != form[id] ) ! AjaxCallHelper.WriteSetFieldScript (id, ddlist.SelectedValue); } else if ( control is ListBox ) --- 822,826 ---- if ( (ddlist.SelectedIndex == -1 && form[id] != null) || ddlist.SelectedValue != form[id] ) ! AjaxCallHelper.WriteSetFieldScript (id, ddlist.SelectedValue); } else if ( control is ListBox ) *************** *** 707,711 **** else if ( control is RadioButtonList ) { ! RadioButtonList rblist = control as RadioButtonList; if ( rblist.SelectedIndex == -1 && form[id] != null ) { --- 876,880 ---- else if ( control is RadioButtonList ) { ! RadioButtonList rblist = control as RadioButtonList; if ( rblist.SelectedIndex == -1 && form[id] != null ) { *************** *** 725,729 **** AjaxCallHelper.WriteFormat ("document.forms[0][\"{0}\"].checked=true;\r\n", String.Format("{0}_{1}", control.ClientID, rblist.SelectedIndex)); } ! } else if ( control is TextBox ) { --- 894,898 ---- AjaxCallHelper.WriteFormat ("document.forms[0][\"{0}\"].checked=true;\r\n", String.Format("{0}_{1}", control.ClientID, rblist.SelectedIndex)); } ! } else if ( control is TextBox ) { *************** *** 934,938 **** // Increase script writing level because we want literal content to // be rendered before the child controls renderings ! AjaxCallHelper.IncreaseWritingLevel(); foreach (Control con in _removedControls) --- 1103,1107 ---- // Increase script writing level because we want literal content to // be rendered before the child controls renderings ! AjaxCallHelper.IncreaseWritingLevel(); foreach (Control con in _removedControls) *************** *** 989,1017 **** string htmlFingerprint = Util.GetFingerprint(html); ! if (htmlFingerprint != (string)_controlHtmlFingerprints[con] ! || ContainsUpdatedFormValues(html)) { // Its html rendering fingerprint has changed, // "reflect" its html on client. ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); ! _controlHtmlFingerprints[con] = htmlFingerprint; } - - //previous code, commented by Dion - // - //if (htmlFingerprint == (string)_controlHtmlFingerprints[con]) - //{ - // // Its html rendering fingerprint is the same, ignore the - // // html and compare the form elements that it contains - // // with those on client's page. - // ReflectContainedFormControls (con); - //} - //else - //{ - // // Its html rendering fingerprint has changed, - // // "reflect" its html on client. - // ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); - // _controlHtmlFingerprints[con] = htmlFingerprint; - //} } else --- 1158,1186 ---- string htmlFingerprint = Util.GetFingerprint(html); ! //previous code, commented by Argiris ! // ! //if (htmlFingerprint != (string)_controlHtmlFingerprints[con] ! // || ContainsUpdatedFormValues(html)) ! //{ ! // // Its html rendering fingerprint has changed, ! // // "reflect" its html on client. ! // ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); ! // _controlHtmlFingerprints[con] = htmlFingerprint; ! //} ! ! if (htmlFingerprint == (string)_controlHtmlFingerprints[con]) ! { ! // Its html rendering fingerprint is the same, ignore the ! // html and "reflect" the form elements that it contains ! // on client's page if they're different. ! ReflectUpdatedFormValues (html); ! } ! else { // Its html rendering fingerprint has changed, // "reflect" its html on client. ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); ! _controlHtmlFingerprints[con] = htmlFingerprint; } } else *************** *** 1031,1037 **** string literalhtml = sbLiteral.ToString(); string literalFingerprint = Util.GetFingerprint(literalhtml); ! if ( _controlState.LiteralFingerprint == literalFingerprint ) { ! ReflectLiteralFormControls(); } else --- 1200,1209 ---- string literalhtml = sbLiteral.ToString(); string literalFingerprint = Util.GetFingerprint(literalhtml); ! if ( literalFingerprint == _controlState.LiteralFingerprint ) { ! // Its html rendering fingerprint is the same, ignore the ! // html and "reflect" the form elements that it contains ! // on client's page if they're different. ! ReflectUpdatedFormValues (literalhtml); } else |