Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/vreg/plugins/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/vreg/plugins/form.js
class Errors {
  /**
   * Create a new Errors instance.
   */
  constructor() {
    this.errors = {};
  }


  /**
   * Determine if an errors exists for the given field.
   *
   * @param {string} field
   */
  has(field) {
    return this.errors.hasOwnProperty(field);
  }

  /**
   * Determine if we have any errors.
   */
  any() {
    return Object.keys(this.errors).length > 0;
  }


  /**
   * Retrieve the error message for a field.
   *
   * @param {string} field
   */
  get(field) {
    if (this.errors[field]) {
      return this.errors[field][0];
    } else {
      return null;
    }
  }


  /**
   * Record the new errors.
   *
   * @param {object} errors
   */
  record(errors) {
    this.errors = errors;
  }


  /**
   * Clear one or all error fields.
   *
   * @param {string|null} field
   */
  clear(field) {
    if (field) {
      delete this.errors[field];

      return;
    }

    this.purge();
  }

  purge() {
    this.errors = {};
  }
}

class Form {
  isReady = false;

  config = {
    resetOnSuccess: true,
    fetchFormDataURL: null,
  }

  /**
   *
   * @param ctx
   * @param data
   * @param config
   */
  constructor(ctx, data, config) {
    this.originalData = data;

    this.ctx = ctx;

    for (let field in data) {
      this[field] = data[field];
    }

    this.errors = new Errors();

    Object.assign(this.config, config);

    this.loadFormData().then(() => this.isReady = true);
  }

  async loadFormData() {
    if (!this.config.fetchFormDataURL) {
      return true;
    }

    await this.ctx.$axios.get(this.config.fetchFormDataURL).then(({data}) => {
      let response = data.response;
      if (response.defaults) {
        for (let key in response.defaults) {
          this[key] = response.defaults[key];
          this.originalData[key] = response.defaults[key];
        }
        delete response.defaults;
      }

      this.formData = response;
    });

    return true;
  }

  getFormData(key) {
    if (!this.formData || !this.formData[key]) {
      return []
    }

    return this.formData[key];
  }

  error(field) {
    return this.errors.get(field);
  }

  state(field) {
    if (this.errors.has(field)) {
      return false;
    }

    if (!this[field] || !this[field].length) {
      return;
    }

    return true;
  }

  /**
   * Fetch all relevant data for the form.
   */
  data() {
    let data = {};

    for (let property in this.originalData) {
      data[property] = this[property];
    }

    return data;
  }


  /**
   * Reset the form fields.
   */
  reset(force = false) {
    if (!this.config.resetOnSuccess && !force) {
      return;
    }
    for (let field in this.originalData) {
      this[field] = '';
    }

    this.errors.clear();
  }


  /**
   * Send a POST request to the given URL.
   * .
   * @param {string} url
   */
  post(url) {
    return this.submit('post', url);
  }


  /**
   * Send a PUT request to the given URL.
   * .
   * @param {string} url
   */
  put(url) {
    return this.submit('put', url);
  }


  /**
   * Send a PATCH request to the given URL.
   * .
   * @param {string} url
   */
  patch(url) {
    return this.submit('patch', url);
  }


  /**
   * Send a DELETE request to the given URL.
   * .
   * @param {string} url
   */
  delete(url) {
    return this.submit('delete', url);
  }


  /**
   * Submit the form.
   *
   * @param {string} requestType
   * @param {string} url
   */
  submit(requestType, url) {
    this.isReady = false;
    return new Promise((resolve, reject) => {
      this.ctx.$axios[requestType](url, this.data())
        .then(response => {
          this.onSuccess(response.data);
          this.isReady = true;
          resolve(response.data);
        })
        .catch(error => {
          console.log(error);
          this.onFail(error.response.data);
          this.isReady = true;
          reject(error.response.data);
        });
    });
  }


  /**
   * Handle a successful form submission.
   *
   * @param {object} data
   */
  onSuccess(data) {
    let message = this.getResponseMessage(data);

    if (message) {
      this.ctx.$toast.success(message);
    }

    this.reset();
  }


  /**
   * Handle a failed form submission.
   *
   * @param {object} errorData
   */
  onFail(errorData) {
    let errors = [];

    if (errorData.errors) {
      errors = errorData.errors;
    } else if (errorData.response && errorData.response.errors) {
      errors = errorData.response.errors;
    }

    let message = this.getResponseMessage(errorData);

    if (errors) {
      message += this.formatResponseErrors(errors);
      this.ctx.$eventBus.$emit('setFormErrorMsg', message);
    }

    if (message) {
      this.ctx.$toast.error(message);
    }

    this.errors.record(errors);
  }

  formatResponseErrors(errors) {

    let errorsMsg = '';

    for (let err in errors) {
      errorsMsg += `<br>`;
      for (let e of errors[err]) {
        errorsMsg += `${e}`;
      }
    }

    return errorsMsg;
  }


  getResponseMessage(data) {
    let message = '';

    if (data.message) {
      message = data.message;
    } else if (data.response && data.response.message) {
      message = data.response.message;
    }

    return message;
  }


  /**
   *
   * @param data
   */
  replace(data) {
    for (let field in data) {
      this[field] = data[field];
    }
  }
}

export default (ctx, inject) => {
  inject('form', (data, config) => {
    return new Form(ctx, data, config);
  });
}

Spamworldpro Mini