laravel-jetstream
Version:
<p align="center"><img src="https://laravel.com/assets/img/components/logo-jetstream.svg"></p>
353 lines (291 loc) • 12.5 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.useForm = useForm;
var _inertia = require('@inertiajs/inertia');
var _vue = require('vue');
var _util = require('./util');
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var InertiaForm = function () {
function InertiaForm() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, InertiaForm);
this.processing = false;
this.successful = false;
this.recentlySuccessful = false;
this.isDirty = false;
this.withData(data).withOptions(options);
return new Proxy(this, {
set: function set(obj, prop, value) {
obj[prop] = value;
if (_util.reservedFieldNames.indexOf(prop) === -1 && value !== obj.initial[prop]) {
obj.isDirty = true;
}
return true;
}
});
}
_createClass(InertiaForm, [{
key: 'withData',
value: function withData(data) {
if ((0, _util.isArray)(data)) {
data = data.reduce(function (carry, element) {
carry[element] = '';
return carry;
}, {});
}
this.setInitialValues(data);
this.processing = false;
this.successful = false;
for (var field in data) {
(0, _util.guardAgainstReservedFieldName)(field);
this[field] = data[field];
}
this.isDirty = false;
return this;
}
}, {
key: 'withOptions',
value: function withOptions(options) {
this.__options = {
bag: 'default',
resetOnSuccess: true,
setInitialOnSuccess: false
};
if (options.hasOwnProperty('bag')) {
this.__options.bag = options.bag;
}
if (options.hasOwnProperty('resetOnSuccess')) {
this.__options.resetOnSuccess = options.resetOnSuccess;
}
if (options.hasOwnProperty('setInitialOnSuccess')) {
this.__options.setInitialOnSuccess = options.setInitialOnSuccess;
}
return this;
}
}, {
key: 'withInertia',
value: function withInertia(inertia) {
this.__inertia = inertia;
return this;
}
}, {
key: 'withPage',
value: function withPage(page) {
this.__page = page;
return this;
}
}, {
key: 'data',
value: function data() {
var data = {
'_error_bag': this.__options.bag
};
for (var property in this.initial) {
data[property] = this[property];
}
return data;
}
}, {
key: 'reset',
value: function reset() {
(0, _util.merge)(this, this.initial);
this.isDirty = false;
}
}, {
key: 'setInitialValues',
value: function setInitialValues(values) {
this.initial = {};
(0, _util.merge)(this.initial, values);
}
}, {
key: 'post',
value: function post(url) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.submit('post', url, options);
}
}, {
key: 'put',
value: function put(url) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.submit('put', url, options);
}
}, {
key: 'patch',
value: function patch(url) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.submit('patch', url, options);
}
}, {
key: 'delete',
value: function _delete(url, options) {
return this.submit('delete', url, options);
}
}, {
key: 'submit',
value: function submit(requestType, url, options) {
var _this = this;
this.__validateRequestType(requestType);
this.processing = true;
this.successful = false;
var onSuccess = function onSuccess(page) {
_this.processing = false;
if (!_this.hasErrors()) {
_this.onSuccess();
} else {
_this.onFail();
}
if (options.onSuccess) {
options.onSuccess(page);
}
};
if (requestType === 'delete') {
return this.__inertia[requestType](url, _extends({}, options, { onSuccess: onSuccess }));
}
return this.__inertia[requestType](url, this.hasFiles() ? (0, _util.objectToFormData)(this.data()) : this.data(), _extends({}, options, { onSuccess: onSuccess }));
}
}, {
key: 'hasFiles',
value: function hasFiles() {
for (var property in this.initial) {
if (this.hasFilesDeep(this[property])) {
return true;
}
}
return false;
}
}, {
key: 'hasFilesDeep',
value: function hasFilesDeep(object) {
if (object === null) {
return false;
}
if ((typeof object === 'undefined' ? 'undefined' : _typeof(object)) === 'object') {
for (var key in object) {
if (object.hasOwnProperty(key)) {
if (this.hasFilesDeep(object[key])) {
return true;
}
}
}
}
if (Array.isArray(object)) {
for (var _key in object) {
if (object.hasOwnProperty(_key)) {
return this.hasFilesDeep(object[_key]);
}
}
}
return (0, _util.isFile)(object);
}
}, {
key: 'onSuccess',
value: function onSuccess() {
var _this2 = this;
this.successful = true;
this.recentlySuccessful = true;
setTimeout(function () {
return _this2.recentlySuccessful = false;
}, 2000);
if (this.__options.resetOnSuccess) {
this.reset();
} else if (this.__options.setInitialOnSuccess) {
var _data = this.data(),
_error_bag = _data._error_bag,
data = _objectWithoutProperties(_data, ['_error_bag']);
this.setInitialValues(data);
this.isDirty = false;
}
}
}, {
key: 'onFail',
value: function onFail() {
this.successful = false;
this.recentlySuccessful = false;
}
}, {
key: 'hasErrors',
value: function hasErrors() {
return this.inertiaPage().errorBags[this.__options.bag] && Object.keys(this.inertiaPage().errorBags[this.__options.bag]).length > 0;
}
}, {
key: 'hasError',
value: function hasError(field) {
return this.hasErrors() && this.inertiaPage().errorBags[this.__options.bag][field] && this.inertiaPage().errorBags[this.__options.bag][field].length > 0;
}
}, {
key: 'error',
value: function error(field) {
if (this.hasError(field)) {
return this.inertiaPage().errorBags[this.__options.bag][field][0];
}
}
}, {
key: 'errors',
value: function errors() {
var field = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
if (field === null) {
return this.hasErrors() ? Object.values(this.inertiaPage().errorBags[this.__options.bag]).reduce(function (a, b) {
return a.concat(b);
}, []) : [];
}
return this.hasError(field) ? this.inertiaPage().errorBags[this.__options.bag][field] : [];
}
}, {
key: 'inertiaPage',
value: function inertiaPage() {
return this.__page();
}
}, {
key: '__validateRequestType',
value: function __validateRequestType(requestType) {
var requestTypes = ['get', 'post', 'put', 'patch', 'delete'];
if (requestTypes.indexOf(requestType) === -1) {
throw new Error('`' + requestType + '` is not a valid request type, ' + ('must be one of: `' + requestTypes.join('`, `') + '`.'));
}
}
}], [{
key: 'create',
value: function create() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new InertiaForm().withData(data);
}
}]);
return InertiaForm;
}();
function useForm(data, options) {
var adapter = require('@inertiajs/inertia-vue3');
if (!adapter || !_vue.shallowReactive) throw Error('The useForm hook may only be used with Vue 3 and @inertiajs/inertia-vue3.');
return (0, _vue.shallowReactive)(InertiaForm.create().withData(data).withOptions(options).withInertia(_inertia.Inertia).withPage(function () {
return adapter.usePage().props.value;
}));
}
exports.default = {
install: function install(app) {
if (app.version.split('.')[0] == 3) {
Object.defineProperty(app.config.globalProperties.$inertia, 'form', {
value: function value() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return InertiaForm.create().withData(data).withOptions(options).withInertia(app.config.globalProperties.$inertia).withPage(function () {
return app.config.globalProperties.$page.hasOwnProperty('props') ? app.config.globalProperties.$page.props : app.config.globalProperties.$page;
});
}
});
} else {
app.prototype.$inertia.form = function () {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return InertiaForm.create().withData(data).withOptions(options).withInertia(app.prototype.$inertia).withPage(function () {
return app.prototype.$page.hasOwnProperty('props') ? app.prototype.$page.props : app.prototype.$page;
});
};
}
}
};