1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: (c) 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 m_require('core/utility/logger.js'); 12 13 /** 14 * @class 15 * 16 * Object for dispatching all incoming events. 17 * 18 * @extends M.Object 19 */ 20 M.EventDispatcher = M.Object.create( 21 /** @scope M.EventDispatcher.prototype */ { 22 23 /** 24 * The type of this object. 25 * 26 * @type String 27 */ 28 type: 'M.EventDispatcher', 29 30 /** 31 * This method is called whenever an event is triggered within the app. 32 * 33 * @param {Object} evt The event. 34 */ 35 eventDidHappen: function(evt) { 36 this.delegateEvent(evt.type, evt.currentTarget.id, evt.keyCode, evt.orientation); 37 }, 38 39 /** 40 * This method is called whenever an onClick event is triggered within the app. This is 41 * not the common way to catch events, but in some cases it might be necessary to use 42 * the onClick property. 43 * 44 * @param {String} type The type of event that occured, e.g. 'click'. 45 * @param {String} id The id of the element that triggered the event. 46 * @param {Number} keyCode The keyCode property of the event, necessary for keypress event, e.g. keyCode is 13 when enter is pressed. 47 * @param {String} orientation The orientation of the device (only passed if an orientationChange event did happen). 48 */ 49 onClickEventDidHappen: function(type, id, keyCode, orientation) { 50 if(!M.Application.viewManager.getViewById(id).inEditMode) { 51 this.delegateEvent(type, id, keyCode, orientation); 52 } 53 }, 54 55 /** 56 * This method looks for a corresponding event inside the view manager and 57 * delegates the call directly to the responsible controller defined by the 58 * target and action properties of the view. 59 * 60 * @param {String} type The type of event that occured, e.g. 'click'. 61 * @param {String} id The id of the element that triggered the event. 62 * @param {Number} keyCode The keyCode property of the event, necessary for keypress event, e.g. keyCode is 13 when enter is pressed. 63 * @param {String} orientation The orientation of the device (only passed if an orientationChange event did happen). 64 */ 65 delegateEvent: function(type, id, keyCode, orientation) { 66 var view = M.Application.viewManager.getViewById(id); 67 68 switch(type) { 69 case 'click': 70 if(view && view.internalTarget && view.internalAction) { 71 view.internalTarget[view.internalAction](id, view.modelId); 72 } 73 if(view && view.target && view.action && view.type !== 'M.TextFieldView' && view.type !== 'M.SearchBarView') { 74 view.target[view.action](id, view.modelId); 75 } 76 break; 77 case 'change': 78 view.setValueFromDOM(type); 79 break; 80 case 'keyup': 81 if(keyCode === 13 && view.triggerActionOnEnter && (view.type === 'M.TextFieldView' || view.type === 'M.SearchBarView')) { 82 if(view && view.target && view.action) { 83 view.target[view.action](id); 84 } 85 } else if(view.type === 'M.TextFieldView' || view.type === 'M.SearchBarView') { 86 view.setValueFromDOM(type); 87 } 88 break; 89 case 'focusin': 90 view.gotFocus(type); 91 break; 92 case 'focusout': 93 view.lostFocus(type); 94 break; 95 case 'orientationchange': 96 M.Application.viewManager.getCurrentPage().orientationDidChange(orientation); 97 break; 98 } 99 }, 100 101 /** 102 * Registers events given from eventList to a view defined by an id. Can be used to register events after application load. 103 * @param {String} id The View Id, e.g. m_123 104 * @param {String} eventList The Events one after another in a string divided by whitespace. 105 */ 106 registerEvents: function(id, eventList) { 107 var that = this; 108 $('#' + id).bind(eventList, function(evt) { 109 that.eventDidHappen(evt); 110 }); 111 } 112 113 }); 114