/frameworks/The-M-Project/modules/core/foundation/controller.js

https://github.com/christrees/spoolspartyplay · JavaScript · 133 lines · 63 code · 18 blank · 52 comment · 11 complexity · 4fe6c80c77931bf874b74c62003b1ce1 MD5 · raw file

  1. // ==========================================================================
  2. // Project: The M-Project - Mobile HTML5 Application Framework
  3. // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
  4. // (c) 2011 panacoda GmbH. All rights reserved.
  5. // Creator: Dominik
  6. // Date: 27.10.2010
  7. // License: Dual licensed under the MIT or GPL Version 2 licenses.
  8. // http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
  9. // http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
  10. // ==========================================================================
  11. /* Available transitions for page changes */
  12. M.TRANSITION = {};
  13. M.TRANSITION.NONE = 'none';
  14. M.TRANSITION.SLIDE = 'slide';
  15. M.TRANSITION.SLIDEUP = 'slideup';
  16. M.TRANSITION.SLIDEDOWN = 'slidedown';
  17. M.TRANSITION.POP = 'pop';
  18. M.TRANSITION.FADE = 'fade';
  19. M.TRANSITION.FLIP = 'flip';
  20. m_require('core/foundation/observable.js');
  21. /**
  22. * @class
  23. *
  24. * The root class for every controller.
  25. *
  26. * Controllers, respectively their properties, are observables. Views can observe them.
  27. *
  28. * @extends M.Object
  29. */
  30. M.Controller = M.Object.extend(
  31. /** @scope M.Controller.prototype */ {
  32. /**
  33. * The type of this object.
  34. *
  35. * @type String
  36. */
  37. type: 'M.Controller',
  38. /**
  39. * Makes the controller's properties observable.
  40. */
  41. observable: null,
  42. /**
  43. * Switch the active tab in the application. This includes both activating this tab
  44. * visually and switching the page.
  45. *
  46. * @param {M.TabBarView} tab The tab to be activated.
  47. */
  48. switchToTab: function(tab) {
  49. var currentTab = tab.parentView.activeTab;
  50. var newPage = M.ViewManager.getPage(tab.page);
  51. /* store the active tab in tab bar view */
  52. tab.parentView.setActiveTab(tab);
  53. if(tab === currentTab) {
  54. var currentPage = M.ViewManager.getCurrentPage();
  55. if(currentPage !== newPage) {
  56. this.switchToPage(newPage, null, YES, NO);
  57. }
  58. } else {
  59. this.switchToPage(newPage, M.TRANSITION.NONE, NO, YES);
  60. }
  61. },
  62. /**
  63. * Switch the active page in the application.
  64. *
  65. * @param {Object|String} page The page to be displayed or its name.
  66. * @param {String} transition The transition that should be used. Default: horizontal slide
  67. * @param {Boolean} isBack YES will cause a reverse-direction transition. Default: NO
  68. * @param {Boolean} updateHistory Update the browser history. Default: YES
  69. */
  70. switchToPage: function(page, transition, isBack, updateHistory) {
  71. var timeStart = M.Date.now();
  72. page = page && typeof(page) === 'object' ? page : M.ViewManager.getPage(page);
  73. if(page) {
  74. transition = transition ? transition : M.TRANSITION.SLIDE;
  75. isBack = isBack !== undefined ? isBack : NO;
  76. updateHistory = updateHistory !== undefined ? updateHistory : YES;
  77. /* Now do the page change by using a jquery mobile method and pass the properties */
  78. if(page.type === 'M.PageView') {
  79. //console.log('$.mobile.changePage(' + page.id + ', ' + (M.Application.useTransitions ? transition : M.TRANSITION.NONE) + ', ' + (M.Application.useTransitions ? isBack : NO) + ', ' + updateHistory + ');');
  80. $.mobile.changePage($('#' + page.id), {
  81. transition: M.Application.useTransitions ? transition : M.TRANSITION.NONE,
  82. reverse: M.Application.useTransitions ? isBack : NO,
  83. changeHash: YES,
  84. showLoadMsg: NO
  85. });
  86. }
  87. /* Save the current page in the view manager */
  88. M.ViewManager.setCurrentPage(page);
  89. } else {
  90. M.Logger.log('Page "' + page + '" not found', M.ERR);
  91. }
  92. },
  93. /**
  94. * This method initializes the notification of all observers, that observe the property behind 'key'.
  95. *
  96. * @param {String} key The key of the property to be changed.
  97. * @param {Object|String} value The value to be set.
  98. */
  99. set: function(key, value) {
  100. var keyPath = key.split('.');
  101. if(keyPath.length === 1) {
  102. this[key] = value;
  103. } else {
  104. var t = (this[keyPath[0]] = this[keyPath[0]] ? this[keyPath[0]] : {});
  105. for(var i = 1; i < keyPath.length - 1; i++) {
  106. t = (t[keyPath[i]] = t[keyPath[i]] ? t[keyPath[i]] : {});
  107. }
  108. t[keyPath[keyPath.length - 1]] = value;
  109. }
  110. if(!this.observable) {
  111. return;
  112. }
  113. this.observable.notifyObservers(key);
  114. }
  115. });