lctv-badges.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /** lctv-badges.js
  2. * loads per-style badge images into per-style placeholder img tags
  3. * also assigns mouseover and mousedown images for "flip" badge styles
  4. * expects 'data-channel' param to be set on an img with a canonical per-style id
  5. * optionally wraps image in anchor if 'data-link' param exists and is true
  6. */
  7. var BADGES_ROOT_URL = "https://codiad-billauger.rhcloud.com" ;
  8. var BADGES_IMG_URL = BADGES_ROOT_URL + '/img/' ;
  9. var BADGES_PHP_URL = BADGES_ROOT_URL + '/badges/' ;
  10. var LCTV_URL = "https://www.liveedu.tv/" ;
  11. var CHANNEL_KEY = 'channel' ;
  12. var STYLE_KEY = 'style' ;
  13. var STATUS_V3_STYLE = 'online-status-v3' ;
  14. var BADGE_FLIP_STYLES = [ STATUS_V3_STYLE ] ;
  15. var BADGES_DATA = {} ;
  16. var BADGE_IMG_ID_KEY = 'badge-img-id'
  17. var BADGE_IMGS_KEY = 'badge-img-urls'
  18. var ONLINE_IDLE_IMG_KEY = 'online-img' ;
  19. var ONLINE_HOVER_IMG_KEY = 'online-hover-img' ;
  20. var ONLINE_PUSHED_IMG_KEY = 'online-pushed-img' ;
  21. var OFFLINE_IDLE_IMG_KEY = 'offline-img' ;
  22. var OFFLINE_HOVER_IMG_KEY = 'offline-hover-img' ;
  23. var OFFLINE_PUSHED_IMG_KEY = 'offline-pushed-img' ;
  24. BADGE_FLIP_STYLES.forEach(function(flip_style)
  25. {
  26. var badge_data = {} ;
  27. var badge_urls = {} ;
  28. BADGES_DATA[flip_style] = badge_data ;
  29. badge_data[BADGE_IMG_ID_KEY] = 'lctv-status-img' ;
  30. badge_data[BADGE_IMGS_KEY ] = badge_urls ;
  31. badge_urls[ONLINE_IDLE_IMG_KEY ] = BADGES_IMG_URL + 'v3/lctv-online.png' ;
  32. badge_urls[ONLINE_HOVER_IMG_KEY ] = BADGES_IMG_URL + 'v3/lctv-online-hover.png' ;
  33. badge_urls[ONLINE_PUSHED_IMG_KEY ] = BADGES_IMG_URL + 'v3/lctv-online-pushed.png' ;
  34. badge_urls[OFFLINE_IDLE_IMG_KEY ] = BADGES_IMG_URL + 'v3/lctv-offline.png' ;
  35. badge_urls[OFFLINE_HOVER_IMG_KEY ] = BADGES_IMG_URL + 'v3/lctv-offline-hover.png' ;
  36. badge_urls[OFFLINE_PUSHED_IMG_KEY] = BADGES_IMG_URL + 'v3/lctv-offline-pushed.png' ;
  37. }) ;
  38. function loadImages()
  39. {
  40. Object.keys(BADGES_DATA).forEach(function(badge_style)
  41. {
  42. var badge_img_id = BADGES_DATA[badge_style][BADGE_IMG_ID_KEY] ;
  43. var badge_img = document.getElementById(badge_img_id) ;
  44. if (!badge_img) return ;
  45. setBadgeUrl(badge_img , badge_style) ;
  46. setFlipUrls(badge_img , badge_style) ;
  47. wrapLink (badge_img) ;
  48. }) ;
  49. }
  50. function setBadgeUrl(badge_img , badge_style)
  51. {
  52. var channel_name = badge_img.dataset.channel ;
  53. var style_param = '?' + STYLE_KEY + '=' + badge_style ;
  54. var channel_param = '&' + CHANNEL_KEY + '=' + channel_name ;
  55. badge_img.src = BADGES_PHP_URL + style_param + channel_param ;
  56. }
  57. function setFlipUrls(badge_img , badge_style)
  58. {
  59. var is_flip_style = !!(~BADGE_FLIP_STYLES.indexOf(badge_style)) ;
  60. if (!is_flip_style) return ;
  61. // preload flip images
  62. Object.keys(BADGES_DATA[badge_style][BADGE_IMGS_KEY]).forEach(function(img_key)
  63. {
  64. var img_url = BADGES_DATA[badge_style][BADGE_IMGS_KEY][img_key] ;
  65. var head = document.getElementsByTagName('head')[0] ;
  66. var badge_img = document.createElement('link') ;
  67. badge_img.setAttribute('rel' , 'prefetch') ;
  68. badge_img.setAttribute('href' , img_url) ;
  69. head.appendChild(badge_img) ;
  70. }) ;
  71. // attach event handlers
  72. badge_img.onmouseover = function() { setBadgeUrl(badge_img , badge_style + '-hover' ) ; }
  73. badge_img.onmouseleave = function() { setBadgeUrl(badge_img , badge_style ) ; }
  74. badge_img.onmousedown = function() { setBadgeUrl(badge_img , badge_style + '-pushed') ; }
  75. badge_img.onmouseup = function() { setBadgeUrl(badge_img , badge_style + '-hover' ) ; }
  76. }
  77. function wrapLink(badge_img)
  78. {
  79. var channel_name = badge_img.dataset.channel ;
  80. var wrap_link = badge_img.dataset.link !== 'false' ;
  81. var badge_a = document.createElement('a') ;
  82. badge_a .id = badge_img.id.substring(0 , badge_img.id.length - 3) + 'a' ;
  83. badge_a .href = LCTV_URL + channel_name ;
  84. if (!wrap_link) return ;
  85. badge_img.parentNode.replaceChild(badge_a , badge_img) ;
  86. badge_a .appendChild (badge_img) ;
  87. }
  88. // disable auto-load when included from demos
  89. if (!(~BADGES_ROOT_URL.indexOf(window.location.hostname)))
  90. window.onload = function() { loadImages() ; window.setInterval(loadImages , 60000) ; } ;