//
// Copyright 2014 Perforce Software Inc.
//
using Perforce.Helper;
using Perforce.Model;
using Perforce.ViewModel;
using System;
using System.Windows.Controls;
using System.Windows.Input;
namespace Perforce.View {
///
/// Interaction logic for Sidebar.xaml
///
public partial class Sidebar : UserControl {
private SidebarViewModel _model;
public Sidebar() {
InitializeComponent();
_model = new SidebarViewModel();
this.DataContext = _model;
}
public SidebarViewModel Model {
get { return _model; }
set { _model = value; }
}
public void Refresh() {
var children = UIHelper.FindVisualChildren(this);
foreach (var child in children) {
child.Model.Refresh();
}
_model.Refresh();
}
public void DeselectAll() {
var children = UIHelper.FindVisualChildren(this);
foreach (var child in children) {
child.Model.IsSelected = false;
}
}
public SidebarSelector FindSelected() {
SidebarSelector selected = null;
var children = UIHelper.FindVisualChildren(this);
foreach (var child in children) {
if (child.Model.IsSelected) {
selected = child;
break;
}
}
return selected;
}
public SidebarSelector GetSelector(SELECTOR_TYPE type) {
SidebarSelector found = null;
var children = UIHelper.FindVisualChildren(this);
foreach (var child in children) {
if (child.SelectorType == type) {
found = child;
break;
}
}
return found;
}
private void Sidebar_Loaded(object sender, System.Windows.RoutedEventArgs e) {
Refresh();
}
private void AddTag_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
AddTagPopup.IsOpen = true;
TagName.Focus();
}
private void TagName_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
if (e.Key == Key.Enter) {
AddTag();
TagName.Text = string.Empty;
AddTagPopup.IsOpen = false;
} else if (e.Key == Key.Escape) {
TagName.Text = string.Empty;
AddTagPopup.IsOpen = false;
}
}
private void AddTagButton_Click(object sender, System.Windows.RoutedEventArgs e) {
AddTag();
TagName.Text = string.Empty;
AddTagPopup.IsOpen = false;
}
private void AddTag() {
string tagText = TagName.Text.Trim();
if (!string.IsNullOrEmpty(tagText)) {
var tag = new FavoriteTagItem { TagName = tagText, Selected = false };
var helper = Utility.GetFavoritesHelper();
helper.SaveFavoriteTag(tag);
_model.Refresh("FavoriteTags");
}
}
private void TagName_LostFocus(object sender, System.Windows.RoutedEventArgs e) {
AddTagPopup.IsOpen = false;
}
}
}