All files FormulateForm.vue

11.76% Statements 2/17
0% Branches 0/20
0% Functions 0/10
11.76% Lines 2/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85  3x   3x                                                                                                                                                                  
 
import { shallowEqualObjects } from './libs/utils'
 
export default {
  provide () {
    return {
      formulateFormSetter: this.setFieldValue,
      formulateFormRegister: this.register
    }
  },
  name: 'FormulateForm',
  model: {
    prop: 'formulateValue',
    event: 'input'
  },
  props: {
    name: {
      type: [String, Boolean],
      default: false
    },
    formulateValue: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      registry: {}
    }
  },
  computed: {
    formModel: {
      get () {
        return this.formulateValue
      },
      set (value) {
        this.$emit('input', value)
      }
    },
    hasFormulateValue () {
      return this.formulateValue && typeof this.formulateValue === 'object'
    },
    isVmodeled () {
      return !!(this.$options.propsData.hasOwnProperty('formulateValue') &&
        this._events &&
        Array.isArray(this._events.input) &&
        this._events.input.length)
    }
  },
  watch: {
    formulateValue: {
      handler (newValue, oldValue) {
        if (this.isVmodeled &&
          newValue &&
          typeof newValue === 'object'
        ) {
          for (const field in newValue) {
            if (this.registry.hasOwnProperty(field) && !shallowEqualObjects(newValue[field], this.registry[field].internalModelProxy)) {
              // If the value of the formulateValue changed (probably as a prop)
              // and it doesn't match the internal proxied value of the registered
              // component, we set it explicitly. Its important we check the
              // model proxy here since the model itself is not fully synchronous.
              this.registry[field].context.model = newValue[field]
            }
          }
        }
      },
      deep: true
    }
  },
  methods: {
    setFieldValue (field, value) {
      this.formModel = Object.assign({}, this.formulateValue, { [field]: value })
    },
    register (field, component) {
      this.registry[field] = component
      if (!component.$options.propsData.hasOwnProperty('formulateValue') && this.hasFormulateValue && this.formulateValue[field]) {
        // In the case that the form is carrying an initial value and the
        // element is not, set it directly.
        component.context.model = this.formulateValue[field]
      }
    }
  }
}