Input.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. export default class Input {
  2. constructor(Uploder) {
  3. this.uploader = Uploder;
  4. this.$selector = Uploder.$selector.find(Uploder.options.inputSelector)
  5. }
  6. // 获取上传的文件名
  7. get() {
  8. let val = this.$selector.val();
  9. return val ? val.split(',') : [];
  10. }
  11. // 增加文件名
  12. add(id) {
  13. let val = this.get();
  14. val.push(id);
  15. this.set(val);
  16. }
  17. // 设置表单值
  18. set(arr) {
  19. arr = arr.filter(function (v, k, self) {
  20. return self.indexOf(v) === k;
  21. }).filter(function (v) {
  22. return v ? true : false;
  23. });
  24. // 手动触发change事件,方便监听文件变化
  25. this.$selector.val(arr.join(',')).trigger('change');
  26. }
  27. // 删除表单值
  28. delete(id) {
  29. let _this = this;
  30. _this.deleteUploadedFile(id);
  31. if (!id) {
  32. return _this.$selector.val('');
  33. }
  34. _this.set(_this.get().filter(function (v) {
  35. return v != id;
  36. }));
  37. }
  38. deleteUploadedFile(fileId) {
  39. let addUploadedFile = this.uploader.addUploadedFile;
  40. addUploadedFile.uploadedFiles = addUploadedFile.uploadedFiles.filter(function (v) {
  41. return v.serverId != fileId;
  42. });
  43. }
  44. // 移除字段验证错误提示信息
  45. removeValidatorErrors() {
  46. this.$selector.parents('.form-group,.form-label-group,.form-field').find('.with-errors').html('')
  47. }
  48. }