Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(914)

Unified Diff: ui/file_manager/gallery/js/image_editor/image_resize.js

Issue 2299493002: Add an ability for resize in gallery. (Closed)
Patch Set: Add an ability for resize in gallery. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/file_manager/gallery/js/image_editor/image_resize.js
diff --git a/ui/file_manager/gallery/js/image_editor/image_resize.js b/ui/file_manager/gallery/js/image_editor/image_resize.js
new file mode 100644
index 0000000000000000000000000000000000000000..6679e4b0a864092e11841b66c12ac9242a4b6038
--- /dev/null
+++ b/ui/file_manager/gallery/js/image_editor/image_resize.js
@@ -0,0 +1,250 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * Resize Mode
+ *
+ * @extends {ImageEditor.Mode}
+ * @constructor
+ * @struct
+ */
+ImageEditor.Mode.Resize = function() {
+ ImageEditor.Mode.call(this, 'resize', 'GALLERY_RESIZE');
+
+ /** @private {number} */
+ this.imageWidth_ = 0;
+
+ /** @private {number} */
+ this.imageHeight_ = 0;
+
+ /** @private {number} */
+ this.maxValidWidth_ = 0;
+
+ /** @private {number} */
+ this.maxValidHeight_ = 0;
+
+ /** @private {number} */
+ this.widthInputValue_ = 0;
+
+ /** @private {number} */
+ this.heightInputValue_ = 0;
+
+ /** @private {HTMLElement} */
+ this.widthInputElement_ = null;
+
+ /** @private {HTMLElement} */
+ this.heightInputElement_ = null;
+
+ /** @private {number} */
+ this.ratio_ = 0;
+
+ /** @private {boolean} */
+ this.fixedRatio_ = true;
+
+ /** @private {boolean} */
+ this.showingAlertDialog_ = false;
+};
+
+/** @const {number} */
+ImageEditor.Mode.Resize.MINIMUM_VALID_VALUE = 1;
+
+/** @const {number} */
+ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE = 10000;
+
+ImageEditor.Mode.Resize.prototype = {
+ __proto__ : ImageEditor.Mode.prototype
+};
+
+/** @override */
+ImageEditor.Mode.Resize.prototype.setUp = function() {
+ ImageEditor.Mode.prototype.setUp.apply(this, arguments);
+
+ this.setDefault_();
+};
+
+/** @override */
+ImageEditor.Mode.Resize.prototype.createTools = function(toolbar) {
+ this.widthInputElement_ = toolbar.addInput('width', 'GALLERY_WIDTH',
+ this.onInputChanged_.bind(this, 'width'),
fukino 2016/09/06 10:50:35 indent should be 4.
harukam 2016/09/09 10:37:42 Thanks, done.
+ this.onInputBlurred_.bind(this, 'width'),
+ this.widthInputValue_, 'px');
+ this.heightInputElement_ = toolbar.addInput('height', 'GALLERY_HEIGHT',
+ this.onInputChanged_.bind(this, 'height'),
fukino 2016/09/06 10:50:35 ditto
harukam 2016/09/09 10:37:42 Done.
+ this.onInputBlurred_.bind(this, 'height'),
+ this.heightInputValue_, 'px');
+ toolbar.addCheckbox('ratio-fixed', 'GALLERY_FIXRATIO',
+ this.onCheckBoxChanged_.bind(this), this.fixedRatio_);
fukino 2016/09/06 10:50:35 ditto
harukam 2016/09/09 10:37:43 Done.
+};
+
+/**
+ * Handlers change events of width/height input element.
+ * @param {string} name Name
+ * @param {Event} event An event.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.onInputChanged_ = function(
+ name, event) {
+
+ if(name !== 'height' && name !== 'width')
+ return;
+
+ this.updateInputValues_();
+
+ function adjustToRatio() {
+ switch (name) {
+ case 'width':
+ var newHeight = this.widthInputValue_ / this.ratio_;
+ this.heightInputValue_ = Math.floor(newHeight);
+ this.setHeightInputValue_();
+ break;
+ case 'height':
+ var newWidth = this.heightInputValue_ * this.ratio_;
+ this.widthInputValue_ = Math.floor(newWidth);
+ this.setWidthInputValue_();
+ break;
+ }
+ }
+
+ if(this.fixedRatio_ && this.isInputValid())
+ adjustToRatio.call(this);
+};
+
+/**
+ * Handlers blur events of width/height input element.
+ * @param {string} name Name
+ * @param {Event} event An event.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.onInputBlurred_ = function(
+ name, event) {
+
+ if(name !== 'height' && name !== 'width')
+ return;
+
+ if(!this.isInputValid()) {
+ this.showAlertDialog();
+ }
+}
+
+/**
+ * Handlers change events of fixed-ratio checkbox.
+ * @param {Event} event An event.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.onCheckBoxChanged_ = function(event) {
+ var checked = event.target.checked;
+
+ if(!this.fixedRatio_ && checked)
+ this.initializeInputValues_();
+
+ this.fixedRatio_ = checked;
+};
+
+/**
+ * Set default values.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.setDefault_ = function() {
+ var viewport = this.getViewport();
+ assert(viewport);
+
+ var rect = viewport.getImageBounds();
+ this.imageWidth_ = rect.width;
+ this.imageHeight_ = rect.height;
+
+ this.initializeInputValues_();
+
+ this.ratio_ = this.imageWidth_ / this.imageHeight_;
+
+ this.maxValidWidth_ = Math.max(
+ this.imageWidth_, ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE);
+ this.maxValidHeight_ = Math.max(
+ this.imageHeight_, ImageEditor.Mode.Resize.DEFAULT_MAX_VALID_VALUE);
+};
+
+/**
+ * Initialize width/height input values.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.initializeInputValues_ = function() {
+ this.widthInputValue_ = this.imageWidth_;
+ this.setWidthInputValue_();
+
+ this.heightInputValue_ = this.imageHeight_;
+ this.setHeightInputValue_();
+};
+
+/**
+ * Update input values to local variales.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.updateInputValues_ = function() {
+ if(this.widthInputElement_)
+ this.widthInputValue_ = parseInt(this.widthInputElement_.getValue(), 10);
+ if(this.heightInputElement_)
+ this.heightInputValue_ = parseInt(this.heightInputElement_.getValue(), 10);
+};
+
+/**
+ * Apply local variables' change to width input element.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.setWidthInputValue_ = function() {
+ if(this.widthInputElement_)
+ this.widthInputElement_.setValue(this.widthInputValue_);
+};
+
+/**
+ * Apply local variables' change to height input element.
+ * @private
+ */
+ImageEditor.Mode.Resize.prototype.setHeightInputValue_ = function() {
+ if(this.heightInputElement_)
+ this.heightInputElement_.setValue(this.heightInputValue_);
+};
+
+/**
+ * Check if width/height input values are valid.
+ * @return {boolean} true if both values are valid.
+ */
+ImageEditor.Mode.Resize.prototype.isInputValid = function() {
+ var limitCheck = function(value, limit) {
+ return ImageEditor.Mode.Resize.MINIMUM_VALID_VALUE <= value &&
+ value <= limit;
+ }.bind(this);
+ return limitCheck(this.widthInputValue_, this.maxValidWidth_) &&
+ limitCheck(this.heightInputValue_, this.maxValidHeight_);
+};
+
+/**
+ * Show alert dialog only if input value is invalid.
+ */
+ImageEditor.Mode.Resize.prototype.showAlertDialog = function() {
+ if(this.isInputValid() || this.showingAlertDialog_)
+ return;
+
+ var container = this.getImageView().container_;
+ var alertDialog = new FilesAlertDialog(container);
+
+ // The Editor shouldn't handle key events in order to focus an alertDialog.
+ this.editor_.setKeyEventHandled(false);
+ this.showingAlertDialog_ = true;
+ alertDialog.showWithTitle('', strf('GALLERY_INVALIDVALUE'),
+ function() {
+ this.initializeInputValues_();
+ this.editor_.setKeyEventHandled(true);
+ this.showingAlertDialog_ = false;
+ }.bind(this), null, null);
+};
+
+/** @override */
+ImageEditor.Mode.Resize.prototype.reset = function() {
+ ImageEditor.Mode.prototype.reset.call(this);
+ this.setDefault_();
+};
+
+/** @override */
+ImageEditor.Mode.Resize.prototype.getCommand = function() {
+ return new Command.Resize(this.widthInputValue_, this.heightInputValue_);
+};

Powered by Google App Engine
This is Rietveld 408576698