Esempio n. 1
0
        public static MvcHtmlString TreeViewFor <VM, TItem>(
            this HtmlHelper <VM> htmlHelper,
            Expression <Func <VM, IEnumerable <TItem> > > expression,
            Func <int, string> collection,
            ExternalContainerType itemContainer = ExternalContainerType.span,
            string rootClassDisplay             = null,
            object[] displayTemplates           = null,
            Func <object, int, int> itemTemplateSelectorDisplay = null,
            string rootClassEdit   = null,
            object[] editTemplates = null,
            Func <object, int, int> itemTemplateSelectorEdit = null,
            TreeViewMode mode = TreeViewMode.InitializeDisplay,
            Func <int, string> itemClassSelector = null,
            Func <object, int, TreeViewItemStatus> itemStatus = null,
            TreeViewOptions treeViewOptions = null)
        {
            if (expression == null)
            {
                throw(new ArgumentNullException("expression"));
            }
            if (collection == null)
            {
                throw(new ArgumentNullException("collection"));
            }
            if ((displayTemplates == null || displayTemplates.Length == 0) &&
                (editTemplates == null || editTemplates.Length == 0))
            {
                throw (new ArgumentNullException("displayTemplates editTemplates"));
            }

            return(TreeViewFor
                       (htmlHelper,
                       htmlHelper.ExtractFromModel(expression),
                       collection,
                       itemContainer,
                       rootClassDisplay,
                       displayTemplates,
                       itemTemplateSelectorDisplay,
                       rootClassEdit,
                       editTemplates,
                       itemTemplateSelectorEdit,
                       mode,
                       itemClassSelector,
                       itemStatus,
                       treeViewOptions));
        }
Esempio n. 2
0
        public static MvcHtmlString TreeViewFor <VM, TItem>(
            this HtmlHelper <VM> htmlHelper,
            RenderInfo <IEnumerable <TItem> > renderInfo,
            Func <int, string> collectionName,
            ExternalContainerType itemContainer = ExternalContainerType.span,
            string rootClassDisplay             = null,
            object[] displayTemplates           = null,
            Func <object, int, int> itemTemplateSelectorDisplay = null,
            string rootClassEdit   = null,
            object[] editTemplates = null,
            Func <object, int, int> itemTemplateSelectorEdit = null,
            TreeViewMode mode = TreeViewMode.InitializeDisplay,
            Func <int, string> itemClassSelector = null,
            Func <object, int, TreeViewItemStatus> itemStatus = null,
            TreeViewOptions treeViewOptions = null)
        {
            if (renderInfo == null)
            {
                throw(new ArgumentNullException("renderInfo"));
            }
            if (collectionName == null)
            {
                throw(new ArgumentNullException("collectionName"));
            }
            if ((displayTemplates == null || itemTemplateSelectorDisplay == null || displayTemplates.Length == 0) &&
                (editTemplates == null || itemTemplateSelectorEdit == null || editTemplates.Length == 0))
            {
                throw (new ArgumentNullException("displayTemplates/itemTemplateSelectorDisplay or editTemplates/itemTemplateSelectorEdit"));
            }
            if (itemStatus == null)
            {
                itemStatus = (mbox, i) => TreeViewItemStatus.initializeShow;
            }
            if (treeViewOptions == null)
            {
                treeViewOptions = new TreeViewOptions();
            }

            if (MvcEnvironment.Validation(htmlHelper) == ValidationType.StandardClient)
            {
                treeViewOptions.CanAdd = false;
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(renderInfo.PartialRendering);

            RenderInfo <IEnumerable <TItem> > renderInfoO = new RenderInfo <IEnumerable <TItem> >();

            renderInfoO.Model            = renderInfo.Model;
            renderInfoO.PartialPrefix    = renderInfo.PartialPrefix;
            renderInfoO.Prefix           = renderInfo.Prefix;
            renderInfoO.PartialRendering = renderInfo.PartialRendering;



            if (editTemplates == null || editTemplates.Length == 0)
            {
                TreeViewTop <VM, TItem>(
                    sb, false, htmlHelper, renderInfoO, collectionName, itemContainer,
                    rootClassDisplay, displayTemplates, itemTemplateSelectorDisplay,
                    itemClassSelector, itemStatus, treeViewOptions.Opacity, false, false, treeViewOptions);
            }
            else if (displayTemplates == null || displayTemplates.Length == 0)
            {
                TreeViewTop <VM, TItem>(
                    sb, true, htmlHelper, renderInfoO, collectionName, itemContainer,
                    rootClassEdit, editTemplates, itemTemplateSelectorEdit,
                    itemClassSelector, itemStatus, treeViewOptions.Opacity, treeViewOptions.CanMove, treeViewOptions.CanAdd, treeViewOptions);
            }
            else
            {
                bool isEdit = mode == TreeViewMode.Edit || mode == TreeViewMode.InitializeEdit;
                if (mode == TreeViewMode.InitializeEdit || mode == TreeViewMode.InitializeDisplay)
                {
                    IDictionary vars = htmlHelper.ViewContext.RequestContext.HttpContext.Items;
                    if (vars.Contains(renderInfo.Prefix))
                    {
                        isEdit = (bool)(vars[renderInfo.Prefix]);
                    }
                }
                string toggleScript = null;
                if (isEdit)
                {
                    toggleScript = string.Format(completeTreeScriptEdit,
                                                 BasicHtmlHelper.IdFromName(renderInfo.Prefix));
                }
                else
                {
                    toggleScript = string.Format(completeTreeScriptDisplay,
                                                 BasicHtmlHelper.IdFromName(renderInfo.Prefix));
                }

                RenderInfo <TwoWayChoice <IEnumerable <TItem> > > toRender = htmlHelper.InvokeTransform(renderInfo, new TwoWayChoice <IEnumerable <TItem> >());
                toRender.Model.IsChoice2 = isEdit;
                sb.Append(toRender.PartialRendering);
                sb.Append(htmlHelper.Hidden(BasicHtmlHelper.AddField(toRender.PartialPrefix, "IsChoice2"), toRender.Model.IsChoice2));
                renderInfoO.Model            = toRender.Model.Choice1;
                renderInfoO.PartialPrefix    = BasicHtmlHelper.AddField(toRender.PartialPrefix, "Choice1");
                renderInfoO.Prefix           = BasicHtmlHelper.AddField(toRender.Prefix, "Choice1");
                renderInfoO.PartialRendering = string.Empty;

                TreeViewTop <VM, TItem>(
                    sb, false, htmlHelper, renderInfoO, collectionName, itemContainer,
                    rootClassDisplay, displayTemplates, itemTemplateSelectorDisplay,
                    itemClassSelector, itemStatus, treeViewOptions.Opacity, false, false, treeViewOptions);


                renderInfoO.Model         = toRender.Model.Choice2;
                renderInfoO.PartialPrefix = BasicHtmlHelper.AddField(toRender.PartialPrefix, "Choice2");
                renderInfoO.Prefix        = BasicHtmlHelper.AddField(toRender.Prefix, "Choice2");


                TreeViewTop <VM, TItem>(
                    sb, true, htmlHelper, renderInfoO, collectionName, itemContainer,
                    rootClassEdit, editTemplates, itemTemplateSelectorEdit,
                    itemClassSelector, itemStatus, treeViewOptions.Opacity, treeViewOptions.CanMove, treeViewOptions.CanAdd, treeViewOptions);

                sb.Append(toggleScript);
            }

            return(MvcHtmlString.Create(sb.ToString()));
        }
Esempio n. 3
0
        private static void TreeViewTop <VM, T>(
            StringBuilder sb,
            bool editMode,
            HtmlHelper <VM> htmlHelper,
            RenderInfo <IEnumerable <T> > renderInfo,
            Func <int, string> collectionName,
            ExternalContainerType itemContainer,
            string rootClass,
            object[] itemTemplates,
            Func <object, int, int> itemTemplateSelector,
            Func <int, string> itemClassSelector,
            Func <object, int, TreeViewItemStatus> itemStatus,
            float opacity,
            bool canMove,
            bool canAdd,
            TreeViewOptions treeViewOptions)
        {
            if (rootClass == null)
            {
                rootClass = defaultRootClass;
            }


            RenderInfo <TreeViewDisplay <T> > branchesRenderInfo = htmlHelper.InvokeTransform(renderInfo, new TreeViewDisplay <T>());

            if (editMode)
            {
                sb.Append(branchesRenderInfo.PartialRendering);
            }

            renderInfo.Prefix        = BasicHtmlHelper.AddField(branchesRenderInfo.Prefix, "flattened");
            renderInfo.PartialPrefix = BasicHtmlHelper.AddField(branchesRenderInfo.PartialPrefix, "flattened");

            StringBuilder sbInit = new StringBuilder();

            sbInit.Append(treeViewOptions.Render(renderInfo.Prefix));
            string basic_id = BasicHtmlHelper.IdFromName(renderInfo.Prefix);
            int    res      = TreeViewRec <VM, T>(
                sb,
                sbInit,
                editMode,
                htmlHelper,
                renderInfo,
                collectionName,
                itemContainer,
                rootClass,
                itemTemplates,
                itemTemplateSelector,
                itemClassSelector,
                itemStatus,
                opacity,
                canMove,
                0,
                0,
                basic_id,
                basic_id);

            if (canAdd)
            {
                StringBuilder templatesId      = new StringBuilder();
                string        myTemplateSymbol = BasicHtmlHelper.GetUniqueSymbol(htmlHelper, templateSymbol);
                sb.AppendFormat("<div id='{0}_Templates'>", basic_id);
                int templateIndex = -1;
                IDictionary <string, object> htmlAttributesContainer = new Dictionary <string, object>();
                foreach (object template in itemTemplates)
                {
                    templateIndex++;
                    Type type = TemplateInvoker <string> .ExtractModelType(template);

                    string initCollection = collectionName(templateIndex);

                    ITreeViewNodeContainer wrapper =
                        typeof(TreeViewUpdater <string>).GetGenericTypeDefinition()
                        .MakeGenericType(new Type[] { type })
                        .GetConstructor(new Type[0])
                        .Invoke(new object[0]) as ITreeViewNodeContainer;


                    bool closed = false;
                    (wrapper as IUpdateModel).ImportFromModel(null, null, null, new object[] { closed });

                    string prefix        = renderInfo.Prefix;
                    string partialPrefix = renderInfo.PartialPrefix;
                    string updater       = BasicHtmlHelper.RenderUpdateInfoI(
                        htmlHelper, wrapper as IUpdateModel,
                        ref partialPrefix, new string[0], myTemplateSymbol + templateIndex.ToString());

                    prefix = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(partialPrefix);



                    wrapper.FatherOriginalId  = string.Empty;
                    wrapper.OriginalId        = prefix;
                    wrapper.PositionAsSon     = 0;
                    wrapper.SonNumber         = 0;
                    wrapper.SonCollectionName = initCollection;
                    wrapper.Closed            = false;
                    string itemOpenTag  = null;
                    string itemCloseTag = null;

                    string innerItemOpenTag  = null;
                    string innerItemCloseTag = null;

                    string templateId       = BasicHtmlHelper.IdFromName(BasicHtmlHelper.AddField(prefix, "Container"));
                    string templateUniqueId = templateId + templateIndex.ToString();
                    addTemplateId(templatesId, templateUniqueId);
                    htmlAttributesContainer["id"]    = templateId;
                    htmlAttributesContainer["class"] = closed ? "closed" : "open";
                    BasicHtmlHelper.GetContainerTags(ExternalContainerType.li, htmlAttributesContainer, out itemOpenTag, out itemCloseTag);

                    htmlAttributesContainer["id"]    = BasicHtmlHelper.IdFromName(BasicHtmlHelper.AddField(prefix, "$.Item_SubContainer"));
                    htmlAttributesContainer["class"] = string.Empty;
                    BasicHtmlHelper.GetContainerTags(itemContainer, htmlAttributesContainer, out innerItemOpenTag, out innerItemCloseTag);

                    bool hasCollection = true;

                    PropertyInfo property = null;
                    if (initCollection == null)
                    {
                        hasCollection = false;
                    }
                    else
                    {
                        property      = type.GetProperty(initCollection);
                        hasCollection = typeof(IEnumerable).IsAssignableFrom(property.PropertyType);
                    }
                    sb.Append(string.Format("<span id='{0}' style='display:none' class='MVCCT_EncodedTemplate'>", templateUniqueId));
                    StringBuilder itemSb = new StringBuilder();
                    itemSb.Append(itemOpenTag);

                    if (canMove && hasCollection)
                    {
                        itemSb.Append(string.Format("<input  type='checkbox' class ='level-select_{0} level-select' />", basic_id));
                    }
                    itemSb.Append(innerItemOpenTag);

                    itemSb.Append(
                        (typeof(TemplateInvoker <string>)
                         .GetGenericTypeDefinition()
                         .MakeGenericType(new Type[] { type })
                         .GetConstructor(new Type[] { typeof(object) })
                         .Invoke(new object[] { template }) as ITemplateInvoker)
                        .Invoke(htmlHelper, null, BasicHtmlHelper.AddField(prefix, "$.Item")));
                    itemSb.Append(innerItemCloseTag);

                    itemSb.Append(string.Format("<span class= 'MvcCT_init_info_{0}'>", basic_id));
                    itemSb.Append(updater);
                    RenderWrapper <VM>(htmlHelper, partialPrefix, wrapper, itemSb);
                    itemSb.Append("</span>");

                    if (hasCollection)
                    {
                        string base_id       = BasicHtmlHelper.IdFromName(prefix);
                        string currItemClass = itemClassSelector == null ?
                                               null :
                                               itemClassSelector(templateIndex);
                        if (currItemClass != null)
                        {
                            currItemClass = currItemClass + "_" + basic_id;
                        }
                        else
                        {
                            currItemClass = string.Empty;
                        }
                        string externalOpenTag  = null;
                        string externalCloseTag = null;
                        htmlAttributesContainer["id"] = base_id + "_ItemsContainer";

                        htmlAttributesContainer["class"] = currItemClass + " mvcct-items-container";

                        BasicHtmlHelper.GetContainerTags(ExternalContainerType.ul, htmlAttributesContainer, out externalOpenTag, out externalCloseTag);

                        itemSb.Append(externalOpenTag);
                        itemSb.Append(externalCloseTag);
                        if (canMove)
                        {
                            string javasctiptOpacity = string.Empty;
                            if (opacity > 1f)
                            {
                                opacity = 1f;
                            }
                            else if (opacity < 0.01f)
                            {
                                opacity = 0.01f;
                            }

                            if (opacity < 1f)
                            {
                                javasctiptOpacity = string.Format(" opacity: {0}, ", opacity.ToString(CultureInfo.InvariantCulture));
                            }
                            string javascriptRootClass = string.Empty;
                            if (currItemClass != null)
                            {
                                javascriptRootClass = string.Format(" connectWith: '.{0}', ", currItemClass);
                            }

                            itemSb.Append(string.Format(startScriptFormat,
                                                        base_id,
                                                        javasctiptOpacity,
                                                        javascriptRootClass,
                                                        basic_id));
                        }
                    }
                    itemSb.Append(itemCloseTag);
                    sb.Append(htmlHelper.Encode(itemSb.ToString()));
                    sb.Append("</span>");
                }
                sb.Append("</div>");
                sb.AppendFormat(
                    startTemplateScriptFormat,
                    basic_id, canMove ? "true" : "false",
                    myTemplateSymbol,
                    templatesId.ToString());
            }
            if (editMode)
            {
                sbInit.Append(htmlHelper.Hidden(BasicHtmlHelper.AddField(renderInfo.PartialPrefix, "$.ItemsCount"),
                                                res).ToString());
            }
            if (canMove)
            {
                sbInit.Append(string.Format(levelSelection, basic_id));
            }
            sb.Append(sbInit.ToString());
        }