Chromium Code Reviews| 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.maxValidValue_ = 0; | |
| 23 | |
| 24 /** @private {number} */ | |
| 25 this.widthInputValue_ = 0; | |
| 26 | |
| 27 /** @private {number} */ | |
| 28 this.heightInputValue_ = 0; | |
| 29 | |
| 30 /** @private {HTMLElement} */ | |
| 31 this.widthInputElement_ = null; | |
| 32 | |
| 33 /** @private {HTMLElement} */ | |
| 34 this.heightInputElement_ = null; | |
| 35 | |
| 36 /** @private {number} */ | |
| 37 this.ratio_ = 0; | |
| 38 | |
| 39 /** @private {boolean} */ | |
| 40 this.fixedRatio_ = true; | |
| 41 }; | |
| 42 | |
| 43 /** @const {number} */ | |
| 44 ImageEditor.Mode.Resize.MINIMUM_VALID_VALUE = 1; | |
| 45 | |
| 46 /** @const {number} */ | |
| 47 ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE = 10000; | |
| 48 | |
| 49 ImageEditor.Mode.Resize.prototype = { | |
| 50 __proto__ : ImageEditor.Mode.prototype | |
| 51 }; | |
| 52 | |
| 53 /** @override */ | |
| 54 ImageEditor.Mode.Resize.prototype.setUp = function() { | |
| 55 ImageEditor.Mode.prototype.setUp.apply(this, arguments); | |
| 56 | |
| 57 this.setDefault_(); | |
| 58 }; | |
| 59 | |
| 60 /** @override */ | |
| 61 ImageEditor.Mode.Resize.prototype.createTools = function(toolbar) { | |
| 62 this.widthInputElement_ = toolbar.addInput('width', 'GALLERY_WIDTH', | |
| 63 this.onInputChanged_.bind(this, 'width'), this.widthInputValue_, 'px'); | |
| 64 this.heightInputElement_ = toolbar.addInput('height', 'GALLERY_HEIGHT', | |
| 65 this.onInputChanged_.bind(this, 'height'), this.heightInputValue_, 'px'); | |
| 66 toolbar.addCheckbox('ratio-fixed', 'GALLERY_FIXRATIO', | |
| 67 this.onCheckBoxChanged_.bind(this), this.fixedRatio_); | |
| 68 }; | |
| 69 | |
| 70 /** | |
| 71 * Handlers change events of width/height input element. | |
| 72 * @param {string} name Name | |
| 73 * @param {Event} event An event. | |
| 74 * @private | |
| 75 */ | |
| 76 ImageEditor.Mode.Resize.prototype.onInputChanged_ = function( | |
| 77 name, event) { | |
| 78 | |
| 79 if(name !== 'height' && name !== 'width') | |
| 80 return; | |
| 81 | |
| 82 this.updateInputValues_(); | |
| 83 | |
| 84 function adjustToRatio() { | |
| 85 switch (name) { | |
| 86 case 'width': | |
| 87 var newHeight = this.widthInputValue_ / this.ratio_; | |
| 88 this.heightInputValue_ = Math.floor(newHeight); | |
| 89 this.setHeightInputValue_(); | |
| 90 break; | |
| 91 case 'height': | |
| 92 var newWidth = this.heightInputValue_ * this.ratio_; | |
| 93 this.widthInputValue_ = Math.floor(newWidth); | |
| 94 this.setWidthInputValue_(); | |
| 95 break; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 if(this.fixedRatio_ && this.isInputValid()) | |
| 100 adjustToRatio.call(this); | |
| 101 }; | |
| 102 | |
| 103 /** | |
| 104 * Handlers change events of fixed-ratio checkbox. | |
| 105 * @param {Event} event An event. | |
| 106 * @private | |
| 107 */ | |
| 108 ImageEditor.Mode.Resize.prototype.onCheckBoxChanged_ = function(event) { | |
| 109 var checked = event.target.checked; | |
| 110 | |
| 111 if(!this.fixedRatio_ && checked) | |
| 112 this.initializeInputValues_(); | |
| 113 | |
| 114 this.fixedRatio_ = checked; | |
| 115 }; | |
| 116 | |
| 117 /** | |
| 118 * Set default values. | |
| 119 * @private | |
| 120 */ | |
| 121 ImageEditor.Mode.Resize.prototype.setDefault_ = function() { | |
| 122 var viewport = this.getViewport(); | |
| 123 assert(viewport); | |
| 124 | |
| 125 var rect = viewport.getImageBounds(); | |
| 126 this.imageWidth_ = rect.width; | |
| 127 this.imageHeight_ = rect.height; | |
| 128 | |
| 129 this.initializeInputValues_(); | |
| 130 | |
| 131 this.ratio_ = this.imageWidth_ / this.imageHeight_; | |
| 132 | |
| 133 this.maxValidValue_ = Math.max( | |
|
fukino
2016/09/05 13:44:35
Can we have max value for width and height separat
harukam
2016/09/06 08:10:29
Acknowledged.
| |
| 134 this.imageWidth_, this.imageHeight_, | |
| 135 ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE); | |
| 136 }; | |
| 137 | |
| 138 /** | |
| 139 * Initialize width/height input values. | |
| 140 * @private | |
| 141 */ | |
| 142 ImageEditor.Mode.Resize.prototype.initializeInputValues_ = function() { | |
| 143 this.widthInputValue_ = this.imageWidth_; | |
| 144 this.setWidthInputValue_(); | |
| 145 | |
| 146 this.heightInputValue_ = this.imageHeight_; | |
| 147 this.setHeightInputValue_(); | |
| 148 }; | |
| 149 | |
| 150 /** | |
| 151 * Update input values to local variales. | |
| 152 * @private | |
| 153 */ | |
| 154 ImageEditor.Mode.Resize.prototype.updateInputValues_ = function() { | |
| 155 if(this.widthInputElement_) | |
| 156 this.widthInputValue_ = parseInt(this.widthInputElement_.getValue(), 10); | |
| 157 if(this.heightInputElement_) | |
| 158 this.heightInputValue_ = parseInt(this.heightInputElement_.getValue(), 10); | |
| 159 }; | |
| 160 | |
| 161 /** | |
| 162 * Apply local variables' change to width input element. | |
| 163 * @private | |
| 164 */ | |
| 165 ImageEditor.Mode.Resize.prototype.setWidthInputValue_ = function() { | |
| 166 if(this.widthInputElement_) | |
| 167 this.widthInputElement_.setValue(this.widthInputValue_); | |
| 168 }; | |
| 169 | |
| 170 /** | |
| 171 * Apply local variables' change to height input element. | |
| 172 * @private | |
| 173 */ | |
| 174 ImageEditor.Mode.Resize.prototype.setHeightInputValue_ = function() { | |
| 175 if(this.heightInputElement_) | |
| 176 this.heightInputElement_.setValue(this.heightInputValue_); | |
| 177 }; | |
| 178 | |
| 179 /** | |
| 180 * Check if width/height input values are valid. | |
| 181 * @return {boolean} true if both values are valid. | |
| 182 */ | |
| 183 ImageEditor.Mode.Resize.prototype.isInputValid = function() { | |
| 184 var limitCheck = function(value) { | |
| 185 return ImageEditor.Mode.Resize.MINIMUM_VALID_VALUE <= value && | |
| 186 value <= this.maxValidValue_; | |
| 187 }.bind(this); | |
| 188 return limitCheck(this.widthInputValue_) && | |
| 189 limitCheck(this.heightInputValue_); | |
| 190 }; | |
| 191 | |
| 192 /** | |
| 193 * show alert dialog only if input value is invalid. | |
| 194 * @param {function()} callback Callback | |
| 195 */ | |
| 196 ImageEditor.Mode.Resize.prototype.showAlertDialog = function(callback) { | |
| 197 if(this.isInputValid()) | |
| 198 return; | |
| 199 | |
| 200 var container = this.getImageView().container_; | |
| 201 var alertDialog = new FilesAlertDialog(container); | |
| 202 alertDialog.showWithTitle('', strf('GALLERY_INVALIDVALUE'), | |
| 203 function() { | |
| 204 this.initializeInputValues_(); | |
| 205 callback(); | |
| 206 }.bind(this), null, null); | |
| 207 }; | |
| 208 | |
| 209 /** @override */ | |
| 210 ImageEditor.Mode.Resize.prototype.reset = function() { | |
| 211 ImageEditor.Mode.prototype.reset.call(this); | |
| 212 this.setDefault_(); | |
| 213 }; | |
| 214 | |
| 215 /** @override */ | |
| 216 ImageEditor.Mode.Resize.prototype.getCommand = function() { | |
| 217 return new Command.Resize(this.widthInputValue_, this.heightInputValue_); | |
| 218 }; | |
| OLD | NEW |