main.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright (c) 2013 Mapo developers and contributors <mapo.tizen@gmail.com>
  3. This file is part of Mapo.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var backEventListener = null;
  16. var unregister = function() {
  17. if ( backEventListener !== null ) {
  18. document.removeEventListener( 'tizenhwkey', backEventListener );
  19. backEventListener = null;
  20. window.tizen.application.getCurrentApplication().exit();
  21. }
  22. }
  23. //Initialize function
  24. var init = function () {
  25. // register once
  26. if ( backEventListener !== null ) {
  27. return;
  28. }
  29. // TODO:: Do your initialization job
  30. console.log("init() called");
  31. var backEvent = function(e) {
  32. if ( e.keyName == "back" ) {
  33. try {
  34. if ( $.mobile.urlHistory.activeIndex <= 0 ) {
  35. quit();
  36. // if first page, terminate app
  37. unregister();
  38. } else {
  39. // move previous page
  40. $.mobile.urlHistory.activeIndex -= 1;
  41. $.mobile.urlHistory.clearForward();
  42. window.history.back();
  43. }
  44. } catch( ex ) {
  45. unregister();
  46. }
  47. } else if ( e.keyName == "menu") {
  48. $.mobile.changePage("#settings");
  49. }
  50. }
  51. // add eventListener for tizenhwkey (Back Button)
  52. document.addEventListener( 'tizenhwkey', backEvent );
  53. backEventListener = backEvent;
  54. start();
  55. };
  56. $(document).bind( 'pageinit', init );
  57. $(document).unload( unregister );