Last active
January 28, 2018 15:53
-
-
Save gsimone/23bebf555f69e6cfd7db1e21c3278484 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const NUMERO_DI_PROSSIMI_PROGRAMMI = 3 | |
| const weekDaysString = [ | |
| 'Lunedì', | |
| 'Martedì', | |
| 'Mercoledì', | |
| 'Giovedì', | |
| 'Venerdì', | |
| 'Sabato', | |
| 'Domenica', | |
| ] | |
| // l'insieme dei programmi, serve solo per rappresentarli poi nell'html | |
| // e ci puoi mettere tutti i dati che ti serviranno (es. presentatore, immagine di anteprima) | |
| const _PROGRAMMI_ = [{ | |
| name: 'Revival', | |
| id: 'revival', | |
| }, { | |
| name: 'Best Of', | |
| id: 'best-of', | |
| }, { | |
| name: 'Italiana', | |
| id: 'italiana', | |
| }, { | |
| name: 'Novità', | |
| id: 'novita' | |
| }, { | |
| name: 'Revival', | |
| id: 'revival', | |
| }] | |
| // questo è l'oggetto che rappresenta il palinsesto | |
| // array di 7 elementi ( uno per ogni giorno della settimana ) | |
| // con un oggetto programs che rappresenta il programma del giorno | |
| // programs è un array di lunghezza qualsiasi | |
| // i programmi vengono valorizzati da id ( per collegarsi a _PROGRAMMI_ ) | |
| // e 'inizio', 'fine' per determinare i timeslot dei programmi | |
| const _PALINSESTO_ = [{}, {}, {}, {}, { | |
| giorno: 'Sabato', | |
| programs: [{ | |
| id: 'revival', | |
| inizio: 10, | |
| to: 16, | |
| }], | |
| }, { | |
| programs: [{ | |
| id: 'best-of', | |
| inizio: 10, | |
| fine: 16, | |
| }, { | |
| id: 'revival', | |
| inizio: 16, | |
| fine: 18, | |
| }, { | |
| id: 'italiana', | |
| inizio: 18, | |
| fine: 20, | |
| }, { | |
| id: 'novita', | |
| inizio: 18, | |
| fine: 20, | |
| }, { | |
| id: 'revival', | |
| inizio: 20, | |
| fine: 23, | |
| }] | |
| }, { | |
| programs: [{ | |
| id: 'best-of', | |
| inizio: 10, | |
| fine: 16, | |
| }, { | |
| id: 'revival', | |
| inizio: 16, | |
| fine: 18, | |
| }, { | |
| id: 'italiana', | |
| inizio: 18, | |
| fine: 20, | |
| }, { | |
| id: 'novita', | |
| inizio: 18, | |
| fine: 20, | |
| }, { | |
| id: 'revival', | |
| inizio: 20, | |
| fine: 23, | |
| }] | |
| }] | |
| const getProgramRunningNow = (date) => { | |
| const day = date.getDay() - 1 | |
| const hours = date.getHours() + date.getMinutes()/60 | |
| const programmiDelGiorno = _PALINSESTO_[day].programs | |
| // trova l'indice del programma attivo confrontando | |
| // l'ora corrente con l'ora di inizio e fine dei programmi di oggi | |
| const runningProgramIndex = programmiDelGiorno.findIndex(({ inizio, fine }) => inizio <= hours && fine >= hours) | |
| const runningProgram = programmiDelGiorno[runningProgramIndex] | |
| const nextPrograms = programmiDelGiorno.slice(runningProgramIndex + 1, runningProgramIndex + 1 + NUMERO_DI_PROSSIMI_PROGRAMMI) | |
| let fillerPrograms = [] | |
| // controlliamo se i programmi di oggi NON sono abbastanza da riempire l'insieme | |
| // di programmi da mostrare | |
| if( nextPrograms.length < NUMERO_DI_PROSSIMI_PROGRAMMI ) { | |
| // prende i programmi del prossimo giorno | |
| // facendo attenzione che la domenica deve prendere i giorni del lunedì | |
| const programmiDiDomani = _PALINSESTO_[day + 1 % 8] | |
| // calcola quanti programmi servono per arrivare al numero impostato | |
| const numeroDiFiller = NUMERO_DI_PROSSIMI_PROGRAMMI - nextPrograms.length | |
| fillerPrograms = programmiDiDomani.programs.slice(0, numeroDiFiller) | |
| } | |
| return { | |
| runningProgram, | |
| nextPrograms: [ | |
| ...nextPrograms, | |
| ...fillerPrograms, | |
| ], | |
| } | |
| } | |
| // questo oggetto contiene l'id dei programmi e la fascia oraria | |
| const programmiDaMostrare = getProgramRunningNow(new Date('2018-01-27T18:31:34.160Z')) | |
| // se vogliamo stampare il programma princiapale | |
| const programmaPrincipale = _PROGRAMMI_ | |
| .find(programma => programmiDaMostrare.runningProgram.id === programma.id) | |
| console.log('programmaPrincipale ', programmaPrincipale) | |
| const stampaDiv = () => { | |
| const newDiv = $('<div data-program="'+ programmaPrincipale.id +'">') | |
| newDiv.append('<div> dalle <strong>'+ programmiDaMostrare.runningProgram.inizio +'</strong> alle <strong>'+ programmiDaMostrare.runningProgram.fine +'</strong></div>') | |
| newDiv.append('<span>'+ programmaPrincipale.name +'</span>') | |
| $('body').append(newDiv) | |
| } | |
| stampaDiv(programmaPrincipale) | |
| console.log(programmiDaMostrare.nextPrograms.map(x => x.id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment