guide.coffee 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #
  2. # Config
  3. #
  4. FETCH_URL = 'programs.php'
  5. HOUR_WIDTH = 200
  6. CHANNEL_LABEL_WIDTH = 180
  7. STORAGE_NAME = 'tvgids-channels'
  8. #SCROLL_MULTIPLIER = HOUR_WIDTH
  9. #
  10. # Utils
  11. #
  12. seconds_today = (time) -> (time - (new Date()).setHours(0, 0, 0, 0)) / 1000
  13. time2px = (seconds) -> HOUR_WIDTH / 3600 * seconds
  14. zeropad = (digit) -> if digit < 10 then '0' + digit else String(digit)
  15. format_time = (time) ->
  16. date = new Date(time)
  17. zeropad(date.getHours()) + ':' + zeropad(date.getMinutes())
  18. #
  19. # Models & collections
  20. #
  21. Channel = Backbone.Model.extend(
  22. defaults: ->
  23. id: null
  24. name: 'Some channel'
  25. visible: true
  26. programs: []
  27. )
  28. Program = Backbone.Model.extend(
  29. defaults: ->
  30. title: 'Some program'
  31. genre: ''
  32. sort: ''
  33. start: 0
  34. end: 0
  35. article_id: null
  36. article_title: null
  37. )
  38. ChannelList = Backbone.Collection.extend(
  39. model: Channel
  40. comparator: (a, b) -> parseInt(a.get('id')) - parseInt(b.get('id'))
  41. fetch: ->
  42. @reset(CHANNELS)
  43. #@reset(CHANNELS.slice(0,3))
  44. #$.getJSON('channels.php', (data) => @reset(data))
  45. @propagateVisible()
  46. propagateVisible: ->
  47. visible = if localStorage.hasOwnProperty(STORAGE_NAME) \
  48. then localStorage.getItem(STORAGE_NAME).split(',') else @pluck('id')
  49. for id in visible
  50. if @length and not @findWhere(id: id)
  51. console.log 'not found:', id, typeof id, typeof @at(0).get('id')
  52. @findWhere(id: id)?.set(visible: true)
  53. for id in _.difference(@pluck('id'), visible)
  54. if not @findWhere(id: id)
  55. console.log 'not found:', id
  56. @findWhere(id: id)?.set(visible: false)
  57. fetchPrograms: (day) ->
  58. $.getJSON(
  59. FETCH_URL
  60. channels: @pluck('id').join(','), day: day
  61. (channels) ->
  62. _.each channels, (programs, id) ->
  63. channel = Channels.findWhere(id: id)
  64. channel.set('programs', (
  65. new Program(
  66. title: p.titel
  67. genre: p.genre
  68. sort: p.soort
  69. start: Date.parse(p.datum_start)
  70. end: Date.parse(p.datum_end)
  71. article_id: p.artikel_id
  72. article_title: p.artikel_titel
  73. ) for p in programs
  74. ))
  75. )
  76. )
  77. #
  78. # Views
  79. #
  80. ChannelView = Backbone.View.extend(
  81. tagName: 'div'
  82. className: 'channel'
  83. initialize: ->
  84. @listenTo(@model, 'change:programs', @render)
  85. @listenTo(@model, 'change:visible', @toggleVisible)
  86. #@$el.text(@model.get('title'))
  87. render: ->
  88. @$el.empty()
  89. _.each @model.get('programs'), (program) =>
  90. view = new ProgramView(model: program)
  91. view.render()
  92. @$el.append(view.el)
  93. toggleVisible: ->
  94. @$el.toggle(@model.get('visible'))
  95. )
  96. ProgramView = Backbone.View.extend(
  97. tagName: 'div'
  98. className: 'program'
  99. initialize: ->
  100. @$el.text(@model.get('title'))
  101. from = format_time(@model.get('start'))
  102. to = format_time(@model.get('end'))
  103. @$el.attr('title', @model.get('title') + " (#{from} - #{to})")
  104. left = time2px(Math.max(0, seconds_today(@model.get('start'))))
  105. width = time2px(seconds_today(@model.get('end'))) - left
  106. @$el.css(
  107. left: left + 'px'
  108. width: (width - 10) + 'px'
  109. )
  110. render: ->
  111. if @model.get('start') <= Date.now()
  112. if @model.get('end') < Date.now()
  113. @$el.removeClass('current').addClass('past')
  114. else
  115. @$el.addClass('current')
  116. )
  117. ChannelLabelView = Backbone.View.extend(
  118. el: $('.channel-labels')
  119. initialize: (options) ->
  120. @listenTo(Channels, 'reset', @addChannels)
  121. @listenTo(options.app, 'scroll', @moveTop)
  122. addChannels: ->
  123. @$el.empty()
  124. Channels.each((channel) ->
  125. elem = $('<div id="label-' + channel.get('id') + '" class="label"/>')
  126. elem.html(channel.get('name')).toggle(channel.get('visible')).appendTo(@el)
  127. @listenTo(channel, 'change:visible', -> @toggleVisible(channel))
  128. , @)
  129. moveTop: (delta) ->
  130. @$el.css('top', (@$el.position().top - delta) + 'px')
  131. toggleVisible: (channel) ->
  132. @$('#label-' + channel.get('id')).toggle(channel.get('visible'))
  133. )
  134. AppView = Backbone.View.extend(
  135. el: $('#guide')
  136. events:
  137. 'click #yesterday': -> @loadDay(-1)
  138. 'click #today': -> @loadDay(0)
  139. 'click #tomorrow': -> @loadDay(1)
  140. 'scroll': 'moveTimeline'
  141. moveTimeline: ->
  142. if @$el.scrollTop() != @prevScrollTop
  143. @trigger('scroll', @$el.scrollTop() - @prevScrollTop)
  144. @prevScrollTop = @$el.scrollTop()
  145. @$('.timeline').css('top', (@$el.scrollTop() + 37) + 'px')
  146. initialize: ->
  147. @prevScrollTop = null
  148. @listenTo(Channels, 'reset', @addChannels)
  149. @listenTo(Settings, 'change:day', @fetchPrograms)
  150. @labelview = new ChannelLabelView(app: @)
  151. @updateIndicator()
  152. @centerIndicator()
  153. Channels.fetch()
  154. setInterval((=> @updateIndicator()), 3600000 / HOUR_WIDTH)
  155. addChannels: ->
  156. @$('.channels > .channel').remove()
  157. Channels.each((channel) ->
  158. view = new ChannelView(model: channel)
  159. view.render()
  160. @$('.channels').append(view.el)
  161. , @)
  162. @$('.indicator').height(@$('.channels').height())
  163. @fetchPrograms()
  164. loadDay: (day) ->
  165. Settings.set(day: day)
  166. @$('.navbar .active').removeClass('active')
  167. $(@$('.navbar .navitem')[day + 1]).addClass('active')
  168. updateIndicator: ->
  169. left = time2px(seconds_today(Date.now())) + CHANNEL_LABEL_WIDTH - 1
  170. @$('.indicator').css('left', left + 'px')
  171. centerIndicator: ->
  172. @$el.scrollLeft(@$('.indicator').position().left - @$el.width() / 2)
  173. fetchPrograms: ->
  174. Channels.fetchPrograms(Settings.get('day'))
  175. )
  176. #
  177. # Main
  178. #
  179. Settings = new (Backbone.Model.extend(
  180. defaults: ->
  181. day: 0
  182. ))()
  183. Channels = new ChannelList()
  184. App = new AppView()