This commit is contained in:
2024-09-01 18:54:23 +05:00
parent 76d18365a5
commit 061f09eca1
1597 changed files with 109451 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
import axios from 'axios'
import isNil from 'lodash/isNil'
export function setupAxios() {
const instance = axios.create()
instance.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
instance.defaults.headers.common['X-CSRF-TOKEN'] =
document.head.querySelector('meta[name="csrf-token"]').content
instance.interceptors.response.use(
response => response,
error => {
if (axios.isCancel(error)) {
return Promise.reject(error)
}
const response = error.response
const {
status,
data: { redirect },
} = response
// Show the user a 500 error
if (status >= 500) {
Nova.$emit('error', error.response.data.message)
}
// Handle Session Timeouts (Unauthorized)
if (status === 401) {
// Use redirect if being specificed by the response
if (!isNil(redirect)) {
location.href = redirect
return
}
Nova.redirectToLogin()
}
// Handle Forbidden
if (status === 403) {
Nova.visit('/403')
}
// Handle Token Timeouts
if (status === 419) {
Nova.$emit('token-expired')
}
return Promise.reject(error)
}
)
return instance
}

View File

@@ -0,0 +1,5 @@
import upperFirst from 'lodash/upperFirst'
export default function (string) {
return upperFirst(string)
}

View File

@@ -0,0 +1,6 @@
export function escapeUnicode(str) {
return str.replace(
/[^\0-~]/g,
c => '\\u' + ('000' + c.charCodeAt().toString(16)).slice(-4)
)
}

View File

@@ -0,0 +1,5 @@
import isNil from 'lodash/isNil'
export default function filled(value) {
return Boolean(!isNil(value) && value !== '')
}

View File

@@ -0,0 +1,11 @@
export default function (locale) {
let hourCycle = Intl.DateTimeFormat(locale, {
hour: 'numeric',
}).resolvedOptions().hourCycle
if (hourCycle == 'h23' || hourCycle == 'h24') {
return 24
}
return 12
}

View File

@@ -0,0 +1,11 @@
export default function increaseOrDecrease(currentValue, startingValue) {
if (startingValue === 0) {
return null
}
if (currentValue > startingValue) {
return ((currentValue - startingValue) / Math.abs(startingValue)) * 100
} else {
return ((startingValue - currentValue) / Math.abs(startingValue)) * -100
}
}

View File

@@ -0,0 +1,5 @@
export { default as hourCycle } from './hourCycle'
export { default as increaseOrDecrease } from './increaseOrDecrease'
export { default as minimum } from './minimum'
export { default as singularOrPlural } from './singularOrPlural'
export { default as capitalize } from './capitalize'

View File

@@ -0,0 +1,28 @@
import { Inertia } from '@inertiajs/inertia'
import { InertiaProgress } from '@inertiajs/progress'
import debounce from 'lodash/debounce'
export function setupInertia() {
InertiaProgress.init({
delay: 250,
includeCSS: false,
showSpinner: false,
})
const handlePopstateEvent = function (event) {
if (this.ignoreHistoryState === false) {
this.handlePopstateEvent(event)
}
}
Inertia.ignoreHistoryState = false
Inertia.setupEventListeners = function () {
window.addEventListener('popstate', handlePopstateEvent.bind(Inertia))
document.addEventListener(
'scroll',
debounce(Inertia.handleScrollEvent.bind(Inertia), 100),
true
)
}
}

View File

@@ -0,0 +1,39 @@
import forEach from 'lodash/forEach'
export default function __(key, replace) {
let translation = Nova.config('translations')[key]
? Nova.config('translations')[key]
: key
forEach(replace, (value, key) => {
key = new String(key)
if (value === null) {
console.error(
`Translation '${translation}' for key '${key}' contains a null replacement.`
)
return
}
value = new String(value)
const searches = [
':' + key,
':' + key.toUpperCase(),
':' + key.charAt(0).toUpperCase() + key.slice(1),
]
const replacements = [
value,
value.toUpperCase(),
value.charAt(0).toUpperCase() + value.slice(1),
]
for (let i = searches.length - 1; i >= 0; i--) {
translation = translation.replace(searches[i], replacements[i])
}
})
return translation
}

View File

@@ -0,0 +1,13 @@
export default function (originalPromise, delay = 100) {
return Promise.all([
originalPromise,
new Promise(resolve => {
setTimeout(() => resolve(), delay)
}),
]).then(result => result[0])
}
// Usage
// minimum(axios.get('/'))
// .then(response => console.log('done'))
// .catch(error => console.log(error))

View File

@@ -0,0 +1,24 @@
import numbro from 'numbro'
import numbroLanguages from 'numbro/dist/languages.min'
export function setupNumbro(locale) {
if (locale) {
locale = locale.replace('_', '-')
Object.values(numbroLanguages).forEach(language => {
let name = language.languageTag
if (locale === name || locale === name.substr(0, 2)) {
numbro.registerLanguage(language)
}
})
numbro.setLanguage(locale)
}
numbro.setDefaults({
thousandSeparated: true,
})
return numbro
}

View File

@@ -0,0 +1,10 @@
import { Fragment } from 'vue'
export function renderSlotFragments(children) {
if (!children) return []
return children.flatMap(child => {
if (child.type === Fragment) return renderSlotFragments(child.children)
return [child]
})
}

View File

@@ -0,0 +1,9 @@
import inflector from 'inflector-js'
import isString from 'lodash/isString'
export default function singularOrPlural(value, suffix) {
if (isString(suffix) && suffix.match(/^(.*)[A-Za-zÀ-ÖØ-öø-ÿ]$/) == null)
return suffix
if (value > 1 || value == 0) return inflector.pluralize(suffix)
return inflector.singularize(suffix)
}

View File

@@ -0,0 +1,8 @@
import lowerCase from 'lodash/lowerCase'
import slug from 'slugify'
const slugify = (value, separator = '-') => {
return slug(lowerCase(value), separator)
}
export default slugify

View File

@@ -0,0 +1,14 @@
import identity from 'lodash/identity'
import pickBy from 'lodash/pickBy'
export default function url(base, path, parameters) {
let searchParams = new URLSearchParams(pickBy(parameters || {}, identity))
let queryString = searchParams.toString()
if (base == '/' && path.startsWith('/')) {
base = ''
}
return base + path + (queryString.length > 0 ? `?${queryString}` : '')
}