|
| 1 | +var InputView = require('./input-view'); |
| 2 | +var _ = require('lodash'); |
| 3 | +// var path = require('path'); |
| 4 | +var fileReaderTemplate = require('./filereader-default.jade'); |
| 5 | + |
| 6 | +// var debug = require('debug')('scout:connect:filereader-view'); |
| 7 | + |
| 8 | +module.exports = InputView.extend({ |
| 9 | + template: fileReaderTemplate, |
| 10 | + clean: function() { |
| 11 | + var value; |
| 12 | + value = _.chain(this.input.files) |
| 13 | + .map(function(file) { |
| 14 | + return _.get(file, 'path', null); |
| 15 | + }) |
| 16 | + .filter() |
| 17 | + .value(); |
| 18 | + if (value.length === 0) { |
| 19 | + value = ''; |
| 20 | + } |
| 21 | + return value; |
| 22 | + }, |
| 23 | + setValue: function(value, skipValidation) { |
| 24 | + if (!this.input) { |
| 25 | + this.inputValue = value; |
| 26 | + return; |
| 27 | + } |
| 28 | + this.input.value = ''; |
| 29 | + /** |
| 30 | + * Cannot set input value for file types. @see INT-780 |
| 31 | + */ |
| 32 | + // if (value || value === 0) { |
| 33 | + // if (!_.isArray(value)) { |
| 34 | + // value = [value]; |
| 35 | + // } |
| 36 | + // if (value.length <= 1) { |
| 37 | + // this.input.value = path.basename(value); |
| 38 | + // } else { |
| 39 | + // this.input.value = 'multiple files'; |
| 40 | + // } |
| 41 | + // this.input.files = _.map(value, function(f) { |
| 42 | + // return { |
| 43 | + // name: path.basename(f), |
| 44 | + // path: f |
| 45 | + // }; |
| 46 | + // }); |
| 47 | + // } |
| 48 | + this.inputValue = this.clean(); |
| 49 | + if (!skipValidation && !this.getErrorMessage()) { |
| 50 | + this.shouldValidate = true; |
| 51 | + } else if (skipValidation) { |
| 52 | + this.shouldValidate = false; |
| 53 | + } |
| 54 | + }, |
| 55 | + handleChange: function() { |
| 56 | + if (this.inputValue && this.changed) { |
| 57 | + this.shouldValidate = true; |
| 58 | + } |
| 59 | + // for `file` type input fields, this is the only event and we need |
| 60 | + // to set this.inputValue here again. |
| 61 | + this.inputValue = this.clean(); |
| 62 | + this.runTests(); |
| 63 | + } |
| 64 | +}); |
0 commit comments