core-utils.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;(function() {
  2. var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June',
  3. 'July', 'August', 'September', 'October', 'November', 'December'];
  4. Nokia.GBCUtils = {
  5. /*
  6. * Resize an image accordingly to the width of its parent DOM node
  7. */
  8. normalizeImageWidth: function(event) {
  9. var imgElement = event.target;
  10. var imgWidth = imgElement.offsetWidth;
  11. imgElement.style.display = 'none';
  12. var maxImgWidth = imgElement.parentNode.offsetWidth - 10;
  13. if(imgWidth > maxImgWidth)
  14. {
  15. var w = maxImgWidth;
  16. var h = w * imgElement.offsetHeight / imgElement.offsetWidth;
  17. imgElement.style.width = w + 'px';
  18. imgElement.style.height = h + 'px';
  19. }
  20. imgElement.style.display = '';
  21. },
  22. /*
  23. * Parse the weather date string, as returned from the weather Web service
  24. */
  25. parseWeatherDate: function(dateString)
  26. {
  27. var year = parseInt(dateString.substring(0, 4));
  28. var month = parseInt(dateString.substring(5, 7), 10);
  29. var day = parseInt(dateString.substring(8, 10), 10);
  30. var hour = parseInt(dateString.substring(11, 13), 10);
  31. var minute = parseInt(dateString.substring(14, 16), 10);
  32. var second = parseInt(dateString.substring(17, 19), 10);
  33. var d = new Date(year, month, day, hour, minute, second);
  34. return d;
  35. },
  36. /*
  37. * The following section defines a set of date-formatting utility methods
  38. */
  39. formatLongDate: function(date)
  40. {
  41. return date.getDate() + " " + MONTHS[date.getMonth()] + " " + date.getFullYear();
  42. },
  43. formatDate: function(date)
  44. {
  45. return this._normalize(date.getDate()) + "/" + this._normalize(date.getMonth());
  46. },
  47. formatDateTime: function(date)
  48. {
  49. return this._normalize(date.getDate()) + "/" + this._normalize(date.getMonth()) + "/" + date.getFullYear() + " at " +
  50. this._normalize(date.getHours()) + ":" + this._normalize(date.getMinutes());
  51. },
  52. formatTime: function(date)
  53. {
  54. return this._normalize(date.getHours()) + ":" + this._normalize(date.getMinutes());
  55. },
  56. _normalize: function(num)
  57. {
  58. return num < 10 ? '0' + num : num;
  59. }
  60. };
  61. })();