Example #1
0
        protected CodeList Suggest(CodeSuggestion suggestion)
        {
            CodeList codes = AbstractMessenger.cachedCodeLists[suggestion.Code];

            if (null == codes)
            {
                throw new Exception("CodeTable invalid.");
            }

            var parent     = suggestion.Parent;
            var isTopLevel = suggestion.TopLevel;
            var prefix     = suggestion.Prefix;
            var pageSize   = suggestion.PageSize;

            if (pageSize <= 0)
            {
                pageSize = 15;
            }

            {
                IList <CodeItem> items = codes.Items;
                if (false == string.IsNullOrEmpty(parent) || false == isTopLevel)
                {
                    items = codes.Filter(parent).Items;
                }
                // make a copy.
                codes = new CodeList {
                    Items = new List <CodeItem>(items), Name = codes.Name
                };
            }

            if (prefix.Length == 0)
            {
                var x = (from c in codes.Items
                         where c.IsEnabled
                         orderby c.ItemIndex, c.CodeName ascending
                         select c);
                codes.Items = x.Skip(pageSize * suggestion.PageNo).Take(pageSize).ToArray();
            }
            else
            {
                var x = (from c in codes.Items
                         where c.IsEnabled &&
                         (this.IndexOf((c.CodeName + '|' + c.Spell).ToLower(), prefix) >= 0)
                         orderby c.ItemIndex, this.IndexOf((c.CodeName + '|' + c.Spell).ToLower(), prefix), c.CodeName ascending
                         select c);
                codes.Items = x.Skip(pageSize * suggestion.PageNo).Take(pageSize).ToArray();
            }

            return(codes);
        }