Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/fitzchak1/ravendb
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Dec 5, 2011
2 parents 092b01f + 4154a2e commit f00a9e3
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 37 deletions.
42 changes: 42 additions & 0 deletions Raven.Studio/Behaviors/SelectItemOnRightClick.cs
@@ -0,0 +1,42 @@
// -----------------------------------------------------------------------
// <copyright file="SelectItemOnRightClick.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace Raven.Studio.Behaviors
{
public class SelectItemOnRightClick : Behavior<ListBox>
{
protected override void OnAttached()
{
AssociatedObject.MouseRightButtonDown += AssociatedObjectOnMouseRightButtonDown;
base.OnAttached();
}

protected override void OnDetaching()
{
AssociatedObject.MouseRightButtonDown -= AssociatedObjectOnMouseRightButtonDown;
base.OnDetaching();
}

void AssociatedObjectOnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var item = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null),AssociatedObject)
.OfType<ListBoxItem>()
.FirstOrDefault();

if (item == null || AssociatedObject.SelectedItems.Contains(item.DataContext))
return;

AssociatedObject.SelectedItems.Clear();
item.IsSelected = true;
}
}
}
20 changes: 14 additions & 6 deletions Raven.Studio/Behaviors/ShowItemsBasedOnControlDimensions.cs
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Controls;
using System.Windows.Interactivity;
using Raven.Studio.Models;
using Raven.Studio.Infrastructure;

namespace Raven.Studio.Behaviors
{
Expand All @@ -22,10 +23,10 @@ public DocumentsModel Model

protected override void OnAttached()
{
var events = Observable.FromEventPattern<EventArgs>(AssociatedObject, "SizeChanged")
.Concat(Observable.FromEventPattern<EventArgs>(DocumentsModel.DocumentSize, "PropertyChanged"))
var events = Observable.FromEventPattern<SizeChangedEventHandler, SizeChangedEventArgs>(e => AssociatedObject.SizeChanged += e, e => AssociatedObject.SizeChanged -= e).NoSignature()
.Merge(Observable.FromEventPattern<EventHandler, EventArgs>(e => DocumentsModel.DocumentSize.SizeChanged += e, e => DocumentsModel.DocumentSize.SizeChanged -= e))
.Throttle(TimeSpan.FromSeconds(0.5))
.Concat(Observable.FromEventPattern<EventArgs>(AssociatedObject, "Loaded")) // Loaded should execute immediately.
.Merge(Observable.FromEventPattern<RoutedEventHandler, EventArgs>(e => AssociatedObject.Loaded += e, e => AssociatedObject.Loaded -= e)) // Loaded should execute immediately.
.ObserveOnDispatcher();

disposable = events.Subscribe(_ => CalculatePageSize());
Expand All @@ -42,9 +43,16 @@ private void CalculatePageSize()
if (Model == null)
return;

var row = AssociatedObject.ActualWidth / DocumentsModel.DocumentSize.Width;
var column = AssociatedObject.ActualHeight / DocumentsModel.DocumentSize.Height;
Model.Pager.PageSize = (int)(row * column);
// ReSharper disable CompareOfFloatsByEqualityOperator
if (AssociatedObject.ActualWidth == 0)
return;
// ReSharper restore CompareOfFloatsByEqualityOperator

int row = (int) (AssociatedObject.ActualWidth / (DocumentsModel.DocumentSize.Width + 28));
int column = (int) (AssociatedObject.ActualHeight / (DocumentsModel.DocumentSize.Height + 24));
int pageSize = row * column;
Model.Pager.PageSize = pageSize;
Model.Pager.OnPagerChanged();
}
}
}
9 changes: 5 additions & 4 deletions Raven.Studio/Features/Documents/DocumentsView.xaml
Expand Up @@ -118,6 +118,11 @@
Grid.ColumnSpan="3"
Margin="10"
ItemTemplate="{StaticResource DataTemplate_Document_CardView}">

<i:Interaction.Behaviors>
<Behaviors:SelectItemOnRightClick />
<Behaviors:ShowItemsBasedOnControlDimensions Model="{Binding}" />
</i:Interaction.Behaviors>

<Controls:ContextMenuService.ContextMenu>
<Controls:ContextMenu x:Name="DocumentsContextMenu">
Expand All @@ -129,10 +134,6 @@
CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
</Controls:ContextMenu>
</Controls:ContextMenuService.ContextMenu>

<i:Interaction.Behaviors>
<Behaviors:ShowItemsBasedOnControlDimensions Model="{Binding}" />
</i:Interaction.Behaviors>
</ListBox>
</toolkit:HeaderedContentControl>

Expand Down
25 changes: 13 additions & 12 deletions Raven.Studio/Features/Documents/ViewableDocument.cs
Expand Up @@ -29,19 +29,11 @@ public ViewableDocument(JsonDocument inner)
ClrType = inner.Metadata.IfPresent<string>(Constants.RavenClrType);
CollectionType = DetermineCollectionType(inner.Metadata);

Observable.FromEventPattern<EventArgs>(DocumentsModel.DocumentSize, "SizeChanged")
Observable.FromEventPattern<EventHandler, EventArgs>(e => DocumentsModel.DocumentSize.SizeChanged += e, e => DocumentsModel.DocumentSize.SizeChanged -= e)
.Throttle(TimeSpan.FromSeconds(0.5))
.Subscribe(e =>
{
string d = null;
if (DocumentsModel.DocumentSize.Height >= DocumentsModel.ExpandedMinimumHeight)
{
var margin = Math.Sqrt(DocumentsModel.DocumentSize.Width) - 4;
d = ShortViewOfJson.GetContentDataWithMargin(inner.DataAsJson, (int)margin);
}
Execute.OnTheUI(() => Data = d);
});
.Subscribe(_ => CalculateData());

CalculateData();
ToolTipText = ShortViewOfJson.GetContentDataWithMargin(inner.DataAsJson, 10);
}

Expand Down Expand Up @@ -78,7 +70,16 @@ public string Data
}
}


private void CalculateData()
{
string d = null;
if (DocumentsModel.DocumentSize.Height >= DocumentsModel.ExpandedMinimumHeight)
{
var margin = Math.Sqrt(DocumentsModel.DocumentSize.Width) - 4;
d = ShortViewOfJson.GetContentDataWithMargin(inner.DataAsJson, (int)margin);
}
Execute.OnTheUI(() => Data = d);
}

public string DisplayId
{
Expand Down
18 changes: 12 additions & 6 deletions Raven.Studio/Features/Query/QueryView.xaml
Expand Up @@ -8,7 +8,8 @@
xmlns:Documents="clr-namespace:Raven.Studio.Features.Documents"
xmlns:Editors="clr-namespace:Raven.Studio.Controls.Editors"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:Controls="clr-namespace:System.Windows.Controls">

<toolkit:DockPanel Margin="{StaticResource Margin_ContentPage}">
<Border Background="{StaticResource Brush_HeaderBackground}"
Expand Down Expand Up @@ -69,7 +70,7 @@
ToolTipService.ToolTip="Add sort by option for this query"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="0,0,4,0"
Margin="0,0,6,0"
Command="{Binding AddSortBy}"
Visibility="{Binding Path=SortByOptions.Count, Converter={StaticResource HiddenWhenEmpty}}"
VerticalContentAlignment="Center">
Expand All @@ -86,6 +87,11 @@
</Button.Content>
</Button>

<Controls:Separator Style="{StaticResource Style_Separator_HeaderLink}"
VerticalAlignment="Center"
Height="20"
Margin="2,0,6,0" />

<Button Grid.Column="1"
Command="{Binding Execute}"
HorizontalAlignment="Right"
Expand Down Expand Up @@ -259,12 +265,12 @@

</StackPanel>

<Border Grid.Row="2"
Margin="{StaticResource Margin_ContentPage}"
Visibility="{Binding Path=DocumentsResult.Value, Converter={StaticResource HiddenWhenNull}}">
<Grid Grid.Row="2"
Margin="{StaticResource Margin_ContentPage}"
Visibility="{Binding Path=DocumentsResult.Value, Converter={StaticResource HiddenWhenNull}}">
<Documents:DocumentsView Visibility="{Binding Path=Documents.Count, Converter={StaticResource HiddenWhenEmpty}}"
DataContext="{Binding Path=DocumentsResult.Value}" />
</Border>
</Grid>
</Grid>
</toolkit:DockPanel>
</Infrastructure:View>
14 changes: 14 additions & 0 deletions Raven.Studio/Infrastructure/RxExtensions.cs
@@ -0,0 +1,14 @@
using System;
using System.Reactive;
using System.Reactive.Linq;

namespace Raven.Studio.Infrastructure
{
public static class RxExtensions
{
public static IObservable<object> NoSignature<T>(this IObservable<EventPattern<T>> observable) where T : EventArgs
{
return observable.Select(e => (object) null);
}
}
}
8 changes: 7 additions & 1 deletion Raven.Studio/Models/AllDocumentsModel.cs
Expand Up @@ -9,7 +9,13 @@ static AllDocumentsModel()
{
Documents = new Observable<DocumentsModel>();
Documents.Value = new DocumentsModel();
Documents.Value.Pager.SetTotalResults(new Observable<long?>(ApplicationModel.Database.Value.Statistics, v => ((DatabaseStatistics)v).CountOfDocuments));
SetTotalResults();
ApplicationModel.Current.Server.Value.SelectedDatabase.PropertyChanged += (sender, args) => SetTotalResults();
}

private static void SetTotalResults()
{
Documents.Value.Pager.SetTotalResults(new Observable<long?>(ApplicationModel.Database.Value.Statistics, v => ((DatabaseStatistics) v).CountOfDocuments));
}

public AllDocumentsModel()
Expand Down
2 changes: 1 addition & 1 deletion Raven.Studio/Models/DocumentsModel.cs
Expand Up @@ -30,7 +30,7 @@ public DocumentsModel()
Documents.CollectionChanged += (sender, args) => DetermineDocumentViewStyle();

Pager = new PagerModel();
Pager.Navigated += (sender, args) => ForceTimerTicked();
Pager.PagerChanged += (sender, args) => ForceTimerTicked();

ShowEditControls = true;
}
Expand Down
30 changes: 23 additions & 7 deletions Raven.Studio/Models/PagerModel.cs
Expand Up @@ -7,25 +7,36 @@ namespace Raven.Studio.Models
{
public class PagerModel : NotifyPropertyChangedBase
{
public event EventHandler Navigated;
public event EventHandler PagerChanged;

public PagerModel()
{
PageSize = 25;
SetTotalResults(new Observable<long?>());
SetTotalResults();
}

public void SetTotalResults(Observable<long?> observable)
public void SetTotalResults(Observable<long?> observable = null)
{
TotalResults = observable;
TotalResults = observable ?? new Observable<long?>();
TotalResults.PropertyChanged += (sender, args) =>
{
OnPropertyChanged("TotalPages");
OnPropertyChanged("HasNextPage");
};
}

public int PageSize { get; set; }
private int pageSize;
public int PageSize
{
get { return pageSize; }
set
{
pageSize = value;
OnPropertyChanged();
OnPropertyChanged("CurrentPage");
OnPropertyChanged("TotalPages");
}
}

public int CurrentPage
{
Expand Down Expand Up @@ -92,8 +103,13 @@ private void NavigateToPage(int pageOffset)
urlParser.SetQueryParam("skip", Skip);
UrlUtil.Navigate(urlParser.BuildUrl());

if (Navigated != null)
Navigated(this, EventArgs.Empty);
OnPagerChanged();
}

public void OnPagerChanged()
{
if (PagerChanged != null)
PagerChanged(this, EventArgs.Empty);
}

public ICommand NextPage
Expand Down
2 changes: 2 additions & 0 deletions Raven.Studio/Raven.Studio.csproj
Expand Up @@ -115,6 +115,7 @@
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<EmbeddedResource Include="Assets\EmbeddedData\MvcMusicStore_Dump.json" />
<Compile Include="Behaviors\SelectItemOnRightClick.cs" />
<Compile Include="Behaviors\ShowItemsBasedOnControlDimensions.cs" />
<Compile Include="Behaviors\UpdateTextBindingOnPropertyChanged.cs" />
<Compile Include="Behaviors\DoubleClickBehavior.cs" />
Expand Down Expand Up @@ -261,6 +262,7 @@
<Compile Include="Infrastructure\Converters\NullToVisibilityConverter.cs" />
<Compile Include="Infrastructure\KeysComparer.cs" />
<Compile Include="Infrastructure\Execute.cs" />
<Compile Include="Infrastructure\RxExtensions.cs" />
<Compile Include="Infrastructure\StringExtensions.cs" />
<Compile Include="Infrastructure\UrlParser.cs" />
<Compile Include="Infrastructure\UrlUtil.cs" />
Expand Down

0 comments on commit f00a9e3

Please sign in to comment.