Skip to content

Commit

Permalink
Add lens support (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski authored Oct 5, 2023
1 parent 4d626ab commit 8890f0e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 84 deletions.
2 changes: 1 addition & 1 deletion dist/js/card.js

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions resources/js/card.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import MegaFilterCard from './components/MegaFilterCard.vue'
import resourceStore from '@/store/resources'
import { registerMixin } from './components/MegaFilter'

Nova.booting((Vue, store) => {
Nova.booting(app => {

store.registerModule('mega-filter-store', resourceStore)
const componentFn = app.component

Vue.component('mega-filter-card', MegaFilterCard)
app.component = function (name, component) {

if (name === 'FilterMenu') {
registerMixin(component)
}

return componentFn.call(this, name, component)

}

app.component('mega-filter-card', MegaFilterCard)

})
40 changes: 40 additions & 0 deletions resources/js/components/MegaFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import reduce from 'lodash/reduce'

export function filtersAreApplied(store, resourceName, filterFunction) {

const filters = store.getters[ `${ resourceName }/filters` ].filter(filterFunction)

return reduce(filters, (result, filter) => {
const originalFilter = store.getters[ `${ resourceName }/getOriginalFilter` ](filter.class)
const originalFilterCloneValue = JSON.stringify(originalFilter.currentValue)
const currentFilterCloneValue = JSON.stringify(filter.currentValue)
return currentFilterCloneValue === originalFilterCloneValue ? result : result + 1
}, 0)

}

export function withoutMegaFilter(filter) {
return filter.megaFilter !== true
}

export function megaFilterOnly(filter) {
return filter.megaFilter === true
}

export function registerMixin(component) {

const filters = component.computed.filters

component.computed.filters = function () {
return filters.call(this).filter(withoutMegaFilter)
}

component.computed.activeFilterCount = function () {
return filtersAreApplied(this.$store, this.resourceName, withoutMegaFilter)
}

component.computed.filtersAreApplied = function () {
return this.activeFilterCount > 0
}

}
26 changes: 3 additions & 23 deletions resources/js/components/MegaFilter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
import Filterable from '@/mixins/Filterable'
import InteractsWithQueryString from '@/mixins/InteractsWithQueryString'
import { cleanUpInterceptors, interceptors } from './RequestHighjacker'
import { Collapse } from 'vue-collapsed'
import { filtersAreApplied, megaFilterOnly } from './MegaFilter'
export default {
name: 'MegaFilter',
Expand All @@ -98,7 +98,6 @@
props: [
'filters',
'columns',
'realResourceName',
'resourceName',
'viaResource',
'viaResourceId',
Expand Down Expand Up @@ -127,42 +126,23 @@
},
computed: {
filtersAreApplied() {
return this.$store.getters[ `${ this.resourceName }/filtersAreApplied` ]
return filtersAreApplied(this.$store, this.resourceName, megaFilterOnly)
},
initialEncodedFilters() {
return this.queryStringParams[ this.filterParameter ] || ''
},
pageParameter() {
return this.viaRelationship
? this.viaRelationship + '_page'
: this.realResourceName + '_page'
: this.resourceName + '_page'
},
},
async created() {
interceptors.push(config => {
if (config.params === undefined || config.params === null) {
config.params = {}
}
if (config.method === 'get' && config.url === `/nova-api/${ this.realResourceName }`) {
config.params.filters = this.encodedFilters
}
return config
})
await this.initializeState()
},
beforeMount() {
this.collapsed = this.filtersAreApplied
},
unmounted() {
cleanUpInterceptors()
},
}
</script>
Expand Down
18 changes: 9 additions & 9 deletions resources/js/components/MegaFilterCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

<MegaFilter
class="nova-mega-filter"
:lens="lens"
:filters="card.filters"
:columns="card.columns"
:realResource-name="resourceName"
:resource-name="customResourceName"
:resource-name="resourceName"
:via-resource="viaResource"
:via-resource-id="viaResourceId"
:via-relationship="viaRelationship"
Expand All @@ -22,19 +22,19 @@
components: { MegaFilter },
props: [
'card',
'lens',
'resourceName',
'viaResource',
'viaResourceId',
'viaRelationship',
],
created() {
this.$store.commit(`${ this.customResourceName }/clearFilters`)
this.$store.commit(`${ this.customResourceName }/storeFilters`, this.card.filters)
},
computed: {
customResourceName() {
return 'mega-filter-store'
},
const standardFilters = this.$store.getters[ `${ this.resourceName }/originalFilters` ]
const merged = standardFilters.concat(this.card.filters.map(filter => ({ ...filter, megaFilter: true })))
this.$store.commit(`${ this.resourceName }/storeFilters`, merged)
},
}
Expand Down
47 changes: 0 additions & 47 deletions resources/js/components/RequestHighjacker.js

This file was deleted.

0 comments on commit 8890f0e

Please sign in to comment.