Skip to content

Commit

Permalink
refactor source to be more vue and nova compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
scramatte committed May 17, 2024
1 parent 054eb0c commit e019324
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 50 deletions.
3 changes: 2 additions & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/js/tool.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
6 changes: 2 additions & 4 deletions nova.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class NovaExtension {

webpackConfig.resolve.alias = {
...(webpackConfig.resolve.alias || {}),
'laravel-nova': path.join(
__dirname,
'../../vendor/laravel/nova/resources/js/mixins/packages.js'
),
'laravel-nova': path.join(__dirname,'../../vendor/laravel/nova/resources/js/mixins/packages.js'),
'laravel-nova-ui': path.join(__dirname, '../../vendor/laravel/nova/node_modules/laravel-nova-ui'),
}

webpackConfig.output = {
Expand Down
35 changes: 19 additions & 16 deletions resources/js/components/LanguageSwitcher.vue
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
<template>
<Dropdown placement="bottom-end">
<DropdownTrigger
:show-arrow="false"
class="h-10 w-full hover:text-primary-500"
>

<Icon type="language" />
<span class="ml-1 font-bold hidden md:inline">{{selectedDisplay}}</span>
</DropdownTrigger>
<Dropdown placement="bottom-end" class="relative">
<Button variant="action" icon="globe-alt" class="text-primary-500 md:w-24" >
<span class="hidden md:inline">{{ selectedDisplay }}</span>
</Button>
<template #menu>
<DropdownMenu>
<template v-for="(value,key) in langs">
<div class="flex flex-col py-1">
<nav class="flex flex-col py-1">
<DropdownMenuItem
:key="key"
as="button"
class="flex items-center hover:bg-gray-100"
@click.prevent="changeLang(key)"
>
<span class="ml-2" v-if="selected!==key">{{value}}</span>
<span class="ml-2 font-bold text-primary-500" v-else>{{value}}</span>
<span class="ml-2" v-if="selected!==key">
{{value}}
</span>
<span class="ml-2 font-mediun text-primary-500" v-else>{{value}}</span>
</DropdownMenuItem>
</div>
</nav>
</template>

</DropdownMenu>
</template>
</Dropdown>

</template>

<script>
import { Button } from 'laravel-nova-ui'
export default {
name: "LanguageSwitcher",
components: { Button, Icon },
props: [ 'langs', 'selected', 'selectedDisplay' ],
methods:{
changeLang(key) {
if (key !== this.selected) {
Expand All @@ -43,6 +40,12 @@ export default {
});
}
},
},
computed: {
languageIcon() {
return 'sun';
}
}
}
Expand Down
61 changes: 32 additions & 29 deletions resources/js/tool.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import LanguageSwitcher from "./components/LanguageSwitcher";
import {createApp,defineComponent} from 'vue';
import { createVNode, render} from 'vue';

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

window.addEventListener('DOMContentLoaded',()=>{
let appHeader = document.getElementsByTagName('header');

if (appHeader.length > 0) {
let languages = Nova.config('nova_language_switcher').languages;
let selected = Nova.config('nova_language_switcher').current_lang;
let switchLang = defineComponent({
extends: LanguageSwitcher, data() {
return {
langs: languages,
selectedDisplay:languages[selected],
selected:selected
}
app.mixin({
data() {
return {
toDestroy: [],
}
},
async mounted() {
if (this._.type?.__file?.endsWith('MainHeader.vue')) {
const screen = this._.attrs[ 'data-screen' ]

console.log('language-switcher:mounted',screen);

const languages = Nova.config('nova_language_switcher').languages;
let selected = Nova.config('nova_language_switcher').current_lang;

const container = document.createElement('div')
container.className = 'relative z-50';

const vnode = createVNode(LanguageSwitcher, {
langs: languages,
selectedDisplay:languages[selected],
selected:selected
})

vnode.appContext = app._context
render(vnode, container)

const element = this._.vnode.el.querySelector('header > div:last-child > div:last-child > div');
if (element) {
element.insertAdjacentElement('beforebegin', container)
}
})

let lang = document.createElement('div');
lang.className = 'mr-3';
let newApp = createApp(switchLang);

newApp.component('Dropdown',app._context.components.Dropdown);
newApp.component('DropdownTrigger',app._context.components.DropdownTrigger);
newApp.component('DropdownMenu',app._context.components.DropdownMenu);
newApp.component('DropdownMenuItem',app._context.components.DropdownMenuItem);
newApp.component('Icon', app._context.components.HeroiconsOutlineGlobe);

newApp.mount(lang);

appHeader[0].lastChild.lastChild.insertBefore(lang,appHeader[0].lastChild.lastChild.firstChild);
}
}
})
})
Expand Down

0 comments on commit e019324

Please sign in to comment.