示例#1
0
 public ActionBase GetAction(string actionName)
 {
     return(Actions.FirstOrDefault(a => a.Name == actionName) ?? PinnedActions.FirstOrDefault(a => a.Name == actionName));
 }
示例#2
0
        internal PersistentObject(Client client, JObject model)
            : base(client, model)
        {
            if (model.TryGetValue("attributes", out var attributesToken))
            {
                var attributes = (JArray)attributesToken;
                Attributes = attributes.Select(jAttr =>
                {
                    if (jAttr["lookup"] != null)
                    {
                        return(new PersistentObjectAttributeWithReference(client, (JObject)jAttr, this));
                    }

                    if (jAttr["details"] != null)
                    {
                        return(new PersistentObjectAttributeAsDetail(client, (JObject)jAttr, this));
                    }

                    return(new PersistentObjectAttribute(client, (JObject)jAttr, this));
                }).ToArray();
            }
            else
            {
                Attributes = new PersistentObjectAttribute[0];
            }

            if (model.TryGetValue("queries", out var queriesToken))
            {
                var queries = (JArray)queriesToken;
                Queries = new KeyValueList <string, Query>(queries.Select(jQuery /* :-) */ => client.Hooks.OnConstruct(client, (JObject)jQuery, this, false)).ToDictionary(q => q.Name, q => q));
            }
            else
            {
                Queries = new KeyValueList <string, Query>(new Dictionary <string, Query>());
            }

            var parent = (JObject)model["parent"];

            if (parent != null)
            {
                Parent = client.Hooks.OnConstruct(client, parent);
            }

            // Initialize Tabs and Groups
            var tabIndex      = 0;
            var attributeTabs = !IsHidden?Attributes.OrderBy(attr => attr.Offset).GroupBy(attr => attr.Tab).Select(tab =>
            {
                var groups = tab.OrderBy(attr => attr.Offset).GroupBy(attr => attr.GroupName).Select(group => new PersistentObjectAttributeGroup(client, group.Key, group.ToArray())).ToArray();
                if (groups.Length == 1)
                {
                    groups[0].IsNameVisible = false;
                }

                var t   = (PersistentObjectTab)CreateAttributesTab(groups.SelectMany(g => g.Attributes).ToArray(), string.IsNullOrEmpty(tab.Key) ? Label : tab.Key, this);
                t.Index = tabIndex++;
                return(t);
            }) : new PersistentObjectTabAttributes[0];

            Tabs = attributeTabs.Concat(Queries.OrderBy(q => q.Value.Offset).Select(q => CreateQueryTab(q.Value))).ToList();

            if (!IsHidden)
            {
                // Initialize Action
                if (model.TryGetValue("actions", out var actionsToken))
                {
                    var actions = ActionBase.GetActions(client, actionsToken, this);

                    Actions       = actions.Where(a => !a.IsPinned).ToArray();
                    PinnedActions = actions.Where(a => a.IsPinned).ToArray();

                    Actions.Run(a => a.Initialize());
                    PinnedActions.Run(a => a.Initialize());
                }
                else
                {
                    Actions = PinnedActions = new ActionBase[0];
                }
            }
            else
            {
                Actions = PinnedActions = new ActionBase[0];
            }

            // Also check IsInEdit (Object could have been reconstructed after suspend/resume)
            IsInEdit = IsInEdit || IsNew || StateBehavior.HasFlag(StateBehavior.OpenInEdit) || StateBehavior.HasFlag(StateBehavior.StayInEdit);
            IsDirty  = IsDirty; // Also triggers reconstructed changes

            // Specials
            HasNotification = !string.IsNullOrWhiteSpace(Notification);

            client.Hooks.OnConstruct(this);

            Tabs.Select((tab, n) => tab.Index = n).Run();
        }