public CatalogSearchViewModel(CatalogViewModel catalog)
        {
            Shell = catalog.Shell;
            InitFields();
            Items.Value = new List <CatalogDisplayItem>();
            CurrentItem
#if !DEBUG
            .Throttle(Consts.ScrollLoadTimeout, UiScheduler)
#endif
            .SelectMany(x => Env.RxQuery(s => {
                if (x == null)
                {
                    return(null);
                }
                var catalogId = x.CatalogId;
                return(s.Query <Catalog>()
                       .Fetch(c => c.Name)
                       .ThenFetch(n => n.Mnn)
                       .FirstOrDefault(c => c.Id == catalogId));
            }))
            .Subscribe(CurrentCatalog, CloseCancellation.Token);
            ParentModel = catalog;
            QuickSearch = new QuickSearch <CatalogDisplayItem>(UiScheduler,
                                                               v => Items.Value.FirstOrDefault(c => c.Name.StartsWith(v, StringComparison.CurrentCultureIgnoreCase)),
                                                               CurrentItem);
            QuickSearch.IsEnabled = false;

            SearchBehavior  = new SearchBehavior(this);
            IsLoading.Value = true;
            IsQuickSearchEnabled.Subscribe(v => {
                QuickSearch.IsEnabled = v;
                SearchBehavior.HandleGridKeyboardInput = !v;
            });
        }
        public CatalogNameViewModel(CatalogViewModel catalog)
        {
            InitFields();
            Shell              = catalog.Shell;
            ParentModel        = catalog;
            CatalogNames.Value = new List <CatalogName>();
            Catalogs.Value     = new List <Catalog>();

            CatalogNamesSearch = new QuickSearch <CatalogName>(UiScheduler,
                                                               v => CatalogNames.Value.FirstOrDefault(n => n.Name.StartsWith(v, StringComparison.CurrentCultureIgnoreCase)),
                                                               CurrentCatalogName);

            CatalogsSearch = new QuickSearch <Catalog>(UiScheduler,
                                                       v => Catalogs.Value.FirstOrDefault(n => n.Form.StartsWith(v, StringComparison.CurrentCultureIgnoreCase)),
                                                       c => CurrentCatalog = c);

            ParentModel.ObservableForProperty(m => m.ViewOffersByCatalog, skipInitial: false)
            .Select(x => x.Value)
            .Subscribe(CatalogsEnabled, CloseCancellation.Token);

            CurrentCatalogName
            .Subscribe(_ => {
                if (activeItemType == typeof(CatalogName))
                {
                    CurrentItem.Value = CurrentCatalogName.Value;
                }
            });

            //в жизни нет смысла грузить формы выпуска при каждом изменении наименования
            //это приведет к визуальным тормозам при скролинге
            //но в тестах это приведет к геморою
            CurrentCatalogName
#if !DEBUG
            .Throttle(Consts.ScrollLoadTimeout)
            .ObserveOn(UiScheduler)
#endif
            .Merge(ParentModel.ObservableForProperty(m => m.CurrentFilter).Cast <object>())
            .Merge(ParentModel.ObservableForProperty(m => m.ShowWithoutOffers))
            .Select(_ => {
                if (CurrentCatalogName.Value == null)
                {
                    return(Observable.Return(Enumerable.Empty <Catalog>().ToList()));
                }
                var nameId = CurrentCatalogName.Value.Id;
                return(Env.RxQuery(s => {
                    var queryable = s.Query <Catalog>()
                                    .Fetch(c => c.Name)
                                    .ThenFetch(c => c.Mnn)
                                    .Where(c => c.Name.Id == nameId);

                    if (!ParentModel.ShowWithoutOffers)
                    {
                        if (ParentModel.Mode == CatalogViewMode.Basic)
                        {
                            queryable = queryable.Where(c => c.HaveOffers);
                        }
                        else if (ParentModel.Mode == CatalogViewMode.CatalogSelector)
                        {
                            var catalogIds = s.Query <WaybillLine>().Where(x => x.Waybill.Status == DocStatus.Posted).Select(x => x.CatalogId).Distinct().ToList();
                            queryable = queryable.Where(c => catalogIds.Contains(c.Id));
                        }
                    }

                    if (ParentModel.CurrentFilter == ParentModel.Filters[1])
                    {
                        queryable = queryable.Where(c => c.VitallyImportant);
                    }

                    if (ParentModel.CurrentFilter == ParentModel.Filters[2])
                    {
                        queryable = queryable.Where(c => c.MandatoryList);
                    }

                    if (ParentModel.CurrentFilter == ParentModel.Filters[3])
                    {
                        queryable = queryable.Where(c => s.Query <AwaitedItem>().Any(i => i.Catalog == c));
                    }

                    return queryable.OrderBy(c => c.Form).ToList();
                }));
            })
            .Switch()
            .Subscribe(Catalogs, CloseCancellation.Token);
            ExcelExporter.ActiveProperty.Value = "CatalogNames";
        }