Esempio n. 1
0
        public void CreateDocument(MarkdownCategoryViewModel parent = null)
        {
            NameItemDialog dialog       = new NameItemDialog(DialogOwner, "New Note");
            bool?          dialogResult = dialog.ShowDialog();

            if (dialogResult.HasValue && dialogResult.Value)
            {
                MarkdownDocument doc = new MarkdownDocument(UniverseVm.Model.Connection);
                doc.Name         = dialog.UserInput;
                doc.UniverseId   = UniverseVm.Model.id;
                doc.MarkdownText = string.Format("# {0}\r\nThis is a new note document.", doc.Name);
                doc.PlainText    = Markdig.Markdown.ToPlainText(doc.MarkdownText);
                doc.Create();

                MarkdownDocumentViewModel docVm = new MarkdownDocumentViewModel(doc, UniverseVm);

                if (parent != null)
                {
                    MarkdownCategoryDocument link = new MarkdownCategoryDocument(UniverseVm.Model.Connection);
                    link.MarkdownDocumentId = doc.id;
                    link.MarkdownCategoryId = parent.Model.id;
                    link.Create();

                    docVm.Parent = parent;
                    parent.Children.Add(docVm);
                }
                else
                {
                    Items.Add(docVm);
                }
            }
        }
        public void Delete()
        {
            ConfirmDeleteDialog dialog = new ConfirmDeleteDialog(DialogOwner, "Its subitems will not be removed.");
            bool?result = dialog.ShowDialog();

            if (result.HasValue && result.Value)
            {
                Model.Delete();
                if (Parent != null)
                {
                    Parent.Children.Remove(this);
                    foreach (var item in Children)
                    {
                        if (item is MarkdownCategoryViewModel)
                        {
                            MarkdownCategoryViewModel child = (MarkdownCategoryViewModel)item;
                            child.Parent         = Parent;
                            child.Model.ParentId = (Parent as MarkdownCategoryViewModel).Model.id;
                            Parent.Children.Add(child);
                            child.Model.Save();
                        }
                        else
                        {
                            MarkdownDocumentViewModel child = (MarkdownDocumentViewModel)item;
                            child.Parent = Parent;

                            MarkdownCategoryDocument link = new MarkdownCategoryDocument(Model.Connection);
                            link.MarkdownCategoryId = (Parent as MarkdownCategoryViewModel).Model.id;
                            link.MarkdownDocumentId = child.Model.id;
                            link.Create();
                            Parent.Children.Add(child);
                        }
                    }
                }
                else
                {
                    if (UniverseVm.MarkdownTree.Items.Contains(this))
                    {
                        UniverseVm.MarkdownTree.Items.Remove(this);
                    }
                    foreach (var item in Children)
                    {
                        if (item is MarkdownCategoryViewModel)
                        {
                            MarkdownCategoryViewModel child = (MarkdownCategoryViewModel)item;
                            child.Parent         = null;
                            child.Model.ParentId = null;
                            child.Model.Save();
                            UniverseVm.MarkdownTree.Items.Add(child);
                        }
                        else
                        {
                            MarkdownDocumentViewModel child = (MarkdownDocumentViewModel)item;
                            UniverseVm.MarkdownTree.Items.Add(child);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public void UpdateDocumentCategories(MarkdownDocumentViewModel doc, IEnumerable <MarkdownCategoryViewModel> newCategories)
        {
            // First, load all document/category links from the database.
            var currentCategories = MarkdownCategoryDocument.GetCategoriesForDocument(doc.Model.Connection, doc.Model.id);

            // Delete any that were removed.
            foreach (var cat in currentCategories)
            {
                MarkdownCategoryViewModel vm = Categories.SingleOrDefault(i => i.Model.id == cat.MarkdownCategoryId);
                if (vm != null)
                {
                    cat.Delete();
                    vm.Children.Remove(doc);
                }
            }

            // Add new ones.
            foreach (var cat in newCategories)
            {
                MarkdownCategoryDocument dbLink = currentCategories.SingleOrDefault(i => i.MarkdownCategoryId == cat.Model.id);
                if (dbLink == null)
                {
                    dbLink = new MarkdownCategoryDocument(doc.Model.Connection);
                    dbLink.MarkdownCategoryId = cat.Model.id;
                    dbLink.MarkdownDocumentId = doc.Model.id;
                    dbLink.Create();
                }

                if (!cat.Children.Contains(doc))
                {
                    cat.Children.Add(doc);
                }
            }

            // Add/Remove from uncategoried as necessary.
            if (newCategories.Count() == 0)
            {
                if (!Items.Contains(doc))
                {
                    Items.Add(doc);
                }
            }
            else
            {
                if (Items.Contains(doc))
                {
                    Items.Remove(doc);
                }
            }
        }