/// <summary>
        /// Returns horizontal display item of a model value with caption truncated by the given length and use bootstrap pop-over to display the complete value
        /// </summary>
        /// <param name="expression">Lambda expression for the property</param>
        /// <param name="spanOf12">Number represents bootstrap column span for the item width</param>
        /// <param name="textMaxLength">Maximum length of the displayed text</param>
        /// <param name="popOverTitle">The pop-over title text</param>
        /// <param name="seeMoreHtml">html text for see more if the text is truncated</param>
        /// <param name="showAllContentInPopover">True if you want to show the complete value in the pop-over. False to display the rest of the value only</param>
        /// <param name="htmlAttributes">custom htmlAttributes applied to the value tag</param>
        /// <param name="postBackValue">If true, value will be posted back in hidden input field</param>
        /// <returns>HtmlString</returns>
        public static IHtmlContent DisplayFormItemWithTruncateFor_H <TModel, TProperty>(
            this IHtmlHelper <TModel> helper,
            Expression <Func <TModel, TProperty> > expression, int spanOf12 = 6,
            int textMaxLength     = 50, string popOverTitle  = "", string seeMoreHtml       = " المزيد >>", bool showAllContentInPopover = false,
            object htmlAttributes = null, bool postBackValue = false, TruncateMethod method = TruncateMethod.Popover)
        {
            var expresionProvider = helper.ViewContext.HttpContext.RequestServices
                                    .GetService(typeof(ModelExpressionProvider)) as ModelExpressionProvider;
            string name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expresionProvider.GetExpressionText(expression));

            HtmlContentBuilder htmlContentBuilder = new HtmlContentBuilder();

            var col       = new ItemsColumnTagBuilder(spanOf12, null);
            var frmGroup  = new FormDisplayTagBuilder();
            var valueSpan = new SpanTagBuilder(name, htmlAttributes);

            valueSpan.InnerHtml.AppendHtml(helper.DisplayWithTruncateFor(expression, textMaxLength, popOverTitle, seeMoreHtml, showAllContentInPopover, method));

            htmlContentBuilder.AppendHtml(col.RenderStartTag())
            .AppendHtml(frmGroup.RenderStartTag())
            .AppendHtml(helper.LabelFor(expression));

            htmlContentBuilder.AppendHtml(valueSpan);

            if (postBackValue)
            {
                htmlContentBuilder.AppendHtml(helper.HiddenFor(expression));
            }

            htmlContentBuilder.AppendHtml(frmGroup.RenderEndTag())
            .AppendHtml(col.RenderEndTag());

            return(htmlContentBuilder);
        }
        /// <summary>
        /// Returns display tag of a value truncated by the given length and use bootstrap pop-over to display the complete value
        /// </summary>
        /// <param name="name">Unique name for the display tag</param>
        /// <param name="value">Value to display</param>
        /// <param name="textMaxLength">Maximum length of the displayed text</param>
        /// <param name="popOverTitle">The pop-over title text</param>
        /// <param name="seeMoreHtml">html text for see more if the text is truncated</param>
        /// <param name="showAllContentInPopover">True if you want to show the complete value in the pop-over. False to display the rest of the value only</param>
        /// <returns>HtmlString</returns>
        public static HtmlContentBuilder DisplayWithTruncate <TModel>(
            this IHtmlHelper <TModel> helper, string name, string value, int textMaxLength = 50, string popOverTitle = "", string seeMoreHtml = " المزيد >>",
            bool showAllContentInPopover = false, TruncateMethod method = TruncateMethod.Popover)
        {
            HtmlContentBuilder result = new HtmlContentBuilder();

            var needTruncate = value.Length > textMaxLength;

            if (needTruncate)//compute real max length with space
            {
                string seperators = " .,-_،()[]&!";
                if (!seperators.Contains(value[textMaxLength]) && !seperators.Contains(value[textMaxLength - 1]))
                {
                    textMaxLength = value.Substring(0, textMaxLength).LastIndexOfAny(seperators.ToCharArray());
                }
            }

            string toDisplayText = needTruncate ? value.Substring(0, textMaxLength) + " ... " : value;
            string popoverText   = value;

            if (needTruncate && !showAllContentInPopover)
            {
                popoverText = "..." + value.Substring(textMaxLength, value.Length - textMaxLength);
            }

            result.Append(toDisplayText);

            if (needTruncate)
            {
                string truncateHtml = "";
                if (method == TruncateMethod.Popover)
                {
                    truncateHtml  = string.Format("<a id='{0}_popoverDiv' class='mci-popover' tabindex='0' data-toggle='popover' data-placement='bottom auto' data-trigger='focus' data-container='body' data-title='{1}' data-content='{2}'>{3}</a>", name, popOverTitle, popoverText, seeMoreHtml);
                    truncateHtml += string.Format(@"
                            <script>
                                $(function () {{
                                    $('#{0}_popoverDiv').popover();
                                }});
                            </script>", name);
                }
                else
                {
                    truncateHtml =
                        string.Format(
                            "<a id='{0}_popoverDiv' class='mci-popover' tabindex='0' data-toggle='modal' data-title='{1}' data-content='{2}'>{3}</a>",
                            name, popOverTitle, popoverText, seeMoreHtml);
                }
                result.AppendHtml(truncateHtml);
            }
            return(result);
        }
 public static float truncate(float number, TruncateMethod tm, int decimals = 0)
 {
     if (tm == TruncateMethod.ROUND)
     {
         return(round(number, decimals));
     }
     else if (tm == TruncateMethod.FLOOR)
     {
         return(floor(number, decimals));
     }
     else if (tm == TruncateMethod.CEIL)
     {
         return(ceil(number, decimals));
     }
     else
     {
         throw new ApplicationException("Unknown TruncateMethod");
     }
 }
Exemple #4
0
 public Truncate(long number, TruncateMethod method)
 {
     this.numberString = number.ToString();
       this.method = method;
 }
        /// <summary>
        /// Returns horizontal display item of a value with caption truncated by the given length and use bootstrap pop-over to display the complete value
        /// </summary>
        /// <param name="name">Unique name for the display tag</param>
        /// <param name="value">Value to display</param>
        /// <param name="labelText">Item caption text</param>
        /// <param name="spanOf12">Number represents bootstrap column span for the item width</param>
        /// <param name="textMaxLength">Maximum length of the displayed text</param>
        /// <param name="popOverTitle">The pop-over title text</param>
        /// <param name="seeMoreHtml">html text for see more if the text is truncated</param>
        /// <param name="showAllContentInPopover">True if you want to show the complete value in the pop-over. False to display the rest of the value only</param>
        /// <param name="htmlAttributes">custom htmlAttributes applied to the value tag</param>
        /// <param name="postBackValue">If true, value will be posted back in hidden input field</param>
        /// <returns>HtmlString</returns>
        public static IHtmlContent DisplayFormItemWithTruncate_H <TModel>(
            this IHtmlHelper <TModel> helper, string value, string labelText = "", int spanOf12 = 6,
            int textMaxLength     = 50, string popOverTitle  = "", string seeMoreHtml = " المزيد >>", bool showAllContentInPopover = false,
            object htmlAttributes = null, bool postBackValue = false, string name     = "postValue", TruncateMethod method         = TruncateMethod.Popover)
        {
            HtmlContentBuilder htmlContentBuilder = new HtmlContentBuilder();

            var col       = new ItemsColumnTagBuilder(spanOf12, null);
            var frmGroup  = new FormDisplayTagBuilder();
            var valueSpan = new SpanTagBuilder(name, value, htmlAttributes);

            valueSpan.InnerHtml.AppendHtml(helper.DisplayWithTruncate(name, value, textMaxLength, popOverTitle, seeMoreHtml, showAllContentInPopover, method));

            htmlContentBuilder.AppendHtml(col.RenderStartTag())
            .AppendHtml(frmGroup.RenderStartTag())
            .AppendHtml(helper.Label(labelText));

            htmlContentBuilder.AppendHtml(valueSpan);

            if (postBackValue)
            {
                htmlContentBuilder.AppendHtml(helper.Hidden(name, value));
            }

            htmlContentBuilder.AppendHtml(frmGroup.RenderEndTag())
            .AppendHtml(col.RenderEndTag());

            return(htmlContentBuilder);
        }
        /// <summary>
        /// Returns display tag of a value truncated by the given length and use bootstrap pop-over to display the complete value
        /// </summary>
        /// <param name="expression">Lambda expression for the property</param>
        /// <param name="textMaxLength">Maximum length of the displayed text</param>
        /// <param name="popOverTitle">The pop-over title text</param>
        /// <param name="seeMoreHtml">html text for see more if the text is truncated</param>
        /// <param name="showAllContentInPopover">True if you want to show the complete value in the pop-over. False to display the rest of the value only</param>
        /// <returns>HtmlString</returns>
        public static HtmlContentBuilder DisplayWithTruncateFor <TModel, TProperty>(
            this IHtmlHelper <TModel> helper, Expression <Func <TModel, TProperty> > expression,
            int textMaxLength = 50, string popOverTitle = "", string seeMoreHtml = " المزيد >>", bool showAllContentInPopover = false, TruncateMethod method = TruncateMethod.Popover)
        {
            var expresionProvider = helper.ViewContext.HttpContext.RequestServices
                                    .GetService(typeof(ModelExpressionProvider)) as ModelExpressionProvider;

            string       name  = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expresionProvider.GetExpressionText(expression));
            IHtmlContent value = helper.DisplayFor(expression);

            HtmlContentBuilder result = helper.DisplayWithTruncate(name, value.GetString(), textMaxLength, popOverTitle, seeMoreHtml, showAllContentInPopover, method);

            return(result);
        }
        public static string floatToString(float number, TruncateMethod tm, int decimals)
        {
            if (decimals < 0)
            {
                throw new ApplicationException("decimals cannot be negative");
            }
            else if (decimals == 0)
            {
                return(truncate(number, tm).ToString());
            }
            else if (number == 0f)
            {
                char[] ret = new char[decimals + 2];
                ret[0] = '0';
                ret[1] = '.';
                for (int i = 0; i < decimals; i++)
                {
                    ret[i + 2] = '0';
                }
                return(new String(ret));
            }
            else if (number < 0f)
            {
                return("-" + floatToString(-number, tm, decimals));                // TODO string joining
            }
            else
            {
                float power = Mathf.Pow(10f, decimals);

                float poweredFloat = truncate(number * power, tm, 0);
                int   poweredInt   = (int)poweredFloat;

                string str    = poweredInt.ToString();
                int    strLen = str.Length;

                int    retLen = Mathf.Max(strLen + 1, decimals + 2);
                char[] ret    = new char[retLen];

                int dotIndex = retLen - 1 - decimals;

                for (int j = 0; j < retLen; j++)
                {
                    char ch;
                    if (j < dotIndex)
                    {
                        int index = strLen - retLen + j + 1;
                        ch = index >= 0 ? str[index] : '0';
                    }
                    else if (j == dotIndex)
                    {
                        ch = '.';
                    }
                    else
                    {
                        int index = strLen - retLen + j;
                        ch = index >= 0 ? str[index] : '0';
                    }
                    ret[j] = ch;
                }

                return(new String(ret));
            }
        }
Exemple #8
0
 public Truncate(long number, TruncateMethod method)
 {
     this.numberString = number.ToString();
     this.method       = method;
 }