-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4e9816
commit 6e4c20d
Showing
7 changed files
with
180 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,165 @@ | ||
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license. | ||
|
||
import { Constants, I18n, IApplication, ObservableCollection, PubSub, RecentDocumentDTO } from "chili-core"; | ||
import { | ||
Constants, | ||
I18n, | ||
I18nKeys, | ||
IApplication, | ||
ObservableCollection, | ||
PubSub, | ||
RecentDocumentDTO, | ||
} from "chili-core"; | ||
import { LanguageSelector } from "../components"; | ||
import { a, button, div, img, items, label, localize, span, svg } from "../controls"; | ||
import style from "./home.module.css"; | ||
|
||
function hasOpen(app: IApplication, documentId: string) { | ||
for (const document of app.documents) { | ||
if (document.id === documentId) return true; | ||
} | ||
return false; | ||
interface ApplicationCommand { | ||
display: I18nKeys; | ||
icon?: string; | ||
onclick: () => void; | ||
} | ||
|
||
export const Home = async (app: IApplication) => { | ||
let documentArray: RecentDocumentDTO[] = await app.storage.page( | ||
Constants.DBName, | ||
Constants.RecentTable, | ||
0, | ||
); | ||
let documents = new ObservableCollection(...documentArray); | ||
return div( | ||
{ className: style.root }, | ||
div( | ||
{ className: style.left }, | ||
const applicationCommands = new ObservableCollection<ApplicationCommand>( | ||
{ | ||
display: "command.document.new", | ||
onclick: () => PubSub.default.pub("executeCommand", "doc.new"), | ||
}, | ||
{ | ||
display: "command.document.open", | ||
onclick: () => PubSub.default.pub("executeCommand", "doc.open"), | ||
}, | ||
); | ||
|
||
export class Home extends HTMLElement { | ||
constructor(readonly app: IApplication) { | ||
super(); | ||
this.className = style.root; | ||
} | ||
|
||
private hasOpen(documentId: string) { | ||
for (const document of this.app.documents) { | ||
if (document.id === documentId) return true; | ||
} | ||
return false; | ||
} | ||
|
||
private async getDocuments() { | ||
let documentArray: RecentDocumentDTO[] = await this.app.storage.page( | ||
Constants.DBName, | ||
Constants.RecentTable, | ||
0, | ||
); | ||
return new ObservableCollection(...documentArray); | ||
} | ||
|
||
async render() { | ||
let documents = await this.getDocuments(); | ||
this.append( | ||
div( | ||
{ className: style.top }, | ||
{ className: style.left }, | ||
div( | ||
{ className: style.logo }, | ||
svg({ icon: "icon-chili" }), | ||
span({ | ||
textContent: "CHILI3D", | ||
{ className: style.top }, | ||
div( | ||
{ className: style.logo }, | ||
svg({ icon: "icon-chili" }), | ||
span({ | ||
textContent: "CHILI3D", | ||
}), | ||
), | ||
items({ | ||
className: style.buttons, | ||
sources: applicationCommands, | ||
template: (item) => | ||
button({ | ||
className: style.button, | ||
textContent: localize(item.display), | ||
onclick: item.onclick, | ||
}), | ||
}), | ||
this.app.activeView?.document | ||
? button({ | ||
className: `${style.button} ${style.back}`, | ||
textContent: localize("common.back"), | ||
onclick: () => { | ||
PubSub.default.pub("displayHome", false); | ||
}, | ||
}) | ||
: "", | ||
), | ||
div( | ||
{ className: style.bottom }, | ||
a({ | ||
textContent: "Github", | ||
href: "https://github.com/xiangechen/chili3d", | ||
target: "_blank", | ||
}), | ||
), | ||
button({ | ||
textContent: localize("command.document.new"), | ||
onclick: () => PubSub.default.pub("executeCommand", "doc.new"), | ||
}), | ||
button({ | ||
textContent: localize("command.document.open"), | ||
onclick: () => PubSub.default.pub("executeCommand", "doc.open"), | ||
}), | ||
app.activeView?.document | ||
? button({ | ||
className: style.back, | ||
textContent: localize("common.back"), | ||
onclick: () => { | ||
PubSub.default.pub("displayHome", false); | ||
}, | ||
}) | ||
: "", | ||
), | ||
div( | ||
{ className: style.bottom }, | ||
a({ | ||
textContent: "Github", | ||
href: "https://github.com/xiangechen/chili3d", | ||
target: "_blank", | ||
}), | ||
), | ||
), | ||
div( | ||
{ className: style.right }, | ||
label({ className: style.welcome, textContent: localize("home.welcome") }), | ||
div({ className: style.recent, textContent: localize("home.recent") }), | ||
items({ | ||
className: style.documents, | ||
sources: documents, | ||
template: (item) => | ||
div( | ||
{ | ||
className: style.document, | ||
onclick: () => { | ||
if (hasOpen(app, item.id)) { | ||
PubSub.default.pub("displayHome", false); | ||
} else { | ||
PubSub.default.pub( | ||
"showPermanent", | ||
async () => { | ||
let document = await app.openDocument(item.id); | ||
document?.application.activeView?.cameraController.fitContent(); | ||
}, | ||
"toast.excuting{0}", | ||
I18n.translate("command.document.open"), | ||
); | ||
} | ||
}, | ||
}, | ||
img({ className: style.img, src: item.image }), | ||
{ className: style.right }, | ||
label({ className: style.welcome, textContent: localize("home.welcome") }), | ||
div({ className: style.recent, textContent: localize("home.recent") }), | ||
items({ | ||
className: style.documents, | ||
sources: documents, | ||
template: (item) => | ||
div( | ||
{ className: style.description }, | ||
span({ className: style.title, textContent: item.name }), | ||
span({ | ||
className: style.date, | ||
textContent: new Date(item.date).toLocaleDateString(), | ||
{ | ||
className: style.document, | ||
onclick: () => { | ||
if (this.hasOpen(item.id)) { | ||
PubSub.default.pub("displayHome", false); | ||
} else { | ||
PubSub.default.pub( | ||
"showPermanent", | ||
async () => { | ||
let document = await this.app.openDocument(item.id); | ||
document?.application.activeView?.cameraController.fitContent(); | ||
}, | ||
"toast.excuting{0}", | ||
I18n.translate("command.document.open"), | ||
); | ||
} | ||
}, | ||
}, | ||
img({ className: style.img, src: item.image }), | ||
div( | ||
{ className: style.description }, | ||
span({ className: style.title, textContent: item.name }), | ||
span({ | ||
className: style.date, | ||
textContent: new Date(item.date).toLocaleDateString(), | ||
}), | ||
), | ||
svg({ | ||
className: style.delete, | ||
icon: "icon-times", | ||
onclick: async (e) => { | ||
e.stopPropagation(); | ||
if ( | ||
window.confirm(I18n.translate("prompt.deleteDocument{0}", item.name)) | ||
) { | ||
await this.app.storage.delete( | ||
Constants.DBName, | ||
Constants.DocumentTable, | ||
item.id, | ||
); | ||
await this.app.storage.delete( | ||
Constants.DBName, | ||
Constants.RecentTable, | ||
item.id, | ||
); | ||
documents.remove(item); | ||
} | ||
}, | ||
}), | ||
), | ||
svg({ | ||
className: style.delete, | ||
icon: "icon-times", | ||
onclick: async (e) => { | ||
e.stopPropagation(); | ||
if (window.confirm(I18n.translate("prompt.deleteDocument{0}", item.name))) { | ||
await app.storage.delete( | ||
Constants.DBName, | ||
Constants.DocumentTable, | ||
item.id, | ||
); | ||
await app.storage.delete( | ||
Constants.DBName, | ||
Constants.RecentTable, | ||
item.id, | ||
); | ||
documents.remove(item); | ||
} | ||
}, | ||
}), | ||
), | ||
}), | ||
), | ||
LanguageSelector({ className: style.language }), | ||
); | ||
}; | ||
}), | ||
), | ||
LanguageSelector({ className: style.language }), | ||
); | ||
document.body.appendChild(this); | ||
} | ||
} | ||
|
||
customElements.define("chili-home", Home); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters