Exemplo n.º 1
0
        private static void WriteJsonData <TModelRecord>(
            HttpResponseBase response,
            RoboUIGridRequest request,
            RoboUIGridAjaxData <TModelRecord> data,
            int totalRecords,
            IRoboUIFormProvider formProvider,
            IEnumerable <RoboUIGridColumn> columns,
            IEnumerable <IRoboUIGridRowAction> rowActions,
            Func <TModelRecord, object> getModelId,
            bool isSubGrid,
            bool isTreeGrid,
            Func <TModelRecord, dynamic> getParentId,
            Func <TModelRecord, bool> hasChildren)
        {
            using (var writer = new JsonTextWriter(response.Output)
            {
                Formatting = Formatting.None
            })
            {
                writer.WriteStartObject();

                writer.WritePropertyName("page");
                writer.WriteValue(request.PageIndex);

                writer.WritePropertyName("records");
                writer.WriteValue(totalRecords);

                writer.WritePropertyName("total");
                writer.WriteValue((int)Math.Ceiling((totalRecords * 1d) / request.PageSize));

                if (data.Callbacks.Count > 0)
                {
                    writer.WritePropertyName("callback");
                    writer.WriteValue(string.Join("", data.Callbacks));
                }

                writer.WritePropertyName("rows");

                writer.WriteStartArray();

                var needWriteValueDelimiter = false;
                foreach (TModelRecord item in data)
                {
                    var jsonObject = new JObject {
                        { "_id", Convert.ToString(getModelId(item)) }
                    };

                    foreach (var column in columns)
                    {
                        jsonObject.Add(column.PropertyName, Convert.ToString(column.BuildHtml(item)));
                    }

                    if (isTreeGrid)
                    {
                        jsonObject.Add("_level", request.NodeLevel + 1);
                        jsonObject.Add("_parentId", getParentId(item));
                        jsonObject.Add("_isLeaf", !hasChildren(item));
                        jsonObject.Add("_isExpanded", false);
                    }

                    if (rowActions.Any())
                    {
                        var sb = new StringBuilder();
                        sb.Append("<table style=\"margin-left: auto; margin-right: auto; border: none; padding: 0;\">");
                        sb.Append("<tr>");

                        foreach (var action in rowActions)
                        {
                            if (!action.IsVisible(item))
                            {
                                continue;
                            }

                            var enabled    = action.IsEnabled(item);
                            var attributes = new RouteValueDictionary(action.GetAttributes(item));

                            sb.Append("<td style=\"border: none; background-color: transparent; padding: 0 2px;\">");
                            if (action.IsSubmitButton)
                            {
                                var value = action.GetValue(item);

                                var cssClass =
                                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                                     formProvider.GetButtonStyleCssClass(action.ButtonStyle) + " " + action.CssClass).Trim();

                                if (!string.IsNullOrEmpty(cssClass))
                                {
                                    attributes.Add("class", cssClass);
                                }

                                if (!enabled)
                                {
                                    attributes.Add("disabled", "disabled");
                                }

                                if (!string.IsNullOrEmpty(action.ClientClickCode))
                                {
                                    attributes.Add("onclick", action.ClientClickCode);
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(action.ConfirmMessage))
                                    {
                                        attributes.Add("onclick", string.Format("return confirm('{0}');", action.ConfirmMessage));
                                    }
                                }

                                attributes.Add("id", "btn" + Guid.NewGuid().ToString("N").ToLowerInvariant());
                                attributes.Add("style", "white-space: nowrap;");

                                var tagBuilder = new TagBuilder("button");
                                tagBuilder.MergeAttribute("type", "submit");
                                tagBuilder.MergeAttribute("name", action.Name);
                                tagBuilder.InnerHtml = action.Text;
                                tagBuilder.MergeAttribute("value", Convert.ToString(value));
                                tagBuilder.MergeAttributes(attributes, true);
                                sb.Append(tagBuilder.ToString(TagRenderMode.Normal));
                            }
                            else
                            {
                                var href = action.GetUrl(item);

                                var cssClass =
                                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                                     formProvider.GetButtonStyleCssClass(action.ButtonStyle) + " " + action.CssClass).Trim();

                                if (!string.IsNullOrEmpty(cssClass))
                                {
                                    if (enabled)
                                    {
                                        attributes.Add("class", cssClass);
                                    }
                                    else
                                    {
                                        attributes.Add("class", cssClass + " disabled");
                                    }
                                }
                                else
                                {
                                    if (!enabled)
                                    {
                                        attributes.Add("class", "disabled");
                                    }
                                }

                                attributes.Add("style", "white-space: nowrap;");

                                if (action.IsShowModalDialog && enabled)
                                {
                                    attributes.Add("data-toggle", "fancybox");
                                    attributes.Add("data-fancybox-type", "iframe");
                                    attributes.Add("data-fancybox-width", action.ModalDialogWidth);
                                }

                                var tagBuilder = new TagBuilder("a");
                                if (enabled)
                                {
                                    tagBuilder.MergeAttribute("href", href ?? "javascript:void(0)");
                                }
                                else
                                {
                                    tagBuilder.MergeAttribute("href", "javascript:void(0)");
                                }
                                tagBuilder.InnerHtml = action.Text;
                                tagBuilder.MergeAttributes(attributes, true);
                                sb.Append(tagBuilder.ToString(TagRenderMode.Normal));
                            }
                            sb.Append("</td>");
                        }

                        sb.Append("</tr>");
                        sb.Append("</table>");

                        jsonObject.Add("_RowActions", sb.ToString());
                    }
                    else
                    {
                        jsonObject.Add("_RowActions", null);
                    }

                    if (needWriteValueDelimiter)
                    {
                        writer.WriteRaw(",");
                    }

                    writer.WriteRaw(jsonObject.ToString());
                    needWriteValueDelimiter = true;
                }

                if (isSubGrid && data.UserData.ContainsKey("_RowActions"))
                {
                    if (needWriteValueDelimiter)
                    {
                        writer.WriteRaw(",");
                    }

                    writer.WriteStartObject();
                    writer.WritePropertyName("_RowActions");
                    writer.WriteValue(data.UserData["_RowActions"]);
                    writer.WriteEndObject();

                    data.UserData.Remove("_RowActions");
                }

                writer.WriteEndArray();

                if (data.UserData.Count > 0)
                {
                    writer.WritePropertyName("userdata");

                    writer.WriteStartObject();

                    foreach (var item in data.UserData)
                    {
                        writer.WritePropertyName(item.Key);
                        writer.WriteValue(item.Value);
                    }

                    writer.WriteEndObject();
                }

                writer.WriteEndObject();
                writer.Flush();
            }
        }
Exemplo n.º 2
0
        private static void BuildAction <TModelRecord>(StringBuilder sb, TModelRecord item, IRoboUIGridRowAction action, IRoboUIFormProvider formProvider, bool addLiTag)
        {
            if (!action.IsVisible(item))
            {
                return;
            }

            var enabled    = action.IsEnabled(item);
            var attributes = new RouteValueDictionary(action.GetAttributes(item));

            if (addLiTag)
            {
                sb.Append("<li>");
            }

            if (action.ChildActions.Count > 0)
            {
                var dropdownId = Guid.NewGuid().ToString();

                sb.AppendFormat("<div id=\"dropdown-content-{0}\" style=\"display: none;\">", dropdownId);

                foreach (var childAction in action.ChildActions)
                {
                    BuildAction(sb, item, childAction, formProvider, false);
                }

                sb.Append("</div>");

                var cssClass =
                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                     formProvider.GetButtonStyleCssClass(action.ButtonStyle)).Trim();

                sb.AppendFormat("<button id=\"dropdown-button-{2}\" type=\"button\" class=\"{0}\" data-container=\"body\" data-toggle=\"popover\" data-placement=\"bottom\" data-html=\"true\" data-trigger=\"focus\" data-template=\"<div class='popover popover-dropdown' role='tooltip'><div class='arrow'></div><div class='popover-content'></div></div>\" onclick=\"if(!$('#dropdown-button-{2}').data('bs.popover')){{ $('#dropdown-button-{2}').popover({{ content: function(){{ return $('#dropdown-content-{2}').html(); }} }}).popover('show'); }}\">{1} <span class=\"caret\"></span></button>", cssClass, action.Text, dropdownId);

                return;
            }

            if (action.IsSubmitButton)
            {
                var value = action.GetValue(item);

                var cssClass =
                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                     formProvider.GetButtonStyleCssClass(action.ButtonStyle) + " " + action.CssClass).Trim();

                if (!string.IsNullOrEmpty(cssClass))
                {
                    attributes.Add("class", cssClass);
                }

                if (!enabled)
                {
                    attributes.Add("disabled", "disabled");
                }

                if (!string.IsNullOrEmpty(action.ClientClickCode))
                {
                    attributes.Add("onclick", action.ClientClickCode);
                }
                else
                {
                    if (!string.IsNullOrEmpty(action.ConfirmMessage))
                    {
                        attributes.Add("onclick", string.Format("return confirm('{0}');", action.ConfirmMessage));
                    }
                }

                attributes.Add("id", "btn" + Guid.NewGuid().ToString("N").ToLowerInvariant());
                attributes.Add("style", "white-space: nowrap;");

                var tagBuilder = new TagBuilder("button");
                tagBuilder.MergeAttribute("type", "submit");
                tagBuilder.MergeAttribute("name", action.Name);
                tagBuilder.InnerHtml.AppendHtml(action.Text);
                tagBuilder.MergeAttribute("value", Convert.ToString(value));
                tagBuilder.MergeAttributes(attributes, true);
                sb.Append(tagBuilder.ToHtmlString());
            }
            else
            {
                var href = action.GetUrl(item);

                var cssClass =
                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                     formProvider.GetButtonStyleCssClass(action.ButtonStyle) + " " + action.CssClass).Trim();

                if (!string.IsNullOrEmpty(cssClass))
                {
                    if (enabled)
                    {
                        attributes.Add("class", cssClass);
                    }
                    else
                    {
                        attributes.Add("class", cssClass + " disabled");
                    }
                }
                else
                {
                    if (!enabled)
                    {
                        attributes.Add("class", "disabled");
                    }
                }

                attributes.Add("style", "white-space: nowrap;");

                if (action.IsShowModalDialog && enabled)
                {
                    attributes.Add("data-toggle", "fancybox");
                    attributes.Add("data-fancybox-type", "iframe");
                    attributes.Add("data-fancybox-width", action.ModalDialogWidth);
                }

                var tagBuilder = new TagBuilder("a");
                if (enabled)
                {
                    tagBuilder.MergeAttribute("href", href ?? "javascript:void(0)");
                }
                else
                {
                    tagBuilder.MergeAttribute("href", "javascript:void(0)");
                }
                tagBuilder.InnerHtml.AppendHtml(action.Text);
                tagBuilder.MergeAttributes(attributes, true);
                sb.Append(tagBuilder.ToHtmlString());
            }

            if (addLiTag)
            {
                sb.Append("</li>");
            }
        }