2019-11-06 19:27:16 -06:00
|
|
|
<template>
|
2019-11-08 19:45:58 -06:00
|
|
|
<div class="model-index" :class="getModelSlug()">
|
2019-11-06 19:27:16 -06:00
|
|
|
|
|
|
|
<nav class="breadcrumb" aria-label="breadcrumbs">
|
|
|
|
<ul>
|
|
|
|
<li>{{ getModelIndexTitle() }}</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
<b-button v-if="allowCreate && hasModelPerm('create')"
|
|
|
|
class="new-object"
|
|
|
|
type="is-primary"
|
|
|
|
tag="router-link"
|
|
|
|
:to="getModelPathPrefix() + '/new'">
|
|
|
|
New {{ getModelTitle() }}
|
|
|
|
</b-button>
|
|
|
|
|
|
|
|
<slot></slot>
|
|
|
|
|
|
|
|
<b-menu>
|
|
|
|
<b-menu-list>
|
2019-11-06 22:29:59 -06:00
|
|
|
<b-menu-item v-for="obj in data.data"
|
2019-11-06 19:27:16 -06:00
|
|
|
:key="obj.uuid"
|
|
|
|
:label="renderLabel(obj)"
|
|
|
|
:tag="hasModelPerm('view') ? 'router-link' : 'a'"
|
|
|
|
:to="getModelPathPrefix() + '/' + obj.uuid">
|
|
|
|
</b-menu-item>
|
|
|
|
</b-menu-list>
|
|
|
|
</b-menu>
|
2019-11-06 22:29:59 -06:00
|
|
|
|
|
|
|
<b-pagination v-if="paginated"
|
|
|
|
:total="data.total"
|
|
|
|
:current.sync="page"
|
|
|
|
:per-page="perPage"
|
|
|
|
@change="changePagination"
|
|
|
|
>
|
|
|
|
<!-- icon-pack="fas" -->
|
|
|
|
</b-pagination>
|
|
|
|
|
2019-11-06 19:27:16 -06:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
name: 'ByjoveModelIndex',
|
|
|
|
props: {
|
|
|
|
modelName: String,
|
|
|
|
modelSlug: String,
|
|
|
|
modelTitle: String,
|
|
|
|
modelTitlePlural: String,
|
|
|
|
modelIndexTitle: String,
|
|
|
|
modelPermissionPrefix: String,
|
|
|
|
modelPathPrefix: String,
|
|
|
|
labelRenderer: Function,
|
|
|
|
apiIndexUrl: String,
|
|
|
|
apiIndexSort: Object,
|
|
|
|
apiIndexFilters: Array,
|
|
|
|
allowCreate: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2019-11-06 22:29:59 -06:00
|
|
|
paginated: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
perPage: {
|
|
|
|
type: Number,
|
|
|
|
default: 20,
|
|
|
|
},
|
2019-11-06 19:27:16 -06:00
|
|
|
},
|
|
|
|
data: function() {
|
|
|
|
return {
|
2019-11-06 22:29:59 -06:00
|
|
|
data: {},
|
|
|
|
page: 1,
|
2019-11-06 19:27:16 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.fetchData()
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
'apiIndexFilters' (to, from) {
|
|
|
|
this.fetchData()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
|
2019-11-06 22:29:59 -06:00
|
|
|
changePagination(value) {
|
|
|
|
this.fetchData()
|
|
|
|
},
|
|
|
|
|
2019-11-06 19:27:16 -06:00
|
|
|
fetchData() {
|
|
|
|
let params = {
|
|
|
|
filters: JSON.stringify(this.apiIndexFilters),
|
|
|
|
orderBy: this.apiIndexSort.field,
|
|
|
|
ascending: (this.apiIndexSort.dir == 'asc') ? 1 : 0,
|
|
|
|
}
|
2019-11-06 22:29:59 -06:00
|
|
|
if (this.paginated) {
|
|
|
|
params.per_page = this.perPage
|
2020-02-23 21:09:26 -06:00
|
|
|
params.page = this.page || 1
|
2019-11-06 22:29:59 -06:00
|
|
|
}
|
2019-11-06 19:27:16 -06:00
|
|
|
this.$http.get(this.getApiIndexUrl(), {params: params}).then(response => {
|
2019-11-06 22:29:59 -06:00
|
|
|
this.data = response.data
|
2019-11-06 19:27:16 -06:00
|
|
|
|
|
|
|
}, response => {
|
|
|
|
if (response.status == 403) { // forbidden; redirect to home page
|
|
|
|
this.$buefy.toast.open({
|
|
|
|
message: "You do not have permission to access that page.",
|
|
|
|
type: 'is-danger',
|
|
|
|
position: 'is-bottom',
|
|
|
|
})
|
|
|
|
this.$router.push('/')
|
|
|
|
} else {
|
|
|
|
this.$buefy.toast.open({
|
|
|
|
message: "Failed to fetch page data!",
|
|
|
|
type: 'is-danger',
|
|
|
|
position: 'is-bottom',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getModelSlug() {
|
|
|
|
if (this.modelSlug) {
|
|
|
|
return this.modelSlug
|
|
|
|
}
|
|
|
|
return this.modelName.toLowerCase() + 's'
|
|
|
|
},
|
|
|
|
|
|
|
|
getModelTitle() {
|
|
|
|
if (this.modelTitle) {
|
|
|
|
return this.modelTitle
|
|
|
|
}
|
|
|
|
return this.modelName
|
|
|
|
},
|
|
|
|
|
|
|
|
getModelTitlePlural() {
|
|
|
|
if (this.modelTitlePlural) {
|
|
|
|
return this.modelTitlePlural
|
|
|
|
}
|
|
|
|
return this.getModelTitle() + 's'
|
|
|
|
},
|
|
|
|
|
|
|
|
getModelIndexTitle() {
|
|
|
|
if (this.modelIndexTitle) {
|
|
|
|
return this.modelIndexTitle
|
|
|
|
}
|
|
|
|
return this.getModelTitlePlural()
|
|
|
|
},
|
|
|
|
|
|
|
|
getModelPathPrefix() {
|
|
|
|
if (this.modelPathPrefix) {
|
|
|
|
return this.modelPathPrefix
|
|
|
|
}
|
|
|
|
return '/' + this.getModelSlug()
|
|
|
|
},
|
|
|
|
|
|
|
|
getApiIndexUrl() {
|
|
|
|
if (this.apiIndexUrl) {
|
|
|
|
return this.apiIndexUrl
|
|
|
|
}
|
|
|
|
return '/api/' + this.getModelSlug()
|
|
|
|
},
|
|
|
|
|
|
|
|
getModelPermissionPrefix() {
|
|
|
|
if (this.modelPermissionPrefix) {
|
|
|
|
return this.modelPermissionPrefix
|
|
|
|
}
|
|
|
|
return this.getModelSlug()
|
|
|
|
},
|
|
|
|
|
|
|
|
hasModelPerm(perm) {
|
|
|
|
|
|
|
|
// do normal check, but first add prefix
|
|
|
|
let prefix = this.getModelPermissionPrefix()
|
2020-02-23 21:09:04 -06:00
|
|
|
return this.$hasPerm(prefix + '.' + perm)
|
2019-11-06 19:27:16 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
renderLabel(obj) {
|
|
|
|
if (this.labelRenderer) {
|
|
|
|
return this.labelRenderer(obj)
|
|
|
|
}
|
|
|
|
return obj._str
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.new-object {
|
|
|
|
margin-bottom: 0.5rem;
|
|
|
|
}
|
|
|
|
</style>
|