|
1 | 1 | var InputView = require('./input-view');
|
2 | 2 | var _ = require('lodash');
|
3 |
| -// var path = require('path'); |
| 3 | +var path = require('path'); |
| 4 | +var remote = window.require('remote'); |
| 5 | +var dialog = remote.require('dialog'); |
| 6 | +var format = require('util').format; |
| 7 | +var bindings = require('ampersand-dom-bindings'); |
4 | 8 | var fileReaderTemplate = require('./filereader-default.jade');
|
5 | 9 |
|
6 | 10 | // var debug = require('debug')('scout:connect:filereader-view');
|
7 | 11 |
|
8 | 12 | module.exports = InputView.extend({
|
9 | 13 | 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 = ''; |
| 14 | + props: { |
| 15 | + inputValue: { |
| 16 | + type: 'array', |
| 17 | + required: true, |
| 18 | + default: function() { |
| 19 | + return []; |
| 20 | + } |
| 21 | + }, |
| 22 | + removed: { |
| 23 | + type: 'boolean', |
| 24 | + required: true, |
| 25 | + default: false |
20 | 26 | }
|
21 |
| - return value; |
22 | 27 | },
|
23 |
| - setValue: function(value, skipValidation) { |
24 |
| - if (!this.input) { |
25 |
| - this.inputValue = value; |
26 |
| - return; |
| 28 | + derived: { |
| 29 | + buttonTitle: { |
| 30 | + deps: ['inputValue'], |
| 31 | + fn: function() { |
| 32 | + if (this.inputValue.length === 0) { |
| 33 | + return 'Choose certificate(s)'; |
| 34 | + } else if (this.inputValue.length === 1) { |
| 35 | + return path.basename(this.inputValue[0]); |
| 36 | + } |
| 37 | + return format('%d files selected', this.inputValue.length); |
| 38 | + } |
| 39 | + }, |
| 40 | + numSelectedFiles: { |
| 41 | + deps: ['inputValue'], |
| 42 | + fn: function() { |
| 43 | + return this.inputValue.length; |
| 44 | + } |
| 45 | + } |
| 46 | + }, |
| 47 | + events: { |
| 48 | + 'click [data-hook=load-file-button]': 'loadFileButtonClicked' |
| 49 | + }, |
| 50 | + bindings: { |
| 51 | + buttonTitle: { |
| 52 | + type: 'text', |
| 53 | + hook: 'button-label' |
| 54 | + }, |
| 55 | + 'label': [ |
| 56 | + { |
| 57 | + hook: 'label' |
| 58 | + }, |
| 59 | + { |
| 60 | + type: 'toggle', |
| 61 | + hook: 'label' |
| 62 | + } |
| 63 | + ], |
| 64 | + 'message': { |
| 65 | + type: 'text', |
| 66 | + hook: 'message-text' |
| 67 | + }, |
| 68 | + 'showMessage': { |
| 69 | + type: 'toggle', |
| 70 | + hook: 'message-container' |
27 | 71 | }
|
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(); |
| 72 | + }, |
| 73 | + /** |
| 74 | + * Set value to empty array in spec, instead of '', set appropriate |
| 75 | + * invalidClass and validityClassSelector and always validate. |
| 76 | + * @param {Object} spec the spec to set up this input view |
| 77 | + */ |
| 78 | + initialize: function(spec) { |
| 79 | + spec = spec || {}; |
| 80 | + _.defaults(spec, {value: []}); |
| 81 | + this.invalidClass = 'has-error'; |
| 82 | + this.validityClassSelector = '.form-item-file'; |
| 83 | + InputView.prototype.initialize.call(this, spec); |
| 84 | + }, |
| 85 | + /** |
| 86 | + * @todo (thomasr) |
| 87 | + * Because ampersand-input-view still uses [email protected] where |
| 88 | + * the render/remove/render cycle doesn't correctly set up the bindings |
| 89 | + * again, we need to re-initialize the bindings manually here. Once they |
| 90 | + * upgrade to [email protected] we can probably remove this entire |
| 91 | + * render method. |
| 92 | + * |
| 93 | + * @return {Object} this |
| 94 | + */ |
| 95 | + render: function() { |
| 96 | + this.renderWithTemplate(this); |
| 97 | + this.input = this.queryByHook('load-file-button'); |
| 98 | + if (this.removed) { |
| 99 | + this._parsedBindings = bindings(this.bindings, this); |
| 100 | + this._initializeBindings(); |
| 101 | + this.removed = false; |
| 102 | + } |
| 103 | + this.setValue(this.inputValue, !this.required); |
| 104 | + return this; |
| 105 | + }, |
| 106 | + /** |
| 107 | + * Turn into no-op, as we don't work on input elements |
| 108 | + * @see ampersand-input-view.js#handleTypeChange |
| 109 | + */ |
| 110 | + handleTypeChange: function() { |
| 111 | + }, |
| 112 | + /** |
| 113 | + * Turn into identity, as we don't need to trim the value |
| 114 | + * @param {Array} val the value to pass through |
| 115 | + * @return {Array} return the unchanged value |
| 116 | + */ |
| 117 | + clean: function(val) { |
| 118 | + return val; |
| 119 | + }, |
| 120 | + /** |
| 121 | + * Turn into no-op, as we don't work on input elements |
| 122 | + * @see ampersand-input-view.js#initInputBindings |
| 123 | + */ |
| 124 | + // initInputBindings: function() { |
| 125 | + // }, |
| 126 | + /** |
| 127 | + * Only call ampersand-view's remove, we don't need to remove event listeners |
| 128 | + * @see ampersand-input-view.js#remove |
| 129 | + */ |
| 130 | + remove: function() { |
| 131 | + this.removed = true; |
| 132 | + InputView.prototype.remove.apply(this, arguments); |
| 133 | + }, |
| 134 | + /** |
| 135 | + * Not setting this.input.value here because our this.input is a div |
| 136 | + * @param {Array} value the value to assign to this.inputValue |
| 137 | + * @param {Boolean} skipValidation whether it should be validated or not |
| 138 | + * @see ampersand-input-view.js#setValue |
| 139 | + */ |
| 140 | + setValue: function(value, skipValidation) { |
| 141 | + this.inputValue = value; |
49 | 142 | if (!skipValidation && !this.getErrorMessage()) {
|
50 | 143 | this.shouldValidate = true;
|
51 | 144 | } else if (skipValidation) {
|
52 | 145 | this.shouldValidate = false;
|
53 | 146 | }
|
54 | 147 | },
|
55 |
| - handleChange: function() { |
56 |
| - if (this.inputValue && this.changed) { |
57 |
| - this.shouldValidate = true; |
| 148 | + /** |
| 149 | + * Need to change the value empty check to empty arrays instead |
| 150 | + * @return {String} error message |
| 151 | + * @see ampersand-input-view.js#getErrorMessage |
| 152 | + */ |
| 153 | + getErrorMessage: function() { |
| 154 | + var message = ''; |
| 155 | + if (this.required && this.value.length === 0) { |
| 156 | + return this.requiredMessage; |
58 | 157 | }
|
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(); |
| 158 | + (this.tests || []).some(function(test) { |
| 159 | + message = test.call(this, this.value) || ''; |
| 160 | + return message; |
| 161 | + }, this); |
| 162 | + return message; |
| 163 | + }, |
| 164 | + /** |
| 165 | + * Don't access this.input.value as we don't work on input elements |
| 166 | + * @see ampersand-input-view.js#beforeSubmit |
| 167 | + */ |
| 168 | + beforeSubmit: function() { |
| 169 | + // at the point where we've tried to submit, we want to validate |
| 170 | + // everything from now on. |
| 171 | + this.shouldValidate = true; |
62 | 172 | this.runTests();
|
| 173 | + }, |
| 174 | + loadFileButtonClicked: function() { |
| 175 | + dialog.showOpenDialog({ |
| 176 | + properties: ['openFile', 'multiSelections'] |
| 177 | + }, function(filenames) { |
| 178 | + this.inputValue = filenames || []; |
| 179 | + this.handleChange(); |
| 180 | + }.bind(this)); |
63 | 181 | }
|
64 | 182 | });
|
0 commit comments