Exemple #1
0
        public async Task Server(String command, String baseUrl, Int64 userId, HttpResponseBase response)
        {
            var rm = await RequestModel.CreateFromBaseUrl(_host, Admin, baseUrl);

            RequestView rw       = rm.GetCurrentAction();
            String      loadProc = rw.LoadProcedure;

            if (loadProc == null)
            {
                throw new RequestModelException("The data model is empty");
            }
            var prms = new ExpandoObject();

            prms.Set("UserId", userId);
            prms.Set("Id", rw.Id);
            IDataModel model = await _dbContext.LoadModelAsync(rw.CurrentSource, loadProc, prms);

            var msi = new ModelScriptInfo()
            {
                DataModel = model,
                Template  = rw.template,
                Path      = rw.Path
            };
            var ss = _scripter.GetServerScript(msi);

            response.ContentType = "text/javascript";
            response.Write(ss.Script);
        }
Exemple #2
0
        public ScriptInfo GetServerScript(ModelScriptInfo msi)
        {
            StringBuilder sbRequired   = null;
            String        templateText = "{}";

            if (msi.Template != null)
            {
                String fileTemplateText = _host.ApplicationReader.ReadTextFile(msi.Path, msi.Template + ".js");
                if (fileTemplateText == null)
                {
                    throw new FileNotFoundException($"File not found. '{Path.Combine(msi.Path, msi.Template)}'");
                }
                sbRequired = new StringBuilder();
                AddRequiredModules(sbRequired, fileTemplateText);
                templateText = CreateTemplateForWrite(Localize(fileTemplateText));
            }
            var sb = new StringBuilder(SCRIPT_PARTS.DATAFUNC_SERVER);

            sb.Replace("$(TemplateText)", templateText);
            sb.Replace("$(RequiredModules)", sbRequired?.ToString());
            String modelScript = msi.DataModel.CreateScript(this);
            String rawData     = JsonConvert.SerializeObject(msi.DataModel.Root, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));

            sb.Replace("$(DataModelText)", rawData);
            sb.Replace("$(RawDataText)", msi.RawData ?? "{}");
            sb.Replace("$(ModelScript)", modelScript);

            return(new ScriptInfo()
            {
                Script = sb.ToString()
            });
        }
Exemple #3
0
        async Task <ViewInfo> LoadViewKind(String pathInfo, RequestUrlKind kind)
        {
            var host = _baseController.Host;
            var rm   = await RequestModel.CreateFromUrl(host, false, kind, NormalizeUrl(pathInfo));

            var rw     = rm.GetCurrentAction();
            var pageId = $"el{Guid.NewGuid()}";
            var dmrw   = await _baseController.GetDataModelForView(rw, CreateParams());

            rw = dmrw.RequestView;
            var viewInfo = new ViewInfo()
            {
                PageId    = pageId,
                View      = host.MakeRelativePath(rw.Path, $"{rw.GetView()}.cshtml"),
                Path      = rw.Path,
                BaseUrl   = rw.ParentModel.BasePath,
                DataModel = dmrw.Model,
                Id        = rw.Id
            };

            var msi = new ModelScriptInfo()
            {
                DataModel = viewInfo.DataModel,
                RootId    = pageId,
                IsDialog  = rw.IsDialog,
                Template  = rw.template,
                Path      = rw.Path,
                BaseUrl   = rw.ParentModel.BasePath
            };

            viewInfo.Scripts = await _baseController.Scripter.GetModelScript(msi);

            return(viewInfo);
        }
Exemple #4
0
        public async Task <ScriptInfo> GetModelScript(ModelScriptInfo msi)
        {
            var           result        = new ScriptInfo();
            StringBuilder output        = new StringBuilder();
            String        dataModelText = "{}";
            String        templateText  = "{}";
            StringBuilder sbRequired    = new StringBuilder();

            // write model script
            String fileTemplateText = null;

            if (msi.Template != null)
            {
                fileTemplateText = await _host.ApplicationReader.ReadTextFileAsync(msi.Path, msi.Template + ".js");

                if (fileTemplateText == null)
                {
                    throw new FileNotFoundException($"Template file '{Path.Combine(msi.Path, msi.Template + ".js").Replace('\\', '/')}' not found.");
                }
                AddRequiredModules(sbRequired, fileTemplateText);
                templateText = CreateTemplateForWrite(Localize(fileTemplateText));
            }
            if (msi.DataModel != null)
            {
                dataModelText = JsonConvert.SerializeObject(msi.DataModel.Root, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));
            }

            var header = new StringBuilder(SCRIPT_PARTS.HEADER);

            header.Replace("$(RootId)", msi.RootId);

            var modelFunc = new StringBuilder(SCRIPT_PARTS.DATAFUNC);

            modelFunc.Replace("$(RequiredModules)", sbRequired?.ToString());
            modelFunc.Replace("$(TemplateText)", Localize(templateText));
            modelFunc.Replace("$(DataModelText)", dataModelText);
            String modelScript = CreateDataModelScript(msi.DataModel);

            modelFunc.Replace("$(ModelScript)", modelScript);
            result.DataScript = modelFunc.ToString();

            header.Replace("$(CurrentModule)", modelFunc.ToString());
            output.Append(header);

            var footer = new StringBuilder(SCRIPT_PARTS.FOOTER);

            footer.Replace("$(RootId)", msi.RootId);
            footer.Replace("$(BaseUrl)", msi.BaseUrl);
            footer.Replace("$(IsDialog)", msi.IsDialog.ToString().ToLowerInvariant());
            footer.Replace("$(IsIndex)", msi.IsIndex.ToString().ToLowerInvariant());
            footer.Replace("$(IsSkipDataStack)", msi.IsSkipDataStack.ToString().ToLowerInvariant());
            output.Append(footer);
            result.Script = output.ToString();

            return(result);
        }
Exemple #5
0
        public async Task <ScriptInfo> GetModelScript(ModelScriptInfo msi)
        {
            var           result        = new ScriptInfo();
            StringBuilder output        = new StringBuilder();
            String        dataModelText = "{}";
            String        templateText  = "{}";
            StringBuilder sbRequired    = new StringBuilder();

            // write model script
            String fileTemplateText = null;

            if (msi.Template != null)
            {
                //fileTemplateText = await _host.ReadTextFileAsync(msi.Admin, msi.Path, msi.Template + ".js");
                fileTemplateText = await _host.ApplicationReader.ReadTextFileAsync(msi.Path, msi.Template + ".js");

                AddRequiredModules(sbRequired, fileTemplateText);
                templateText = CreateTemplateForWrite(Localize(fileTemplateText));
            }
            if (msi.DataModel != null)
            {
                dataModelText = JsonConvert.SerializeObject(msi.DataModel.Root, JsonHelpers.StandardSerializerSettings);
            }

            var header = new StringBuilder(SCRIPT_PARTS.HEADER);

            header.Replace("$(RootId)", msi.RootId);

            var modelFunc = new StringBuilder(SCRIPT_PARTS.DATAFUNC);

            modelFunc.Replace("$(RequiredModules)", sbRequired?.ToString());
            modelFunc.Replace("$(TemplateText)", Localize(templateText));
            modelFunc.Replace("$(DataModelText)", dataModelText);
            String modelScript = CreateDataModelScript(msi.DataModel);

            modelFunc.Replace("$(ModelScript)", modelScript);
            result.DataScript = modelFunc.ToString();

            header.Replace("$(CurrentModule)", modelFunc.ToString());
            output.Append(header);

            var footer = new StringBuilder(SCRIPT_PARTS.FOOTER);

            footer.Replace("$(RootId)", msi.RootId);
            footer.Replace("$(BaseUrl)", msi.BaseUrl);
            footer.Replace("$(IsDialog)", msi.IsDialog.ToString().ToLowerInvariant());
            output.Append(footer);
            result.Script = output.ToString();

            return(result);
        }
Exemple #6
0
        Object RunDataScript(IScriptContext sc, ServerScriptInfo ssi, String operation)
        {
            var msi = new ModelScriptInfo()
            {
                DataModel = ssi.DataModel,
                Template  = ssi.Template,
                Path      = ssi.Path,
                RawData   = ssi.RawData
            };
            var ss = _scripter.GetServerScript(msi);

            sc.RunScript(ss.Script, null);
            var opScript = LoadFile(operation);

            return(sc.RunScript(opScript, ssi.Parameter));
        }
Exemple #7
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 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() + ".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,
                        CurrentLocale        = null,
                        IsDebugConfiguration = _host.IsDebugConfiguration,
                        SecondPhase          = secondPhase
                    };
                    _renderer.Render(ri);
                    // write markup
                    writer.Write(strWriter.ToString());
                    bRendered = true;
                }
            }
            else
            {
                // try html
                fileName = rw.GetView() + ".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);
                            writer.Write(htmlText);
                            bRendered = true;
                        }
                    }
                }
            }
            if (!bRendered)
            {
                throw new RequestModelException($"The view '{rw.GetView()}' was not found. The following locations were searched:\n{rw.GetRelativePath(".xaml")}\n{rw.GetRelativePath(".html")}");
            }
            writer.Write(modelScript);
        }
 public ScriptInfo GetServerScript(ModelScriptInfo msi)
 {
     throw new NotImplementedException();
 }
 public Task <ScriptInfo> GetModelScript(ModelScriptInfo msi)
 {
     throw new NotImplementedException();
 }