-
Notifications
You must be signed in to change notification settings - Fork 17
/
page-admin.js.es6
138 lines (121 loc) · 3.43 KB
/
page-admin.js.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import Component from "@ember/component";
import discourseComputed from "discourse-common/utils/decorators";
import { not, notEmpty, or } from "@ember/object/computed";
import { dasherize } from "@ember/string";
import LandingPage from "../models/landing-page";
import { extractError } from "discourse/lib/ajax-error";
import I18n from "I18n";
const location = window.location;
const port = location.port ? ":" + location.port : "";
const baseUrl = location.protocol + "//" + location.hostname + port;
export default Component.extend({
classNames: "page-admin",
updatingPage: or("destroyingPage", "savingPage"),
hasParent: notEmpty("parent"),
noParent: not("hasParent"),
hasPath: or("page.path", "page.parent_id"),
@discourseComputed("page.parent_id")
parent(parentId) {
const parent = this.pages.findBy("id", parentId);
return parent ? parent : null;
},
@discourseComputed("page.path", "parent")
pagePath(path, parent) {
return parent ? parent.path : path;
},
@discourseComputed("pagePath")
pageUrl(pagePath) {
let url = baseUrl;
if (pagePath) {
url += `/${dasherize(pagePath)}`;
} else {
url += `/${I18n.t("admin.landing_pages.page.path.placeholder")}`;
}
if (this.hasParent) {
url += `/1`;
}
return url;
},
actions: {
onChangePath(path) {
if (!this.page.parent_id) {
this.set("page.path", path);
}
},
onChangeParent(pageId) {
this.set("page.parent_id", pageId);
},
savePage() {
this.set("savingPage", true);
const page = this.get("page");
let self = this;
page
.savePage()
.then((result) => {
if (result.page) {
self.setProperties({
page: LandingPage.create(result.page),
currentPage: JSON.parse(JSON.stringify(result.page)),
});
self.updatePages(result.pages);
} else {
self.set("page", self.currentPage);
}
})
.catch((error) => {
self.set("resultMessage", {
type: "error",
icon: "times",
text: extractError(error),
});
setTimeout(() => {
self.set("resultMessage", null);
}, 5000);
if (self.currentPage) {
self.set("page", self.currentPage);
}
})
.finally(() => {
self.set("savingPage", false);
});
},
destroyPage() {
this.set("destroyingPage", true);
this.page
.destroyPage()
.then((result) => {
if (result.success) {
this.setProperties({
page: null,
pages: result.pages,
});
}
})
.finally(() => {
this.set("destroyingPage", false);
});
},
exportPage() {
const page = this.get("page");
let self = this;
page
.exportPage()
.then((file) => {
const link = document.createElement("a");
link.href = URL.createObjectURL(file);
link.setAttribute("download", `discourse-${page.name.toLowerCase()}.zip`);
link.click();
})
.catch((error) => {
self.set("resultMessage", {
type: "error",
icon: "times",
text: extractError(error),
});
setTimeout(() => {
self.set("resultMessage", null);
}, 5000);
});
},
},
});