1
0
mirror of synced 2025-03-26 01:43:51 +03:00
vue-formulario/src/Formulate.js

314 lines
8.6 KiB
JavaScript
Raw Normal View History

import library from './libs/library'
import rules from './libs/rules'
import mimes from './libs/mimes'
import FileUpload from './FileUpload'
2020-04-22 00:00:02 -04:00
import { arrayify, parseLocale, has } from './libs/utils'
import isPlainObject from 'is-plain-object'
import { en } from '@braid/vue-formulate-i18n'
import fauxUploader from './libs/faux-uploader'
2020-04-16 09:22:58 -04:00
import FormulateSlot from './FormulateSlot'
import FormulateForm from './FormulateForm.vue'
2020-04-18 00:39:25 -04:00
import FormulateInput from './FormulateInput.vue'
import FormulateErrors from './FormulateErrors.vue'
2020-04-06 09:29:04 -04:00
import FormulateHelp from './slots/FormulateHelp.vue'
2020-04-16 09:22:58 -04:00
import FormulateGrouping from './FormulateGrouping.vue'
2020-04-06 09:29:04 -04:00
import FormulateLabel from './slots/FormulateLabel.vue'
2020-04-18 00:39:25 -04:00
import FormulateAddMore from './slots/FormulateAddMore.vue'
import FormulateInputBox from './inputs/FormulateInputBox.vue'
import FormulateInputText from './inputs/FormulateInputText.vue'
import FormulateInputFile from './inputs/FormulateInputFile.vue'
2020-04-16 09:22:58 -04:00
import FormulateRepeatable from './slots/FormulateRepeatable.vue'
import FormulateInputGroup from './inputs/FormulateInputGroup.vue'
2020-01-28 12:12:08 -05:00
import FormulateInputButton from './inputs/FormulateInputButton.vue'
import FormulateInputSelect from './inputs/FormulateInputSelect.vue'
import FormulateInputSlider from './inputs/FormulateInputSlider.vue'
import FormulateInputTextArea from './inputs/FormulateInputTextArea.vue'
import FormulateRepeatableProvider from './FormulateRepeatableProvider.vue'
/**
* The base formulate library.
*/
2018-01-30 17:21:21 -05:00
class Formulate {
/**
* Instantiate our base options.
2018-01-30 17:21:21 -05:00
*/
constructor () {
2020-02-27 14:47:24 -05:00
this.options = {}
this.defaults = {
components: {
2020-04-16 09:22:58 -04:00
FormulateSlot,
FormulateForm,
2020-04-06 09:29:04 -04:00
FormulateHelp,
FormulateLabel,
FormulateInput,
FormulateErrors,
2020-04-18 00:39:25 -04:00
FormulateAddMore,
2020-04-16 09:22:58 -04:00
FormulateGrouping,
FormulateInputBox,
FormulateInputText,
FormulateInputFile,
2020-04-16 09:22:58 -04:00
FormulateRepeatable,
FormulateInputGroup,
2020-01-28 12:12:08 -05:00
FormulateInputButton,
FormulateInputSelect,
FormulateInputSlider,
2020-04-18 23:57:43 -04:00
FormulateInputTextArea,
FormulateRepeatableProvider
2018-01-30 17:21:21 -05:00
},
2020-04-06 09:29:04 -04:00
slotDefaults: {
label: 'FormulateLabel',
help: 'FormulateHelp',
2020-04-18 00:39:25 -04:00
errors: 'FormulateErrors',
repeatable: 'FormulateRepeatable',
addMore: 'FormulateAddMore'
2020-04-06 09:29:04 -04:00
},
library,
rules,
mimes,
locale: false,
uploader: fauxUploader,
2020-03-01 13:28:46 -05:00
uploadUrl: false,
fileUrlKey: 'url',
uploadJustCompleteDuration: 1000,
errorHandler: (err) => err,
plugins: [ en ],
locales: {}
2018-01-30 17:21:21 -05:00
}
this.registry = new Map()
2018-01-30 17:21:21 -05:00
}
/**
* Install vue formulate, and register its components.
2018-01-30 17:21:21 -05:00
*/
install (Vue, options) {
2018-01-30 17:21:21 -05:00
Vue.prototype.$formulate = this
this.options = this.defaults
var plugins = this.defaults.plugins
if (options && Array.isArray(options.plugins) && options.plugins.length) {
plugins = plugins.concat(options.plugins)
2020-02-27 14:47:24 -05:00
}
plugins.forEach(plugin => (typeof plugin === 'function') ? plugin(this) : null)
this.extend(options || {})
for (var componentName in this.options.components) {
Vue.component(componentName, this.options.components[componentName])
2018-01-30 17:21:21 -05:00
}
}
/**
2020-02-27 14:47:24 -05:00
* Given a set of options, apply them to the pre-existing options.
* @param {Object} extendWith
2018-01-30 17:21:21 -05:00
*/
2020-02-27 14:47:24 -05:00
extend (extendWith) {
if (typeof extendWith === 'object') {
this.options = this.merge(this.options, extendWith)
return this
}
throw new Error(`VueFormulate extend() should be passed an object (was ${typeof extendWith})`)
}
/**
* Create a new object by copying properties of base and mergeWith.
* Note: arrays don't overwrite - they push
*
* @param {Object} base
* @param {Object} mergeWith
* @param {boolean} concatArrays
*/
merge (base, mergeWith, concatArrays = true) {
var merged = {}
for (var key in base) {
2020-02-27 14:47:24 -05:00
if (mergeWith.hasOwnProperty(key)) {
if (isPlainObject(mergeWith[key]) && isPlainObject(base[key])) {
merged[key] = this.merge(base[key], mergeWith[key], concatArrays)
} else if (concatArrays && Array.isArray(base[key]) && Array.isArray(mergeWith[key])) {
merged[key] = base[key].concat(mergeWith[key])
} else {
merged[key] = mergeWith[key]
}
} else {
merged[key] = base[key]
}
}
2020-02-27 14:47:24 -05:00
for (var prop in mergeWith) {
if (!merged.hasOwnProperty(prop)) {
2020-02-27 14:47:24 -05:00
merged[prop] = mergeWith[prop]
}
}
return merged
2018-01-30 17:21:21 -05:00
}
/**
* Determine what "class" of input this element is given the "type".
* @param {string} type
2018-01-30 17:21:21 -05:00
*/
classify (type) {
if (this.options.library.hasOwnProperty(type)) {
return this.options.library[type].classification
}
return 'unknown'
2018-01-30 17:21:21 -05:00
}
/**
* Determine what type of component to render given the "type".
* @param {string} type
2018-01-30 17:21:21 -05:00
*/
component (type) {
if (this.options.library.hasOwnProperty(type)) {
return this.options.library[type].component
2018-01-30 17:21:21 -05:00
}
return false
2018-01-30 17:21:21 -05:00
}
2020-04-06 09:29:04 -04:00
/**
* What component should be rendered for the given slot location and type.
* @param {string} type the type of component
* @param {string} slot the name of the slot
*/
slotComponent (type, slot) {
const def = this.options.library[type]
2020-04-18 00:39:25 -04:00
if (def.slotComponents && def.slotComponents[slot]) {
return def.slotComponents[slot]
2020-04-06 09:29:04 -04:00
}
return this.options.slotDefaults[slot]
}
/**
* Get validation rules by merging any passed in with global rules.
* @return {object} object of validation functions
*/
rules (rules = {}) {
return { ...this.options.rules, ...rules }
}
/**
* Attempt to get the vue-i18n configured locale.
*/
i18n (vm) {
if (vm.$i18n) {
switch (typeof vm.$i18n.locale) {
case 'string':
return vm.$i18n.locale
case 'function':
return vm.$i18n.locale()
}
}
return false
}
/**
* Select the proper locale to use.
*/
getLocale (vm) {
if (!this.selectedLocale) {
this.selectedLocale = [
this.options.locale,
this.i18n(vm),
'en'
].reduce((selection, locale) => {
if (selection) {
return selection
}
if (locale) {
const option = parseLocale(locale)
2020-04-22 00:00:02 -04:00
.find(locale => has(this.options.locales, locale))
if (option) {
selection = option
}
}
return selection
}, false)
}
return this.selectedLocale
}
/**
* Get the validation message for a particular error.
*/
validationMessage (rule, validationContext, vm) {
const generators = this.options.locales[this.getLocale(vm)]
if (generators.hasOwnProperty(rule)) {
return generators[rule](validationContext)
} else if (rule[0] === '_' && generators.hasOwnProperty(rule.substr(1))) {
return generators[rule.substr(1)](validationContext)
}
if (generators.hasOwnProperty('default')) {
return generators.default(validationContext)
}
return 'This field does not have a valid value'
}
/**
* Given an instance of a FormulateForm register it.
* @param {vm} form
*/
register (form) {
if (form.$options.name === 'FormulateForm' && form.name) {
this.registry.set(form.name, form)
}
}
/**
* Given an instance of a form, remove it from the registry.
* @param {vm} form
*/
deregister (form) {
if (
form.$options.name === 'FormulateForm' &&
form.name &&
this.registry.has(form.name)
) {
this.registry.delete(form.name)
}
}
/**
* Given an array, this function will attempt to make sense of the given error
* and hydrate a form with the resulting errors.
*
* @param {error} err
* @param {string} formName
* @param {error}
*/
handle (err, formName, skip = false) {
const e = skip ? err : this.options.errorHandler(err, formName)
if (formName && this.registry.has(formName)) {
this.registry.get(formName).applyErrors({
formErrors: arrayify(e.formErrors),
inputErrors: e.inputErrors || {}
})
}
return e
}
/**
* Get the file uploader.
*/
getUploader () {
return this.options.uploader || false
}
2020-03-01 13:28:46 -05:00
/**
* Get the global upload url.
*/
getUploadUrl () {
return this.options.uploadUrl || false
}
/**
* When re-hydrating a file uploader with an array, get the sub-object key to
* access the url of the file. Usually this is just "url".
*/
getFileUrlKey () {
return this.options.fileUrlKey || 'url'
}
/**
* Create a new instance of an upload.
*/
createUpload (fileList, context) {
return new FileUpload(fileList, context, this.options)
}
2018-01-30 17:21:21 -05:00
}
export default new Formulate()