228 lines
7.7 KiB
C#
228 lines
7.7 KiB
C#
// Copyright (c) 2023 schick Informatik
|
|
// Description: Search filter bar for top window, generates events if filter criteria change
|
|
//
|
|
|
|
using BreCalClient.misc.Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
namespace BreCalClient
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for SearchFilterControl.xaml
|
|
/// </summary>
|
|
public partial class SearchFilterControl : UserControl
|
|
{
|
|
|
|
#region private fields
|
|
|
|
private SearchFilterModel _model = new();
|
|
|
|
#endregion
|
|
|
|
#region Construction
|
|
|
|
public SearchFilterControl()
|
|
{
|
|
InitializeComponent();
|
|
this.DataContext = this._model;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region events
|
|
|
|
/// <summary>
|
|
/// historically we love a clickable logo and see what will happen
|
|
/// </summary>
|
|
public event Action? LogoImageClicked;
|
|
|
|
/// <summary>
|
|
/// if the user somewhat changes the filters trigger this
|
|
/// </summary>
|
|
internal event Action? SearchFilterChanged;
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public string FilterAsJson { get; set; } = "";
|
|
|
|
internal SearchFilterModel SearchFilter { get { return _model; } }
|
|
|
|
#endregion
|
|
|
|
#region public methods
|
|
|
|
public void SetBerths(List<Berth> berths)
|
|
{
|
|
this.comboBoxBerths.ItemsSource = berths;
|
|
}
|
|
|
|
public void SetAgencies(List<Participant> agencies)
|
|
{
|
|
this.comboBoxAgencies.ItemsSource = agencies;
|
|
}
|
|
|
|
public void ClearFilters()
|
|
{
|
|
this._model = new SearchFilterModel();
|
|
this.comboBoxAgencies.UnSelectAll();
|
|
this.comboBoxBerths.UnSelectAll();
|
|
this.comboBoxCategories.UnSelectAll();
|
|
this.datePickerETAFrom.SelectedDate = DateTime.Now.AddDays(-2);
|
|
this.datePickerETATo.SelectedDate = null;
|
|
this.textBoxSearch.Clear();
|
|
this.upDownShiplengthFrom.Value = null;
|
|
this.upDownShiplengthTo.Value = null;
|
|
this.checkBoxOwn.IsChecked = false;
|
|
}
|
|
|
|
|
|
internal void SetFilterFromModel(SearchFilterModel sfm)
|
|
{
|
|
this.ClearFilters();
|
|
|
|
if(sfm.Berths != null)
|
|
{
|
|
foreach(Berth berth in this.comboBoxBerths.ItemsSource)
|
|
{
|
|
if (sfm.Berths.Contains(berth.Id)) this.comboBoxBerths.SelectedItems.Add(berth);
|
|
}
|
|
}
|
|
if(sfm.Agencies != null)
|
|
{
|
|
foreach(Participant p in this.comboBoxAgencies.ItemsSource)
|
|
{
|
|
if(sfm.Agencies.Contains(p.Id)) this.comboBoxAgencies.SelectedItems.Add(p);
|
|
}
|
|
}
|
|
if(sfm.Categories != null)
|
|
{
|
|
EnumToStringConverter enumToStringConverter = new();
|
|
foreach (ShipcallType category in sfm.Categories)
|
|
{
|
|
this.comboBoxCategories.SelectedItems.Add(enumToStringConverter.Convert(category, typeof(ShipcallControl), new object(), System.Globalization.CultureInfo.CurrentCulture));
|
|
}
|
|
}
|
|
if (sfm.SearchString != null) this.textBoxSearch.Text = sfm.SearchString;
|
|
this.upDownShiplengthFrom.Value = sfm.ShipLengthFrom;
|
|
this.upDownShiplengthTo.Value = sfm.ShipLengthTo;
|
|
if (sfm.EtaFrom != null)
|
|
this.datePickerETAFrom.SelectedDate = sfm.EtaFrom;
|
|
else
|
|
this.datePickerETAFrom.SelectedDate = DateTime.Now.AddDays(-2);
|
|
this.datePickerETATo.SelectedDate = sfm.EtaTo;
|
|
this.checkBoxOwn.IsChecked = sfm.MineOnly;
|
|
|
|
this._model = sfm;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region event handler
|
|
|
|
private void logoImage_MouseUp(object sender, MouseButtonEventArgs e)
|
|
{
|
|
LogoImageClicked?.Invoke();
|
|
}
|
|
|
|
private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void datePickerETAFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
this._model.EtaFrom = this.datePickerETAFrom.SelectedDate;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void datePickerETATo_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
this._model.EtaTo = datePickerETATo.SelectedDate;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void comboBoxCategories_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
|
|
{
|
|
EnumToStringConverter enumToStringConverter = new();
|
|
|
|
_model.Categories.Clear();
|
|
foreach (string categoryString in comboBoxCategories.SelectedItems)
|
|
{
|
|
ShipcallType? type = (ShipcallType?)enumToStringConverter.ConvertBack(categoryString, typeof(ShipcallType), new object(), System.Globalization.CultureInfo.CurrentCulture);
|
|
if(type != null)
|
|
_model.Categories.Add(type.Value);
|
|
}
|
|
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void upDownShiplengthFrom_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
|
|
{
|
|
this._model.ShipLengthFrom = this.upDownShiplengthFrom.Value;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void upDownShiplengthTo_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
|
|
{
|
|
this._model.ShipLengthTo = this.upDownShiplengthTo.Value;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void comboBoxBerths_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
|
|
{
|
|
_model.Berths.Clear();
|
|
foreach(Berth berth in comboBoxBerths.SelectedItems)
|
|
_model.Berths.Add(berth.Id);
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void comboBoxAgencies_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
|
|
{
|
|
_model.Agencies.Clear();
|
|
foreach (Participant agency in comboBoxAgencies.SelectedItems)
|
|
_model.Agencies.Add(agency.Id);
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void textBoxSearch_Pasting(object sender, System.Windows.DataObjectPastingEventArgs e)
|
|
{
|
|
this.SearchFilter.SearchString = e.SourceDataObject.ToString();
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void textBoxSearch_PreviewTextInput(object sender, TextCompositionEventArgs e)
|
|
{
|
|
this.SearchFilter.SearchString = this.textBoxSearch.Text + e.Text;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void textBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
this.SearchFilter.SearchString = this.textBoxSearch.Text;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void checkBoxOwn_Checked(object sender, System.Windows.RoutedEventArgs e)
|
|
{
|
|
this._model.MineOnly = this.checkBoxOwn.IsChecked;
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
private void toggleButton24Hrs_Click(object sender, System.Windows.RoutedEventArgs e)
|
|
{
|
|
this.datePickerETAFrom.SelectedDate = DateTime.Now;
|
|
this.datePickerETATo.SelectedDate = DateTime.Now.AddDays(1);
|
|
SearchFilterChanged?.Invoke();
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|