2FAuth/public/build/assets/Form-EFIuWKGI.js.map
2024-11-27 12:03:02 +01:00

1 line
15 KiB
Plaintext

{"version":3,"file":"Form-EFIuWKGI.js","sources":["../../../resources/js/components/formElements/FormErrors.js","../../../resources/js/components/formElements/Form.js"],"sourcesContent":["\nexport default class Errors {\n /**\n * Create a new error bag instance.\n */\n constructor () {\n this.errors = {}\n }\n\n /**\n * Set the errors object or field error messages.\n *\n * @param {Object|String} field\n * @param {Array|String|undefined} messages\n */\n set (field, messages) {\n if (typeof field === 'object') {\n this.errors = field\n } else {\n this.set({ ...this.errors, [field]: arrayWrap(messages) })\n }\n }\n\n /**\n * Get all the errors.\n *\n * @return {Object}\n */\n all () {\n return this.errors\n }\n\n /**\n * Determine if there is an error for the given field.\n *\n * @param {String} field\n * @return {Boolean}\n */\n has (field) {\n return this.errors.hasOwnProperty(field)\n }\n\n /**\n * Determine if there are any errors for the given fields.\n *\n * @param {...String} fields\n * @return {Boolean}\n */\n hasAny (...fields) {\n return fields.some(field => this.has(field))\n }\n\n /**\n * Determine if there are any errors.\n *\n * @return {Boolean}\n */\n any () {\n return Object.keys(this.errors).length > 0\n }\n\n /**\n * Get the first error message for the given field.\n *\n * @param String} field\n * @return {String|undefined}\n */\n get (field) {\n if (this.has(field)) {\n return this.getAll(field)[0]\n }\n }\n\n /**\n * Get all the error messages for the given field.\n *\n * @param {String} field\n * @return {Array}\n */\n getAll (field) {\n return arrayWrap(this.errors[field] || [])\n }\n\n /**\n * Get the error message for the given fields.\n *\n * @param {...String} fields\n * @return {Array}\n */\n only (...fields) {\n const messages = []\n\n fields.forEach(field => {\n const message = this.get(field)\n\n if (message) {\n messages.push(message)\n }\n })\n\n return messages\n }\n\n /**\n * Get all the errors in a flat array.\n *\n * @return {Array}\n */\n flatten () {\n return Object.values(this.errors).reduce((a, b) => a.concat(b), [])\n }\n\n /**\n * Clear one or all error fields.\n *\n * @param {String|undefined} field\n */\n clear (field) {\n const errors = {}\n\n if (field) {\n Object.keys(this.errors).forEach(key => {\n if (key !== field) {\n errors[key] = this.errors[key]\n }\n })\n }\n\n this.set(errors)\n }\n}\n\n/**\n * If the given value is not an array, wrap it in one.\n *\n * @param {Any} value\n * @return {Array}\n */\nfunction arrayWrap (value) {\n return Array.isArray(value) ? value : [value]\n}","import { httpClientFactory } from '@/services/httpClientFactory'\nimport Errors from './FormErrors'\n\nclass Form {\n /**\n * Create a new form instance.\n *\n * @param {Object} data\n */\n constructor (data = {}) {\n this.axios = httpClientFactory('web')\n this.isBusy = false\n this.isDisabled = false\n // this.successful = false\n this.errors = new Errors()\n this.originalData = this.deepCopy(data)\n\n Object.assign(this, data)\n }\n\n /**\n * Fill form data.\n *\n * @param {Object} data\n */\n fill (data) {\n this.keys().forEach(key => {\n this[key] = data[key]\n })\n }\n\n /**\n * Update original form data.\n */\n setOriginal () {\n Object.keys(this)\n .filter(key => !Form.ignore.includes(key))\n .forEach(key => {\n this.originalData[key] = this.deepCopy(this[key])\n })\n }\n\n /**\n * Fill form data.\n *\n * @param {Object} data\n */\n hasChanged () {\n return this.keys().some(key => this[key] !== this.originalData[key])\n }\n\n /**\n * Fill form data.\n *\n * @param {Object} data\n */\n fillWithKeyValueObject (data) {\n this.keys().forEach(key => {\n const keyValueObject = data.find(s => s.key === key.toString())\n if(keyValueObject != undefined) {\n this[key] = keyValueObject.value\n }\n })\n }\n\n /**\n * Get the form data.\n *\n * @return {Object}\n */\n data () {\n return this.keys().reduce((data, key) => (\n { ...data, [key]: this[key] }\n ), {})\n }\n\n /**\n * Get the form data keys.\n *\n * @return {Array}\n */\n keys () {\n return Object.keys(this)\n .filter(key => !Form.ignore.includes(key))\n }\n\n /**\n * Start processing the form.\n */\n startProcessing () {\n this.errors.clear()\n this.isBusy = true\n // this.successful = false\n }\n\n /**\n * Finish processing the form.\n */\n finishProcessing () {\n this.isBusy = false\n // this.successful = true\n }\n\n /**\n * Clear the form errors.\n */\n clear () {\n this.errors.clear()\n // this.successful = false\n }\n\n /**\n * Reset the form fields.\n */\n reset () {\n Object.keys(this)\n .filter(key => !Form.ignore.includes(key))\n .forEach(key => {\n this[key] = this.deepCopy(this.originalData[key])\n })\n }\n\n /**\n * Submit the form via a GET request.\n *\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n get (url, config = {}) {\n return this.submit('get', url, config)\n }\n\n /**\n * Submit the form via a POST request.\n *\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n post (url, config = {}) {\n return this.submit('post', url, config)\n }\n\n /**\n * Submit the form via a PATCH request.\n *\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n patch (url, config = {}) {\n return this.submit('patch', url, config)\n }\n\n /**\n * Submit the form via a PUT request.\n *\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n put (url, config = {}) {\n return this.submit('put', url, config)\n }\n\n /**\n * Submit the form via a DELETE request.\n *\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n delete (url, config = {}) {\n return this.submit('delete', url, config)\n }\n\n /**\n * Submit the form data via an HTTP request.\n *\n * @param {String} method (get, post, patch, put)\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n submit (method, url, config = {}) {\n this.startProcessing()\n\n const data = method === 'get'\n ? { params: this.data() }\n : this.data()\n\n return new Promise((resolve, reject) => {\n // (Form.axios || axios).request({ url: this.route(url), method, data, ...config })\n this.axios.request({ url: this.route(url), method, data, ...config })\n .then(response => {\n this.finishProcessing()\n\n resolve(response)\n })\n .catch(error => {\n this.isBusy = false\n\n if (error.response) {\n this.errors.set(this.extractErrors(error.response))\n }\n if (error.response?.status != 422) {\n reject(error)\n }\n })\n })\n }\n\n /**\n * Submit the form data via an HTTP request.\n *\n * @param {String} method (get, post, patch, put)\n * @param {String} url\n * @param {Object} config (axios config)\n * @return {Promise}\n */\n upload (url, config = {}) {\n this.startProcessing()\n\n return new Promise((resolve, reject) => {\n // https://www.npmjs.com/package/axios#-automatic-serialization-to-formdata\n this.axios.post(this.route(url), this.data(), { headers: {'Content-Type' : 'multipart/form-data'}, ...config })\n .then(response => {\n this.finishProcessing()\n\n resolve(response)\n })\n .catch(error => {\n this.isBusy = false\n\n if (error.response) {\n this.errors.set(this.extractErrors(error.response))\n }\n // if (error.response?.status != 422) {\n reject(error)\n // }\n })\n })\n }\n\n /**\n * Extract the errors from the response object.\n *\n * @param {Object} response\n * @return {Object}\n */\n extractErrors (response) {\n if (!response.data || typeof response.data !== 'object') {\n return { error: Form.errorMessage }\n }\n\n if (response.data.errors) {\n return { ...response.data.errors }\n }\n\n if (response.data.message) {\n return { error: response.data.message }\n }\n\n return { ...response.data }\n }\n\n /**\n * Get a named route.\n *\n * @param {String} name\n * @return {Object} parameters\n * @return {String}\n */\n route (name, parameters = {}) {\n let url = name\n\n if (Form.routes.hasOwnProperty(name)) {\n url = decodeURI(Form.routes[name])\n }\n\n if (typeof parameters !== 'object') {\n parameters = { id: parameters }\n }\n\n Object.keys(parameters).forEach(key => {\n url = url.replace(`{${key}}`, parameters[key])\n })\n\n return url\n }\n\n /**\n * Clear errors on keydown.\n *\n * @param {KeyboardEvent} event\n */\n onKeydown (event) {\n if (event.target.name) {\n this.errors.clear(event.target.name)\n }\n }\n\n /**\n * Deep copy the given object.\n *\n * @param {Object} obj\n * @return {Object}\n */\n deepCopy (obj) {\n if (obj === null || typeof obj !== 'object') {\n return obj\n }\n \n const copy = Array.isArray(obj) ? [] : {}\n \n Object.keys(obj).forEach(key => {\n copy[key] = this.deepCopy(obj[key])\n })\n \n return copy\n }\n}\n\nForm.routes = {}\nForm.errorMessage = 'Something went wrong. Please try again.'\nForm.ignore = ['isBusy', 'isDisabled', 'errors', 'originalData', 'axios']\n\nexport default Form\n"],"names":["Errors","field","messages","arrayWrap","fields","message","a","b","errors","key","value","Form","data","httpClientFactory","keyValueObject","s","url","config","method","resolve","reject","response","error","_a","name","parameters","event","obj","copy"],"mappings":"+HACe,MAAMA,CAAO,CAI1B,aAAe,CACb,KAAK,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