[Piano]update Piano(tizen_2.1)
[samples/web/Piano.git] / js / main.js
1 /*
2  *      Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.0 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global tizen, $, app: true */
19
20 var App = null, app = null;
21
22 (function () { // strict mode wrapper
23         'use strict';
24
25         /**
26          * Creates a new application object
27          *
28          * @class Application
29          */
30         App = function App() {
31                 this.init();
32         };
33
34         App.prototype = {
35                 /**
36                  * @type {Array}
37                  */
38                 audio: [],
39
40                 /**
41                  * @type {Array}
42                  */
43                 touchPianoKey: [],
44
45                 /**
46                  * Initialization
47                  */
48                 init: function App_init() {
49                         this.bindEvents();
50                         this.audioInit();
51                 },
52
53                 /**
54                  * binds application events
55                  */
56                 bindEvents: function bindEvents() {
57                         $(document).bind('touchstart touchmove', this.onPianoKeyTouchDown.bind(this));
58                         $(document).bind('touchend', this.onPianoKeyTouchUp.bind(this));
59
60                         $('#back').bind('touchstart', function (event) {
61                                 event.preventDefault();
62                                 event.stopPropagation();
63                                 $(this).addClass('active');
64                                 tizen.application.getCurrentApplication().exit();
65                         });
66
67                         $('#back').bind('touchend', function (event) {
68                                 $(this).removeClass('active');
69                         });
70                 },
71
72                 onPianoKeyTouchDown: function onPianoKeyTouchDown(data) {
73                         data.originalEvent.preventDefault();
74                         var element, touch, i, len;
75                         for (i = 0, len = data.originalEvent.changedTouches.length; i < len; i += 1) {
76                                 touch = data.originalEvent.changedTouches[i];
77                                 element = document.elementFromPoint(touch.clientX, touch.clientY);
78                                 if (element) {
79                                         if (this.touchPianoKey[touch.identifier] !== element.id) {
80                                                 $('#' + this.touchPianoKey[touch.identifier]).removeClass('pressed');
81                                                 this.playAudioByElement(element, touch.identifier);
82                                         }
83                                 }
84                         }
85                 },
86
87                 onPianoKeyTouchUp: function onPianoKeyTouchUp(data) {
88                         var i, touchId, len;
89                         for (i = 0, len = data.originalEvent.changedTouches.length; i < len; i += 1) {
90                                 touchId = data.originalEvent.changedTouches[i].identifier;
91
92                                 $('#' + this.touchPianoKey[touchId]).removeClass('pressed');
93                                 this.touchPianoKey[touchId] = undefined;
94                         }
95                 },
96
97                 audioInit: function audioInit() {
98                         var i;
99                         for (i = 0; i <= 13; i += 1) {
100                                 this.audio[i] = document.createElement('audio');
101                                 this.audio[i].name = i;
102                         }
103                 },
104
105                 /**
106                  * Plays sound for specified element
107                  * @param {Object} element
108                  * @param {Number} touchId
109                  */
110                 playAudioByElement: function playAudioByElement(element, touchId) {
111                         var nrAudio = parseInt(element.id.replace(/[a-zA-Z]*/, ''), 10);
112                         $(element).addClass('pressed');
113                         this.touchPianoKey[touchId] = element.id;
114                         if (this.audio[nrAudio]) {
115                                 if (this.audio[nrAudio].src) {
116                                         this.audio[nrAudio].pause();
117                                         this.audio[nrAudio].currentTime = 0;
118                                 } else {
119                                         this.audio[nrAudio].src = this.audio[nrAudio].name + ".wav";
120                                 }
121                                 this.audio[nrAudio].play();
122                         }
123                 }
124         };
125 }());
126
127 $(document).ready(function () {
128         'use strict';
129         app = new App();
130 });