      1 // ==========================================================================
      2 // Project:   The M-Project - Mobile HTML5 Application Framework
      3 // Copyright: �2010 M-Way Solutions GmbH. All rights reserved.
      4 // Creator:   Dominik
      5 // Date:      27.10.2010
      6 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
      7 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
      8 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
      9 // ==========================================================================
     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
     21 m_require('core/foundation/observable.js');
     22
     23 /**
     24  * @class
     25  *
     26  * The root class for every controller.
     27  *
     28  * Controllers, respectively their properties, are observables. Views can observe them.
     29  *
     30  * @extends M.Object
     31  */
     32 M.Controller = M.Object.extend(
     33 /** @scope M.Controller.prototype */ {
     34
     35     /**
     36      * The type of this object.
     37      *
     38      * @type String
     39      */
     40     type: 'M.Controller',
     41
     42     /**
     43      * Makes the controller's properties observable.
     44      */
     45     observable: M.Observable.extend({}),
     46
     47     /**
     48      * Helper function to build the location href for the view to be displayed.
     49      *
     50      * @param {String} id The id of the new target.
     51      */
     52     buildLocationHref: function(id) {
     53         return location.pathname + '#' + id;
     54     },
     55
     56     /**
     57      * Returns the class property behind the given key and informs its observers.
     58      *
     59      * @param {Object} page The page to be displayed.
     60      * @param {String} transition The transition that should be used. Default: horizontal slide
     61      * @param {Boolean} isBack YES will cause a reverse-direction transition. Default: NO
     62      * @param {Boolean} changeLoc Update the browser history. Default: YES
     63      */
     64     switchToPage: function(page, transition, isBack, changeLoc) {
     65         var id = M.Application.viewManager.getIdByView(page);
     66         var isTabBarViewTopPage = NO;
     67
     68         if(id) {
     69             if(page.hasTabBarView) {
     70                 if(page.tabBarView.childViews) {
     71                     var tabItemViews = $.trim(page.tabBarView.childViews).split(' ');
     72                     for(var i in tabItemViews) {
     73                         var tabItemView = page.tabBarView[tabItemViews[i]];
     74                         if(M.ViewManager.getPage(tabItemView.page) === page) {
     75                             page.tabBarView.setActiveTab(tabItemView.page, M.Application.viewManager.getIdByView(tabItemView));
     76                             isTabBarViewTopPage = YES;
     77                         }
     78                     }
     79                 }
     80             }
     81             /* If the new page is a real tabBarViewPage (has a tabBarView and is no sub view), use no transition. */
     82             if(isTabBarViewTopPage) {
     83                 transition = page.tabBarView.transition ? page.tabBarView.transition : M.TRANSITION.NONE;
     84                 isBack = NO;
     85                 changeLoc = changeLoc !== undefined ? changeLoc : NO;
     86             } else {
     87                 transition = transition ? transition : M.TRANSITION.SLIDE;
     88                 isBack = isBack !== undefined ? isBack : NO;
     89                 changeLoc = changeLoc !== undefined ? changeLoc : YES;
     90             }
     91
     92             /* Now do the page change by using a jquery mobile method and pass the properties */
     93             $.mobile.changePage(id, transition, isBack, changeLoc);
     94
     95             /* Save the current page in the view manager */
     96             M.Application.viewManager.setCurrentPage(page);
     97         } else {
     98             M.Logger.log('"' + page + '" not found', M.WARN);
     99         }
    100     },
    101
    102     /**
    103      * Returns the class property behind the given key and informs its observers.
    104      *
    105      * @param {String} key The key of the property to be changed.
    106      * @param {Object, String} value The value to be set.
    107      */
    108     set: function(key, value) {
    109         this[key] = value;
    110         this.observable.notifyObservers(key);
    111     }
    112
    113 });