()
| 25 | } |
| 26 | |
| 27 | create_element(): JQuery { |
| 28 | let spec = deep_copy(this.spec); |
| 29 | const id_name = spec.name + '-' + Math.floor(Math.random() * Math.floor(9999)); |
| 30 | spec['id_name'] = id_name; |
| 31 | |
| 32 | const html = Mustache.render(file_input_tpl, {...spec, browse_file: t('browse_file')}); |
| 33 | this.element = $(html); |
| 34 | let input_elem = this.element.find('input[type="file"]'); |
| 35 | |
| 36 | const ignore_keys = { |
| 37 | 'label': '', |
| 38 | 'invalid_feedback': '', |
| 39 | 'valid_feedback': '', |
| 40 | 'help_text': '', |
| 41 | 'placeholder': '' |
| 42 | }; |
| 43 | for (let key in this.spec) { |
| 44 | if (key in ignore_keys) continue; |
| 45 | input_elem.attr(key, this.spec[key]); |
| 46 | } |
| 47 | |
| 48 | // 文件选中后先不通知后端 |
| 49 | let that = this; |
| 50 | input_elem.on('change', function () { |
| 51 | that.files = []; |
| 52 | let total_size = 0; |
| 53 | that.valid = true; |
| 54 | let file = (input_elem[0] as HTMLInputElement).files; |
| 55 | for (let f of file) { |
| 56 | total_size += f.size; |
| 57 | |
| 58 | if (that.spec.max_size && f.size > that.spec.max_size) { |
| 59 | that.valid = false; |
| 60 | that.update_input_helper(-1, { |
| 61 | 'valid_status': false, |
| 62 | 'invalid_feedback': t("file_size_exceed", f.name, that._formate_size(that.spec.max_size)), |
| 63 | }); |
| 64 | } else if (that.spec.max_total_size && total_size > that.spec.max_total_size) { |
| 65 | that.valid = false; |
| 66 | that.update_input_helper(-1, { |
| 67 | 'valid_status': false, |
| 68 | 'invalid_feedback': t("file_total_size_exceed", that._formate_size(that.spec.max_total_size)) |
| 69 | }); |
| 70 | return; |
| 71 | } |
| 72 | if (!that.valid) return; |
| 73 | that.update_input_helper(-1, {'valid_status': 0}); |
| 74 | |
| 75 | that.files.push(f); |
| 76 | } |
| 77 | |
| 78 | if (spec.onchange) { |
| 79 | that.on_input_event("change", that); |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | return this.element; |
| 84 | } |
nothing calls this directly
no test coverage detected