Initial Spark Install
This commit is contained in:
23
spark/resources/assets/js/forms/bootstrap.js
vendored
Normal file
23
spark/resources/assets/js/forms/bootstrap.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Initialize the Spark form extension points.
|
||||
*/
|
||||
Spark.forms = {
|
||||
register: {},
|
||||
updateContactInformation: {},
|
||||
updateTeamMember: {}
|
||||
};
|
||||
|
||||
/**
|
||||
* Load the SparkForm helper class.
|
||||
*/
|
||||
require('./form');
|
||||
|
||||
/**
|
||||
* Define the SparkFormError collection class.
|
||||
*/
|
||||
require('./errors');
|
||||
|
||||
/**
|
||||
* Add additional HTTP / form helpers to the Spark object.
|
||||
*/
|
||||
$.extend(Spark, require('./http'));
|
71
spark/resources/assets/js/forms/errors.js
vendored
Normal file
71
spark/resources/assets/js/forms/errors.js
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Spark form error collection class.
|
||||
*/
|
||||
window.SparkFormErrors = function () {
|
||||
this.errors = {};
|
||||
|
||||
/**
|
||||
* Determine if the collection has any errors.
|
||||
*/
|
||||
this.hasErrors = function () {
|
||||
return ! _.isEmpty(this.errors);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the collection has errors for a given field.
|
||||
*/
|
||||
this.has = function (field) {
|
||||
return _.indexOf(_.keys(this.errors), field) > -1;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get all of the raw errors for the collection.
|
||||
*/
|
||||
this.all = function () {
|
||||
return this.errors;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get all of the errors for the collection in a flat array.
|
||||
*/
|
||||
this.flatten = function () {
|
||||
return _.flatten(_.toArray(this.errors));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the first error message for a given field.
|
||||
*/
|
||||
this.get = function (field) {
|
||||
if (this.has(field)) {
|
||||
return this.errors[field][0];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the raw errors for the collection.
|
||||
*/
|
||||
this.set = function (errors) {
|
||||
if (typeof errors === 'object') {
|
||||
this.errors = errors;
|
||||
} else {
|
||||
this.errors = {'form': ['Something went wrong. Please try again or contact customer support.']};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Remove errors from the collection.
|
||||
*/
|
||||
this.forget = function (field) {
|
||||
if (typeof field === 'undefined') {
|
||||
this.errors = {};
|
||||
} else {
|
||||
Vue.delete(this.errors, field);
|
||||
}
|
||||
};
|
||||
};
|
51
spark/resources/assets/js/forms/form.js
vendored
Normal file
51
spark/resources/assets/js/forms/form.js
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* SparkForm helper class. Used to set common properties on all forms.
|
||||
*/
|
||||
window.SparkForm = function (data) {
|
||||
var form = this;
|
||||
|
||||
$.extend(this, data);
|
||||
|
||||
/**
|
||||
* Create the form error helper instance.
|
||||
*/
|
||||
this.errors = new SparkFormErrors();
|
||||
|
||||
this.busy = false;
|
||||
this.successful = false;
|
||||
|
||||
/**
|
||||
* Start processing the form.
|
||||
*/
|
||||
this.startProcessing = function () {
|
||||
form.errors.forget();
|
||||
form.busy = true;
|
||||
form.successful = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Finish processing the form.
|
||||
*/
|
||||
this.finishProcessing = function () {
|
||||
form.busy = false;
|
||||
form.successful = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reset the errors and other state for the form.
|
||||
*/
|
||||
this.resetStatus = function () {
|
||||
form.errors.forget();
|
||||
form.busy = false;
|
||||
form.successful = false;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the errors on the form.
|
||||
*/
|
||||
this.setErrors = function (errors) {
|
||||
form.busy = false;
|
||||
form.errors.set(errors);
|
||||
};
|
||||
};
|
56
spark/resources/assets/js/forms/http.js
vendored
Normal file
56
spark/resources/assets/js/forms/http.js
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
module.exports = {
|
||||
/**
|
||||
* Helper method for making POST HTTP requests.
|
||||
*/
|
||||
post(uri, form) {
|
||||
return Spark.sendForm('post', uri, form);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Helper method for making PUT HTTP requests.
|
||||
*/
|
||||
put(uri, form) {
|
||||
return Spark.sendForm('put', uri, form);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Helper method for making PATCH HTTP requests.
|
||||
*/
|
||||
patch(uri, form) {
|
||||
return Spark.sendForm('patch', uri, form);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Helper method for making DELETE HTTP requests.
|
||||
*/
|
||||
delete(uri, form) {
|
||||
return Spark.sendForm('delete', uri, form);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Send the form to the back-end server.
|
||||
*
|
||||
* This function will clear old errors, update "busy" status, etc.
|
||||
*/
|
||||
sendForm(method, uri, form) {
|
||||
return new Promise((resolve, reject) => {
|
||||
form.startProcessing();
|
||||
|
||||
axios[method](uri, JSON.parse(JSON.stringify(form)))
|
||||
.then(response => {
|
||||
form.finishProcessing();
|
||||
|
||||
resolve(response.data);
|
||||
})
|
||||
.catch(errors => {
|
||||
form.setErrors(errors.response.data.errors);
|
||||
|
||||
reject(errors.response.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user