guide.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #
  2. # Config
  3. #
  4. HOUR_WIDTH = 200
  5. CHANNEL_LABEL_WIDTH = 180
  6. STORAGE_CHANNELS = 'tvgids-channels'
  7. STORAGE_PROGRAMS = 'tvgids-programs'
  8. HOURS_BEFORE = HOURS_AFTER = 2
  9. DEFAULT_CHANNELS = [1, 2, 3, 4, 31, 46, 92, 36, 37, 34, 29, 18, 91]
  10. DEFAULT_CHANNELS = _.map(DEFAULT_CHANNELS, String)
  11. DETAILS_WINDOW_PADDING = 22 # top/bottom margin between details div and window edge
  12. #
  13. # Utils
  14. #
  15. day_start = -> (new Date()).setHours(0, 0, 0, 0)
  16. day_offset = -> Settings.get('day') * 24 * 60 * 60 * 1000
  17. seconds_today = (time) -> (time - day_start() - day_offset()) / 1000
  18. time2px = (seconds) -> HOUR_WIDTH / 3600 * seconds
  19. zeropad = (digit) -> if digit < 10 then '0' + digit else String(digit)
  20. format_time = (time) ->
  21. date = new Date(time)
  22. zeropad(date.getHours()) + ':' + zeropad(date.getMinutes())
  23. parse_date = (str) ->
  24. [date, time] = str.split(' ')
  25. [year, month, day] = date.split('-')
  26. [hours, minutes, seconds] = time.split(':')
  27. (new Date(year, month - 1, day, hours, minutes, seconds)).getTime()
  28. store_list = (name, values) -> localStorage.setItem(name, values.join(';'))
  29. load_stored_list = (name, def) ->
  30. store_list(name, def) if not localStorage.hasOwnProperty(name)
  31. value = localStorage.getItem(name)
  32. if value.length > 0 then value.split(';') else []
  33. #
  34. # Models & collections
  35. #
  36. Channel = Backbone.Model.extend
  37. defaults:
  38. id: null
  39. name: 'Some channel'
  40. visible: true
  41. programs: []
  42. Program = Backbone.Model.extend
  43. defaults:
  44. id: null
  45. title: 'Some program'
  46. genre: ''
  47. sort: ''
  48. start: 0
  49. end: 0
  50. article_id: null
  51. article_title: null
  52. ChannelList = Backbone.Collection.extend
  53. model: Channel
  54. #comparator: (a, b) -> parseInt(a.get('id')) - parseInt(b.get('id'))
  55. initialize: ->
  56. @listenTo(Settings, 'change:favourite_channels', @propagateVisible)
  57. fetch: ->
  58. @reset(CHANNELS)
  59. #$.getJSON('channels.php', (data) => @reset(data))
  60. @propagateVisible()
  61. propagateVisible: ->
  62. visible = Settings.get('favourite_channels')
  63. for id in visible
  64. @findWhere(id: id)?.set(visible: true)
  65. for id in _.difference(@pluck('id'), visible)
  66. @findWhere(id: id)?.set(visible: false)
  67. fetchPrograms: (day) ->
  68. # Sometimes a program list is an object (PHP's json_encode() is
  69. # probably given an associative array)
  70. to_array = (o) -> if o.length? then o else _.map(o, ((v, k) -> v))
  71. $('#loading-screen').show()
  72. $.getJSON(
  73. 'programs.php'
  74. channels: Settings.get('favourite_channels').join(','), day: day
  75. (channels) ->
  76. _.each channels, (programs, id) ->
  77. channel = Channels.findWhere(id: id)
  78. channel.set(programs: (
  79. new Program(
  80. id: p.db_id
  81. title: p.titel
  82. genre: p.genre
  83. sort: p.soort
  84. start: parse_date(p.datum_start)
  85. end: parse_date(p.datum_end)
  86. article_id: p.artikel_id
  87. article_title: p.artikel_titel
  88. ) for p in to_array(programs)
  89. )) if channel?
  90. $('#loading-screen').hide()
  91. )
  92. #
  93. # Views
  94. #
  95. ChannelView = Backbone.View.extend
  96. tagName: 'div'
  97. className: 'channel'
  98. initialize: ->
  99. @listenTo(@model, 'change:programs', @render)
  100. @listenTo(@model, 'change:visible', @toggleVisible)
  101. #@$el.text(@model.get('title'))
  102. render: ->
  103. @$el.empty()
  104. _.each @model.get('programs'), (program) =>
  105. view = new ProgramView(model: program)
  106. view.render()
  107. @$el.append(view.el)
  108. toggleVisible: ->
  109. @$el.toggle(@model.get('visible'))
  110. ProgramView = Backbone.View.extend
  111. tagName: 'div'
  112. className: 'program'
  113. events:
  114. 'click .favlink': 'toggleFavourite'
  115. 'click': -> Settings.set(selected_program: @model.get('id'))
  116. initialize: ->
  117. $('<span class="title"/>').text(@model.get('title')).appendTo(@el)
  118. from = format_time(@model.get('start'))
  119. to = format_time(@model.get('end'))
  120. @$el.attr('title', @model.get('title') + " (#{from} - #{to})")
  121. @$fav = $('<a class="favlink icon-heart"/>').appendTo(@el)
  122. @$fav.attr('title', 'Als favoriet instellen')
  123. @updateFavlink()
  124. left = time2px(Math.max(-HOURS_BEFORE * 60 * 60,
  125. seconds_today(@model.get('start'))))
  126. width = time2px(seconds_today(@model.get('end'))) - left
  127. @$el.css(
  128. left: ((HOURS_BEFORE * HOUR_WIDTH) + left) + 'px'
  129. width: (width - 10) + 'px'
  130. )
  131. @listenTo(Settings, 'change:favourite_programs', @updateFavlink)
  132. @listenTo(Clock, 'tick', @render)
  133. toggleFavourite: (e) ->
  134. Settings.toggleFavouriteProgram(@model.get('title'))
  135. e.stopPropagation()
  136. updateFavlink: ->
  137. isfav = Settings.isFavouriteProgram(@model.get('title'))
  138. @$el.toggleClass('favourite', isfav)
  139. render: ->
  140. if @model.get('start') <= Date.now()
  141. if @model.get('end') < Date.now()
  142. @$el.removeClass('current').addClass('past')
  143. @stopListening(Clock, 'tick')
  144. else
  145. @$el.addClass('current')
  146. ChannelLabelsView = Backbone.View.extend
  147. el: $('#channel-labels')
  148. initialize: (options) ->
  149. @listenTo(Channels, 'reset', @addChannels)
  150. @listenTo(options.app, 'scroll', @moveTop)
  151. addChannels: ->
  152. @$el.empty()
  153. Channels.each((channel) ->
  154. elem = $('<div id="label-' + channel.get('id') + '" class="label"/>')
  155. elem.html(channel.get('name')).toggle(channel.get('visible')).appendTo(@el)
  156. @listenTo(channel, 'change:visible', -> @toggleVisible(channel))
  157. , @)
  158. moveTop: (delta) ->
  159. @$el.css('top', (@$el.position().top - delta) + 'px')
  160. toggleVisible: (channel) ->
  161. @$('#label-' + channel.get('id')).toggle(channel.get('visible'))
  162. ProgramDetailsView = Backbone.View.extend
  163. el: $('#program-details')
  164. template: _.template($('#details-template').html())
  165. events:
  166. 'click .bg': -> Settings.set(selected_program: null)
  167. initialize: (options) ->
  168. @listenTo(Settings, 'change:selected_program', @toggleDetails)
  169. @setBounds()
  170. $(window).resize(=> @setBounds())
  171. toggleDetails: ->
  172. id = Settings.get('selected_program')
  173. if id
  174. $('#loading-screen').show()
  175. $.getJSON(
  176. 'details.php'
  177. id: id
  178. (data) =>
  179. $('#loading-screen').hide()
  180. @$el.show()
  181. @$('.content').html(@template(_.extend(id: id, data)))
  182. @alignMiddle()
  183. # Align again after images are loaded
  184. @$('.content img').load(-> $(@).css(height: 'auto'))
  185. @$('.content img').load(=> @alignMiddle())
  186. )
  187. else
  188. @$el.hide()
  189. @$('.content').empty()
  190. setBounds: ->
  191. max = $(window).height() - 2 * DETAILS_WINDOW_PADDING
  192. @$('.content').css(maxHeight: max)
  193. @alignMiddle()
  194. alignMiddle: ->
  195. height = @$('.content').outerHeight()
  196. @$('.content').css(marginTop: "-#{height / 2}px")
  197. AppView = Backbone.View.extend
  198. el: $('#guide')
  199. events:
  200. # TODO: move to initialize
  201. 'scroll': 'moveTimeline'
  202. initialize: ->
  203. @prevScrollTop = null
  204. @listenTo(Channels, 'reset', @addChannels)
  205. @listenTo(Settings, 'change:day', @fetchPrograms)
  206. @labelview = new ChannelLabelsView(app: @)
  207. @detailsview = new ProgramDetailsView(app: @)
  208. #@$el.smoothTouchScroll(
  209. # scrollableAreaClass: 'channels'
  210. # scrollWrapperClass: 'guide'
  211. #)
  212. #@iscroll = new iScroll('guide', vScroll: false, hScrollbar: true)
  213. $('#beforeyesterday').click(-> Settings.set(day: -2))
  214. $('#yesterday').click(-> Settings.set(day: -1))
  215. $('#today').click(-> Settings.set(day: 0))
  216. $('#tomorrow').click(-> Settings.set(day: 1))
  217. $('#overmorrow').click(-> Settings.set(day: 2))
  218. $('#help').click((e) -> e.stopPropagation(); $('#help-popup').show())
  219. $(document).click(-> $('#help-popup').hide())
  220. Channels.fetch()
  221. @centerIndicator()
  222. @updateIndicator()
  223. @listenTo(Clock, 'tick', @updateIndicator)
  224. addChannels: ->
  225. @$('.channels > .channel').remove()
  226. Channels.each((channel) ->
  227. view = new ChannelView(model: channel)
  228. view.render()
  229. @$('.channels').append(view.el)
  230. , @)
  231. @fetchPrograms()
  232. updateIndicator: ->
  233. if Settings.get('day') == 0
  234. left = time2px(seconds_today(Date.now())) + CHANNEL_LABEL_WIDTH - 1
  235. @$('.indicator')
  236. .css(left: ((HOURS_BEFORE * HOUR_WIDTH) + left) + 'px')
  237. .height(@$('.channels').height() - 2)
  238. .show()
  239. else
  240. @$('.indicator').hide()
  241. centerIndicator: ->
  242. @$el.scrollLeft(@$('.indicator').position().left - @$el.width() / 2)
  243. fetchPrograms: ->
  244. day = Settings.get('day')
  245. Channels.fetchPrograms(day)
  246. @updateIndicator()
  247. $('.navbar .active').removeClass('active')
  248. $($('.navbar .navitem')[day + 2]).addClass('active')
  249. moveTimeline: ->
  250. if @$el.scrollTop() != @prevScrollTop
  251. @trigger('scroll', @$el.scrollTop() - @prevScrollTop)
  252. @prevScrollTop = @$el.scrollTop()
  253. @$('.timeline').css('top', (@$el.scrollTop() + 37) + 'px')
  254. #
  255. # Main
  256. #
  257. Settings = new do Backbone.Model.extend
  258. defaults:
  259. day: 0
  260. favourite_channels: load_stored_list(STORAGE_CHANNELS, DEFAULT_CHANNELS)
  261. favourite_programs: load_stored_list(STORAGE_PROGRAMS, [])
  262. selected_program: null
  263. toggleFavouriteProgram: (title) ->
  264. list = @get('favourite_programs')
  265. if @isFavouriteProgram(title)
  266. list.splice(list.indexOf(title), 1)
  267. else
  268. list.push(title)
  269. @attributes.favourite_programs = list
  270. @trigger('change:favourite_programs')
  271. store_list(STORAGE_PROGRAMS, list)
  272. isFavouriteProgram: (title) ->
  273. _.contains(@get('favourite_programs'), title)
  274. Clock = new do ->
  275. _.extend(@, Backbone.Events)
  276. setInterval((=> @trigger('tick')), 60 * 60 * 1000 / HOUR_WIDTH)
  277. Channels = new ChannelList()
  278. App = new AppView()