| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * ImageEditor is the top level object that holds together and connects | 6 * ImageEditor is the top level object that holds together and connects |
| 7 * everything needed for image editing. | 7 * everything needed for image editing. |
| 8 * @param {HTMLElement} container | 8 * @param {HTMLElement} container |
| 9 * @param {function(Blob)} saveCallback | 9 * @param {function(Blob)} saveCallback |
| 10 * @param {function()} closeCallback | 10 * @param {function()} closeCallback |
| 11 * @param {HTMLElement} toolbarContainer |
| 11 */ | 12 */ |
| 12 function ImageEditor(container, saveCallback, closeCallback) { | 13 function ImageEditor(container, toolbarContainer, saveCallback, closeCallback) { |
| 13 this.container_ = container; | 14 this.container_ = container; |
| 14 this.saveCallback_ = saveCallback; | 15 this.saveCallback_ = saveCallback; |
| 15 this.closeCallback_ = closeCallback; | 16 this.closeCallback_ = closeCallback; |
| 16 | 17 |
| 17 this.container_.innerHTML = ''; | 18 this.container_.innerHTML = ''; |
| 18 | 19 |
| 19 var document = this.container_.ownerDocument; | 20 var document = this.container_.ownerDocument; |
| 20 | 21 |
| 21 this.canvasWrapper_ = document.createElement('div'); | 22 this.canvasWrapper_ = document.createElement('div'); |
| 22 this.canvasWrapper_.className = 'canvas-wrapper'; | 23 this.canvasWrapper_.className = 'canvas-wrapper'; |
| 23 container.appendChild(this.canvasWrapper_); | 24 container.appendChild(this.canvasWrapper_); |
| 24 | 25 |
| 25 var canvas = document.createElement('canvas'); | 26 var canvas = document.createElement('canvas'); |
| 26 this.canvasWrapper_.appendChild(canvas); | 27 this.canvasWrapper_.appendChild(canvas); |
| 27 canvas.width = this.canvasWrapper_.clientWidth; | 28 canvas.width = this.canvasWrapper_.clientWidth; |
| 28 canvas.height = this.canvasWrapper_.clientHeight; | 29 canvas.height = this.canvasWrapper_.clientHeight; |
| 29 | 30 |
| 30 this.buffer_ = new ImageBuffer(canvas); | 31 this.buffer_ = new ImageBuffer(canvas); |
| 31 | 32 |
| 32 this.scaleControl_ = new ImageEditor.ScaleControl( | 33 // TODO(dgozman): consider adding a ScaleControl in v2. |
| 33 this.canvasWrapper_, this.getBuffer().getViewport()); | |
| 34 | 34 |
| 35 this.panControl_ = new ImageEditor.MouseControl(canvas, this.getBuffer()); | 35 this.panControl_ = new ImageEditor.MouseControl(canvas, this.getBuffer()); |
| 36 | 36 |
| 37 this.toolbar_ = | 37 this.toolbar_ = new ImageEditor.Toolbar(toolbarContainer, |
| 38 new ImageEditor.Toolbar(container, this.onOptionsChange.bind(this)); | 38 this.onOptionsChange.bind(this)); |
| 39 this.initToolbar(); | 39 this.initToolbar(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Create an ImageEditor instance bound to a current web page, load the content. | 43 * Create an ImageEditor instance bound to a current web page, load the content. |
| 44 * | 44 * |
| 45 * Use this method when image_editor.html is loaded into an iframe. | 45 * Use this method when image_editor.html is loaded into an iframe. |
| 46 * | 46 * |
| 47 * @param {function(Blob)} saveCallback | 47 * @param {function(Blob)} saveCallback |
| 48 * @param {function()} closeCallback | 48 * @param {function()} closeCallback |
| 49 * @param {HTMLCanvasElement|HTMLImageElement|String} source | 49 * @param {HTMLCanvasElement|HTMLImageElement|String} source |
| 50 * @param {Object} opt_metadata | 50 * @param {Object} opt_metadata |
| 51 * @return {ImageEditor} | 51 * @return {ImageEditor} |
| 52 */ | 52 */ |
| 53 ImageEditor.open = function(saveCallback, closeCallback, source, opt_metadata) { | 53 ImageEditor.open = function(saveCallback, closeCallback, source, opt_metadata) { |
| 54 var container = document.getElementsByClassName('image-editor')[0]; | 54 var container = document.getElementsByClassName('image-editor')[0]; |
| 55 var editor = new ImageEditor(container, saveCallback, closeCallback); | 55 var toolbar = document.getElementsByClassName('toolbar-container')[0]; |
| 56 var editor = new ImageEditor(container, toolbar, saveCallback, closeCallback); |
| 56 if (ImageEditor.resizeListener) { | 57 if (ImageEditor.resizeListener) { |
| 57 // Make sure we do not leak the previous instance. | 58 // Make sure we do not leak the previous instance. |
| 58 window.removeEventListener('resize', ImageEditor.resizeListener, false); | 59 window.removeEventListener('resize', ImageEditor.resizeListener, false); |
| 59 } | 60 } |
| 60 ImageEditor.resizeListener = editor.resizeFrame.bind(editor); | 61 ImageEditor.resizeListener = editor.resizeFrame.bind(editor); |
| 61 window.addEventListener('resize', ImageEditor.resizeListener, false); | 62 window.addEventListener('resize', ImageEditor.resizeListener, false); |
| 62 editor.load(source, opt_metadata); | 63 editor.load(source, opt_metadata); |
| 63 return editor; | 64 return editor; |
| 64 }; | 65 }; |
| 65 | 66 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 if (this.currentMode_) | 111 if (this.currentMode_) |
| 111 this.currentMode_.update(options); | 112 this.currentMode_.update(options); |
| 112 ImageUtil.trace.reportTimer('update'); | 113 ImageUtil.trace.reportTimer('update'); |
| 113 }; | 114 }; |
| 114 | 115 |
| 115 ImageEditor.prototype.initToolbar = function() { | 116 ImageEditor.prototype.initToolbar = function() { |
| 116 this.toolbar_.clear(); | 117 this.toolbar_.clear(); |
| 117 | 118 |
| 118 this.createModeButtons(); | 119 this.createModeButtons(); |
| 119 this.toolbar_.addButton('Save', this.save.bind(this, true)); | 120 this.toolbar_.addButton('Save', this.save.bind(this, true)); |
| 120 this.toolbar_.addButton('Close', this.close.bind(this, false)); | |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 /** | 123 /** |
| 124 * ImageEditor.Mode represents a modal state dedicated to a specific operation. | 124 * ImageEditor.Mode represents a modal state dedicated to a specific operation. |
| 125 * Inherits from ImageBuffer.Overlay to simplify the drawing of | 125 * Inherits from ImageBuffer.Overlay to simplify the drawing of |
| 126 * mode-specific tools. | 126 * mode-specific tools. |
| 127 */ | 127 */ |
| 128 | 128 |
| 129 ImageEditor.Mode = function(displayName) { | 129 ImageEditor.Mode = function(displayName) { |
| 130 this.displayName = displayName; | 130 this.displayName = displayName; |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 values[child.name] = child.getValue(); | 549 values[child.name] = child.getValue(); |
| 550 } | 550 } |
| 551 return values; | 551 return values; |
| 552 }; | 552 }; |
| 553 | 553 |
| 554 ImageEditor.Toolbar.prototype.reset = function() { | 554 ImageEditor.Toolbar.prototype.reset = function() { |
| 555 for (var child = this.wrapper_.firstChild; child; child = child.nextSibling) { | 555 for (var child = this.wrapper_.firstChild; child; child = child.nextSibling) { |
| 556 if (child.reset) child.reset(); | 556 if (child.reset) child.reset(); |
| 557 } | 557 } |
| 558 }; | 558 }; |
| OLD | NEW |