1 // ==========================================================================
  2 // Project:   The M-Project - Mobile HTML5 Application Framework
  3 // Copyright: �2010 M-Way Solutions GmbH. All rights reserved.
  4 // Creator:   Sebastian
  5 // Date:      02.11.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 m_require('core/foundation/view_manager.js');
 12 
 13 /**
 14  * @class
 15  *
 16  * The root class for an application.
 17  *
 18  * @extends M.Object
 19  */
 20 M.Application = M.Object.extend(
 21 /** @scope M.Application.prototype */ {
 22 
 23     /**
 24      * The type of this object.
 25      *
 26      * @type String
 27      */
 28     type: 'M.Application',
 29 
 30     /**
 31      * The application's name.
 32      *
 33      * @type String
 34      */
 35     name: null,
 36 
 37     /**
 38      * The application's view manager.
 39      *
 40      * @type Object
 41      */
 42     viewManager: M.ViewManager,
 43 
 44     /**
 45      * The application's model registry.
 46      *
 47      * @type Object
 48      */
 49     modelRegistry: M.ModelRegistry,
 50 
 51     /**
 52      * The application's event dispatcher.
 53      *
 54      * @type Object
 55      */
 56     eventDispatcher: M.EventDispatcher,
 57 
 58     /**
 59      * The application's cypher object, used for encoding and decoding.
 60      *
 61      * @type Object
 62      */
 63     cypher: M.Cypher,
 64 
 65     /**
 66      * The application's current language.
 67      *
 68      * @type String
 69      */
 70     currentLanguage: null,
 71 
 72     /**
 73      * The application's default / fallback language.
 74      *
 75      * @type String
 76      */
 77     defaultLanguage: null,
 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         this.include({
 88             pages: obj
 89         });
 90         return this;
 91     },
 92 
 93     /**
 94      * The application's main-method, that is called automatically on load of the app.
 95      * Inside this method the rendering is initiated and all pages are bound to the 'pageshow'
 96      * event so one can do some action whenever a page is loaded.
 97      */
 98     main: function() {
 99         var that = this;
100 
101         /* live is jQuery fn that binds an event to all elements matching a certain selector now and in the future */
102         var eventList = 'click change keyup focus blur orientationchange';
103         $('*[id]').live(eventList, function(evt) {
104             that.eventDispatcher.eventDidHappen(evt);
105         });
106 
107         /* also bind the orientationchange event to the body of the application */
108         $('body').bind('orientationchange', function(evt) {
109             that.eventDispatcher.eventDidHappen(evt);
110         });
111 
112         var html = '';
113         for(i in this.viewManager.viewList) {
114             if(this.viewManager.viewList[i].type === 'M.PageView') {
115                 html += this.viewManager.viewList[i].render();
116                 /* bind the pageshow event to any view's pageDidLoad property function */
117                 $('#' + this.viewManager.viewList[i].id).bind('pagebeforeshow', this.bindToCaller(this.viewManager.viewList[i], this.viewManager.viewList[i].pageWillLoad));
118 
119                 /* bind the pageshow event to any view's pageWillLoad property function */
120                 $('#' + this.viewManager.viewList[i].id).bind('pageshow', this.bindToCaller(this.viewManager.viewList[i], this.viewManager.viewList[i].pageDidLoad));
121 
122                 /* bind the pagebeforehide event to any view's pageWillHide property function */
123                 $('#' + this.viewManager.viewList[i].id).bind('pagebeforehide', this.bindToCaller(this.viewManager.viewList[i], this.viewManager.viewList[i].pageWillHide));
124 
125                 /* bind the pagehide event to any view's pageDidHide property function */
126                 $('#' + this.viewManager.viewList[i].id).bind('pagehide', this.bindToCaller(this.viewManager.viewList[i], this.viewManager.viewList[i].pageDidHide));
127 
128                 /* set the first page as current page to be displayed */
129                 if(!this.viewManager.getCurrentPage()) {
130                     this.viewManager.setCurrentPage(this.viewManager.viewList[i]);
131                 }
132             }
133         }
134     }
135 
136 });