Posted By

rportela on 10/19/09


Tagged

widget utilities


Versions (?)

Widget utilities for W3C compliant Widget Runtimes


 / Published in: JavaScript
 

Here are several functions I think are very helpfull while building mobile widgets (W3C), ready for your copy-paste pleasure =D

  1. //Some utilities for use in a typical widget that runs on W3C compliant Widget Runtimes
  2. //I am using jQuery's support for cookies, so if you are not using it, please tweak the code at will to fit your needs
  3. WidgetUtilities.prototype = {
  4. //Get the value associated with a configuration key
  5. getKey : function(key){
  6. if (typeof(widget) != "undefined"){ // A Widget Engine!
  7. return widget.preferenceForKey(key);
  8. }else{ // It's a browser!
  9. return $.cookie(key);
  10. }
  11. },
  12. //Set the value associated with a configuration key
  13. setKey : function(key, value){
  14. if (typeof(widget) != "undefined"){
  15. return widget.setPreferenceForKey(value, key);
  16. }else{
  17. return($.cookie(key, value));
  18. }
  19. },
  20. //Get the Pixels Per Inch on the device running the widget
  21. getWidth : function(){
  22. var DOM_body = document.getElementsByTagName('body')[0];
  23.  
  24. var DOM_div = document.createElement('div');
  25. try{
  26. DOM_div.style = 'width: 1in; visibility:hidden;';
  27. }
  28. catch(e){
  29.  
  30. }
  31.  
  32. DOM_body.appendChild(DOM_div);
  33. var w = document.defaultView.getComputedStyle(DOM_div, null).getPropertyValue('width');
  34. DOM_body.removeChild(DOM_div);
  35.  
  36. return parseInt(w);
  37. },
  38. //Run a specified function in a "safe" manner. If execution throws an exception, show all available information
  39. catchy : function(fn){
  40. return function(){
  41. try{
  42. fn.apply(fn, arguments);
  43. }catch(e){
  44. var err = [];
  45. for (var key in e){
  46. if (!key || typeof key=="function") continue;
  47. err.push(key + ": " + e[key]);
  48. }
  49. widget.showNotification(err.join("\n"));
  50. }
  51. };
  52. }
  53. }

Report this snippet  

You need to login to post a comment.