Commit 7f68833c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added favourite programs & general code celanup

parent 6e13cb0e
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
FETCH_URL = 'programs.php' FETCH_URL = 'programs.php'
HOUR_WIDTH = 200 HOUR_WIDTH = 200
CHANNEL_LABEL_WIDTH = 180 CHANNEL_LABEL_WIDTH = 180
STORAGE_NAME = 'tvgids-channels' STORAGE_CHANNELS = 'tvgids-channels'
STORAGE_PROGRAMS = 'tvgids-programs'
#SCROLL_MULTIPLIER = HOUR_WIDTH #SCROLL_MULTIPLIER = HOUR_WIDTH
# #
...@@ -19,12 +20,18 @@ format_time = (time) -> ...@@ -19,12 +20,18 @@ format_time = (time) ->
date = new Date(time) date = new Date(time)
zeropad(date.getHours()) + ':' + zeropad(date.getMinutes()) zeropad(date.getHours()) + ':' + zeropad(date.getMinutes())
store_list = (name, values) -> localStorage.setItem(name, values.join(';'))
load_stored_list = (name, def) ->
store_list(name, def) if not localStorage.hasOwnProperty(name)
value = localStorage.getItem(name)
if value.length > 0 then value.split(';') else []
# #
# Models & collections # Models & collections
# #
Channel = Backbone.Model.extend( Channel = Backbone.Model.extend(
defaults: -> defaults:
id: null id: null
name: 'Some channel' name: 'Some channel'
visible: true visible: true
...@@ -32,7 +39,7 @@ Channel = Backbone.Model.extend( ...@@ -32,7 +39,7 @@ Channel = Backbone.Model.extend(
) )
Program = Backbone.Model.extend( Program = Backbone.Model.extend(
defaults: -> defaults:
title: 'Some program' title: 'Some program'
genre: '' genre: ''
sort: '' sort: ''
...@@ -46,24 +53,21 @@ ChannelList = Backbone.Collection.extend( ...@@ -46,24 +53,21 @@ ChannelList = Backbone.Collection.extend(
model: Channel model: Channel
#comparator: (a, b) -> parseInt(a.get('id')) - parseInt(b.get('id')) #comparator: (a, b) -> parseInt(a.get('id')) - parseInt(b.get('id'))
initialize: ->
@listenTo(Settings, 'change:favourite_channels', @propagateVisible)
fetch: -> fetch: ->
@reset(CHANNELS) @reset(CHANNELS)
#@reset(CHANNELS.slice(0,3))
#$.getJSON('channels.php', (data) => @reset(data)) #$.getJSON('channels.php', (data) => @reset(data))
@propagateVisible() @propagateVisible()
propagateVisible: -> propagateVisible: ->
visible = if localStorage.hasOwnProperty(STORAGE_NAME) \ visible = Settings.get('favourite_channels')
then localStorage.getItem(STORAGE_NAME).split(',') else @pluck('id')
for id in visible for id in visible
if @length and not @findWhere(id: id)
console.log 'not found:', id, typeof id, typeof @at(0).get('id')
@findWhere(id: id)?.set(visible: true) @findWhere(id: id)?.set(visible: true)
for id in _.difference(@pluck('id'), visible) for id in _.difference(@pluck('id'), visible)
if not @findWhere(id: id)
console.log 'not found:', id
@findWhere(id: id)?.set(visible: false) @findWhere(id: id)?.set(visible: false)
fetchPrograms: (day) -> fetchPrograms: (day) ->
...@@ -73,7 +77,7 @@ ChannelList = Backbone.Collection.extend( ...@@ -73,7 +77,7 @@ ChannelList = Backbone.Collection.extend(
(channels) -> (channels) ->
_.each channels, (programs, id) -> _.each channels, (programs, id) ->
channel = Channels.findWhere(id: id) channel = Channels.findWhere(id: id)
channel.set('programs', ( channel.set(programs: (
new Program( new Program(
title: p.titel title: p.titel
genre: p.genre genre: p.genre
...@@ -115,12 +119,19 @@ ProgramView = Backbone.View.extend( ...@@ -115,12 +119,19 @@ ProgramView = Backbone.View.extend(
tagName: 'div' tagName: 'div'
className: 'program' className: 'program'
events:
'click .favlink': 'toggleFavourite'
initialize: -> initialize: ->
@$el.text(@model.get('title')) $('<span class="title"/>').text(@model.get('title')).appendTo(@el)
from = format_time(@model.get('start')) from = format_time(@model.get('start'))
to = format_time(@model.get('end')) to = format_time(@model.get('end'))
@$el.attr('title', @model.get('title') + " (#{from} - #{to})") @$el.attr('title', @model.get('title') + " (#{from} - #{to})")
@$fav = $('<a class="favlink icon-heart"/>').appendTo(@el)
@$fav.attr('title', 'Als favoriet instellen')
@updateFavlink()
left = time2px(Math.max(0, seconds_today(@model.get('start')))) left = time2px(Math.max(0, seconds_today(@model.get('start'))))
width = time2px(seconds_today(@model.get('end'))) - left width = time2px(seconds_today(@model.get('end'))) - left
@$el.css( @$el.css(
...@@ -128,6 +139,15 @@ ProgramView = Backbone.View.extend( ...@@ -128,6 +139,15 @@ ProgramView = Backbone.View.extend(
width: (width - 10) + 'px' width: (width - 10) + 'px'
) )
@listenTo(Settings, 'change:favourite_programs', @updateFavlink)
toggleFavourite: ->
Settings.toggleFavouriteProgram(@model.get('title'))
updateFavlink: ->
isfav = Settings.isFavouriteProgram(@model.get('title'))
@$fav.toggleClass('favourite', isfav)
render: -> render: ->
if @model.get('start') <= Date.now() if @model.get('start') <= Date.now()
if @model.get('end') < Date.now() if @model.get('end') < Date.now()
...@@ -162,6 +182,7 @@ AppView = Backbone.View.extend( ...@@ -162,6 +182,7 @@ AppView = Backbone.View.extend(
el: $('#guide') el: $('#guide')
events: events:
# TODO: move to initialize
'click #yesterday': -> @loadDay(-1) 'click #yesterday': -> @loadDay(-1)
'click #today': -> @loadDay(0) 'click #today': -> @loadDay(0)
'click #tomorrow': -> @loadDay(1) 'click #tomorrow': -> @loadDay(1)
...@@ -217,8 +238,26 @@ AppView = Backbone.View.extend( ...@@ -217,8 +238,26 @@ AppView = Backbone.View.extend(
# #
Settings = new (Backbone.Model.extend( Settings = new (Backbone.Model.extend(
defaults: -> defaults:
day: 0 day: 0
favourite_channels: load_stored_list(STORAGE_CHANNELS,
_.pluck(CHANNELS, 'id'))
favourite_programs: load_stored_list(STORAGE_PROGRAMS, [])
toggleFavouriteProgram: (title) ->
list = @get('favourite_programs')
if @isFavouriteProgram(title)
list.splice(list.indexOf(title), 1)
else
list.push(title)
@attributes.favourite_programs = list
@trigger('change:favourite_programs')
store_list(STORAGE_PROGRAMS, list)
isFavouriteProgram: (title) ->
_.contains(@get('favourite_programs'), title)
))() ))()
Channels = new ChannelList() Channels = new ChannelList()
App = new AppView() App = new AppView()
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
<head> <head>
<title>TV gids</title> <title>TV gids</title>
<link href="style.css" type="text/css" rel="stylesheet"> <link href="style.css" type="text/css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet">
</head> </head>
<body> <body>
<div id="guide" class="guide"> <div id="guide" class="guide">
......
STORAGE_NAME = 'tvgids-channels' STORAGE_NAME = 'tvgids-channels'
visible = if localStorage.hasOwnProperty(STORAGE_NAME) \ visible = if localStorage.hasOwnProperty(STORAGE_NAME) \
then localStorage.getItem(STORAGE_NAME).split(',') \ then localStorage.getItem(STORAGE_NAME).split(';') \
else _.pluck(CHANNELS, 'id') else _.pluck(CHANNELS, 'id')
_.each CHANNELS, (channel) -> _.each CHANNELS, (channel) ->
...@@ -20,7 +20,7 @@ _.each CHANNELS, (channel) -> ...@@ -20,7 +20,7 @@ _.each CHANNELS, (channel) ->
$('#select-channels').submit (e) -> $('#select-channels').submit (e) ->
e.preventDefault() e.preventDefault()
selected = ($(i).val() for i in $('input', @) when $(i).is(':checked')) selected = ($(i).val() for i in $('input', @) when $(i).is(':checked'))
localStorage.setItem(STORAGE_NAME, selected.join(',')) localStorage.setItem(STORAGE_NAME, selected.join(';'))
setall = (c) -> $('#select-channels input').prop('checked', c).change() setall = (c) -> $('#select-channels input').prop('checked', c).change()
$('#select-all').click -> setall(true) $('#select-all').click -> setall(true)
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
STORAGE_NAME = 'tvgids-channels'; STORAGE_NAME = 'tvgids-channels';
visible = localStorage.hasOwnProperty(STORAGE_NAME) ? localStorage.getItem(STORAGE_NAME).split(',') : _.pluck(CHANNELS, 'id'); visible = localStorage.hasOwnProperty(STORAGE_NAME) ? localStorage.getItem(STORAGE_NAME).split(';') : _.pluck(CHANNELS, 'id');
_.each(CHANNELS, function(channel) { _.each(CHANNELS, function(channel) {
var elem, input, is_visible; var elem, input, is_visible;
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
} }
return _results; return _results;
}).call(this); }).call(this);
return localStorage.setItem(STORAGE_NAME, selected.join(',')); return localStorage.setItem(STORAGE_NAME, selected.join(';'));
}); });
setall = function(c) { setall = function(c) {
......
...@@ -99,6 +99,24 @@ html, body ...@@ -99,6 +99,24 @@ html, body
background-color: #ddd background-color: #ddd
color: #444 color: #444
.favlink
position: absolute
right: 10px
top: 10px
visibility: hidden
color: #fff
&:hover
color: #bcbcbc
&.favourite
color: orange
visibility: visible
&:hover .favlink
visibility: visible
.indicator .indicator
position: absolute position: absolute
width: 1px width: 1px
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment