Use JSONWriter instead of string concatenation

This commit is contained in:
2016-06-13 01:22:48 +02:00
parent 26f6947f1f
commit 4127b44bc4
2 changed files with 55 additions and 48 deletions

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using VAR.Focus.Web.Code.JSON;
namespace VAR.Focus.Web.Controls
{
@@ -89,32 +91,33 @@ namespace VAR.Focus.Web.Controls
private void InitializeControls()
{
string strCfgName = string.Format("{0}_cfg", this.ClientID);
Panel divBoard = new Panel { ID = "divBoard", CssClass = "divBoard" };
Controls.Add(divBoard);
string strCfgName = string.Format("{0}_cfg", this.ClientID);
Dictionary<string, object> cfg = new Dictionary<string, object>
{
{"divBoard", divBoard.ClientID},
{"IDBoard", _idBoard},
{"UserName", _userName},
{"IDCardEvent", string.Empty},
{"ServiceUrl", _serviceUrl},
{"TimePoolData", _timePoolData},
{"TimeRefresh", _timeRefresh},
{"TimeRefreshDisconnected", _timeRefreshDisconnected},
{"TimeMoveAnimation", _timeMoveAnimation},
{"Texts", new Dictionary<string, object> {
{"Toolbox", "Toolbox"},
{"AddCard", "+ Add card"},
{"Accept", "Accept"},
{"Cancel", "Cancel"},
{"ConfirmDelete", "Are you sure to delete?"},
} },
};
JSONWriter jsonWriter = new JSONWriter();
StringBuilder sbCfg = new StringBuilder();
sbCfg.AppendFormat("<script>\n");
sbCfg.AppendFormat("var {0} = {{\n", strCfgName);
sbCfg.AppendFormat(" divBoard: \"{0}\",\n", divBoard.ClientID);
sbCfg.AppendFormat(" IDBoard: {0},\n", _idBoard);
sbCfg.AppendFormat(" UserName: \"{0}\",\n", _userName);
sbCfg.AppendFormat(" IDCardEvent: \"\",\n");
sbCfg.AppendFormat(" ServiceUrl: \"{0}\",\n", _serviceUrl);
sbCfg.AppendFormat(" TimePoolData: {0},\n", _timePoolData);
sbCfg.AppendFormat(" TimeRefresh: {0},\n", _timeRefresh);
sbCfg.AppendFormat(" TimeRefreshDisconnected: {0},\n", _timeRefreshDisconnected);
sbCfg.AppendFormat(" TimeMoveAnimation: {0},\n", _timeMoveAnimation);
sbCfg.AppendFormat(" Texts: {{\n");
sbCfg.AppendFormat(" Toolbox: \"Toolbox\",\n");
sbCfg.AppendFormat(" AddCard: \"+ Add card\",\n");
sbCfg.AppendFormat(" Accept: \"Accept\",\n");
sbCfg.AppendFormat(" Cancel: \"Cancel\",\n");
sbCfg.AppendFormat(" ConfirmDelete: \"Are you sure to delete?\",\n");
sbCfg.AppendFormat(" StringEmpty: \"\"\n");
sbCfg.AppendFormat(" }}\n");
sbCfg.AppendFormat("}};\n");
sbCfg.AppendFormat("var {0} = {1};\n", strCfgName, jsonWriter.Write(cfg));
sbCfg.AppendFormat("RunCardBoard({0});\n", strCfgName);
sbCfg.AppendFormat("</script>\n");
LiteralControl liScript = new LiteralControl(sbCfg.ToString());

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using VAR.Focus.Web.Code.JSON;
namespace VAR.Focus.Web.Controls
{
@@ -98,7 +100,7 @@ namespace VAR.Focus.Web.Controls
private void InitializeControls()
{
string strCfgName = string.Format("{0}_cfg", this.ClientID);
string strCfgName = string.Format("{0}_cfg", ClientID);
_divChatWindow = new Panel { ID = "divChatWindow", CssClass = "divChatWindow" };
Controls.Add(_divChatWindow);
@@ -123,38 +125,40 @@ namespace VAR.Focus.Web.Controls
var txtText = new TextBox { ID = "txtText", CssClass = "chatTextBox" };
txtText.Attributes.Add("autocomplete", "off");
txtText.Attributes.Add("onkeydown", String.Format("if(event.keyCode==13){{SendChat({0}); return false;}}", strCfgName));
txtText.Attributes.Add("onkeydown", string.Format("if(event.keyCode==13){{SendChat({0}); return false;}}", strCfgName));
divChatControls.Controls.Add(txtText);
var btnSend = new Button { ID = "btnSend", Text = "Send", CssClass = "chatButton" };
divChatControls.Controls.Add(btnSend);
btnSend.Attributes.Add("onclick", String.Format("SendChat({0}); return false;", strCfgName));
btnSend.Attributes.Add("onclick", string.Format("SendChat({0}); return false;", strCfgName));
Dictionary<string, object> cfg = new Dictionary<string, object>
{
{"divChatWindow", _divChatWindow.ClientID},
{"divChatTitleBar", _divChatTitleBar.ClientID},
{"lblTitle", lblTitle.ClientID},
{"divChatContainer", _divChatContainer.ClientID},
{"divChatContainerWidth", _width.ToString()},
{"divChatContainerHeight", _height.ToString()},
{"divChat", divChat.ClientID},
{"txtText", txtText.ClientID},
{"btnSend", btnSend.ClientID},
{"IDMessageBoard", _idMessageBoard},
{"UserName", _userName},
{"IDMessage", 0},
{"ServiceUrl", _serviceUrl},
{"TimePoolData", _timePoolData},
{"Texts", new Dictionary<string, object> {
{"Chat", "Chat"},
{"Close", "Close X"},
{"NewMessages", "New messages"},
{"Disconnected", "Disconnected"},
} },
};
JSONWriter jsonWriter = new JSONWriter();
StringBuilder sbCfg = new StringBuilder();
sbCfg.AppendFormat("<script>\n");
sbCfg.AppendFormat("var {0} = {{\n", strCfgName);
sbCfg.AppendFormat(" divChatWindow: \"{0}\",\n", _divChatWindow.ClientID);
sbCfg.AppendFormat(" divChatTitleBar: \"{0}\",\n", _divChatTitleBar.ClientID);
sbCfg.AppendFormat(" lblTitle: \"{0}\",\n", lblTitle.ClientID);
sbCfg.AppendFormat(" divChatContainer: \"{0}\",\n", _divChatContainer.ClientID);
sbCfg.AppendFormat(" divChatContainerWidth: \"{0}\",\n", _width);
sbCfg.AppendFormat(" divChatContainerHeight: \"{0}\",\n", _height);
sbCfg.AppendFormat(" divChat: \"{0}\",\n", divChat.ClientID);
sbCfg.AppendFormat(" txtText: \"{0}\",\n", txtText.ClientID);
sbCfg.AppendFormat(" btnSend: \"{0}\",\n", btnSend.ClientID);
sbCfg.AppendFormat(" IDMessageBoard: \"{0}\",\n", _idMessageBoard);
sbCfg.AppendFormat(" UserName: \"{0}\",\n", _userName);
sbCfg.AppendFormat(" IDMessage: {0},\n", 0);
sbCfg.AppendFormat(" ServiceUrl: \"{0}\",\n", _serviceUrl);
sbCfg.AppendFormat(" TimePoolData: {0},\n", _timePoolData);
sbCfg.AppendFormat(" Texts: {{\n", _serviceUrl);
sbCfg.AppendFormat(" Chat: \"{0}\",\n", "Chat");
sbCfg.AppendFormat(" Close: \"{0}\",\n", "Close X");
sbCfg.AppendFormat(" NewMessages: \"{0}\",\n", "New messages");
sbCfg.AppendFormat(" Disconnected: \"{0}\",\n", "Disconnected");
sbCfg.AppendFormat(" StringEmpty: \"\"\n");
sbCfg.AppendFormat(" }}\n");
sbCfg.AppendFormat("}};\n");
sbCfg.AppendFormat("var {0} = {1};\n", strCfgName, jsonWriter.Write(cfg));
sbCfg.AppendFormat("RunChat({0});\n", strCfgName);
sbCfg.AppendFormat("</script>\n");
LiteralControl liScript = new LiteralControl(sbCfg.ToString());