Skip to content

Commit 98ffcfa

Browse files
committed
Fixed bug where you could import pages/panels from your own app.
Added duplicate functionality for pages/panels.
1 parent d53129e commit 98ffcfa

File tree

10 files changed

+376
-186
lines changed

10 files changed

+376
-186
lines changed

Low Code App Editor_1/Controllers/AppEditor/Pages/EditorPagesController.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
namespace Low_Code_App_Editor_1.Controllers
44
{
5-
using System;
5+
using System;
66

7-
using Low_Code_App_Editor_1.UI;
7+
using Low_Code_App_Editor_1.UI;
88

9-
using Skyline.DataMiner.Utils.InteractiveAutomationScript;
10-
using Skyline.DataMiner.Web.Common.v1;
9+
using Skyline.DataMiner.Utils.InteractiveAutomationScript;
10+
using Skyline.DataMiner.Web.Common.v1;
1111

12-
public static class EditorPagesController
13-
{
14-
public static void Load(this AppEditorPages editor, DMAApplicationPage page)
15-
{
16-
editor.PageFields.Clear();
17-
editor.PageFields.Add(nameof(page.ID), new Label(page.ID));
18-
editor.PageFields.Add(nameof(page.Name), new Label(page.Name));
19-
var description = page.Description ?? String.Empty;
20-
editor.PageFields.Add(nameof(page.Description), new Label(description));
21-
editor.PageFields.Add(nameof(page.Hidden), new Label(page.Hidden.ToString()));
22-
}
23-
}
12+
public static class EditorPagesController
13+
{
14+
public static void Load(this AppEditorPages editor, DMAApplicationPage page)
15+
{
16+
editor.PageFields.Clear();
17+
editor.PageFields.Add(nameof(page.ID), new Label(page.ID));
18+
editor.PageFields.Add(nameof(page.Name), new Label(page.Name));
19+
var description = page.Description ?? String.Empty;
20+
editor.PageFields.Add(nameof(page.Description), new Label(description));
21+
editor.PageFields.Add(nameof(page.Hidden), new Label(page.Hidden.ToString()));
22+
}
23+
}
2424
}

Low Code App Editor_1/Controllers/AppEditor/Pages/EditorPagesImportController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public static void Load(this AppEditorPagesImport editor, App selectedApp, IEnum
2525
{
2626
editor.SelectedApp = selectedApp;
2727
editor.Apps.Options.Clear();
28-
editor.Apps.Options.AddRange(apps.Select(app => Option.Create($"[{app.LatestVersion.ID}] - {app.Name}", app)));
28+
editor.Apps.Options.AddRange(apps
29+
.Where(app => app.LatestVersion.ID != selectedApp.LatestVersion.ID)
30+
.Select(app => Option.Create($"[{app.LatestVersion.ID}] - {app.Name}", app)));
2931

3032
var defaultSelected = editor.Apps.Options.FirstOrDefault();
3133
if(defaultSelected == null)

Low Code App Editor_1/Controllers/AppEditor/Pages/EditorPagesOverviewController.cs

Lines changed: 152 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,156 @@
22

33
namespace Low_Code_App_Editor_1.Controllers
44
{
5-
using System.IO;
6-
using System.Linq;
7-
8-
using Low_Code_App_Editor_1.LCA;
9-
using Low_Code_App_Editor_1.UI;
10-
using Low_Code_App_Editor_1.UI.General;
11-
12-
using Newtonsoft.Json.Linq;
13-
14-
using Skyline.DataMiner.Utils.InteractiveAutomationScript;
15-
using Skyline.DataMiner.Web.Common.v1;
16-
17-
public static class EditorPagesOverviewController
18-
{
19-
public static void Load(this AppEditorPagesOverview editor, AppEditorPages pageEditor, InteractiveController controller, App app)
20-
{
21-
editor.SelectedApp = app;
22-
editor.Pages.Clear();
23-
if(app.LatestVersion.Pages == null)
24-
{
25-
app.LatestVersion.Pages = new Skyline.DataMiner.Web.Common.v1.DMAApplicationPage[0];
26-
}
27-
28-
foreach(var page in app.LatestVersion.Pages)
29-
{
30-
var editButton = new DoubleButton("Edit...", "Delete");
31-
editButton.LeftButton.Pressed += (sender, e) =>
32-
{
33-
pageEditor.Load(page);
34-
controller.ShowDialog(pageEditor);
35-
};
36-
editButton.RightButton.Pressed += (sender, e) =>
37-
{
38-
var confirmation = new ConfirmationDialog(controller.Engine, $"Are you sure you want to delete page '{page.Name}' from the app '{app.Name}'?");
39-
confirmation.Ok.Pressed += (s, ev) =>
40-
{
41-
Delete(app, page);
42-
editor.Load(pageEditor, controller, app);
43-
controller.ShowDialog(editor);
44-
};
45-
confirmation.Cancel.Pressed += (s, ev) =>
46-
{
47-
controller.ShowDialog(editor);
48-
};
49-
controller.ShowDialog(confirmation);
50-
};
51-
52-
editor.Pages.Add(page.Name, editButton);
53-
}
54-
}
55-
56-
public static void Delete(App app, DMAApplicationPage page)
57-
{
58-
// Delete from latest version
59-
DeleteFromVersion(app.LatestVersion, page);
60-
61-
// Delete from draft version
62-
if(app.LatestDraftVersion != null)
63-
{
64-
DeleteFromVersion(app.LatestDraftVersion, page);
65-
}
66-
}
67-
68-
private static void DeleteFromVersion(AppVersion version, DMAApplicationPage page)
69-
{
70-
// Remove the pages file
71-
File.Delete(Path.Combine(version.FolderPath, "pages", $"{page.ID}.dmadb.json"));
72-
73-
// Remove reference from config
74-
var config = JObject.Parse(File.ReadAllText(version.Path));
75-
var pages = (JArray)config.SelectToken("Pages");
76-
var selectedPageIndex = pages.FirstOrDefault(token => token["ID"].Value<string>() == page.ID);
77-
pages.Remove(selectedPageIndex);
78-
config["Pages"] = pages;
79-
File.WriteAllText(version.Path, config.ToString(Newtonsoft.Json.Formatting.None));
80-
81-
// Remove from local copy
82-
var localPages = version.Pages.ToList();
83-
localPages.Remove(version.Pages.FirstOrDefault(p => p.ID == page.ID));
84-
version.Pages = localPages.ToArray();
85-
}
86-
}
5+
using System;
6+
using System.IO;
7+
using System.Linq;
8+
9+
using Low_Code_App_Editor_1.LCA;
10+
using Low_Code_App_Editor_1.UI;
11+
using Low_Code_App_Editor_1.UI.General;
12+
13+
using Newtonsoft.Json;
14+
using Newtonsoft.Json.Linq;
15+
16+
using Skyline.DataMiner.Utils.InteractiveAutomationScript;
17+
using Skyline.DataMiner.Web.Common.v1;
18+
19+
public static class EditorPagesOverviewController
20+
{
21+
public static void Load(this AppEditorPagesOverview editor, AppEditorPages pageEditor, InteractiveController controller, App app)
22+
{
23+
editor.SelectedApp = app;
24+
editor.Pages.Clear();
25+
if (app.LatestVersion.Pages == null)
26+
{
27+
app.LatestVersion.Pages = new Skyline.DataMiner.Web.Common.v1.DMAApplicationPage[0];
28+
}
29+
30+
foreach (var page in app.LatestVersion.Pages)
31+
{
32+
var editButton = new TripleButton("Edit...", "Duplicate...", "Delete...");
33+
editButton.LeftButton.Pressed += (sender, e) =>
34+
{
35+
pageEditor.Load(page);
36+
controller.ShowDialog(pageEditor);
37+
};
38+
editButton.MiddleButton.Pressed += (sender, e) =>
39+
{
40+
var confirmation = new ConfirmationDialog(controller.Engine, $"Are you sure you want to duplicate page '{page.Name}'?");
41+
confirmation.Ok.Pressed += (s, ev) =>
42+
{
43+
Duplicate(app, page);
44+
editor.Load(pageEditor, controller, app);
45+
controller.ShowDialog(editor);
46+
};
47+
confirmation.Cancel.Pressed += (s, ev) =>
48+
{
49+
controller.ShowDialog(editor);
50+
};
51+
controller.ShowDialog(confirmation);
52+
};
53+
editButton.RightButton.Pressed += (sender, e) =>
54+
{
55+
var confirmation = new ConfirmationDialog(controller.Engine, $"Are you sure you want to delete page '{page.Name}' from the app '{app.Name}'?");
56+
confirmation.Ok.Pressed += (s, ev) =>
57+
{
58+
Delete(app, page);
59+
editor.Load(pageEditor, controller, app);
60+
controller.ShowDialog(editor);
61+
};
62+
confirmation.Cancel.Pressed += (s, ev) =>
63+
{
64+
controller.ShowDialog(editor);
65+
};
66+
controller.ShowDialog(confirmation);
67+
};
68+
69+
editor.Pages.Add(page.Name, editButton);
70+
}
71+
}
72+
73+
public static void Duplicate(App app, DMAApplicationPage page)
74+
{
75+
var newGuid = Convert.ToString(Guid.NewGuid());
76+
77+
// Create the paths
78+
var originalPath = Path.Combine(app.LatestVersion.FolderPath, "pages", $"{page.ID}.dmadb.json");
79+
var duplicatePath = Path.Combine(app.LatestVersion.FolderPath, "pages", $"{newGuid}.dmadb.json");
80+
81+
// Parse the config
82+
var config = JObject.Parse(File.ReadAllText(app.LatestVersion.Path));
83+
var pagesToken = config.SelectToken("Pages");
84+
var pages = (JArray)pagesToken;
85+
86+
var selectedPage = pages.FirstOrDefault(p => p["ID"].Value<string>() == page.ID);
87+
if (selectedPage is null)
88+
{
89+
throw new LowCodeAppEditorException("Could not find the selected page in the config of the app.");
90+
}
91+
92+
if (!File.Exists(originalPath))
93+
{
94+
throw new LowCodeAppEditorException("Could not find the selected page in the pages folder of the app.");
95+
}
96+
97+
// Read out the page and to the edits for the new id
98+
var content = File.ReadAllText(originalPath)
99+
.Replace(page.ID, newGuid);
100+
101+
// Create the duplicate page file
102+
File.WriteAllText(duplicatePath, content);
103+
104+
// Create the duplicate config entry
105+
var newPage = selectedPage.DeepClone();
106+
newPage["ID"] = newGuid;
107+
newPage["Name"] = $"{newPage["Name"].Value<string>()} - Copy";
108+
109+
var rawPage = newPage.ToString(Formatting.None)
110+
.Replace(page.ID, newGuid);
111+
newPage = JToken.Parse(rawPage);
112+
113+
pages.Add(newPage);
114+
config["Pages"] = pages;
115+
116+
// Save the config
117+
File.WriteAllText(
118+
Path.Combine(app.LatestVersion.Path),
119+
config.ToString(Formatting.None));
120+
121+
// Update our local copy of the app
122+
var latestApp = new App(app.Path);
123+
app.LatestVersion.Pages = latestApp.LatestVersion.Pages;
124+
}
125+
126+
public static void Delete(App app, DMAApplicationPage page)
127+
{
128+
// Delete from latest version
129+
DeleteFromVersion(app.LatestVersion, page);
130+
131+
// Delete from draft version
132+
if (app.LatestDraftVersion != null)
133+
{
134+
DeleteFromVersion(app.LatestDraftVersion, page);
135+
}
136+
}
137+
138+
private static void DeleteFromVersion(AppVersion version, DMAApplicationPage page)
139+
{
140+
// Remove the pages file
141+
File.Delete(Path.Combine(version.FolderPath, "pages", $"{page.ID}.dmadb.json"));
142+
143+
// Remove reference from config
144+
var config = JObject.Parse(File.ReadAllText(version.Path));
145+
var pages = (JArray)config.SelectToken("Pages");
146+
var selectedPageIndex = pages.FirstOrDefault(token => token["ID"].Value<string>() == page.ID);
147+
pages.Remove(selectedPageIndex);
148+
config["Pages"] = pages;
149+
File.WriteAllText(version.Path, config.ToString(Formatting.None));
150+
151+
// Remove from local copy
152+
var localPages = version.Pages.ToList();
153+
localPages.Remove(version.Pages.FirstOrDefault(p => p.ID == page.ID));
154+
version.Pages = localPages.ToArray();
155+
}
156+
}
87157
}

Low Code App Editor_1/Controllers/AppEditor/Panels/EditorPanelsImportController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public static void Load(this AppEditorPanelsImport editor, App selectedApp, IEnu
2424
{
2525
editor.SelectedApp = selectedApp;
2626
editor.Apps.Options.Clear();
27-
editor.Apps.Options.AddRange(apps.Select(app => Option.Create($"[{app.LatestVersion.ID}] - {app.Name}", app)));
27+
editor.Apps.Options.AddRange(apps
28+
.Where(app => app.LatestVersion.ID != selectedApp.LatestVersion.ID)
29+
.Select(app => Option.Create($"[{app.LatestVersion.ID}] - {app.Name}", app)));
2830

2931
var defaultSelected = editor.Apps.Options.FirstOrDefault();
3032
if (defaultSelected == null)

0 commit comments

Comments
 (0)