Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / gallery / js / image_editor / image_buffer.js
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  * A stack of overlays that display itself and handle mouse events.
7  * TODO(kaznacheev) Consider disbanding this class and moving
8  * the functionality to individual objects that display anything or handle
9  * mouse events.
10  * @constructor
11  */
12 function ImageBuffer() {
13   this.overlays_ = [];
14 }
15
16 /**
17  * TODO(JSDOC).
18  * @param {ImageBuffer.Overlay} overlay  // TODO(JSDOC).
19  */
20 ImageBuffer.prototype.addOverlay = function(overlay) {
21   var zIndex = overlay.getZIndex();
22   // Store the overlays in the ascending Z-order.
23   var i;
24   for (i = 0; i != this.overlays_.length; i++) {
25     if (zIndex < this.overlays_[i].getZIndex()) break;
26   }
27   this.overlays_.splice(i, 0, overlay);
28 };
29
30 /**
31  * TODO(JSDOC).
32  * @param {ImageBuffer.Overlay} overlay  // TODO(JSDOC).
33  */
34 ImageBuffer.prototype.removeOverlay = function(overlay) {
35   for (var i = 0; i != this.overlays_.length; i++) {
36     if (this.overlays_[i] == overlay) {
37       this.overlays_.splice(i, 1);
38       return;
39     }
40   }
41   throw new Error('Cannot remove overlay ' + overlay);
42 };
43
44 /**
45  * Draws overlays in the ascending Z-order.
46  */
47 ImageBuffer.prototype.draw = function() {
48   for (var i = 0; i != this.overlays_.length; i++) {
49     this.overlays_[i].draw();
50   }
51 };
52
53 /**
54  * Searches for a cursor style in the descending Z-order.
55  * @param {number} x X coordinate for cursor.
56  * @param {number} y Y coordinate for cursor.
57  * @param {boolean} mouseDown If mouse button is down.
58  * @return {string} A value for style.cursor CSS property.
59  */
60 ImageBuffer.prototype.getCursorStyle = function(x, y, mouseDown) {
61   for (var i = this.overlays_.length - 1; i >= 0; i--) {
62     var style = this.overlays_[i].getCursorStyle(x, y, mouseDown);
63     if (style) return style;
64   }
65   return 'default';
66 };
67
68 /**
69  * Searches for a click handler in the descending Z-order.
70  * @param {number} x X coordinate for click event.
71  * @param {number} y Y coordinate for click event.
72  * @return {boolean} True if handled.
73  */
74 ImageBuffer.prototype.onClick = function(x, y) {
75   for (var i = this.overlays_.length - 1; i >= 0; i--) {
76     if (this.overlays_[i].onClick(x, y)) return true;
77   }
78   return false;
79 };
80
81 /**
82  * Searches for a drag handler in the descending Z-order.
83  * @param {number} x Event X coordinate.
84  * @param {number} y Event Y coordinate.
85  * @param {boolean} touch True if it's a touch event, false if mouse.
86  * @return {function(number,number)} A function to be called on mouse drag.
87  */
88 ImageBuffer.prototype.getDragHandler = function(x, y, touch) {
89   for (var i = this.overlays_.length - 1; i >= 0; i--) {
90     var handler = this.overlays_[i].getDragHandler(x, y, touch);
91     if (handler)
92       return handler;
93   }
94   return null;
95 };
96
97 /**
98  * Searches for an action for the double tap enumerating
99  * layers in the descending Z-order.
100  * @param {number} x X coordinate of the event.
101  * @param {number} y Y coordinate of the event.
102  * @return {ImageBuffer.DoubleTapAction} Action to perform as result.
103  */
104 ImageBuffer.prototype.getDoubleTapAction = function(x, y) {
105   for (var i = this.overlays_.length - 1; i >= 0; i--) {
106     var action = this.overlays_[i].getDoubleTapAction(x, y);
107     if (action != ImageBuffer.DoubleTapAction.NOTHING)
108       return action;
109   }
110   return ImageBuffer.DoubleTapAction.NOTHING;
111 };
112
113 /**
114  * Possible double tap actions.
115  * @enum
116  */
117 ImageBuffer.DoubleTapAction = {
118   NOTHING: 0,
119   COMMIT: 1,
120   CANCEL: 2
121 };
122
123 /**
124  * ImageBuffer.Overlay is a pluggable extension that modifies the outlook
125  * and the behavior of the ImageBuffer instance.
126  * @class
127  */
128 ImageBuffer.Overlay = function() {};
129
130 /**
131  * TODO(JSDOC).
132  * @return {number}  // TODO(JSDOC).
133  */
134 ImageBuffer.Overlay.prototype.getZIndex = function() { return 0; };
135
136 /**
137  * TODO(JSDOC).
138  */
139 ImageBuffer.Overlay.prototype.draw = function() {};
140
141 /**
142  * TODO(JSDOC).
143  * @param {number} x X coordinate for cursor.
144  * @param {number} y Y coordinate for cursor.
145  * @param {boolean} mouseDown If mouse button is down.
146  * @return {?string} A value for style.cursor CSS property or null for
147  *     default.
148  */
149 ImageBuffer.Overlay.prototype.getCursorStyle = function(x, y, mouseDown) {
150   return null;
151 };
152
153 /**
154  * TODO(JSDOC).
155  * @param {number} x  // TODO(JSDOC).
156  * @param {number} y  // TODO(JSDOC).
157  * @return {boolean}  // TODO(JSDOC).
158  */
159 ImageBuffer.Overlay.prototype.onClick = function(x, y) {
160   return false;
161 };
162
163 /**
164  * TODO(JSDOC).
165  * @param {number} x Event X coordinate.
166  * @param {number} y Event Y coordinate.
167  * @param {boolean} touch True if it's a touch event, false if mouse.
168  * @return {function(number,number)} A function to be called on mouse drag.
169  */
170 ImageBuffer.Overlay.prototype.getDragHandler = function(x, y, touch) {
171   return null;
172 };
173
174 /**
175  * TODO(JSDOC).
176  * @param {number} x  // TODO(JSDOC).
177  * @param {number} y  // TODO(JSDOC).
178  * @return {ImageBuffer.DoubleTapAction}  // TODO(JSDOC).
179  */
180 ImageBuffer.Overlay.prototype.getDoubleTapAction = function(x, y) {
181   return ImageBuffer.DoubleTapAction.NOTHING;
182 };