2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
17 /*jslint devel: true*/
18 /*global tizen, $, app: true */
20 var App = null, app = null;
22 (function () { // strict mode wrapper
26 * Creates a new application object
30 App = function App() {
48 init: function App_init() {
54 * binds application events
56 bindEvents: function bindEvents() {
57 $(document).bind('touchstart touchmove', this.onPianoKeyTouchDown.bind(this));
58 $(document).bind('touchend', this.onPianoKeyTouchUp.bind(this));
60 $('#back').bind('touchstart', function (event) {
61 event.preventDefault();
62 event.stopPropagation();
63 $(this).addClass('active');
64 tizen.application.getCurrentApplication().exit();
67 $('#back').bind('touchend', function (event) {
68 $(this).removeClass('active');
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);
79 if (this.touchPianoKey[touch.identifier] !== element.id) {
80 $('#' + this.touchPianoKey[touch.identifier]).removeClass('pressed');
81 this.playAudioByElement(element, touch.identifier);
87 onPianoKeyTouchUp: function onPianoKeyTouchUp(data) {
89 for (i = 0, len = data.originalEvent.changedTouches.length; i < len; i += 1) {
90 touchId = data.originalEvent.changedTouches[i].identifier;
92 $('#' + this.touchPianoKey[touchId]).removeClass('pressed');
93 this.touchPianoKey[touchId] = undefined;
97 audioInit: function audioInit() {
99 for (i = 0; i <= 13; i += 1) {
100 this.audio[i] = document.createElement('audio');
101 this.audio[i].name = i;
106 * Plays sound for specified element
107 * @param {Object} element
108 * @param {Number} touchId
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;
119 this.audio[nrAudio].src = this.audio[nrAudio].name + ".wav";
121 this.audio[nrAudio].play();
127 $(document).ready(function () {