* Updates view on model change. * @return {FieldView} Fluent interface
()
| 143 | * @return {FieldView} Fluent interface |
| 144 | */ |
| 145 | update () { |
| 146 | const $field = this.getElement() |
| 147 | |
| 148 | // Set width attr |
| 149 | $field.dataset.width = this.getModel().getWidth() |
| 150 | |
| 151 | // Set focus modifier |
| 152 | if (this.hasFocus()) { |
| 153 | $field.classList.add('field--focus') |
| 154 | } else { |
| 155 | $field.classList.remove('field--focus') |
| 156 | } |
| 157 | |
| 158 | // Add invalid modifier |
| 159 | if (!this.getModel().isValid() || this.getMessage() !== null) { |
| 160 | $field.classList.add('field--invalid') |
| 161 | } else { |
| 162 | $field.classList.remove('field--invalid') |
| 163 | } |
| 164 | |
| 165 | // Remove old message, if any |
| 166 | if (this._$message !== null && this._$message.parentNode !== null) { |
| 167 | this._$message.parentNode.removeChild(this._$message) |
| 168 | } |
| 169 | this._$message = null |
| 170 | |
| 171 | // Create new message, if any |
| 172 | this._$message = this.renderMessage() |
| 173 | if (this._$message !== null) { |
| 174 | this.getElement().appendChild(this._$message) |
| 175 | } |
| 176 | |
| 177 | return this |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Triggered when view receives focus. |
no test coverage detected