Esempio n. 1
0
 private void _fill_paragraph(ParagraphWindowVM p_windowVM, ParagraphControlVM p_controlVM)
 {
     p_windowVM.Text       = p_controlVM.Text;
     p_windowVM.IsBold     = p_controlVM.IsBold;
     p_windowVM.IsCentered = p_controlVM.IsCentered;
     p_windowVM.IsTab      = p_controlVM.IsTab;
 }
Esempio n. 2
0
        public MainWindowVM(Window window)
        {
            ForBorder = new RelayCommand((parameter) =>
            {
                if (parameter == null)
                {
                    return;
                }

                var border = (Border)parameter;
                if (border.Visibility == Visibility.Visible)
                {
                    border.Visibility = Visibility.Collapsed;
                }
                else
                {
                    border.Visibility = Visibility.Visible;
                }
            });
            ClearList = new RelayCommand(() => { Items.Clear(); });

            _remove = new RelayCommand(async(parameter) =>
            {
                if (parameter == null)
                {
                    return;
                }

                int id   = (int)parameter;
                int indx = 0;
                foreach (var item in Items)
                {
                    if (item.Id == id)
                    {
                        break;
                    }
                    indx++;
                }

                Items.RemoveAt(indx);
                await Task.Delay(1);
            });
            _up = new RelayCommand((parameter) =>
            {
                if (parameter == null)
                {
                    return;
                }

                int id = (int)parameter;
                if (id == Items.First().Id)
                {
                    return;
                }

                int indx = 0;
                foreach (var item in Items)
                {
                    if (item.Id == id)
                    {
                        break;
                    }
                    indx++;
                }
                Items.Move(indx, --indx);
            });
            _down = new RelayCommand((parameter) =>
            {
                if (parameter == null)
                {
                    return;
                }

                int id = (int)parameter;
                if (id == Items.Last().Id)
                {
                    return;
                }

                int indx = 0;
                foreach (var item in Items)
                {
                    if (item.Id == id)
                    {
                        break;
                    }
                    indx++;
                }
                Items.Move(indx, ++indx);
            });
            _edit = new RelayCommand((parameter) =>
            {
                if (parameter == null)
                {
                    return;
                }

                int id   = (int)parameter;
                var item = Items.First(i => i.Id == id);

                if (item is ParagraphControlVM)
                {
                    var p_window = new ParagraphWindow()
                    {
                        Title = "Редактирование параграфа"
                    };
                    _fill_paragraph(p_window.DataContext as ParagraphWindowVM, item as ParagraphControlVM);
                    if (p_window.ShowDialog() != true)
                    {
                        return;
                    }

                    var context   = p_window.DataContext as ParagraphWindowVM;
                    var paragraph = new ParagraphControlVM
                    {
                        Text       = context.Text.Trim(),
                        IsBold     = context.IsBold,
                        IsCentered = context.IsCentered,
                        IsTab      = context.IsTab,
                        Edit       = _edit,
                        Up         = _up,
                        Down       = _down,
                        Remove     = _remove
                    };

                    Items.ReplaceItem(i => i.Id == id, paragraph);
                }
                else if (item is ImageControlVM)
                {
                    var i_window = new ImageWindow()
                    {
                        Title = "Редактирование изображения"
                    };
                    _fill_i_paragraph(i_window.DataContext as ImageWindowVM, item as ImageControlVM);
                    if (i_window.ShowDialog() != true)
                    {
                        return;
                    }

                    var context = i_window.DataContext as ImageWindowVM;
                    var image   = new ImageControlVM
                    {
                        Description   = context.Description,
                        URL           = context.URL,
                        Image         = context.Image,
                        OriginalImage = context.OriginalImage,
                        Size          = context.ImageSize,
                        Edit          = _edit,
                        Up            = _up,
                        Down          = _down,
                        Remove        = _remove
                    };

                    Items.ReplaceItem(i => i.Id == id, image);
                }
            });

            #region FirstPage
            AddBaseBlock = new RelayCommand(() =>
            {
                var bb_window = new BaseBlockWindow();
                if (bb_window.ShowDialog() != true)
                {
                    return;
                }

                var context = bb_window.DataContext as BaseBlockWindowVM;

                var item = new BaseBlockControlVM
                {
                    ArticleName   = context.ArticleName,
                    Date          = context.Date,
                    Description   = context.Description,
                    URL           = context.URL,
                    CommentsCount = context.CommentsCount,
                    Up            = _up,
                    Down          = _down,
                    Remove        = _remove,
                    Edit          = _edit
                };

                Items.Add(item);

                if (context.AddOtherElements)
                {
                    Items.Add(new BreakPageControlVM()
                    {
                        Up = _up, Down = _down, Remove = _remove
                    });
                    Items.Add(new ParagraphControlVM
                    {
                        Text   = context.URL,
                        Up     = _up,
                        Down   = _down,
                        Remove = _remove,
                        Edit   = _edit
                    });
                    Items.Add(new EmptyParagraphControlVM {
                        Up = _up, Down = _down, Remove = _remove
                    });
                    Items.Add(new ParagraphControlVM
                    {
                        Text       = context.ArticleName,
                        IsBold     = true,
                        IsCentered = true,
                        Up         = _up,
                        Down       = _down,
                        Remove     = _remove,
                        Edit       = _edit
                    });
                    Items.Add(new EmptyParagraphControlVM {
                        Up = _up, Down = _down, Remove = _remove
                    });
                    Items.Add(new ParagraphControlVM
                    {
                        Text   = context.Date,
                        Up     = _up,
                        Down   = _down,
                        Remove = _remove,
                        Edit   = _edit
                    });
                }
            });
            AddBreakPage = new RelayCommand(async() =>
            {
                Items.Add(new BreakPageControlVM
                {
                    Up     = _up,
                    Down   = _down,
                    Remove = _remove
                });
                await Task.Delay(1);
            });
            #endregion

            #region Second page
            AddEmptyParagraph = new RelayCommand(async() =>
            {
                Items.Add(new EmptyParagraphControlVM
                {
                    Up     = _up,
                    Down   = _down,
                    Remove = _remove
                });
                await Task.Delay(1);
            });
            AddParagraph = new RelayCommand(async() =>
            {
                var p_window = new ParagraphWindow()
                {
                    Title = "Добавление нового параграфа..."
                };
                if (p_window.ShowDialog() != true)
                {
                    return;
                }

                var context   = p_window.DataContext as ParagraphWindowVM;
                var paragraph = new ParagraphControlVM
                {
                    Text       = context.Text.Trim(),
                    IsBold     = context.IsBold,
                    IsCentered = context.IsCentered,
                    IsTab      = context.IsTab,
                    Edit       = _edit,
                    Up         = _up,
                    Down       = _down,
                    Remove     = _remove
                };
                Items.Add(paragraph);

                await Task.Delay(1);
            });
            AddPicture = new RelayCommand(async() =>
            {
                var i_window = new ImageWindow()
                {
                    Title = "Добавление нового изображения..."
                };
                if (i_window.ShowDialog() != true)
                {
                    return;
                }

                var context = i_window.DataContext as ImageWindowVM;

                var image = new ImageControlVM
                {
                    Description   = context.Description,
                    URL           = context.URL,
                    Image         = context.Image,
                    OriginalImage = context.OriginalImage,
                    Size          = context.ImageSize,
                    Edit          = _edit,
                    Up            = _up,
                    Down          = _down,
                    Remove        = _remove
                };
                Items.Add(image);

                await Task.Delay(1);
            });
            AddYouTube = new RelayCommand(async() =>
            {
                var i_window = new YouTubeWindow()
                {
                    Title = "Добавление YouTube изображения..."
                };
                if (i_window.ShowDialog() != true)
                {
                    return;
                }

                var context = i_window.DataContext as YouTubeWindowVM;

                var yt = new YouTubeControlVM
                {
                    URL           = context.URL,
                    Image         = context.Image,
                    OriginalImage = context.OriginalImage,
                    Edit          = _edit,
                    Up            = _up,
                    Down          = _down,
                    Remove        = _remove
                };
                Items.Add(yt);

                await Task.Delay(1);
            });
            #endregion

            #region Main window
            SelectFolder = new RelayCommand(() =>
            {
                using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
                {
                    dialog.ShowNewFolderButton = false;
                    dialog.Description         = "Выберите конечную папку сохранения документа";
                    if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }

                    Functions.TargetFolder = dialog.SelectedPath;
                }
            });
            Execute = new RelayCommand(async() =>
            {
                var doc  = new DocumentWorks();
                var data = _convert(Items);
                doc.Build(data, Functions.TargetFolder);

                Items.Clear();
                await Task.Delay(1);
            });
            ExecuteAs = new RelayCommand(async() =>
            {
                var doc                 = new DocumentWorks();
                var dialog              = new OpenFileDialog();
                dialog.CheckPathExists  = true;
                dialog.CheckFileExists  = false;
                dialog.Multiselect      = false;
                dialog.RestoreDirectory = true;
                dialog.ValidateNames    = true;
                dialog.Title            = "Сохранить файл как...";
                dialog.FileName         = doc.GetFileName();
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                var data = _convert(Items);
                doc.BuildAs(data, dialog.FileName);

                Items.Clear();
                await Task.Delay(1);
            });
            #endregion

#if DEBUG
            //TODO: сделать один шаблон для текста и картинки для всех контролов а не дублировать их
            Items = new ObservableCollection <BaseListItem>
            {
                new ParagraphControlVM()
                {
                    Text   = "1 - some text",
                    Up     = _up,
                    Down   = _down,
                    Remove = _remove,
                    Edit   = _edit
                },

                new BreakPageControlVM {
                    Up = _up, Down = _down, Remove = _remove
                },
                new ParagraphControlVM()
                {
                    Text   = "2 - some text",
                    Up     = _up,
                    Down   = _down,
                    Remove = _remove,
                    Edit   = _edit
                },
                new EmptyParagraphControlVM {
                    Up = _up, Down = _down, Remove = _remove
                },
                new ImageControlVM()
                {
                    Description = "описание",
                    URL         = "ссылка",
                    Up          = _up,
                    Down        = _down,
                    Remove      = _remove,
                    Image       = new Bitmap(160, 90).Change()
                },
                new BaseBlockControlVM {
                    Date        = "02.12.2019",
                    ArticleName = "some article namr",
                    Description = "some long description",
                    URL         = "some long url on article",
                    Up          = _up,
                    Down        = _down,
                    Remove      = _remove,
                    Edit        = _edit
                }
            };
#else
            Items = new ObservableCollection <BaseListItem>();
#endif
        }