Пример #1
0
        public IEncodedString EmailButton(string linkText, string link, IDictionary <string, string> tableAttributes = null, IDictionary <string, string> ahrefAttributes = null)
        {
            var ahrefAttrStr = string.Empty;
            var tableAttrStr = string.Empty;

            if (ahrefAttributes == null)
            {
                ahrefAttributes = new Dictionary <string, string>
                {
                    { "style", "text-decoration:none" }
                };
            }
            ahrefAttrStr = UtilHelper.ConvertDictionaryToString(ahrefAttributes);

            if (tableAttributes == null)
            {
                tableAttributes = new Dictionary <string, string>
                {
                    { "class", "button-dark" }
                };
            }
            tableAttrStr = UtilHelper.ConvertDictionaryToString(tableAttributes);

            var result = string.Format("<table {2}><tr><td><a {3} href=\"{0}\">{1}</a></td></tr></table>", link, linkText, tableAttrStr, ahrefAttrStr);

            return(new RawString(result));
        }
Пример #2
0
        /// <summary>
        /// {{emailButton 'linkText' (url 'action' 'controller' ...look at url helper) (object class='bold') (object key=value)}}
        /// tableAttributes, ahrefAttributes are optional
        /// </summary>
        /// <return>
        /// <a></a>
        /// </return>
        public void RegisterEmailButton_Helper()
        {
            _hbsService.RegisterHelper("emailButton", (output, context, arguments) =>
            {
                if (arguments.Length < 2 || arguments.Length > 4)
                {
                    throw new HandlebarsException("{{emailLink}} helper must have at least 2 arguments with maximum 4 arguments");
                }

                var linkText = arguments[0].ToString();
                var link     = arguments[1].ToString();
                var initialTableAttributes = arguments.Length > 2 ? arguments[2] as Dictionary <string, object> : null;
                var initialahrefAttributes = arguments.Length > 3 ? arguments[3] as Dictionary <string, object> : null;

                var ahrefAttrStr = string.Empty;
                var tableAttrStr = string.Empty;

                var tableAttributes = CastDictionary(initialTableAttributes);
                var ahrefAttributes = CastDictionary(initialahrefAttributes);

                if (tableAttributes == null && initialTableAttributes != null)
                {
                    throw new ArgumentException("Couldn't Cast Table Attributes. @{emailButton}");
                }

                if (ahrefAttributes == null && initialahrefAttributes != null)
                {
                    throw new ArgumentException("Couldn't Cast ahref Attributes. @{emailButton}");
                }

                if (ahrefAttributes == null)
                {
                    ahrefAttributes = new Dictionary <string, string>
                    {
                        { "style", "text-decoration:none" }
                    };
                }

                ahrefAttrStr = UtilHelper.ConvertDictionaryToString(ahrefAttributes);

                if (tableAttributes == null)
                {
                    tableAttributes = new Dictionary <string, string>
                    {
                        { "class", "button-dark" }
                    };
                }
                tableAttrStr = UtilHelper.ConvertDictionaryToString(tableAttributes);

                var result = string.Format("<table {2}><tr><td><a {3} href=\"{0}\">{1}</a></td></tr></table>", link, linkText, tableAttrStr, ahrefAttrStr);
                output.WriteSafeString(result);
            });
        }
Пример #3
0
        public IEncodedString LabelFor <TValue>(Expression <Func <TModel, TValue> > expression, object htmlAttributes = null, string labelText = null)
        {
            var resolvedLabelText = labelText ?? UtilHelper.GetPropertyDisplayName(expression);
            var propName          = UtilHelper.GetPropertyName(expression);
            var htmlAttributesStr = string.Empty;

            if (String.IsNullOrEmpty(resolvedLabelText))
            {
                return(new HtmlEncodedString(string.Empty));
            }
            if (htmlAttributes != null)
            {
                var htmlAttributesDict = UtilHelper.ObjectToDictionary(htmlAttributes);
                htmlAttributesStr = UtilHelper.ConvertDictionaryToString(htmlAttributesDict);
            }
            var tag = string.Format("<label {2}for=\"{0}\">{1}</label>", propName, HttpUtility.HtmlEncode(resolvedLabelText), htmlAttributesStr);

            return(new RawString(tag));
        }
Пример #4
0
        /// <summary>
        /// Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, route values as a route value dictionary, and HTML attributes as a dictionary.
        /// </summary>
        /// <param name="linkText">The inner text of the anchor element.</param>
        /// <param name="actionName">The name of the action.</param>
        /// <param name="controllerName">The name of the controller.</param>
        /// <param name="protocol">The protocol for the URL, such as "http" or "https".</param>
        /// <param name="hostName">The host name for the URL.</param>
        /// <param name="routeValues">Dictionary that contains the parameters for a route.</param>
        /// <param name="htmlAttributes">Dictionary that contains the HTML attributes to set for the element.</param>
        /// <returns>An anchor element (a element).</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public IEncodedString ActionLink(string linkText, string actionName, string controllerName, string protocol, string hostName, IDictionary <string, string> routeValues, IDictionary <string, string> htmlAttributes)
        {
            if (string.IsNullOrWhiteSpace(actionName))
            {
                throw new ArgumentNullException("actionName");
            }

            if (string.IsNullOrWhiteSpace(controllerName))
            {
                throw new ArgumentNullException("controllerName");
            }

            //var uriBuilder = new UriBuilder(protocol ?? _viewSettings.Protocol, hostName?? _viewSettings.Hostname);
            Uri uri = null;

            if (routeValues != null && routeValues.Count != 0)
            {
                routeValues.Add("action", actionName);
                routeValues.Add("controller", controllerName);

                uri = UtilHelper.BuildURI(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname, _viewSettings.UrlPattern, routeValues);
            }
            else
            {
                var uriBuilder = new UriBuilder(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname)
                {
                    Path = controllerName + "/" + actionName
                };
                uri = uriBuilder.Uri;
            }

            if (linkText == null)
            {
                linkText = uri.ToString();
            }

            var htmlAttrStr = UtilHelper.ConvertDictionaryToString(htmlAttributes);
            var link        = string.Format("<a {2}href=\"{0}\">{1}</a>", uri.ToString(), HttpUtility.HtmlEncode(linkText), htmlAttrStr);

            return(new RawString(link));
        }
Пример #5
0
        /// <summary>
        /// {{actionLink 'linkText' 'action' 'controller' (object key=value) (object key=value) 'protocol' 'host'}}
        /// </summary>
        /// <return>
        /// <a href=protocol://host/controller/action?routeValues>linkText</a>
        /// </return>
        public void RegisterActionLink_Helper()
        {
            _hbsService.RegisterHelper("actionLink", (output, context, arguments) =>
            {
                if (arguments.Length < 3 || arguments.Length > 7)
                {
                    throw new HandlebarsException("{{actionLink}} helper must have at least 3 arguments with maximum 7 arguments");
                }

                var linkText              = arguments.At <string>(0);
                var actionName            = arguments.At <string>(1);
                var controllerName        = arguments.At <string>(2);
                var initialRouteValues    = arguments.Length > 3 ? arguments.At <Dictionary <string, object> >(3) : null;
                var initialHtmlAttributes = arguments.Length > 4 ? arguments.At <Dictionary <string, object> >(4) : null;
                var protocol              = arguments.Length > 5 ? arguments.At <string>(5) : null;
                var hostName              = arguments.Length > 6 ? arguments.At <string>(6) : null;

                if (string.IsNullOrWhiteSpace(actionName))
                {
                    throw missingParameterException("actionLink", "actionName");
                }

                if (string.IsNullOrWhiteSpace(controllerName))
                {
                    throw missingParameterException("actionLink", "controllerName");
                }

                var routeValues = CastDictionary(initialRouteValues);
                if (initialRouteValues != null && routeValues == null)
                {
                    throw new ArgumentException("Couldn't Cast RouteValues. @{actionLink}");
                }

                var htmlAttributes = CastDictionary(initialHtmlAttributes);
                if (initialHtmlAttributes != null && htmlAttributes == null)
                {
                    throw new ArgumentException("Couldn't Cast HtmlAttributes. @{actionLink}");
                }


                Uri uri = null;
                if (routeValues != null && routeValues.Count != 0)
                {
                    routeValues.Add("action", actionName);
                    routeValues.Add("controller", controllerName);

                    uri = UtilHelper.BuildURI(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname, _viewSettings.UrlPattern, routeValues);
                }
                else
                {
                    var uriBuilder = new UriBuilder(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname)
                    {
                        Path = controllerName + "/" + actionName
                    };
                    uri = uriBuilder.Uri;
                }

                if (linkText == null)
                {
                    linkText = uri.ToString();
                }

                var htmlAttrStr = UtilHelper.ConvertDictionaryToString(htmlAttributes);
                var link        = string.Format("<a {2}href=\"{0}\">{1}</a>", uri.ToString(), HttpUtility.HtmlEncode(linkText), htmlAttrStr);

                output.WriteSafeString(link);
            });
        }