protected override void OnActivated(Session aSession) { // get the page model for this session IPageModel model = GetSessionModel(aSession); // enable analytics and send page view event if tracking aSession.Tracker.SetTracking(aSession.Tracking); aSession.Tracker.TrackPageView(Id); // update any ui widgets that reflect tracking state aSession.Send("DataCollectionEnabled", aSession.Tracking); // render widgets foreach (PageDefinitions.Widget widget in iPageDefinition.Widgets) { // get the value of the data from the page model Assert.Check(model != null); System.Reflection.PropertyInfo prop = model.GetType().GetProperty(widget.DataId); string data = prop.GetValue(model, null) as string; // create the json object OpenHome.Xapp.JsonObject json = new OpenHome.Xapp.JsonObject(); // add the basic required properties json.Add("Id", new OpenHome.Xapp.JsonValueString(widget.Id)); json.Add("DataId", new OpenHome.Xapp.JsonValueString(widget.DataId)); json.Add("Value", new OpenHome.Xapp.JsonValueString(data)); // add the optional allowed values if (widget.AllowedValues != null || widget.AllowedValuesSource != null) { // get the list of string allowed values from either the widget XML or an IPageModel property string[] allowedValuesStr = (widget.AllowedValues != null) ? widget.AllowedValues : model.GetType().GetProperty(widget.AllowedValuesSource).GetValue(model, null) as string[]; // create the json array of allowed values OpenHome.Xapp.JsonArray <OpenHome.Xapp.JsonValueString> allowedValues = new OpenHome.Xapp.JsonArray <OpenHome.Xapp.JsonValueString>(); foreach (string value in allowedValuesStr) { allowedValues.Add(new OpenHome.Xapp.JsonValueString(value)); } json.Add("AllowedValues", allowedValues); } // render the widget aSession.Send(widget.XappEvent, json); } }
/// <summary> /// Resolves the path. /// </summary> /// <param name="virtualUrl">The virtual URL.</param> /// <returns></returns> public IPathData ResolvePath(string virtualUrl) { // Set the default action to index _pathData.Action = PageRoute.DefaultAction; // Get an up to date page repository //_pageService = _container.Resolve<IPageServiceFacade>(); // The requested url is for the start page with no action if (string.IsNullOrEmpty(virtualUrl) || string.Equals(virtualUrl, "/") || virtualUrl.StartsWith("classifieds")) { //_pageModel = _pageService.SingleOrDefault<IPageModel>(x => x.Parent == null); //_pageModel = _pageService.GetPageByUrl(null); //This is a site home page _pageModel = null; // new Home(); } else { // Remove the trailing slash virtualUrl = VirtualPathUtility.RemoveTrailingSlash(virtualUrl); // The normal beahaviour should be to load the page based on the url //_pageModel = _pageService.GetPageByUrl<IPageModel>(virtualUrl); _pageModel = _pageService.GetPageByUrl(virtualUrl); // Try to load the page without the last segment of the url and set the last segment as action)) if (_pageModel == null && virtualUrl.LastIndexOf("/") > 0) { var index = virtualUrl.LastIndexOf("/"); var action = virtualUrl.Substring(index, virtualUrl.Length - index).Trim(new[] { '/' }); virtualUrl = virtualUrl.Substring(0, index).TrimStart(new[] { '/' }); //_pageModel = _pageService.GetPageByUrl<IPageModel>(virtualUrl); _pageModel = _pageService.GetPageByUrl(virtualUrl); _pathData.Action = action; } // If the page model still is empty, let's try to resolve if the start page has an action named (virtualUrl) if (_pageModel == null) { //_pageModel = _pageService.SingleOrDefault<IPageModel>(x => x.Parent == null); _pageModel = _pageService.GetPageByUrl(null); var pageModelAttribute = _pageModel.GetType().GetAttribute<PageModelAttribute>(); _controllerName = _controllerMapper.GetControllerName(pageModelAttribute.ControllerType); var action = virtualUrl.TrimStart(new[] { '/' }); if (!_controllerMapper.ControllerHasAction(_controllerName, action)) { return null; } _pathData.Action = action; } } if (_pageModel == null) { return null; } var controllerType = _pageModel.GetType().GetAttribute<PageModelAttribute>().ControllerType; _pathData.Controller = _controllerMapper.GetControllerName(controllerType); _pathData.CurrentPageModel = _pageModel; return _pathData; }
/// <summary> /// Resolves the path. /// </summary> /// <param name="routeData">The route data.</param> /// <param name="virtualUrl">The virtual URL.</param> /// <returns></returns> public IPathData ResolvePath(RouteData routeData, string virtualUrl) { // Set the default action to index _pathData.Action = UIRoute.DefaultAction; // Get an up to date document session from structuremap _session = _container.GetInstance<IDocumentSession>(); // The requested url is for the start page with no action if (string.IsNullOrEmpty(virtualUrl) || string.Equals(virtualUrl, "/")) { _pageModel = _session.Query<IPageModel>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .SingleOrDefault(x => x.Parent == null); } else { // Remove the trailing slash virtualUrl = VirtualPathUtility.RemoveTrailingSlash(virtualUrl).TrimStart(new[] { '/' }); // The normal beahaviour should be to load the page based on the url _pageModel = _session.Query<IPageModel, PageByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); // Try to load the page without the last segment of the url and set the last segment as action)) if (_pageModel == null && virtualUrl.LastIndexOf("/") > 0) { var index = virtualUrl.LastIndexOf("/"); var action = virtualUrl.Substring(index, virtualUrl.Length - index).Trim(new[] { '/' }); virtualUrl = virtualUrl.Substring(0, index).TrimStart(new[] { '/' }); _pageModel = _session.Query<IPageModel, PageByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); _pathData.Action = action; } // If the page model still is empty, let's try to resolve if the start page has an action named (virtualUrl) if (_pageModel == null) { _pageModel = _session.Query<IPageModel>().SingleOrDefault(x => x.Parent == null); if(_pageModel == null) { return null; } var pageTypeAttribute = _pageModel.GetType().GetAttribute<PageTypeAttribute>(); object area; _controllerName = _controllerMapper.GetControllerName(routeData.Values.TryGetValue("area", out area) ? typeof(PagesController) : pageTypeAttribute.ControllerType); var action = virtualUrl.TrimStart(new[] { '/' }); if (!_controllerMapper.ControllerHasAction(_controllerName, action)) { return null; } _pathData.Action = action; } } if (_pageModel == null) { return null; } var controllerType = _pageModel.GetType().GetAttribute<PageTypeAttribute>().ControllerType; _pathData.Controller = controllerType != null ? _controllerMapper.GetControllerName(controllerType) : string.Format("{0}Controller", _pageModel.GetType().Name); _pathData.CurrentPage = _pageModel; _pathData.NavigationContext = _session.GetPublishedPages(_pageModel.Id); return _pathData; }
protected override void OnReceive(Session aSession, string aName, string aValue) { switch (aName) { case "PreviousPage": aSession.Navigator.PreviousPage(aSession); return; case "ReturnToPage": aSession.Navigator.PreviousPage(aSession, aValue); return; case "LinkClicked": System.Diagnostics.Process.Start(aValue); return; case "DataCollectionEnabled": aSession.Tracking = bool.Parse(aValue); return; } if (aName.StartsWith("setData")) { // get the page model for this session IPageModel model = GetSessionModel(aSession); Assert.Check(model != null); // get the data id to set string dataId = aName.Substring(7); System.Reflection.PropertyInfo prop = model.GetType().GetProperty(dataId); prop.SetValue(model, aValue, null); } if (aName.StartsWith("action")) { // get the page model for this session IPageModel model = GetSessionModel(aSession); // get the action id to process string actionId = aName.Substring(6); // process all actions with the given id foreach (PageDefinitions.Action action in iPageDefinition.Actions) { if (action.Id == actionId) { // process <Action type="navigate"> actions PageDefinitions.ActionNavigate actionNav = action as PageDefinitions.ActionNavigate; if (actionNav != null) { if (actionNav.Source != null) { Assert.Check(model != null); // get next page name from data model System.Reflection.PropertyInfo prop = model.GetType().GetProperty(actionNav.Source); string pageId = prop.GetValue(model, null) as string; aSession.Navigator.NextPage(aSession, pageId); } else { // jump to specified page id aSession.Navigator.NextPage(aSession, actionNav.PageId); } } // process <Action type="data"> actions PageDefinitions.ActionData actionData = action as PageDefinitions.ActionData; if (actionData != null) { Assert.Check(model != null); System.Reflection.PropertyInfo prop = model.GetType().GetProperty(actionData.DataId); prop.SetValue(model, (actionData.DataValue != null) ? actionData.DataValue : aValue, null); } // process <Action type="method"> actions PageDefinitions.ActionMethod actionMethod = action as PageDefinitions.ActionMethod; if (actionMethod != null) { Assert.Check(model != null); System.Reflection.MethodInfo method = model.GetType().GetMethod(actionMethod.MethodId); method.Invoke(model, null); } } } } }
/// <summary> /// Resolves the path. /// </summary> /// <param name="routeData">The route data.</param> /// <param name="virtualUrl">The virtual URL.</param> /// <returns></returns> public IPathData ResolvePath(RouteData routeData, string virtualUrl) { // Set the default action to index _pathData.Action = UIRoute.DefaultAction; // Get an up to date document session from structuremap _session = _container.GetInstance <IDocumentSession>(); // The requested url is for the start page with no action if (string.IsNullOrEmpty(virtualUrl) || string.Equals(virtualUrl, "/")) { _pageModel = _session.Query <IPageModel>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .SingleOrDefault(x => x.Parent == null); } else { // Remove the trailing slash virtualUrl = VirtualPathUtility.RemoveTrailingSlash(virtualUrl).TrimStart(new[] { '/' }); // The normal beahaviour should be to load the page based on the url _pageModel = _session.Query <IPageModel, PageByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); // Try to load the page without the last segment of the url and set the last segment as action)) if (_pageModel == null && virtualUrl.LastIndexOf("/") > 0) { var index = virtualUrl.LastIndexOf("/"); var action = virtualUrl.Substring(index, virtualUrl.Length - index).Trim(new[] { '/' }); virtualUrl = virtualUrl.Substring(0, index).TrimStart(new[] { '/' }); _pageModel = _session.Query <IPageModel, PageByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); _pathData.Action = action; } // If the page model still is empty, let's try to resolve if the start page has an action named (virtualUrl) if (_pageModel == null) { _pageModel = _session.Query <IPageModel>().SingleOrDefault(x => x.Parent == null); if (_pageModel == null) { return(null); } var pageTypeAttribute = _pageModel.GetType().GetAttribute <PageTypeAttribute>(); object area; _controllerName = _controllerMapper.GetControllerName(routeData.Values.TryGetValue("area", out area) ? typeof(PagesController) : pageTypeAttribute.ControllerType); var action = virtualUrl.TrimStart(new[] { '/' }); if (!_controllerMapper.ControllerHasAction(_controllerName, action)) { return(null); } _pathData.Action = action; } } if (_pageModel == null) { return(null); } var controllerType = _pageModel.GetType().GetAttribute <PageTypeAttribute>().ControllerType; _pathData.Controller = controllerType != null?_controllerMapper.GetControllerName(controllerType) : string.Format("{0}Controller", _pageModel.GetType().Name); _pathData.CurrentPage = _pageModel; _pathData.NavigationContext = _session.GetPublishedPages(_pageModel.Id); return(_pathData); }