Skip to content

Commit

Permalink
Add support to Nova 4 (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Widlund <[email protected]>
  • Loading branch information
milewski and chrillep authored Nov 15, 2022
1 parent 4dc8a20 commit b81a37e
Show file tree
Hide file tree
Showing 15 changed files with 5,380 additions and 284 deletions.
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
"custom"
],
"license": "MIT",
"homepage": "https://github.com/dcasia/custom-relationship-field",
"authors": [
{
"name": "Christian Widlund",
"email": "[email protected]",
"role": "Contributer"
}
],
"require": {
"php": ">=7.1.0"
"php": ">=8.0",
"laravel/nova": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
99 changes: 0 additions & 99 deletions dist/js/card.js

This file was deleted.

4 changes: 2 additions & 2 deletions dist/js/field.js

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions dist/js/field.js.LICENSE

This file was deleted.

7 changes: 7 additions & 0 deletions dist/js/field.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*!
* vuex v4.0.2
* (c) 2021 Evan You
* @license MIT
*/

/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
30 changes: 30 additions & 0 deletions nova.mix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const mix = require('laravel-mix')
const webpack = require('webpack')
const path = require('path')

class NovaExtension {
name() {
return 'nova-extension'
}

register(name) {
this.name = name
}

webpackConfig(webpackConfig) {
webpackConfig.externals = {
vue: 'Vue',
}

webpackConfig.resolve.alias = {
...(webpackConfig.resolve.alias || {}),
'@': path.resolve(__dirname, '../vendor/laravel/nova/resources/js/'),
}

webpackConfig.output = {
uniqueName: this.name,
}
}
}

mix.extend('nova', new NovaExtension())
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
"production": "mix --production",
"nova:install": "npm --prefix='../vendor/laravel/nova' ci"
},
"devDependencies": {
"cross-env": "^6.0.3",
"laravel-mix": "^5.0.1",
"laravel-nova": "^1.0.11",
"vue-template-compiler": "^2.6.11"
},
"dependencies": {
"vue": "^2.6.11"
"@vue/compiler-sfc": "^3.2.22",
"laravel-mix": "^6.0.49",
"vue-loader": "^16.8.3"
}
}
132 changes: 66 additions & 66 deletions resources/js/components/CustomIndexComponent.vue
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
<script>
import IndexComponent from '~~nova~~/views/Index.vue'
import IndexComponent from '@/pages/Index.vue'
import { defineComponent } from 'vue'
export default {
extends: IndexComponent,
props: [ 'customRelationshipFieldAttribute', 'customRelationshipFieldLabel' ],
beforeCreate() {
const novaRequest = Nova.request
const interceptor = Nova.request().interceptors.request.use(
config => {
const interceptors = []
const interceptorsInstance = []
if (/^\/nova-api\/\S.+\/(filters|action)$/.test(config.url)) {
Nova.request = (...params) => {
if (config.method === 'post' && config.params.viaResourceId === '') {
for (const param of params) {
return config
for (const interceptor of interceptors) {
interceptor(param)
}
}
}
config.params[ 'customRelationshipFieldAttribute' ] = this.customRelationshipFieldAttribute
config.params[ 'customRelationshipFieldLabel' ] = this.customRelationshipFieldLabel
const axiosInstance = novaRequest(...params)
}
if (axiosInstance instanceof Promise) {
return axiosInstance
}
return config
for (const interceptor of interceptors) {
}
)
interceptorsInstance.push({
instance: axiosInstance,
interceptor: axiosInstance.interceptors.request.use(config => interceptor(config)),
})
}
return axiosInstance
this.$on('hook:destroyed', () => Nova.request().interceptors.request.eject(interceptor))
}
export default defineComponent({
extends: IndexComponent,
props: {
customRelationshipFieldAttribute: {
type: String,
required: true,
},
customRelationshipFieldLabel: {
type: String,
required: true,
},
},
computed: {
/**
* Build the resource request query string.
*/
resourceRequestQueryString() {
return {
search: this.currentSearch,
filters: this.encodedFilters,
orderBy: this.currentOrderBy,
orderByDirection: this.currentOrderByDirection,
perPage: this.currentPerPage,
trashed: this.currentTrashed,
page: this.currentPage,
viaResource: this.viaResource,
viaResourceId: this.viaResourceId,
viaRelationship: this.viaRelationship,
viaResourceRelationship: this.viaResourceRelationship,
relationshipType: this.relationshipType,
customRelationshipFieldAttribute: this.customRelationshipFieldAttribute,
customRelationshipFieldLabel: this.customRelationshipFieldLabel
beforeCreate() {
interceptors.push(config => {
if (config.params === undefined) {
config.params = {}
}
}
const regex = new RegExp(`^\/nova-api\/(${ this.resourceName })\/?(filters|actions|relate-authorization)?`)
if (regex.test(config.url)) {
config.params[ 'customRelationshipFieldAttribute' ] = this.customRelationshipFieldAttribute
config.params[ 'customRelationshipFieldLabel' ] = this.customRelationshipFieldLabel
}
return config
})
},
methods: {
/**
* Get the actions available for the current resource.
*/
getActions() {
this.actions = []
this.pivotActions = null
return Nova.request()
.get(`/nova-api/${ this.resourceName }/actions`, {
params: {
viaResource: this.viaResource,
viaResourceId: this.viaResourceId,
viaRelationship: this.viaRelationship,
relationshipType: this.relationshipType,
customRelationshipFieldAttribute: this.customRelationshipFieldAttribute,
customRelationshipFieldLabel: this.customRelationshipFieldLabel
}
})
.then(response => {
this.actions = _.filter(response.data.actions, a => a.showOnIndex)
this.pivotActions = response.data.pivotActions
})
beforeUnmount() {
for (const { instance, interceptor } of interceptorsInstance) {
instance.interceptors.request.eject(interceptor)
}
}
}
while (interceptorsInstance.length) {
interceptorsInstance.pop()
interceptors.pop()
}
},
})
</script>
Loading

0 comments on commit b81a37e

Please sign in to comment.