// VideoLibrary DEMO - DaST Technology Demonstrator
// Copyright (C) 2011 Roman Gubarenko
// http://www.rgubarenko.net
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Licence terms for AspNetDaST.dll (in Bin folder) are below.
//
// ASP.NET DaST Rendering Engine (BETA version)
// Copyright (C) 2011 Roman Gubarenko
// http://www.rgubarenko.net
//
// Redistribution and use of BETA version of DaST Rendering Engine
// is permitted in binary form without modification. Only original and
// unmodified .NET dll (AspNetDaST.dll) can be used in your own projects and
// redistributed. You may not copy, decompile, reverse engineer, disassemble,
// modify, or create derivative works of the software or any part thereof.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.
//
// DaST technology dev site: http://www.rgubarenko.net
// My contact email for questions: rgubarenko@gmail.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Web.Script.Serialization;
using AspNetDaST.Web;
/// <summary>
/// Summary description for DetailsPopupController
/// </summary>
public class DetailsPopupController : ScopeController
{
public override void SetTemplate(SetTemplateArgs template)
{
template.Markup = Utils.LoadTemplate("DetailsPopupTemplate.htm");
}
public override void SetupModel(ControllerModelBuilder model)
{
model.SetDataBind(new DataBindHandler(ROOT_DataBind));
model.Select("DetailsContent").SetDataBind(new DataBindHandler(DetailsContent_DataBind));
model.Select("DetailsContent", "ReviewsInfo").SetDataBind(new DataBindHandler(ReviewsInfo_DataBind));
model.Select("DetailsContent", "AlikeVideosHeader").SetDataBind(new DataBindHandler(AlikeVideosHeader_DataBind));
model.Select("DetailsContent", "AlikeVideosPager").SetController(new PagerController());
model.Select("DetailsContent", "AlikeVideosRepeater").SetDataBind(new DataBindHandler(AlikeVideosRepeater_DataBind));
model.Select("DetailsContent", "AlikeVideosRepeater", "VideoItem").SetController(new VideoItemController());
model.Select("DetailsContent", "CommentsContent").SetDataBind(new DataBindHandler(CommentsContent_DataBind));
model.Select("DetailsContent", "CommentsContent", "CommentsHeader").SetDataBind(new DataBindHandler(CommentsHeader_DataBind));
model.Select("DetailsContent", "CommentsContent", "CommentsPager").SetController(new PagerController());
model.Select("DetailsContent", "CommentsContent", "CommentsRepeater").SetDataBind(new DataBindHandler(CommentsRepeater_DataBind));
model.Select("DetailsContent", "AddCommentScreen").SetDataBind(new DataBindHandler(AddCommentScreen_DataBind));
model.HandleAction("LoadContent", new ActionHandler(Action_LoadContent));
model.HandleAction("LoadComments", new ActionHandler(Action_LoadComments));
model.HandleAction("ShowAddCommentScreen", new ActionHandler(Action_ShowAddCommentScreen));
model.HandleAction("SubmitComment", new ActionHandler(Action_SubmitComment));
model.HandleAction("CancelComment", new ActionHandler(Action_CancelComment));
model.Select("DetailsContent", "AlikeVideosPager").HandleAction("NextPage", new ActionHandler(AlikeVideosPager_NextPage));
model.Select("DetailsContent", "AlikeVideosPager").HandleAction("PrevPage", new ActionHandler(AlikeVideosPager_PrevPage));
model.Select("DetailsContent", "AlikeVideosRepeater", "VideoItem").HandleAction("PlaylistUpdated", new ActionHandler(VideoItem_PlaylistUpdated));
model.Select("DetailsContent", "CommentsContent", "CommentsPager").HandleAction("NextPage", new ActionHandler(CommentsPager_PrevPage_NextPage));
model.Select("DetailsContent", "CommentsContent", "CommentsPager").HandleAction("PrevPage", new ActionHandler(CommentsPager_PrevPage_NextPage));
}
private void Action_LoadContent(ActionArgs args)
{
string videoID = (string)args.ActionData;
ControlPath.Fwd("DetailsContent").Scope.Refresh();
ControlPath.Fwd("DetailsContent").Scope.SetParam("VideoID", videoID);
}
private void Action_LoadComments(ActionArgs args)
{
string videoID = ControlPath.Fwd("DetailsContent").Scope.Param<string>("VideoID");
var videoItem = DataLayer.GetVideoItem(videoID);
int commCount = (int)DaSTUtils.GetValue("CommCount", videoItem);
if (commCount > 0)
{
ControlPath.Fwd("DetailsContent", "CommentsContent").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "CommentsContent").Scope.SetParam("VideoID", videoID);
ControlPath.Fwd("DetailsContent", "AddCommentScreen").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AddCommentScreen").Scope.RenderType = ScopeRenderType.Empty;
}
else
{
Action_ShowAddCommentScreen(null);
}
}
private void Action_ShowAddCommentScreen(ActionArgs args)
{
ControlPath.Fwd("DetailsContent", "AddCommentScreen").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "CommentsContent").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "CommentsContent").Scope.RenderType = ScopeRenderType.Empty;
}
private void Action_SubmitComment(ActionArgs args)
{
string videoID = ControlPath.Fwd("DetailsContent").Scope.Param<string>("VideoID");
var userInput = DaSTUtils.ParseJSON((string)args.ActionData);
string name = ((string)userInput["Name"]).Trim();
int rated = (int)userInput["Rated"];
string comment = ((string)userInput["Comment"]).Trim();
try
{
if (string.IsNullOrEmpty(name)) throw new Exception("Name is required");
else if (name.Length > 50) throw new Exception("Name cannot exceed 50 chars");
else if (string.IsNullOrEmpty(comment)) throw new Exception("Comment is required");
else if (comment.Length > 300) throw new Exception("Comment cannot exceed 300 chars");
DataLayer.AddNewComment(videoID, name, rated, comment);
// refresh review infos on the content screen
var videoItem = DataLayer.GetVideoItem(videoID);
ControlPath.Fwd("DetailsContent", "ReviewsInfo").Scope.SetParams(videoItem, new string[] { "Rating", "CommCount" });
ControlPath.Fwd("DetailsContent", "ReviewsInfo").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "ReviewsInfo2").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AddCommentScreen").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AddCommentScreen").Scope.RenderType = ScopeRenderType.Empty;
ControlPath.Fwd("DetailsContent", "CommentsContent").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "CommentsContent").Scope.SetParam("VideoID", videoID);
int commCount = (int)DaSTUtils.GetValue("CommCount", videoItem);
ControlPath.Fwd("DetailsContent", "CommentsContent", "CommentsPager").Scope.Method("UpdatePagerValues", new { StartItemIdx = commCount - 1, PageSize = 3, ItemTotalCount = commCount });
}
catch (Exception ex)
{
ControlPath.Fwd("DetailsContent", "AddCommentScreen", "ErrorDisplay").Scope.Replace("{ErrorMessage}", ex.Message);
ControlPath.Fwd("DetailsContent", "AddCommentScreen", "ErrorDisplay").Scope.Refresh();
}
}
private void Action_CancelComment(ActionArgs args)
{
Action_LoadComments(null);
}
private void AlikeVideosPager_NextPage(ActionArgs args)
{
ControlPath.Fwd("DetailsContent", "AlikeVideosHeader").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AlikeVideosRepeater").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AlikeVideosPager").Scope.Refresh();
}
private void AlikeVideosPager_PrevPage(ActionArgs args)
{
ControlPath.Fwd("DetailsContent", "AlikeVideosHeader").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AlikeVideosRepeater").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "AlikeVideosPager").Scope.Refresh();
}
private void VideoItem_PlaylistUpdated(ActionArgs args)
{
RaiseAction("PlaylistUpdated", null);
}
private void CommentsPager_PrevPage_NextPage(ActionArgs args)
{
ControlPath.Fwd("DetailsContent", "CommentsContent", "CommentsHeader").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "CommentsContent", "CommentsRepeater").Scope.Refresh();
ControlPath.Fwd("DetailsContent", "CommentsContent", "CommentsPager").Scope.Refresh();
}
private void ROOT_DataBind()
{
ControlPath.Fwd("DetailsContent").Scope.RenderType = ScopeRenderType.Empty;
CurrentPath.Scope.Replace("{DetailsContent_ScopeID}", CurrentPath.Fwd("DetailsContent").Scope.ScopeClientID);
CurrentPath.Scope.Replace("{CommentsContent_ScopeID}", CurrentPath.Fwd("DetailsContent", "CommentsContent").Scope.ScopeClientID);
CurrentPath.Scope.Replace("{AddCommentScreen_ScopeID}", CurrentPath.Fwd("DetailsContent", "AddCommentScreen").Scope.ScopeClientID);
CurrentPath.Scope.Replace("{ErrorDisplay_ScopeID}", CurrentPath.Fwd("DetailsContent", "AddCommentScreen", "ErrorDisplay").Scope.ScopeClientID);
}
private void DetailsContent_DataBind()
{
string videoID = CurrentPath.Scope.Param<string>("VideoID");
CurrentPath.Fwd("CommentsContent").Scope.RenderType = ScopeRenderType.Empty;
CurrentPath.Fwd("AddCommentScreen").Scope.RenderType = ScopeRenderType.Empty;
Thread.Sleep(1000);
var videoItem = DataLayer.GetVideoItem(videoID);
CurrentPath.Scope.ReplaceRange(videoItem, new string[] { "ID", "Name", "Category", "Description" }, "{{name}}");
CurrentPath.Fwd("ReviewsInfo").Scope.SetParams(videoItem, new string[] { "Rating", "CommCount" });
// initialize category videos pager
string videoCategory = (string)DaSTUtils.GetValue("Category", videoItem);
int videoCount = DataLayer.GetVideoItemCount(videoCategory);
CurrentPath.Fwd("AlikeVideosPager").Scope.SetParam("StartItemIdx", 0);
CurrentPath.Fwd("AlikeVideosPager").Scope.SetParam("PageSize", 3);
CurrentPath.Fwd("AlikeVideosPager").Scope.SetParam("ItemTotalCount", videoCount);
// save category in similar items repeater scope
CurrentPath.Fwd("AlikeVideosRepeater").Scope.SetParam("VideoCategory", videoCategory);
}
private void ReviewsInfo_DataBind()
{
decimal rating = CurrentPath.Scope.Param<decimal>("Rating");
int commCount = CurrentPath.Scope.Param<int>("CommCount");
CurrentPath.Scope.ShowConditionArea("comm-empty:yes", commCount <= 0);
CurrentPath.Scope.ShowConditionArea("comm-empty:no", commCount > 0);
CurrentPath.Scope.Replace("{Rating}", rating.ToString("0.0"));
CurrentPath.Scope.Replace("{CommCount}", commCount);
CurrentPath.Rew(1).Fwd("ReviewsInfo2").Scope.ShowConditionArea("comm-empty:yes", commCount <= 0);
CurrentPath.Rew(1).Fwd("ReviewsInfo2").Scope.ShowConditionArea("comm-empty:no", commCount > 0);
CurrentPath.Rew(1).Fwd("ReviewsInfo2").Scope.Replace("{Rating}", rating.ToString("0.0"));
CurrentPath.Rew(1).Fwd("ReviewsInfo2").Scope.Replace("{CommCount}", commCount);
}
private void AlikeVideosHeader_DataBind()
{
int startItemIdx = CurrentPath.Rew(1).Fwd("AlikeVideosPager").Scope.Param<int>("StartItemIdx");
int pageSize = CurrentPath.Rew(1).Fwd("AlikeVideosPager").Scope.Param<int>("PageSize");
int itemTotalCount = CurrentPath.Rew(1).Fwd("AlikeVideosPager").Scope.Param<int>("ItemTotalCount");
CurrentPath.Scope.Replace("{FromItemNum}", startItemIdx + 1);
CurrentPath.Scope.Replace("{ToItemNum}", Math.Min(startItemIdx + pageSize, itemTotalCount));
CurrentPath.Scope.Replace("{TotalItemCount}", itemTotalCount);
}
private void AlikeVideosRepeater_DataBind()
{
string videoCategory = CurrentPath.Scope.Param<string>("VideoCategory");
int startItemIdx = CurrentPath.Rew(1).Fwd("AlikeVideosPager").Scope.Param<int>("StartItemIdx");
int pageSize = CurrentPath.Rew(1).Fwd("AlikeVideosPager").Scope.Param<int>("PageSize");
int itemTotalCount = CurrentPath.Rew(1).Fwd("AlikeVideosPager").Scope.Param<int>("ItemTotalCount");
object[] videoItems = DataLayer.GetVideoItems(startItemIdx, pageSize, videoCategory);
CurrentPath.Scope.RestartRepeater();
for (int i = 0; i < videoItems.Length; i++)
{
CurrentPath.Scope.Repeat();
CurrentPath.Fwd(i, "VideoItem").Scope.SetParam("ItemIndex", startItemIdx + i);
CurrentPath.Fwd(i, "VideoItem").Scope.SetParam("VideoItemObject", videoItems[i]);
}
}
private void CommentsContent_DataBind()
{
Thread.Sleep(1000);
string videoID = CurrentPath.Scope.Param<string>("VideoID");
var videoItem = DataLayer.GetVideoItem(videoID);
CurrentPath.Scope.Replace("{Rating}", ((decimal)DaSTUtils.GetValue("Rating", videoItem)).ToString("0.00"));
CurrentPath.Scope.Replace("{CommCount}", DaSTUtils.GetValue("CommCount", videoItem));
// initialize comment pager
int commCount = (int)DaSTUtils.GetValue("CommCount", videoItem);
CurrentPath.Fwd("CommentsPager").Scope.InitParam("StartItemIdx", 0);
CurrentPath.Fwd("CommentsPager").Scope.SetParam("PageSize", 3);
CurrentPath.Fwd("CommentsPager").Scope.SetParam("ItemTotalCount", commCount);
// save video id in comment repeater scope
CurrentPath.Fwd("CommentsRepeater").Scope.SetParam("VideoID", videoID);
}
private void CommentsHeader_DataBind()
{
int startItemIdx = CurrentPath.Rew(1).Fwd("CommentsPager").Scope.Param<int>("StartItemIdx");
int pageSize = CurrentPath.Rew(1).Fwd("CommentsPager").Scope.Param<int>("PageSize");
int itemTotalCount = CurrentPath.Rew(1).Fwd("CommentsPager").Scope.Param<int>("ItemTotalCount");
CurrentPath.Scope.Replace("{FromItemNum}", startItemIdx + 1);
CurrentPath.Scope.Replace("{ToItemNum}", Math.Min(startItemIdx + pageSize, itemTotalCount));
CurrentPath.Scope.Replace("{TotalItemCount}", itemTotalCount);
}
private void CommentsRepeater_DataBind()
{
string videoID = CurrentPath.Scope.Param<string>("VideoID");
int startItemIdx = CurrentPath.Rew(1).Fwd("CommentsPager").Scope.Param<int>("StartItemIdx");
int pageSize = CurrentPath.Rew(1).Fwd("CommentsPager").Scope.Param<int>("PageSize");
int itemTotalCount = CurrentPath.Rew(1).Fwd("CommentsPager").Scope.Param<int>("ItemTotalCount");
object[] comments = DataLayer.GetVideoComments(startItemIdx, pageSize, videoID);
CurrentPath.Scope.RestartRepeater();
for (int i = 0; i < comments.Length; i++)
{
CurrentPath.Scope.Repeat();
CurrentPath.Scope.Replace("{Num}", startItemIdx + i + 1);
CurrentPath.Scope.ReplaceRange(comments[i], new string[] { "CommID", "Date", "Author", "Rated", "Text" }, "{{name}}");
}
}
private void AddCommentScreen_DataBind()
{
Thread.Sleep(1000);
ControlPath.Fwd("DetailsContent", "AddCommentScreen", "ErrorDisplay").Scope.RenderType = ScopeRenderType.Empty;
}
}