18 changed files with 1041 additions and 138 deletions
@ -0,0 +1,85 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.IO; |
||||||
|
using ICSharpCode.SharpDevelop.Project; |
||||||
|
|
||||||
|
namespace ICSharpCode.AspNet.Mvc |
||||||
|
{ |
||||||
|
public class MvcMasterPageFileName |
||||||
|
{ |
||||||
|
public MvcMasterPageFileName(FileProjectItem fileProjectItem) |
||||||
|
{ |
||||||
|
UpdateFileName(fileProjectItem); |
||||||
|
} |
||||||
|
|
||||||
|
void UpdateFileName(FileProjectItem fileProjectItem) |
||||||
|
{ |
||||||
|
FullPath = fileProjectItem.FileName; |
||||||
|
FileName = Path.GetFileName(FullPath); |
||||||
|
FolderRelativeToProject = GetFolderRelativeToProject(fileProjectItem); |
||||||
|
VirtualPath = GetVirtualPath(); |
||||||
|
} |
||||||
|
|
||||||
|
string GetFolderRelativeToProject(FileProjectItem fileProjectItem) |
||||||
|
{ |
||||||
|
return Path.GetDirectoryName(fileProjectItem.Include); |
||||||
|
} |
||||||
|
|
||||||
|
string GetVirtualPath() |
||||||
|
{ |
||||||
|
var virtualPath = new MvcVirtualPath(FolderRelativeToProject, FileName); |
||||||
|
return virtualPath.VirtualPath; |
||||||
|
} |
||||||
|
|
||||||
|
public MvcMasterPageFileName() |
||||||
|
{ |
||||||
|
FullPath = String.Empty; |
||||||
|
FileName = String.Empty; |
||||||
|
FolderRelativeToProject = String.Empty; |
||||||
|
VirtualPath = String.Empty; |
||||||
|
} |
||||||
|
|
||||||
|
public string FullPath { get; set; } |
||||||
|
public string FileName { get; set; } |
||||||
|
public string FolderRelativeToProject { get; set; } |
||||||
|
public string VirtualPath { get; set; } |
||||||
|
|
||||||
|
public override string ToString() |
||||||
|
{ |
||||||
|
return FullPath; |
||||||
|
} |
||||||
|
|
||||||
|
public static MvcMasterPageFileName CreateMvcMasterPageFileName(ProjectItem projectItem) |
||||||
|
{ |
||||||
|
var fileProjectItem = projectItem as FileProjectItem; |
||||||
|
if (fileProjectItem != null) { |
||||||
|
return CreateMvcMasterPageFileName(fileProjectItem); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public static MvcMasterPageFileName CreateMvcMasterPageFileName(FileProjectItem fileProjectItem) |
||||||
|
{ |
||||||
|
if (IsMasterPageFileName(fileProjectItem.FileName)) { |
||||||
|
return new MvcMasterPageFileName(fileProjectItem); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public static bool IsMasterPageFileName(string fileName) |
||||||
|
{ |
||||||
|
string extension = GetLowerCaseFileExtension(fileName); |
||||||
|
return extension == ".master"; |
||||||
|
} |
||||||
|
|
||||||
|
static string GetLowerCaseFileExtension(string fileName) |
||||||
|
{ |
||||||
|
if (fileName != null) { |
||||||
|
return Path.GetExtension(fileName).ToLowerInvariant(); |
||||||
|
} |
||||||
|
return String.Empty; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,39 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
|
||||||
|
namespace ICSharpCode.AspNet.Mvc |
||||||
|
{ |
||||||
|
public class MvcVirtualPath |
||||||
|
{ |
||||||
|
public MvcVirtualPath(string folderRelativeToProject, string fileName) |
||||||
|
{ |
||||||
|
VirtualPath = GetVirtualPath(folderRelativeToProject, fileName); |
||||||
|
} |
||||||
|
|
||||||
|
public string VirtualPath { get; private set; } |
||||||
|
|
||||||
|
string GetVirtualPath(string folderRelativeToProject, string fileName) |
||||||
|
{ |
||||||
|
return String.Format( |
||||||
|
"~/{0}{1}", |
||||||
|
GetFolderRelativeToProjectAsVirtualPath(folderRelativeToProject), |
||||||
|
fileName); |
||||||
|
} |
||||||
|
|
||||||
|
string GetFolderRelativeToProjectAsVirtualPath(string folderRelativeToProject) |
||||||
|
{ |
||||||
|
string virtualFolder = folderRelativeToProject.Replace('\\', '/'); |
||||||
|
return AppendForwardSlashIfVirtualFolderIsNotEmptyString(virtualFolder); |
||||||
|
} |
||||||
|
|
||||||
|
string AppendForwardSlashIfVirtualFolderIsNotEmptyString(string virtualFolder) |
||||||
|
{ |
||||||
|
if (String.IsNullOrEmpty(virtualFolder)) { |
||||||
|
return String.Empty; |
||||||
|
} |
||||||
|
return virtualFolder + "/"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,126 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Windows; |
||||||
|
using System.Windows.Media.Animation; |
||||||
|
|
||||||
|
namespace ICSharpCode.AspNet.Mvc |
||||||
|
{ |
||||||
|
public static class SlideBehaviour |
||||||
|
{ |
||||||
|
public static readonly DependencyProperty FrameworkElementHiddenBySlideProperty = |
||||||
|
DependencyProperty.RegisterAttached( |
||||||
|
"FrameworkElementHiddenBySlide", |
||||||
|
typeof(FrameworkElement), |
||||||
|
typeof(SlideBehaviour), |
||||||
|
new UIPropertyMetadata(null)); |
||||||
|
|
||||||
|
public static FrameworkElement GetFrameworkElementHiddenBySlide(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
return frameworkElement.GetValue(FrameworkElementHiddenBySlideProperty) as FrameworkElement; |
||||||
|
} |
||||||
|
|
||||||
|
public static void SetFrameworkElementHiddenBySlide(FrameworkElement frameworkElement, FrameworkElement hiddenFrameworkElement) |
||||||
|
{ |
||||||
|
frameworkElement.SetValue(FrameworkElementHiddenBySlideProperty, hiddenFrameworkElement); |
||||||
|
} |
||||||
|
|
||||||
|
public static readonly DependencyProperty IsDisplayedProperty = |
||||||
|
DependencyProperty.RegisterAttached( |
||||||
|
"IsDisplayed", |
||||||
|
typeof(bool), |
||||||
|
typeof(SlideBehaviour), |
||||||
|
new UIPropertyMetadata(false, OnIsDisplayedChanged)); |
||||||
|
|
||||||
|
public static bool GetIsDisplayed(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
return (bool)frameworkElement.GetValue(IsDisplayedProperty); |
||||||
|
} |
||||||
|
|
||||||
|
public static void SetIsDisplayed(FrameworkElement frameworkElement, bool value) |
||||||
|
{ |
||||||
|
frameworkElement.SetValue(IsDisplayedProperty, value); |
||||||
|
} |
||||||
|
|
||||||
|
static void OnIsDisplayedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) |
||||||
|
{ |
||||||
|
var frameworkElement = dependencyObject as FrameworkElement; |
||||||
|
if (frameworkElement == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (e.NewValue is bool) { |
||||||
|
if ((bool)e.NewValue) { |
||||||
|
SlideIn(frameworkElement); |
||||||
|
} else { |
||||||
|
SlideOut(frameworkElement); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void SlideIn(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
AnimationTimeline marginAnimation = CreateSlideInAnimation(frameworkElement); |
||||||
|
frameworkElement.BeginAnimation(FrameworkElement.MarginProperty, marginAnimation); |
||||||
|
frameworkElement.Visibility = Visibility.Visible; |
||||||
|
DisableHiddenFrameworkElement(frameworkElement); |
||||||
|
} |
||||||
|
|
||||||
|
static AnimationTimeline CreateSlideInAnimation(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
var parent = frameworkElement.Parent as FrameworkElement; |
||||||
|
|
||||||
|
var marginAnimation = new ThicknessAnimation(); |
||||||
|
marginAnimation.From = new Thickness(parent.ActualWidth, 0, 0, 0); |
||||||
|
marginAnimation.To = new Thickness(0); |
||||||
|
marginAnimation.Duration = duration; |
||||||
|
return marginAnimation; |
||||||
|
} |
||||||
|
|
||||||
|
static readonly Duration duration = new Duration(new TimeSpan(TimeSpan.TicksPerMillisecond * 100)); |
||||||
|
|
||||||
|
static void DisableHiddenFrameworkElement(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
EnableHiddenFrameworkElement(frameworkElement, enabled: false); |
||||||
|
} |
||||||
|
|
||||||
|
static void EnableHiddenFrameworkElement(FrameworkElement frameworkElement, bool enabled) |
||||||
|
{ |
||||||
|
FrameworkElement hiddenFrameworkElement = GetFrameworkElementHiddenBySlide(frameworkElement); |
||||||
|
if (hiddenFrameworkElement != null) { |
||||||
|
hiddenFrameworkElement.IsEnabled = enabled; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void SlideOut(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
AnimationTimeline marginAnimation = CreateSlideOutAnimation(frameworkElement); |
||||||
|
frameworkElement.BeginAnimation(FrameworkElement.MarginProperty, marginAnimation); |
||||||
|
} |
||||||
|
|
||||||
|
static AnimationTimeline CreateSlideOutAnimation(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
var parent = frameworkElement.Parent as FrameworkElement; |
||||||
|
|
||||||
|
var marginAnimation = new ThicknessAnimation(); |
||||||
|
marginAnimation.From = new Thickness(0); |
||||||
|
marginAnimation.To = new Thickness(parent.ActualWidth, 0, 0, 0); |
||||||
|
marginAnimation.Duration = duration; |
||||||
|
marginAnimation.Completed += (sender, e) => OnSlideOutCompleted(frameworkElement); |
||||||
|
|
||||||
|
return marginAnimation; |
||||||
|
} |
||||||
|
|
||||||
|
static void OnSlideOutCompleted(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
frameworkElement.Visibility = Visibility.Collapsed; |
||||||
|
EnableHiddenFrameworkElement(frameworkElement); |
||||||
|
} |
||||||
|
|
||||||
|
static void EnableHiddenFrameworkElement(FrameworkElement frameworkElement) |
||||||
|
{ |
||||||
|
EnableHiddenFrameworkElement(frameworkElement, true); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using ICSharpCode.AspNet.Mvc; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace AspNet.Mvc.Tests.Helpers |
||||||
|
{ |
||||||
|
public static class MvcMasterPageFileNameAssert |
||||||
|
{ |
||||||
|
public static void AreEqual(MvcMasterPageFileName expected, MvcMasterPageFileName actual) |
||||||
|
{ |
||||||
|
string expectedAsString = GetMvcMasterPageFileNameAsString(expected); |
||||||
|
string actualAsString = GetMvcMasterPageFileNameAsString(actual); |
||||||
|
Assert.AreEqual(expectedAsString, actualAsString); |
||||||
|
} |
||||||
|
|
||||||
|
public static string GetMvcMasterPageFileNameAsString(MvcMasterPageFileName fileName) |
||||||
|
{ |
||||||
|
return String.Format( |
||||||
|
"FileName: {0}\r\nFolder: {1}\r\n, FullPath: {2}", |
||||||
|
fileName.FileName, |
||||||
|
fileName.FolderRelativeToProject, |
||||||
|
fileName.FullPath); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using ICSharpCode.AspNet.Mvc; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace AspNet.Mvc.Tests.Helpers |
||||||
|
{ |
||||||
|
public static class MvcMasterPageFileNameCollectionAssert |
||||||
|
{ |
||||||
|
public static void AreEqual(IEnumerable<MvcMasterPageFileName> expected, IEnumerable<MvcMasterPageFileName> actual) |
||||||
|
{ |
||||||
|
List<string> expectedAsStrings = GetMvcMasterPageFileNamesAsStrings(expected); |
||||||
|
List<string> actualAsStrings = GetMvcMasterPageFileNamesAsStrings(actual); |
||||||
|
|
||||||
|
CollectionAssert.AreEqual(expectedAsStrings, actualAsStrings); |
||||||
|
} |
||||||
|
|
||||||
|
static List<string> GetMvcMasterPageFileNamesAsStrings(IEnumerable<MvcMasterPageFileName> fileNames) |
||||||
|
{ |
||||||
|
var convertedFileNames = new List<string>(); |
||||||
|
foreach (MvcMasterPageFileName fileName in fileNames) { |
||||||
|
string fileNameAsString = MvcMasterPageFileNameAssert.GetMvcMasterPageFileNameAsString(fileName); |
||||||
|
convertedFileNames.Add(fileNameAsString); |
||||||
|
} |
||||||
|
return convertedFileNames; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,86 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using AspNet.Mvc.Tests.Helpers; |
||||||
|
using ICSharpCode.AspNet.Mvc; |
||||||
|
using ICSharpCode.SharpDevelop.Project; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace AspNet.Mvc.Tests |
||||||
|
{ |
||||||
|
[TestFixture] |
||||||
|
public class MvcMasterPageFileNameTests |
||||||
|
{ |
||||||
|
TestableProject project; |
||||||
|
MvcMasterPageFileName masterPageFileName; |
||||||
|
|
||||||
|
void CreateProject(string fileName) |
||||||
|
{ |
||||||
|
project = TestableProject.CreateProject(fileName, "MyProject"); |
||||||
|
} |
||||||
|
|
||||||
|
void CreateMasterPageFileName(string fullPath) |
||||||
|
{ |
||||||
|
var projectItem = new FileProjectItem(project, ItemType.Compile); |
||||||
|
projectItem.FileName = fullPath; |
||||||
|
masterPageFileName = new MvcMasterPageFileName(projectItem); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void FullPath_CreatedFromFileProjectItem_ReturnsFullFileNameIncludingFolder() |
||||||
|
{ |
||||||
|
CreateProject(@"d:\projects\MyProject\MyProject.csproj"); |
||||||
|
string expectedFullPath = @"d:\projects\MyProject\Views\Shared\Site.Master"; |
||||||
|
CreateMasterPageFileName(expectedFullPath); |
||||||
|
|
||||||
|
string fullPath = masterPageFileName.FullPath; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedFullPath, fullPath); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void FileName_CreatedFromFileProjectItem_ReturnsFileNameWithoutFolder() |
||||||
|
{ |
||||||
|
CreateProject(@"d:\projects\MyProject\MyProject.csproj"); |
||||||
|
CreateMasterPageFileName(@"d:\projects\MyProject\Views\Shared\Site.Master"); |
||||||
|
|
||||||
|
string fileName = masterPageFileName.FileName; |
||||||
|
|
||||||
|
Assert.AreEqual("Site.Master", fileName); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void FolderRelativeToProject_CreatedFromFileProjectItem_ReturnsFileNameWithoutFolder() |
||||||
|
{ |
||||||
|
CreateProject(@"d:\projects\MyProject\MyProject.csproj"); |
||||||
|
CreateMasterPageFileName(@"d:\projects\MyProject\Views\Shared\Site.Master"); |
||||||
|
|
||||||
|
string folder = masterPageFileName.FolderRelativeToProject; |
||||||
|
|
||||||
|
Assert.AreEqual(@"Views\Shared", folder); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void VirtualPath_CreatedFromFileProjectItem_ReturnsAspNetVirtualPathForFileName() |
||||||
|
{ |
||||||
|
CreateProject(@"d:\projects\MyProject\MyProject.csproj"); |
||||||
|
CreateMasterPageFileName(@"d:\projects\MyProject\Views\Shared\Site.Master"); |
||||||
|
|
||||||
|
string virtualPath = masterPageFileName.VirtualPath; |
||||||
|
|
||||||
|
Assert.AreEqual("~/Views/Shared/Site.Master", virtualPath); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void VirtualPath_FileInProjectRootDirectory_ReturnsAspNetVirtualPathForFileName() |
||||||
|
{ |
||||||
|
CreateProject(@"d:\projects\MyProject\MyProject.csproj"); |
||||||
|
CreateMasterPageFileName(@"d:\projects\MyProject\Site.Master"); |
||||||
|
|
||||||
|
string virtualPath = masterPageFileName.VirtualPath; |
||||||
|
|
||||||
|
Assert.AreEqual("~/Site.Master", virtualPath); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue