Ejemplo n.º 1
0
        protected internal async Task Render(RequestView rwArg, TextWriter writer, ExpandoObject loadPrms, Boolean secondPhase = false)
        {
            var dmAndView = await GetDataModelForView(rwArg, loadPrms);

            IDataModel model = dmAndView.Model;
            var        rw    = dmAndView.RequestView;

            String rootId = "el" + Guid.NewGuid().ToString();

            if (_renderer == null)
            {
                throw new InvalidOperationException("Service 'IRenderer' not registered");
            }

            var typeChecker = _host.CheckTypes(rw.Path, rw.checkTypes, model);

            var msi = new ModelScriptInfo()
            {
                DataModel = model,
                RootId    = rootId,
                IsDialog  = rw.IsDialog,
                Template  = rw.template,
                Path      = rw.Path,
                BaseUrl   = rw.ParentModel.BasePath
            };
            var si = await _scripter.GetModelScript(msi);

            String modelScript = si.Script;
            // TODO: use view engines
            // try xaml
            String fileName = rw.GetView(_host.Mobile) + ".xaml";
            String basePath = rw.ParentModel.BasePath;

            String filePath = _host.ApplicationReader.MakeFullPath(rw.Path, fileName);

            Boolean bRendered = false;

            if (_host.ApplicationReader.FileExists(filePath))
            {
                // render XAML
                using (var strWriter = new StringWriter())
                {
                    var ri = new RenderInfo()
                    {
                        RootId               = rootId,
                        FileName             = filePath,
                        FileTitle            = fileName,
                        Path                 = basePath,
                        Writer               = strWriter,
                        DataModel            = model,
                        Localizer            = _localizer,
                        TypeChecker          = typeChecker,
                        CurrentLocale        = null,
                        IsDebugConfiguration = _host.IsDebugConfiguration,
                        SecondPhase          = secondPhase
                    };
                    _renderer.Render(ri);
                    // write markup
                    writer.Write(strWriter.ToString());
                    bRendered = true;
                }
            }
            else
            {
                // try html
                fileName = rw.GetView(_host.Mobile) + ".html";
                filePath = _host.ApplicationReader.MakeFullPath(rw.Path, fileName);
                if (_host.ApplicationReader.FileExists(filePath))
                {
                    using (_host.Profiler.CurrentRequest.Start(ProfileAction.Render, $"render: {fileName}"))
                    {
                        using (var tr = new StreamReader(filePath))
                        {
                            String htmlText = await tr.ReadToEndAsync();

                            htmlText = htmlText.Replace("$(RootId)", rootId);
                            htmlText = _localizer.Localize(null, htmlText, false);
                            writer.Write(htmlText);
                            bRendered = true;
                        }
                    }
                }
            }
            if (!bRendered)
            {
                throw new RequestModelException($"The view '{rw.GetView(_host.Mobile)}' was not found. The following locations were searched:\n{rw.GetRelativePath(".xaml", _host.Mobile)}\n{rw.GetRelativePath(".html", _host.Mobile)}");
            }
            await ProcessDbEvents(rw);

            writer.Write(modelScript);
        }