Skip to content

Instantly share code, notes, and snippets.

@elrasguno
Created April 12, 2011 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elrasguno/916547 to your computer and use it in GitHub Desktop.
Save elrasguno/916547 to your computer and use it in GitHub Desktop.
Possible approach to platform specific handling for ringz app.
console && console.log('this is ringz!');
var RingzApp = function(rPlatform) {
var vPlatform = rPlatform || 'web',
vClickerConfig,
vRingzEventHandler;
/***
* Platform specific Clicker/Event config
**/
vClickerConfig = {
'web' : {
'37' : 'left',
'38' : 'up',
'39' : 'right',
'40' : 'down',
'13' : 'enter',
'27' : 'escape'
}
};
/***
* EventHandler Class
**/
vRingzEventHandler = observable.extend({
config : null,
init : function (rPlatform)
{
this._super();
this.config = vClickerConfig[rPlatform] || vClickerConfig['web'];
}
});
/***
* TODO: create platform specific controller objects i.e one
* for web, tablet, remote control (tvs etc). Return
* an instance of that object along with the ringzEventHandler.
**/
return {
ringzEventHandler : new vRingzEventHandler(rPlatform)
}
};
if (!window.ringzapp)
{
$(document).ready(function() {
var vHash = document.location.hash.substr(1),
vParsedHash = (function() {
var vResult = {};
$.each(vHash.split('&'), function(k, v) {
var vKVPair = v.split('=');
vResult[vKVPair[0]] = vKVPair[1];
});
return vResult;
})(),
vPlatform = (vParsedHash && vParsedHash['platform']) || 'web',
vRingzEventHandler;
console.debug('vPlatform', vPlatform);
ringzapp = new RingzApp(vPlatform);
/***
* Observe events being fired, and call the corresponding 'on' + event function(s).
**/
if (vRingzEventHandler = ringzapp.ringzEventHandler) {
$.each(vRingzEventHandler.config, function(k, v) {
vRingzEventHandler.observe(v, function() { console.debug('on' + v.toProperCase()); });
});
}
// TODO: add support for keypress when press and hold is required.
// $(document).keypress(function(rEvt) {});
/***
* Listen for key events.
* If they correspond with current platform's config, fire an event.
**/
$(document).keyup(function(rEvt) {
var vKeyCodeStr = rEvt.keyCode.toString(),
vClickerConfig = vRingzEventHandler.config,
vClickerEvent = vClickerConfig[vKeyCodeStr];
vClickerEvent && console.debug('Event captured', vClickerEvent);
vClickerEvent && vRingzEventHandler.fire(vClickerEvent);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment