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:   Sebastian
  6 // Date:      02.11.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 
 12 m_require('core/foundation/view_manager.js');
 13 
 14 /**
 15  * @class
 16  *
 17  * The root class for an application.
 18  *
 19  * @extends M.Object
 20  */
 21 M.Application = M.Object.extend(
 22 /** @scope M.Application.prototype */ {
 23 
 24     /**
 25      * The type of this object.
 26      *
 27      * @type String
 28      */
 29     type: 'M.Application',
 30 
 31     /**
 32      * The application's name.
 33      *
 34      * @type String
 35      */
 36     name: null,
 37 
 38     /**
 39      * The application's current language.
 40      *
 41      * @type String
 42      */
 43     currentLanguage: null,
 44 
 45     /**
 46      * The application's default / fallback language.
 47      *
 48      * @type String
 49      */
 50     defaultLanguage: null,
 51 
 52     /**
 53      * This property is set to NO once the first page within an application was loaded. So this
 54      * can be used as a hook to trigger some actions at the first load of any view. To do initial
 55      * things for a specific view, use the isFirstLoad property of M.PageView.
 56      *
 57      * @type Boolean
 58      */
 59     isFirstLoad: YES,
 60 
 61     /**
 62      * This property can be used to define the application's entry page. If set, this page will
 63      * be the first to be displayed if your application is started.
 64      *
 65      * Even if this property is not absolutely necessary, we highly recommend to specify an entry
 66      * page! 
 67      *
 68      * @type String
 69      */
 70     entryPage: null,
 71 
 72     /**
 73      * This property contains the application-specific configurations. It is automatically set by Espresso
 74      * during the init process of an application. To access these properties within the application, use the
 75      * getConfig() method of M.Application.
 76      */
 77     config: {},
 78 
 79     /**
 80      * This method encapsulates the 'include' method of M.Object for better reading of code syntax.
 81      * Basically it integrates the defined pages within the application into M.Application and sets
 82      * some basic configuration properties, e.g. the default language.
 83      *
 84      * @param {Object} obj The mixed in object for the extend call.
 85      */
 86     design: function(obj) {
 87         var pages = {};
 88         for(var pageName in obj) {
 89             if(obj[pageName] && obj[pageName].type === 'M.PageView') {
 90                 pages[pageName] = obj[pageName];
 91             }
 92         }      
 93         this.include({
 94             pages: pages
 95         });
 96 
 97         this.entryPage = ((obj.entryPage && typeof(obj.entryPage) === 'string') ? obj.entryPage : null);
 98 
 99         return this;
100     },
101 
102     /**
103      * The application's main-method, that is called automatically on load of the app.
104      * Inside this method the rendering is initiated and all pages are bound to the 'pageshow'
105      * event so one can do some action whenever a page is loaded.
106      */
107     main: function() {
108         var that = this;
109 
110         /* first lets get the entry page and remove it from pagelist and viewlist */
111         var entryPage = M.ViewManager.getPage(M.Application.entryPage);
112         delete M.ViewManager.viewList[entryPage.id];
113         delete M.ViewManager.pageList[entryPage.id];
114 
115         /* set the default id 'm_entryPage' for entry page */
116         entryPage.id = 'm_entryPage';
117 
118         /* now lets render entry page to get it into the DOM first and set it as the current page */
119         entryPage.render();
120         M.ViewManager.setCurrentPage(entryPage);
121 
122         /* now lets render all other pages */
123         _.each(M.ViewManager.pageList, function(page) {
124             page.render();
125         });
126 
127         /* finally add entry page back to pagelist and view list, but with new key 'm_entryPage' */
128         M.ViewManager.viewList['m_entryPage'] = entryPage;
129         M.ViewManager.pageList['m_entryPage'] = entryPage;
130     },
131 
132     /**
133      *
134      * @param {String} key The key of the configuration value to want to retrieve.
135      * @returns {String} The value in the application's config object with the key 'key'.
136      */
137     getConfig: function(key) {
138         if(this.config[key]) {
139             return this.config[key];
140         }
141         return null;
142     }
143 
144 });