1+ using System ;
2+ using Simplify . Web . Modules . Data ;
3+
4+ namespace Simplify . Web . MessageBox
5+ {
6+ /// <summary>
7+ /// The HTML message box
8+ /// Usable template files:
9+ /// "Simplify.Web/MessageBox/InfoMessageBox.tpl"
10+ /// "Simplify.Web/MessageBox/ErrorMessageBox.tpl"
11+ /// "Simplify.Web/MessageBox/OkMessageBox.tpl"
12+ /// "Simplify.Web/MessageBox/InlineInfoMessageBox.tpl"
13+ /// "Simplify.Web/MessageBox/InlineErrorMessageBox.tpl"
14+ /// "Simplify.Web/MessageBox/InlineOkMessageBox.tpl"
15+ /// Usable <see cref="StringTable"/> items:
16+ /// "FormTitleMessageBox"
17+ /// Template variables:
18+ /// "Message"
19+ /// "Title"
20+ /// </summary>
21+ public sealed class MessageBoxHandler : IMessageBoxHandler
22+ {
23+ /// <summary>
24+ /// The message box templates path
25+ /// </summary>
26+ public const string MessageBoxTemplatesPath = "Simplify.Web/MessageBox/" ;
27+
28+ private readonly ITemplateFactory _templateFactory ;
29+ private readonly IStringTable _stringTable ;
30+ private readonly IDataCollector _dataCollector ;
31+
32+ /// <summary>
33+ /// Initializes a new instance of the <see cref="MessageBoxHandler"/> class.
34+ /// </summary>
35+ /// <param name="templateFactory">The template factory.</param>
36+ /// <param name="stringTable">The string table.</param>
37+ /// <param name="dataCollector">The data collector.</param>
38+ public MessageBoxHandler ( ITemplateFactory templateFactory , IStringTable stringTable , IDataCollector dataCollector )
39+ {
40+ _templateFactory = templateFactory ;
41+ _stringTable = stringTable ;
42+ _dataCollector = dataCollector ;
43+ }
44+
45+ /// <summary>
46+ /// Generate message box HTML and set to data collector
47+ /// </summary>
48+ /// <param name="text">Text of a message box</param>
49+ /// <param name="status">Status of a message box</param>
50+ /// <param name="title">Title of a message box</param>
51+ public void Show ( string text , MessageBoxStatus status = MessageBoxStatus . Error , string title = null )
52+ {
53+ if ( string . IsNullOrEmpty ( text ) )
54+ throw new ArgumentNullException ( nameof ( text ) ) ;
55+
56+ var templateFile = MessageBoxTemplatesPath ;
57+
58+ switch ( status )
59+ {
60+ case MessageBoxStatus . Information :
61+ templateFile += "InfoMessageBox.tpl" ;
62+ break ;
63+
64+ case MessageBoxStatus . Error :
65+ templateFile += "ErrorMessageBox.tpl" ;
66+ break ;
67+
68+ case MessageBoxStatus . Ok :
69+ templateFile += "OkMessageBox.tpl" ;
70+ break ;
71+ }
72+
73+ var tpl = _templateFactory . Load ( templateFile ) ;
74+
75+ tpl . Set ( "Message" , text ) ;
76+ tpl . Set ( "Title" , string . IsNullOrEmpty ( title ) ? _stringTable . GetItem ( "FormTitleMessageBox" ) : title ) ;
77+
78+ _dataCollector . Add ( tpl . Get ( ) ) ;
79+ _dataCollector . AddTitle ( string . IsNullOrEmpty ( title ) ? _stringTable . GetItem ( "FormTitleMessageBox" ) : title ) ;
80+ }
81+
82+ /// <summary>
83+ ///Generate message box HTML and set to data collector
84+ /// </summary>
85+ /// <param name="stringTableItemName">Show message from string table item</param>
86+ /// <param name="status">Status of a message box</param>
87+ /// <param name="title">Title of a message box</param>
88+ public void ShowSt ( string stringTableItemName , MessageBoxStatus status = MessageBoxStatus . Error , string title = null )
89+ {
90+ Show ( _stringTable . GetItem ( stringTableItemName ) , status , title ) ;
91+ }
92+
93+ /// <summary>
94+ /// Get inline message box HTML
95+ /// </summary>
96+ /// <param name="text">Text of a message box</param>
97+ /// <param name="status">Status of a message box</param>
98+ /// <returns>Message box html</returns>
99+ public string GetInline ( string text , MessageBoxStatus status = MessageBoxStatus . Error )
100+ {
101+ if ( string . IsNullOrEmpty ( text ) )
102+ throw new ArgumentNullException ( nameof ( text ) ) ;
103+
104+ var templateFile = MessageBoxTemplatesPath ;
105+
106+ switch ( status )
107+ {
108+ case MessageBoxStatus . Information :
109+ templateFile += "InlineInfoMessageBox.tpl" ;
110+ break ;
111+
112+ case MessageBoxStatus . Error :
113+ templateFile += "InlineErrorMessageBox.tpl" ;
114+ break ;
115+
116+ case MessageBoxStatus . Ok :
117+ templateFile += "InlineOkMessageBox.tpl" ;
118+ break ;
119+ }
120+
121+ var tpl = _templateFactory . Load ( templateFile ) ;
122+
123+ tpl . Set ( "Message" , text ) ;
124+
125+ return tpl . Get ( ) ;
126+ }
127+
128+ /// <summary>
129+ /// Get inline message box HTML
130+ /// </summary>
131+ /// <param name="stringTableItemName">Show message from string table item</param>
132+ /// <param name="status">Status of a message box</param>
133+ /// <returns>Message box html</returns>
134+ public string GetInlineSt ( string stringTableItemName , MessageBoxStatus status = MessageBoxStatus . Error )
135+ {
136+ return GetInline ( _stringTable . GetItem ( stringTableItemName ) , status ) ;
137+ }
138+ }
139+ }
0 commit comments