53
var Toolbar = function (UbuntuUI, id) {
54
this.toolbar = document.getElementById(id);
63
this.toolbar.classList.add('revealed');
70
this.toolbar.classList.remove('revealed');
73
* Toggle show/hide status of a Toolbar
77
this.toolbar.classList.toggle('revealed');
80
* Provide a callback function that's called with the Toolbar is touched
82
* @param {Function} function - The function that is called when the Toolbar is touched
84
touch: function (callback) {
85
this.toolbar.addEventListener(UbuntuUI.touchEvents.touchEnd, callback);
88
* Returns the DOM element associated with the id this widget is bind to.
91
var mytoolbar = UI.toolbar("toolbarid").element();
93
element: function () {
53
var Toolbar = (function () {
55
function Toolbar(id, touchInfoDelegate) {
57
this.PHASE_START = "start";
58
this.PHASE_MOVE = "move";
59
this.PHASE_END = "end";
60
this.PHASE_CANCEL = "cancel";
64
this.toolbar = document.getElementById(id);
66
throw "Invalid toolbar id";
68
this._touchDown = false;
69
this._touchInfoDelegate = touchInfoDelegate;
72
this.fingerData.push({
84
var touchEvents = touchInfoDelegate.touchEvents;
85
touchInfoDelegate.registerTouchEvent(
86
touchEvents.touchStart, this.toolbar, this.__onTouchStart.bind(this));
87
touchInfoDelegate.registerTouchEvent(
88
touchEvents.touchEnd, this.toolbar, this.__onTouchEnd.bind(this));
89
touchInfoDelegate.registerTouchEvent(
90
touchEvents.touchMove, this.toolbar, this.__onTouchMove.bind(this));
91
touchInfoDelegate.registerTouchEvent(
92
touchEvents.touchLeave, this.toolbar, this.__onTouchLeave.bind(this));
101
this.toolbar.classList.add('revealed');
109
this.toolbar.classList.remove('revealed');
113
* Toggle show/hide status of a Toolbar
116
toggle: function () {
117
this.toolbar.classList.toggle('revealed');
121
* Returns the DOM element associated with the id this widget is bind to.
124
var mytoolbar = UI.toolbar("toolbarid").element();
126
element: function () {
133
__onTouchStart: function (evt) {
134
this._touchDown = true;
136
evt.preventDefault();
138
this.phase = this.PHASE_START;
139
var identifier = evt.identifier !== undefined ? evt.identifier : 0;
142
this._touchInfoDelegate.translateTouchEvent(evt);
144
this.fingerData[0].identifier = identifier;
145
this.fingerData[0].start.x =
146
this.fingerData[0].end.x = touchEvent.touches[0].pageX;
147
this.fingerData[0].start.y =
148
this.fingerData[0].end.y = touchEvent.touches[0].pageY;
154
__onTouchMove: function (evt) {
155
if ( ! this._touchDown)
158
if (this.phase === this.PHASE_END || this.phase === this.PHASE_CANCEL)
161
if (this.phase == this.PHASE_START) {
162
evt.preventDefault();
165
this._touchInfoDelegate.translateTouchEvent(evt);
167
var identifier = evt.identifier !== undefined ? evt.identifier : 0;
168
var f = this.__getFingerData(identifier);
170
f.end.x = touchEvent.touches[0].pageX;
171
f.end.y = touchEvent.touches[0].pageY;
173
direction = this.__calculateDirection(f.start, f.end);
175
if (direction == "DOWN") {
179
if (direction == "UP") {
183
phase = this.PHASE_MOVE;
190
__onTouchEnd: function (e) {
191
this._touchDown = false;
192
phase = this.PHASE_END;
198
__onTouchLeave: function (e) {
199
this._touchDown = false;
200
phase = this.PHASE_CANCEL;
206
__calculateDirection: function (startPoint, endPoint) {
207
var angle = this.__calculateAngle(startPoint, endPoint);
209
if ((angle <= 45) && (angle >= 0)) {
211
} else if ((angle <= 360) && (angle >= 315)) {
213
} else if ((angle >= 135) && (angle <= 225)) {
215
} else if ((angle > 45) && (angle < 135)) {
225
__getFingerData: function (id) {
226
for (var i = 0; i < this.fingerData.length; i++) {
227
if (this.fingerData[i].identifier == id) {
228
return this.fingerData[i];
236
__calculateAngle: function (startPoint, endPoint) {
237
var x = startPoint.x - endPoint.x;
238
var y = endPoint.y - startPoint.y;
239
var r = Math.atan2(y, x); //radians
240
var angle = Math.round(r * 180 / Math.PI); //degrees
242
//ensure value is positive
244
angle = 360 - Math.abs(angle);