home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Theme / 8GadgetPack / 8GadgetPackSetup.msi / Gadgets.7z / Gadgets / HUD-Time.gadget / js / gadget.js < prev    next >
Text File  |  2012-11-01  |  4KB  |  131 lines

  1. console.log('loaded: gadget.js');
  2.  
  3. var font;
  4. var color;
  5. var opacity;
  6. // var negativeVerticalMargin;
  7. // var negativeHorizontalMargin;
  8.  
  9. var toTime;
  10. var toDate;
  11. var toMonth;
  12. var toDay;
  13.  
  14. var targetWidth;
  15. var targetHeight;
  16.  
  17. function init() {
  18.     console.log('[init]');
  19.  
  20.     System.Gadget.settingsUI = 'settings.html';
  21.     System.Gadget.onSettingsClosed = function() {
  22.         readSettings();
  23.         createTextObjects();
  24.         refresh(false);
  25.     };
  26.  
  27.     readSettings();
  28.     createTextObjects();
  29.     refresh(true);
  30. }
  31.  
  32. function readSettings() {
  33.     console.log('[readSettings]');
  34.  
  35.     font = System.Gadget.Settings.readString('font') || 'Arial';
  36.     color = System.Gadget.Settings.readString('color') || 'white';
  37.     opacity = System.Gadget.Settings.read('opacity') || 30;
  38.     // negativeVerticalMargin = System.Gadget.Settings.read('negativeVerticalMargin') || 0;
  39.     // negativeHorizontalMargin = System.Gadget.Settings.read('negativeHorizontalMargin') || 0;
  40.  
  41.     console.log('    font: '+font);
  42.     console.log('    color: '+color);
  43.     console.log('    opacity: '+opacity);
  44. }
  45.  
  46. function createTextObjects() {
  47.     console.log('[createTextObjects]');
  48.  
  49.     var bg = document.getElementById('bg');
  50.     bg.removeObjects();
  51.  
  52.     // 0's are dummy chars for initial text object alignment
  53.     toTime = bg.addTextObject('00:00 AM', font, 105, color, 0, 0);
  54.     toDate = bg.addTextObject('0', font, 80, color, toTime.left, 90); //(toTime.top+toTime.height)*0.69);
  55.     toMonth = bg.addTextObject('0', font, 45, color, toDate.left+toDate.width, 95);
  56.     toDay = bg.addTextObject('0', font, 30, color, toDate.left+toDate.width, 135);
  57.  
  58.     //we really don't want to be changing the width of the gadget because of clock time
  59.     //because gadgets are anchored on the desktop by the left side, and the content is right-aligned
  60.  
  61.     targetWidth = document.getElementsByTagName('body')[0].style.width = toTime.width;
  62.     targetHeight = document.getElementsByTagName('body')[0].style.height = toDay.top + toDay.height;
  63. }
  64.  
  65. function refresh(autoreload) {
  66.     console.log('[refresh]');
  67.  
  68.     console.log('    font: '+font);
  69.     console.log('    color: '+color);
  70.     console.log('    opacity: '+opacity);
  71.  
  72.  
  73.     var currentTime = System.Time.getLocalTime(System.Time.currentTimeZone);
  74.     var currentDate = new Date(Date.parse(currentTime));
  75.  
  76.     var hours = currentDate.getHours();
  77.     var meridiem = hours >= 12 ? ' PM' : ' AM';
  78.     if(hours == 0) {
  79.         hours = 12;
  80.     }
  81.  
  82.     var minutes = currentDate.getMinutes();
  83.     minutes = ((minutes < 10) ? ':0' : ':') + minutes;
  84.  
  85.  
  86.     toTime.value = hours > 12 ? (hours-12) + minutes + meridiem : hours + minutes + meridiem;
  87.     toTime.font = font;
  88.     toTime.color = color;
  89.     toTime.opacity = opacity;
  90.  
  91.     toTime.left = targetWidth - toTime.width;
  92.     toTime.top = 0;
  93.  
  94.  
  95.     toDate.value = currentDate.getDate();
  96.     toDate.font = font;
  97.     toDate.color = color;
  98.     toDate.opacity = opacity;
  99.  
  100.     toDate.left = toTime.left;
  101.     toDate.top = 90; // - negativeVerticalMargin;
  102.  
  103.  
  104.     toMonth.value = m[currentDate.getMonth()];
  105.     toMonth.font = font;
  106.     toMonth.color = color;
  107.     toMonth.opacity = opacity;
  108.  
  109.     toMonth.left = toDate.left + toDate.width;
  110.     // goofly little formula here to goose the month riiiigh up to but not touching the time, for most fonts
  111.     toMonth.top = toDate.top + 4 + (toDate.top * 0.05) ; //95;
  112.  
  113.  
  114.     toDay.value = d[currentDate.getDay()];
  115.     toDay.font = font;
  116.     toDay.color = color;
  117.     toDay.opacity = opacity;
  118.  
  119.     toDay.left = toMonth.left;
  120.     toDay.top = toMonth.top + 41; //135;
  121.  
  122.  
  123.     console.log('    width: '+toTime.width+', height: '+toDay.top + toDay.height);
  124.     console.log('    toTime: x='+toTime.left+', y='+toTime.top+', h='+toTime.height+', w='+toTime.width);
  125.     
  126.  
  127.     if (autoreload) {
  128.         setTimeout(function() { refresh(true); }, 60*1000);
  129.     }
  130. }
  131.