OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Resize Mode |
| 7 * |
| 8 * @extends {ImageEditor.Mode} |
| 9 * @constructor |
| 10 * @struct |
| 11 */ |
| 12 ImageEditor.Mode.Resize = function() { |
| 13 ImageEditor.Mode.call(this, 'resize', 'GALLERY_RESIZE'); |
| 14 |
| 15 /** @private {number} */ |
| 16 this.imageWidth_ = 0; |
| 17 |
| 18 /** @private {number} */ |
| 19 this.imageHeight_ = 0; |
| 20 |
| 21 /** @private {number} */ |
| 22 this.maxValidWidth_ = 0; |
| 23 |
| 24 /** @private {number} */ |
| 25 this.maxValidHeight_ = 0; |
| 26 |
| 27 /** @private {number} */ |
| 28 this.widthInputValue_ = 0; |
| 29 |
| 30 /** @private {number} */ |
| 31 this.heightInputValue_ = 0; |
| 32 |
| 33 /** @private {HTMLElement} */ |
| 34 this.widthInputElement_ = null; |
| 35 |
| 36 /** @private {HTMLElement} */ |
| 37 this.heightInputElement_ = null; |
| 38 |
| 39 /** @private {number} */ |
| 40 this.ratio_ = 0; |
| 41 |
| 42 /** @private {boolean} */ |
| 43 this.fixedRatio_ = true; |
| 44 |
| 45 /** @private {boolean} */ |
| 46 this.showingAlertDialog_ = false; |
| 47 |
| 48 /** @private {FilesAlertDialog} */ |
| 49 this.alertDialog_ = null; |
| 50 }; |
| 51 |
| 52 /** @const {number} */ |
| 53 ImageEditor.Mode.Resize.MINIMUM_VALID_VALUE = 1; |
| 54 |
| 55 /** @const {number} */ |
| 56 ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE = 10000; |
| 57 |
| 58 ImageEditor.Mode.Resize.prototype = { |
| 59 __proto__ : ImageEditor.Mode.prototype |
| 60 }; |
| 61 |
| 62 /** @override */ |
| 63 ImageEditor.Mode.Resize.prototype.setUp = function() { |
| 64 ImageEditor.Mode.prototype.setUp.apply(this, arguments); |
| 65 |
| 66 this.setDefault_(); |
| 67 }; |
| 68 |
| 69 /** @override */ |
| 70 ImageEditor.Mode.Resize.prototype.createTools = function(toolbar) { |
| 71 this.widthInputElement_ = toolbar.addInput('width', 'GALLERY_WIDTH', |
| 72 this.onInputChanged_.bind(this, 'width'), |
| 73 this.widthInputValue_, 'px'); |
| 74 |
| 75 this.lockIcon_ = toolbar.addButton('GALLERY_FIXRATIO', |
| 76 ImageEditor.Toolbar.ButtonType.ICON_TOGGLEABLE, |
| 77 this.onLockIconClicked_.bind(this), 'lockicon'); |
| 78 if(this.fixedRatio_) |
| 79 this.lockIcon_.setAttribute('locked', ''); |
| 80 |
| 81 this.heightInputElement_ = toolbar.addInput('height', 'GALLERY_HEIGHT', |
| 82 this.onInputChanged_.bind(this, 'height'), |
| 83 this.heightInputValue_, 'px'); |
| 84 }; |
| 85 |
| 86 /** |
| 87 * Handlers change events of width/height input element. |
| 88 * @param {string} name |
| 89 * @param {Event} event |
| 90 * @private |
| 91 */ |
| 92 ImageEditor.Mode.Resize.prototype.onInputChanged_ = function( |
| 93 name, event) { |
| 94 |
| 95 if(name !== 'height' && name !== 'width') |
| 96 return; |
| 97 |
| 98 this.updateInputValues_(); |
| 99 |
| 100 function adjustToRatio() { |
| 101 switch (name) { |
| 102 case 'width': |
| 103 var newHeight = Math.ceil(this.widthInputValue_ / this.ratio_); |
| 104 if(this.isInputValidByName_('heigth', newHeight)) { |
| 105 this.heightInputValue_ = newHeight; |
| 106 this.setHeightInputValue_(); |
| 107 } |
| 108 break; |
| 109 case 'height': |
| 110 var newWidth = Math.ceil(this.heightInputValue_ * this.ratio_); |
| 111 if(this.isInputValidByName_('width', newWidth)) { |
| 112 this.widthInputValue_ = newWidth; |
| 113 this.setWidthInputValue_(); |
| 114 } |
| 115 break; |
| 116 } |
| 117 } |
| 118 |
| 119 if(this.fixedRatio_ && this.isInputValidByName_(name)) |
| 120 adjustToRatio.call(this); |
| 121 }; |
| 122 |
| 123 /** |
| 124 * Handlers change events of lock icon. |
| 125 * @param {Event} event An event. |
| 126 * @private |
| 127 */ |
| 128 ImageEditor.Mode.Resize.prototype.onLockIconClicked_ = function(event) { |
| 129 var toggled = !this.fixedRatio_; |
| 130 if(toggled) { |
| 131 this.initializeInputValues_(); |
| 132 this.lockIcon_.setAttribute('locked', ''); |
| 133 } else { |
| 134 this.lockIcon_.removeAttribute('locked'); |
| 135 } |
| 136 |
| 137 this.fixedRatio_ = toggled; |
| 138 }; |
| 139 |
| 140 /** |
| 141 * Set default values. |
| 142 * @private |
| 143 */ |
| 144 ImageEditor.Mode.Resize.prototype.setDefault_ = function() { |
| 145 var viewport = this.getViewport(); |
| 146 assert(viewport); |
| 147 |
| 148 var rect = viewport.getImageBounds(); |
| 149 this.imageWidth_ = rect.width; |
| 150 this.imageHeight_ = rect.height; |
| 151 |
| 152 this.initializeInputValues_(); |
| 153 |
| 154 this.ratio_ = this.imageWidth_ / this.imageHeight_; |
| 155 |
| 156 this.maxValidWidth_ = Math.max( |
| 157 this.imageWidth_, ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE); |
| 158 this.maxValidHeight_ = Math.max( |
| 159 this.imageHeight_, ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE); |
| 160 }; |
| 161 |
| 162 /** |
| 163 * Initialize width/height input values. |
| 164 * @private |
| 165 */ |
| 166 ImageEditor.Mode.Resize.prototype.initializeInputValues_ = function() { |
| 167 this.widthInputValue_ = this.imageWidth_; |
| 168 this.setWidthInputValue_(); |
| 169 |
| 170 this.heightInputValue_ = this.imageHeight_; |
| 171 this.setHeightInputValue_(); |
| 172 }; |
| 173 |
| 174 /** |
| 175 * Update input values to local variales. |
| 176 * @private |
| 177 */ |
| 178 ImageEditor.Mode.Resize.prototype.updateInputValues_ = function() { |
| 179 if(this.widthInputElement_) |
| 180 this.widthInputValue_ = parseInt(this.widthInputElement_.getValue(), 10); |
| 181 if(this.heightInputElement_) |
| 182 this.heightInputValue_ = parseInt(this.heightInputElement_.getValue(), 10); |
| 183 }; |
| 184 |
| 185 /** |
| 186 * Apply local variables' change to width input element. |
| 187 * @private |
| 188 */ |
| 189 ImageEditor.Mode.Resize.prototype.setWidthInputValue_ = function() { |
| 190 if(this.widthInputElement_) |
| 191 this.widthInputElement_.setValue(this.widthInputValue_); |
| 192 }; |
| 193 |
| 194 /** |
| 195 * Apply local variables' change to height input element. |
| 196 * @private |
| 197 */ |
| 198 ImageEditor.Mode.Resize.prototype.setHeightInputValue_ = function() { |
| 199 if(this.heightInputElement_) |
| 200 this.heightInputElement_.setValue(this.heightInputValue_); |
| 201 }; |
| 202 |
| 203 /** |
| 204 * Check if the given name of input has a valid value. |
| 205 * |
| 206 * @param {string} name Name of input to check. |
| 207 * @param {number=} opt_value Value to be checked. Local input's variable will |
| 208 be checked if undefined. |
| 209 * @return {boolean} True if the input |
| 210 * @private |
| 211 */ |
| 212 ImageEditor.Mode.Resize.prototype.isInputValidByName_ = function( |
| 213 name, opt_value) { |
| 214 console.assert(name === 'height' || name === 'width'); |
| 215 |
| 216 var limit = name === 'width' ? this.maxValidWidth_ : this.maxValidHeight_; |
| 217 var value = opt_value || |
| 218 (name === 'width' ? this.widthInputValue_ : this.heightInputValue_); |
| 219 |
| 220 return ImageEditor.Mode.Resize.MINIMUM_VALID_VALUE <= value && value <= limit; |
| 221 }; |
| 222 |
| 223 /** |
| 224 * Check if width/height input values are valid. |
| 225 * @return {boolean} true if both values are valid. |
| 226 */ |
| 227 ImageEditor.Mode.Resize.prototype.isInputValid = function() { |
| 228 return this.isInputValidByName_('width') && |
| 229 this.isInputValidByName_('height'); |
| 230 }; |
| 231 |
| 232 /** |
| 233 * Show alert dialog only if input value is invalid. |
| 234 */ |
| 235 ImageEditor.Mode.Resize.prototype.showAlertDialog = function() { |
| 236 if(this.isInputValid() || this.showingAlertDialog_) |
| 237 return; |
| 238 |
| 239 this.alertDialog_ = this.alertDialog_ || |
| 240 new FilesAlertDialog(/** @type {!HTMLElement} */ (document.body)); |
| 241 this.showingAlertDialog_ = true; |
| 242 this.alertDialog_.showWithTitle('', strf('GALLERY_INVALIDVALUE'), |
| 243 function() { |
| 244 this.initializeInputValues_(); |
| 245 this.showingAlertDialog_ = false; |
| 246 }.bind(this), null, null); |
| 247 }; |
| 248 |
| 249 /** |
| 250 * @return {boolean} True when showing an alert dialog. |
| 251 * @override |
| 252 */ |
| 253 ImageEditor.Mode.Resize.prototype.isConsumingKeyEvents = function() { |
| 254 return this.showingAlertDialog_; |
| 255 }; |
| 256 |
| 257 /** @override */ |
| 258 ImageEditor.Mode.Resize.prototype.cleanUpUI = function() { |
| 259 ImageEditor.Mode.prototype.cleanUpUI.apply(this, arguments); |
| 260 |
| 261 if(this.showingAlertDialog_) { |
| 262 this.alertDialog_.hide(); |
| 263 this.showingAlertDialog_ = false; |
| 264 } |
| 265 }; |
| 266 |
| 267 /** @override */ |
| 268 ImageEditor.Mode.Resize.prototype.reset = function() { |
| 269 ImageEditor.Mode.prototype.reset.call(this); |
| 270 |
| 271 this.setDefault_(); |
| 272 }; |
| 273 |
| 274 /** @override */ |
| 275 ImageEditor.Mode.Resize.prototype.getCommand = function() { |
| 276 return new Command.Resize(this.widthInputValue_, this.heightInputValue_); |
| 277 }; |
OLD | NEW |