|
| 1 | +var InputView = require('./input-view'); |
| 2 | +var _ = require('lodash'); |
| 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'); |
| 8 | +var fileReaderTemplate = require('./filereader-default.jade'); |
| 9 | + |
| 10 | +// var debug = require('debug')('scout:connect:filereader-view'); |
| 11 | + |
| 12 | +module.exports = InputView.extend({ |
| 13 | + template: fileReaderTemplate, |
| 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 |
| 26 | + } |
| 27 | + }, |
| 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' |
| 71 | + } |
| 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; |
| 142 | + if (!skipValidation && !this.getErrorMessage()) { |
| 143 | + this.shouldValidate = true; |
| 144 | + } else if (skipValidation) { |
| 145 | + this.shouldValidate = false; |
| 146 | + } |
| 147 | + }, |
| 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; |
| 157 | + } |
| 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; |
| 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)); |
| 181 | + } |
| 182 | +}); |
0 commit comments