Broken, trying to take js out

This commit is contained in:
ben 2023-01-25 17:33:24 +00:00
parent 2eef5028f9
commit efa24584f3
2 changed files with 164 additions and 163 deletions

View File

@ -0,0 +1,161 @@
new Vue({
el: '#vue',
data: function () {
return {
searchTerm: '',
tab: 'all',
filteredExtensions: null,
showUninstallDialog: false,
showUpgradeDialog: false,
selectedExtension: null,
selectedExtensionRepos: null,
maxStars: 5
}
},
watch: {
searchTerm(term) {
this.filterExtensions(term, this.tab)
}
},
methods: {
handleTabChanged: function (tab) {
this.filterExtensions(this.searchTerm, tab)
},
filterExtensions(term, tab) {
// Filter the extensions list
function extensionNameContains(searchTerm) {
return function (extension) {
return (
extension.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
extension.shortDescription
?.toLowerCase()
.includes(searchTerm.toLowerCase())
)
}
}
this.filteredExtensions = this.extensions
.filter(e => (tab === 'installed' ? e.isInstalled : true))
.filter(e => (tab === 'featured' ? e.isFeatured : true))
.filter(extensionNameContains(term))
},
async installExtension(release) {
const extension = this.selectedExtension
try {
extension.inProgress = true
this.showUpgradeDialog = false
await LNbits.api.request(
'POST',
`/api/v1/extension?usr=${this.g.user.id}`,
this.g.user.wallets[0].adminkey,
{
ext_id: extension.id,
archive: release.archive,
source_repo: release.source_repo
}
)
window.location.href = [
"{{ url_for('install.extensions') }}",
'?usr=',
this.g.user.id
].join('')
} catch (error) {
LNbits.utils.notifyApiError(error)
extension.inProgress = false
}
},
async uninstallExtension() {
const extension = this.selectedExtension
this.showUpgradeDialog = false
this.showUninstallDialog = false
try {
extension.inProgress = true
await LNbits.api.request(
'DELETE',
`/api/v1/extension/${extension.id}?usr=${this.g.user.id}`,
this.g.user.wallets[0].adminkey
)
window.location.href = [
"{{ url_for('install.extensions') }}",
'?usr=',
this.g.user.id
].join('')
} catch (error) {
LNbits.utils.notifyApiError(error)
extension.inProgress = false
}
},
toggleExtension: function (extension) {
const action = extension.isActive ? 'activate' : 'deactivate'
window.location.href = [
"{{ url_for('install.extensions') }}",
'?usr=',
this.g.user.id,
`&${action}=`,
extension.id
].join('')
},
showUninstall() {
this.showUpgradeDialog = false
this.showUninstallDialog = true
},
async showUpgrade(extension) {
this.selectedExtension = extension
this.showUpgradeDialog = true
this.selectedExtensionRepos = null
try {
const {data} = await LNbits.api.request(
'GET',
`/api/v1/extension/${extension.id}/releases?usr=${this.g.user.id}`,
this.g.user.wallets[0].adminkey
)
this.selectedExtensionRepos = data.reduce((repos, release) => {
repos[release.source_repo] = repos[release.source_repo] || {
releases: [],
isInstalled: false
}
release.isInstalled = this.isInstalledVersion(
this.selectedExtension,
release
)
if (release.isInstalled) {
repos[release.source_repo].isInstalled = true
}
repos[release.source_repo].releases.push(release)
return repos
}, {})
} catch (error) {
LNbits.utils.notifyApiError(error)
extension.inProgress = false
}
},
hasNewVersion: function (extension) {
if (extension.installedRelease && extension.latestRelease) {
return (
extension.installedRelease.version !== extension.latestRelease.version
)
}
},
isInstalledVersion: function (extension, release) {
if (extension.installedRelease) {
return (
extension.installedRelease.source_repo === release.source_repo &&
extension.installedRelease.version === release.version
)
}
}
},
created() {
console.log(window.extensions)
console.log(window.user)
this.extensions = JSON.parse(window.extensions).map(e => ({
...e,
inProgress: false
}))
this.filteredExtensions = this.extensions.concat([])
},
mixins: [windowMixin]
})

View File

@ -1,5 +1,7 @@
{% extends "base.html" %} {% from "macros.jinja" import window_vars with context
%} {% block page %}
%} {{ window_vars(user, extensions) }} {% block scripts %}
<script src="/core/static/js/install.js"></script>
{% endblock %} {% block page %}
<div class="row q-col-gutter-md q-mb-md">
<div class="col-sm-9 col-xs-12">
<p class="text-h4 gt-sm">
@ -313,167 +315,5 @@
</q-card>
</q-dialog>
</div>
{% endblock %} {% block scripts %} {{ window_vars(user) }}
<script>
new Vue({
el: '#vue',
data: function () {
return {
searchTerm: '',
tab: 'all',
filteredExtensions: null,
showUninstallDialog: false,
showUpgradeDialog: false,
selectedExtension: null,
selectedExtensionRepos: null,
maxStars: 5
}
},
watch: {
searchTerm(term) {
this.filterExtensions(term, this.tab)
}
},
methods: {
handleTabChanged: function (tab) {
this.filterExtensions(this.searchTerm, tab)
},
filterExtensions: function (term, tab) {
// Filter the extensions list
function extensionNameContains(searchTerm) {
return function (extension) {
return (
extension.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
extension.shortDescription
?.toLowerCase()
.includes(searchTerm.toLowerCase())
)
}
}
this.filteredExtensions = this.extensions
.filter(e => (tab === 'installed' ? e.isInstalled : true))
.filter(e => (tab === 'featured' ? e.isFeatured : true))
.filter(extensionNameContains(term))
},
installExtension: async function (release) {
const extension = this.selectedExtension
try {
extension.inProgress = true
this.showUpgradeDialog = false
await LNbits.api.request(
'POST',
`/api/v1/extension?usr=${this.g.user.id}`,
this.g.user.wallets[0].adminkey,
{
ext_id: extension.id,
archive: release.archive,
source_repo: release.source_repo
}
)
window.location.href = [
"{{ url_for('install.extensions') }}",
'?usr=',
this.g.user.id
].join('')
} catch (error) {
LNbits.utils.notifyApiError(error)
extension.inProgress = false
}
},
uninstallExtension: async function () {
const extension = this.selectedExtension
this.showUpgradeDialog = false
this.showUninstallDialog = false
try {
extension.inProgress = true
await LNbits.api.request(
'DELETE',
`/api/v1/extension/${extension.id}?usr=${this.g.user.id}`,
this.g.user.wallets[0].adminkey
)
window.location.href = [
"{{ url_for('install.extensions') }}",
'?usr=',
this.g.user.id
].join('')
} catch (error) {
LNbits.utils.notifyApiError(error)
extension.inProgress = false
}
},
toggleExtension: function (extension) {
const action = extension.isActive ? 'activate' : 'deactivate'
window.location.href = [
"{{ url_for('install.extensions') }}",
'?usr=',
this.g.user.id,
`&${action}=`,
extension.id
].join('')
},
showUninstall: function () {
this.showUpgradeDialog = false
this.showUninstallDialog = true
},
showUpgrade: async function (extension) {
this.selectedExtension = extension
this.showUpgradeDialog = true
this.selectedExtensionRepos = null
try {
const {data} = await LNbits.api.request(
'GET',
`/api/v1/extension/${extension.id}/releases?usr=${this.g.user.id}`,
this.g.user.wallets[0].adminkey
)
this.selectedExtensionRepos = data.reduce((repos, release) => {
repos[release.source_repo] = repos[release.source_repo] || {
releases: [],
isInstalled: false
}
release.isInstalled = this.isInstalledVersion(
this.selectedExtension,
release
)
if (release.isInstalled) {
repos[release.source_repo].isInstalled = true
}
repos[release.source_repo].releases.push(release)
return repos
}, {})
} catch (error) {
LNbits.utils.notifyApiError(error)
extension.inProgress = false
}
},
hasNewVersion: function (extension) {
if (extension.installedRelease && extension.latestRelease) {
return (
extension.installedRelease.version !==
extension.latestRelease.version
)
}
},
isInstalledVersion: function (extension, release) {
if (extension.installedRelease) {
return (
extension.installedRelease.source_repo === release.source_repo &&
extension.installedRelease.version === release.version
)
}
}
},
created: function () {
this.extensions = JSON.parse('{{extensions | tojson | safe}}').map(e => ({
...e,
inProgress: false
}))
this.filteredExtensions = this.extensions.concat([])
},
mixins: [windowMixin]
})
</script>
{% endblock %}