Files
ugc/script/ugc.js
2016-06-01 22:22:46 +02:00

626 lines
18 KiB
JavaScript
Executable File

function Film(oSettings)
{
var self = this;
this.consts = {'ugc_url':'http://www.ugc.fr/', 'process_url':'index.php'};
this.settings = oSettings;
this._attr = {};
this._anchor = {};
this._ban = {status:false, reason:''};
this._timetable = {};
this.$Main = $('main');
this.ban = function(bStatus, sReason)
{
sReason = sReason || '';
if(!bStatus) return this._ban;
else if(sReason!='')
{
this._ban.status = true;
this._ban.reason = sReason;
this.anchor().addClass('banned');
this.feedback('Hidding movie "'+this.name()+'": '+this.ban().reason);
}
else this.error('Setting ban status with no reason');
}
this.id = function(iFilmId)
{
return this.attr('id', iFilmId);
}
this.name = function(sName)
{
sName = sName?(sName + '').toLowerCase().replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function($1) {return $1.toUpperCase();}):'';
return this.attr('name', sName);
};
this.poster = function(sPoster)
{
if(sPoster) sPoster = this.getPosterUrl(sPoster, 'small');
return this.attr('poster', sPoster);
};
this.backgroundPoster = function(sPoster)
{
if(sPoster) sPoster = this.getPosterUrl(sPoster, 'medium');
return this.attr('poster_bg', sPoster);
};
this.getPosterUrl = function(sUrl, sSize)
{
var sExt = sUrl.substr(sUrl.lastIndexOf('.')+1).toLowerCase();
if(sExt=='jpg') sExt = 'jpeg';
return this.consts.process_url+'?a=get_poster&type='+sExt+'&data[id]='+this.id()+'&data[url]='+encodeURIComponent(sUrl)+'&data[size]='+sSize;
};
this.edito = function(sEdito)
{
return this.attr('edito', sEdito);
};
this.trailer = function(sTrailer)
{
sTrailer = sTrailer?self.consts.ugc_url+sTrailer:'';
return this.attr('trailer', sTrailer);
};
this.attr = function(sName, sValue)
{
if(!sValue) return this._attr[sName];
else
{
sValue = $.trim(sValue);
if(sValue!='') this._attr[sName] = sValue;
else this.error('Trying to set an empty film attribute: '+sName);
}
};
/*
this.removeSpinner = function()
{
this.anchor().find('.timetable').removeClass('fa fa-fw fa-spinner fa-spin');
}
*/
this.getRank = function()
{
var sEdito = this.edito() || '';
var iRank = 0;
if(this.ban().status) iRank -= 1000000000; //Banned movies
else if(sEdito=='Avant-première') iRank += 1000000000; //Special event
else if(sEdito=='Nouveau') iRank += 10000000; //New releases
else if(sEdito.slice(-7)=='semaine')iRank += 100000 * parseInt(sEdito.substr(0, sEdito.indexOf('è'))); //Popular movies
//Adding movie ID (newer movies have greater IDs)
iRank += parseInt(this.id(), 10);
return iRank;
};
this.anchor = function()
{
if(Object.keys(this._anchor).length > 0) return this._anchor;
else
{
//Add film to DOM
this._anchor = $('<div>', {'class':'film', 'id':'film_'+this.id()})
.data('id', this.id())
.append($('<img>', {'class':'bg', 'src':this.backgroundPoster()}))
.append($('<div>', {'class':'poster-container'})
.append($('<img>', {'class':'poster', 'src':this.backgroundPoster()})
.click(function(){
$This = $(this);
$This.attr('src', $This.attr('src')+'&data[force]=1');
})))
.append($('<div>', {'class':'info'})
.append($('<p>', {'class':'title'})
.append($('<a>', {'class':'name', 'target':'_blank', 'href':this.consts.ugc_url+'film.html?id='+this.id()})
.append($('<i>', {'class':'fa fa-fw inline fa-film'}))
.append(this.name())))
.append($('<p>', {'class':'edito'+(!this.edito()?' hide':'')})
.append($('<i>', {'class':'fa fa-fw inline fa-edito'}))
.append($('<span>').text(this.edito())))
.append($('<p>', {'class':'trailer'})
.append($('<a>', {'target':'_blank', 'href':this.trailer()})
.append($('<i>', {'class':'fa fa-fw inline fa-trailer'}))
.append('Trailer')))
.append($('<div>', {'class':'timetable'})))
//.append($('<div>', {'class':'timetable fa fa-fw fa-spinner fa-spin'})))
.hide()
.appendTo(this.$Main);
}
};
this.timetable = function(iCinemaId, sLang, oTimetable)
{
if(!iCinemaId) return this._timetable || {};
else if(!sLang) return this._timetable[iCinemaId] || {};
else if(!oTimetable) return this._timetable[iCinemaId][sLang] || [];
else
{
if(!(iCinemaId in this._timetable)) this._timetable[iCinemaId] = {};
if(!(sLang in this._timetable[iCinemaId])) this._timetable[iCinemaId][sLang] = [];
this._timetable[iCinemaId][sLang].push(oTimetable);
}
};
this.renderTimetable = function(iCinemaId)
{
var sCinema = self.settings.cinemas(iCinemaId);
$.each(self.timetable(iCinemaId), function(sLang, asTimetable)
{
if(sLang=='VF' && ('VOSTF' in self.timetable(iCinemaId))) self.feedback('Hiding VF of "'+self.name()+'" @'+sCinema+' (VOSTF available)');
else if(sLang=='VFSTF') self.feedback('Hiding hearing-impaired version ('+sLang+') of "'+self.name()+'" @'+sCinema)
else
{
var $CinemaBox = $('<div>', {'class':'cinema_timetable_'+iCinemaId});
$('<p>', {'class':'cinema_lang'})
.append($('<a>', {'class':'cinema', 'target':'_blank', 'href':self.consts.ugc_url+'cinemaAction!execute.action?page=7&id='+iCinemaId})
.append($('<i>', {'class':'fa fa-fw inline fa-cinema'}))
.append($('<span>', {'class':'cinema_name'}).text(sCinema)))
.append($('<span>', {'class':'lang'}).text(sLang))
.appendTo($CinemaBox);
var $Timetable = $('<p>', {'class':'screening'});
$.each(asTimetable.sort(self.timeCompare), function(iKey, oTime){
$Timetable
.append($('<a>', {'class':'button', 'href':oTime.booking, 'target':'_blank', 'title':'Book screening'}).text(oTime.time))
.append(' '); //for line break
});
self.anchor().find('.timetable').append($CinemaBox.append($Timetable));
}
});
};
this.timeCompare = function(sTime1, sTime2)
{
return (Date.parse('01/01/1970 '+sTime1.time+':00') > Date.parse('01/01/1970 '+sTime2.time+':00'));
};
this.feedback = function(sMsg)
{
$('#feed_content').append($('<p>').text(sMsg));
};
this.error = function(sMsg)
{
console.log('Film Error: '+sMsg);
};
}
function Ugc()
{
var self = this;
this.settings = new Settings();
this._films = {};
this._wip = 0;
this.$Header = $('header');
this.$Nav = $('nav');
this.$Main = $('main');
this.$Footer = $('footer');
this.$Feedback = $('#feedback');
this.loadCinemas = function()
{
//Display buffer while loading
buffer(true);
//Load Settings
this.settings.init();
//Set Header
$('#date')
.attr('datetime', this.settings.date('html'))
.append(this.settings.date('formal'))
.append($('<sup>').text(this.settings.date('abbr')));
//Load cinemas
$.each(this.settings.cinemas(), function(iCinemaId, sCinemaName){
getUgcPage( 'filmsAjaxAction!getFilmsForPageCinema.action',
{cinemaId:iCinemaId, cinemaCode:'', page:'7', filmId:'', filmId_widget:'', '_':Date.now()},
function(hDom){self.addCinema(iCinemaId, hDom)},
function(){},
'html');
});
};
this.wip = function(iProgress)
{
if(!iProgress) return this._wip;
else
{
this._wip += iProgress;
if(this._wip==0) this.onPostLoad();
}
};
this.onPostLoad = function()
{
var $Films = $('.film');
//Remove movies with no screening
$Films.not('.banned').each(function(iKey, hFilm){
var $Film = $(hFilm);
if($Film.find('.timetable').is(':empty'))
{
var oFilm = self.film($Film.data('id'));
oFilm.ban(true, 'No screening today');
}
});
//Sort films
$Films.sort(function(oFilm1, oFilm2){
return (self.film($(oFilm1).data('id')).getRank() < self.film($(oFilm2).data('id')).getRank())?1:-1;
}).detach();
//Add films (except bans)
$Films.appendTo(this.$Main);
//remove buffer
buffer(false);
};
this.addCineNav = function(iCinemaId)
{
var sCinema = self.settings.cinemas(iCinemaId);
var sFirstLetter = sCinema/*.replace(/^L(e|a|es)\s/g, '').charAt(0)*/;
this.$Nav.append($('<a>', {title:'Show / Hide '+sCinema+' screenings'})
.data('id', iCinemaId)
.click(function(){
var $This = $(this);
$('.cinema_timetable_'+$This.data('id')).toggle();
$This.toggleClass('hidden');
})
.text(sFirstLetter));
}
this.addCinema = function(iCinemaId, hDom)
{
var $Page = $(hDom);
var $Films = $Page.find('.FilmDiv');
this.wip($Films.length);
this.addCineNav(iCinemaId);
$Films.each(function()
{
var $Film = $(this);
var iFilmId = $Film.find('input[name="filmId"]').val();
var oFilm = self.film(iFilmId);
if(!oFilm)
{
oFilm = new Film(self.settings);
//ID
oFilm.id(iFilmId);
//Edito
var sEdito = $.trim($Film.find('.FilmEditorial').text());
oFilm.edito(sEdito);
//Name
oFilm.name($Film.find('.FilmTitle').text());
//Poster
var sPosterUrl = $Film.find('.FilmThumbnail img').attr('source');
oFilm.poster(sPosterUrl);
oFilm.backgroundPoster(sPosterUrl.replace('152x217/FFFFFF', '274x410/EEEEE8'));
//Trailer
oFilm.trailer($Film.find('.FilmIntroLink a').attr('href'));
//Minimum info acquired: book place on DOM
oFilm.anchor();
//Ban if wrong edito
if((sEdito=='Avant-première' && !self.settings.premiere()) || sEdito=='UGC Culte' || sEdito=='Soirée Club') oFilm.ban(true, sEdito);
//Append film to Ugc
self.film(iFilmId, oFilm);
}
//Timetable
if(!oFilm.ban().status)
{
getUgcPage( 'filmsAfficheAction!reloadSeancesList.action',
{filmId:oFilm.id(), cinemaId:iCinemaId, cinemaCode:'', day:self.settings.date('midnight_timestamp')},
function(hData){
var sLang = '';
var $Timetable = $(hData);
var oTimes = [];
$Timetable.children().each(function(iKey, oDiv){
$Div = $(oDiv);
if($Div.attr('class')=='Line') //New Lang
{
sLang = $.trim($Div.find('.FilmTitle').text());
}
else if($Div.find('a').length > 0)
{
$Div.find('a').each(function(iKey, oLink){
var $Time = $(oLink);
oTime = {'time':$.trim($Time.find('span').text()), 'booking':oFilm.consts.ugc_url+$Time.attr('href')};
oFilm.timetable(iCinemaId, sLang, oTime);
});
}
});
oFilm.renderTimetable(iCinemaId);
self.wip(-1);
},
function(){
self.wip(-1);
self.error('Ajax Error detected on movie "'+oFilm.name()+'" @'+self.settings.cinemas(iCinemaId)+'.');
},
'html');
}
else self.wip(-1);
});
};
this.film = function(iFilmId, oFilm)
{
if(!iFilmId) return this._films;
else if(!oFilm) return (iFilmId in this._films)?this._films[iFilmId]:false;
else this._films[iFilmId] = oFilm;
};
this.feedback = function(sMsg)
{
$('#feed_content').append($('<p>').text(sMsg));
};
this.error = function(sMsg)
{
console.log('Ugc Error: '+sMsg);
};
}
function Settings()
{
var self = this;
this._ugcCinemas = {};
this._cinemas = {};
//this._ugcDates = [];
this._date = null;
this.$Settings = $('#settings');
this.$Header = $('header');
this.$Main = $('main');
this.consts = { days:["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
months:["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]};
this.init = function()
{
//Default date
var iCookieTimestamp = parseInt(Cookies.get('date'));
var oDate;
if(isNaN(iCookieTimestamp) || iCookieTimestamp < this.timestamp()) // no cookie or in the past
{
oDate = new Date();
if(oDate.getHours()==23) oDate = new Date(this.timestamp(oDate) + 24*60*60*1000); //Tomorrow
}
else oDate = new Date(iCookieTimestamp);
this.date(oDate);
//UGC dates
this.addUgcDatesToDom();
//Default cinemas
var asCookieCinemas = Cookies.getJSON('cinemas');
this.cinemas(asCookieCinemas?asCookieCinemas:{'10':'Les Halles', '12':'Bercy', '20':'La Défense'});
//UGC cinemas
getUgcPage( 'headerReservationAction!reloadCinemasList.action',
{'regionsAndCinemasBean.region':'', 'regionsAndCinemasBean.cinema':'', '_':this.date('timestamp')},
function(jData){
self.ugcCinemas(jData.cinemas);
self.addUgcCinemasToDom();
},
function(){self.error('Error: Could not load cinema list');},
'json');
//Avant-Première switch
var bCookiePremiere = Cookies.getJSON('premiere');
this.premiere(bCookiePremiere==null?false:bCookiePremiere);
$('.switches').click(function(){self.premiere(!self.premiere());});
};
this.premiere = function(bActive)
{
var $Premiere = $('#premiere');
if(bActive==null) return $Premiere.hasClass('fa-on');
else $Premiere.removeClass('fa-on fa-off').addClass(bActive?'fa-on':'fa-off');
};
this.ugcCinemas = function(asCinemas)
{
if(!asCinemas) return this._ugcCinemas;
else if($.isNumeric(asCinemas)) return this._ugcCinemas[asCinemas]; //Get Cinema name
else
{
$.each(asCinemas, function(iCinemaId, sCinema){
self._ugcCinemas[iCinemaId] = $.trim(sCinema.replace(/UGC( Ciné Cité|)/i, ''));
});
}
};
this.cinemas = function(asCinemas)
{
if(!asCinemas) return this._cinemas;
else if($.isNumeric(asCinemas)) return this._cinemas[asCinemas]; //Get Cinema name
else this._cinemas = asCinemas;
};
this.date = function(oDate)
{
if(!oDate) return this._date;
else if($.type(oDate)=='string')
{
switch(oDate)
{
case 'timestamp':
return this.timestamp(this._date);
case 'today':
return new Date(this._date.getFullYear(), this._date.getMonth(), this._date.getDate());
case 'midnight_timestamp':
return this.timestamp(this.date('today'));
case 'day':
return this._date.getDate();
case 'weekday':
return this._date.getDay();
case 'month':
return this._date.getMonth();
case 'year':
return this._date.getFullYear();
case 'html':
return this.date('year')+'-'+(("0" + (this.date('month') + 1)).slice(-2))+'-'+(("0" + this.date('day')).slice(-2));
case 'formal':
return this.consts.days[this.date('weekday')]+', '+this.consts.months[this.date('month')]+' '+this.date('day');
case 'abbr':
return (this.date('day')%10==1)?'st':((this.date('day')%10==2)?'nd':((this.date('day')%10==3)?'rd':'th'));
default:
return this._date;
}
}
else this._date = oDate;
};
this.addUgcDatesToDom = function()
{
//Build today at midnight
var oNextDay = new Date();
var bTuesdayNight = (oNextDay.getDay()==2 && oNextDay.getHours()==23);
oNextDay = new Date(oNextDay.getFullYear(), oNextDay.getMonth(), oNextDay.getDate());
//Build available dates
var $Select = $('#ugc_dates').find('select');
var $List = $('#ugc_days');
var iCount = 0;
while(oNextDay.getDay()!=3 || (oNextDay.getDay()==3 && bTuesdayNight) || iCount==0)
{
//Display this day
var iTimestamp = this.timestamp(oNextDay);
switch(iCount)
{
case 0: sDay = 'Today'; break;
case 1: sDay = 'Tomorrow'; break;
default: sDay = this.consts.days[oNextDay.getDay()];
}
$Select.append($('<option>', {'value':iTimestamp}).text(sDay));
$List.append($('<a>', {'class':'button no_bg inline day', 'id':'day_'+iTimestamp}).data('timestamp', iTimestamp).text(sDay));
//Manual break for extra day in case of Tuesday night after 23h
if(oNextDay.getDay()==3 && bTuesdayNight) break;
//Building next day
oNextDay = new Date(this.timestamp(oNextDay) + 24*60*60*1000);
iCount++;
}
//Selecting right value
$Select.val(this.date('midnight_timestamp'));
$List.find('#day_'+this.date('midnight_timestamp')).addClass('clicked');
//event
$Select.change(function(){
Cookies.set('date', $(this).val(), {expires:6});
reload();
});
$List.find('.day').click(function(){
Cookies.set('date', $(this).data('timestamp'), {expires:6});
reload();
});
};
this.addUgcCinemasToDom = function()
{
//Hide buffer
self.$Settings.find('#buffer_settings').hide();
//Add cinemas
var $Cinemas = self.$Settings.find('.cinemas');
$.each(self.ugcCinemas(), function(iKey, sCinemaName){
var iCinemaId = $.trim(iKey);
$Cinemas.append($('<li>', {'class':'cinema'})
.attr('id', 'cinema_'+iCinemaId)
.data('id', iCinemaId)
.toggleClass('include', (iCinemaId in self.cinemas()))
.click(function(){$(this).toggleClass('include');})
.text(sCinemaName));
});
//Event: Show / Hide panel
$('#btn_settings').click(function(){self.toggleSettings(true);});
$('.background').click(function(){self.toggleSettings(false);});
$(window).keyup(function(e){if(e.keyCode == 27) self.toggleSettings(false);});
//Event: Validation of cinemas selection
this.$Settings.find('#ok_settings').click(function(){
var asCines = {};
self.$Settings.find('.include').each(function(iKey, oCinema){
var $Cine = $(oCinema);
asCines[$Cine.data('id')] = $Cine.text();
});
Cookies.set('cinemas', asCines);
Cookies.set('premiere', self.premiere()?'true':'false');
reload();
});
};
this.toggleSettings = function(bShow)
{
self.$Settings.add('.background').toggle((typeof bShow == 'undefined')?null:bShow);
};
this.timestamp = function(oDate)
{
oDate = oDate || new Date();
return oDate.getTime();
}
this.error = function(sMsg)
{
console.log('Settings Error: '+sMsg);
};
}
function getUgcPage(sPath, asData, fOnSuccess, fOnFail, sType)
{
var asParams = {'a':'get_page', 'type':sType, 'data':{'path':sPath, 'vars':asData}};
$.ajax({
url: (new Film()).consts.process_url,
data: asParams,
dataType: sType
})
.done(function(oData){
if(sType=='html')
{
var asMatches = oData.match(/<title>(.*?)<\/title>/);
if(asMatches && asMatches[1] == 'Service Temporarily Unavailable')
{
console.log('UGC website Temporarily Unavailable. Reloading page');
console.log(asParams);
reload();
}
}
fOnSuccess(oData);
})
.fail(function(){
fOnFail();
console.log('UGC website Temporarily Unavailable. Reloading page');
console.log(asParams);
reload();
});
}
function buffer(bActive)
{
var $Block = $('main').add('header').add('nav');
if(bActive) $Block.fadeTo(0, 0);
else $Block.add($('.film').not('.banned')).fadeTo('slow', 1);
$('#buffer').toggle(bActive);
$('footer').add('#feedback').toggle(!bActive);
}
function reload()
{
buffer(true);
location.reload();
}