From 0bfb036be61a7200e4e1d29de8ee2dc8efdb26b9 Mon Sep 17 00:00:00 2001 From: Serik Ibragimov Date: Mon, 18 Mar 2024 18:03:10 +0500 Subject: [PATCH] refactor(csharp-mvc): change formats retrieval method on frontend --- .../csharp-mvc/Scripts/format-manager.js | 46 +++++++++++++++++++ .../csharp-mvc/Scripts/format.js | 39 ++++++++++++++++ .../csharp-mvc/Scripts/jscript.js | 27 +++++++++-- .../csharp-mvc/Views/Home/Index.aspx | 3 -- .../csharp-mvc/WebEditor.ashx.cs | 22 +++++++++ 5 files changed, 131 insertions(+), 6 deletions(-) create mode 100755 web/documentserver-example/csharp-mvc/Scripts/format-manager.js create mode 100755 web/documentserver-example/csharp-mvc/Scripts/format.js diff --git a/web/documentserver-example/csharp-mvc/Scripts/format-manager.js b/web/documentserver-example/csharp-mvc/Scripts/format-manager.js new file mode 100755 index 00000000..cd8cfb57 --- /dev/null +++ b/web/documentserver-example/csharp-mvc/Scripts/format-manager.js @@ -0,0 +1,46 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +class FormatManager { + formats = []; + + constructor(formats) { + if (Array.isArray(formats)) this.formats = formats; + } + + isAutoConvertible(extension) { + let index = this.formats.findIndex(format => { + return format.name == extension && format.isAutoConvertible(); + }) + return index !== -1; + } + + isEditable(extension) { + let index = this.formats.findIndex(format => { + return format.name == extension && format.isEditable(); + }) + return index !== -1; + } + + isFillable(extension) { + let index = this.formats.findIndex(format => { + return format.name == extension && format.isFillable(); + }) + return index !== -1; + } +} diff --git a/web/documentserver-example/csharp-mvc/Scripts/format.js b/web/documentserver-example/csharp-mvc/Scripts/format.js new file mode 100755 index 00000000..9ee103a3 --- /dev/null +++ b/web/documentserver-example/csharp-mvc/Scripts/format.js @@ -0,0 +1,39 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} diff --git a/web/documentserver-example/csharp-mvc/Scripts/jscript.js b/web/documentserver-example/csharp-mvc/Scripts/jscript.js index 35d82796..fcf4ab29 100644 --- a/web/documentserver-example/csharp-mvc/Scripts/jscript.js +++ b/web/documentserver-example/csharp-mvc/Scripts/jscript.js @@ -17,6 +17,27 @@ */ var directUrl; +var formatManager; + +window.onload = function () { + fetch("webeditor.ashx?type=formats") + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + data.formats.forEach(format => { + formats.push(new Format( + format.Name, + format.Type, + format.Actions, + format.Convert, + format.Mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery != "undefined") { jq = jQuery.noConflict(); @@ -103,7 +124,7 @@ if (typeof jQuery != "undefined") { var posExt = fileName.lastIndexOf('.'); posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (ConverExtList.indexOf(posExt) == -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -178,10 +199,10 @@ if (typeof jQuery != "undefined") { jq("#beginView, #beginEmbedded").removeClass("disable"); var fileName = jq("#hiddenFileName").val(); - var posExt = fileName.lastIndexOf('.'); + var posExt = fileName.lastIndexOf('.') + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (EditedExtList.indexOf(posExt) != -1 || FillExtList.indexOf(posExt) != -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx b/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx index 080f867f..7df95e35 100644 --- a/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx +++ b/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx @@ -357,9 +357,6 @@ <%: Scripts.Render("~/bundles/jquery", "~/bundles/scripts") %> diff --git a/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs b/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs index 4e69b85c..0697bacb 100644 --- a/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs +++ b/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs @@ -87,6 +87,9 @@ namespace OnlineEditorsExampleMVC case "reference": Reference(context); break; + case "formats": + Formats(context); + break; } } @@ -951,6 +954,25 @@ namespace OnlineEditorsExampleMVC return history; } + // return all the supported formats + private static void Formats(HttpContext context) + { + try + { + Dictionary data = new Dictionary + { + { "formats", FormatManager.All() } + }; + context.Response.ContentType = "application/json"; + var jss = new JavaScriptSerializer(); + + context.Response.Write(jss.Serialize(data)); + } + catch (Exception e) + { + context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); + } + } } } \ No newline at end of file