Skip to content

Commit

Permalink
Merge pull request mozilla-b2g#30339 from sean2449/Bug-1170067-move-p…
Browse files Browse the repository at this point in the history
…rogram

Bug 1170067 - [Stingray][EPG] Make program element as a web component, r=johnhu
  • Loading branch information
sean2449 committed Jun 2, 2015
2 parents 40d2485 + d74a7a2 commit 748e20c
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 198 deletions.
2 changes: 2 additions & 0 deletions tv_apps/tv-epg/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<!-- stylesheets of building blocks -->
<link rel="stylesheet" type="text/css" href="shared/style/smart-screen/navigation.css">
<link rel="stylesheet" type="text/css" href="shared/style/smart-screen/icons.css">
<link rel="stylesheet" type="text/css" href="style/epg_program.css">
<link rel="stylesheet" type="text/css" href="style/tv_epg.css">

<!-- For test purpose on phone only -->
Expand All @@ -29,6 +30,7 @@
<!-- building blocks -->

<!-- Specific code -->
<script defer src="js/epg_program.js"></script>
<script defer src="js/epg_controller.js"></script>
<script defer src="js/epg.js"></script>
<script defer src="js/index.js"></script>
Expand Down
92 changes: 28 additions & 64 deletions tv_apps/tv-epg/js/epg.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
};

proto._onScanned = function epg__onScanned(stream) {
this.videoElement.src = stream;
this.videoElement.mozSrcObject = stream;
this.epgController.fetchPrograms(
this.visibleTimeOffset - this.visibleTimeSize,
3 * this.visibleTimeSize
Expand Down Expand Up @@ -137,25 +137,16 @@
*/
proto._addProgramSlot = function epg__addProgramSlot(index, time) {
var rowElement;
var columnElement;
var textElement;
var progressElement;
var programElement;
var i;
for (i = 0; i < this.programListElement.children.length; i++) {
rowElement = this.programListElement.children[i];
columnElement = document.createElement('LI');
columnElement.dataset.duration = '1';
columnElement.dataset.startTime = time;
progressElement = document.createElement('DIV');
progressElement.classList.add('background-progress');
textElement = document.createElement('DIV');
textElement.classList.add('title');
columnElement.appendChild(progressElement);
columnElement.appendChild(textElement);
programElement = document.createElement('epg-program');
programElement.startTime = time;
if (index < this.timelineElement.children.length) {
rowElement.insertBefore(columnElement, rowElement.children[index]);
rowElement.insertBefore(programElement, rowElement.children[index]);
} else {
rowElement.appendChild(columnElement);
rowElement.appendChild(programElement);
}
}
};
Expand All @@ -181,43 +172,35 @@

proto._updateProgramSlot = function epg__updateProgramSlot(configs) {
var rowElement = this.programListElement.children[configs.row];
var columnElement = rowElement.children[configs.column];
var duration = parseInt(columnElement.dataset.duration, 10);
var progressElement;
var currentColumn;
var programElement = rowElement.children[configs.column];
var duration = programElement.duration;

if (configs.title) {
columnElement.querySelector('.title').textContent = configs.title;
programElement.title = configs.title;
}

if (configs.duration) {
duration = configs.duration;
columnElement.dataset.duration = duration;
programElement.duration = duration;
}

if (configs.isVisible) {
columnElement.classList.remove('hidden');
this.spatialNavigator.add(columnElement);
currentColumn = this.initialTime - this.epgController.timelineOffset;
progressElement = columnElement.querySelector('.background-progress');
if (configs.column + duration <= currentColumn) {
progressElement.style.transform = 'scaleX(1)';
} else {
progressElement.classList.add('smooth');
}
programElement.show();
this.spatialNavigator.add(programElement);
programElement.resetProgressElement(this.initialTime);
} else {
columnElement.classList.add('hidden');
programElement.hide();
// If the old focus element only contains partial program segment, we have
// to refocus to the first visible element of the same program.
if (columnElement === this.spatialNavigator.getFocusedElement()) {
if (programElement === this.spatialNavigator.getFocusedElement()) {
this.spatialNavigator.focus(
this.epgController.programTable[configs.column][configs.row].element);
}
this.spatialNavigator.remove(columnElement);
this.spatialNavigator.remove(programElement);
}

if (configs.item) {
configs.item.element = columnElement;
configs.item.element = programElement;
}
};

Expand All @@ -236,23 +219,11 @@
var prevPrograms = this.epgController.programTable[timeIndex - 1];
var row;
var programElement;
var startTime;
var duration;
var scaleX;

// Update progress bar in every currently playing program
for (row in playingPrograms) {
programElement = playingPrograms[row].element;
startTime = parseInt(programElement.dataset.startTime, 10);
duration = parseInt(programElement.dataset.duration, 10);

// There is a margin between two programs, so scale has to be normalized
scaleX = (time / this.timelineUnit - startTime) * EPG.COLUMN_WIDTH;
scaleX = scaleX / (duration * EPG.COLUMN_WIDTH - EPG.COLUMN_MARGIN);
scaleX = Math.min(scaleX, 1);
programElement.querySelector('.background-progress').style.transform =
'scaleX(' + scaleX + ')';

programElement.progress = time / this.timelineUnit;
this.timeMarkerElement.style.transform =
'translateX(' +
((time / this.timelineUnit - this.initialTime) * EPG.COLUMN_WIDTH) +
Expand All @@ -263,8 +234,7 @@
if (prevPrograms && prevPrograms[row] &&
programElement !== prevPrograms[row].element) {
programElement = prevPrograms[row].element;
programElement.querySelector('.background-progress').style.transform =
'scaleX(1)';
programElement.fillProgress();
}
}
};
Expand All @@ -287,15 +257,14 @@
};
};

proto._onFocus = function epg__onFocus(element) {
var rowElement = element.parentElement;
proto._onFocus = function epg__onFocus(programElement) {
var rowElement = programElement.parentElement;
var rowIndex = parseInt(rowElement.dataset.row, 10);
var rowOffset = this.epgController.channelOffset;

var startTime = parseInt(element.dataset.startTime, 10);
var startTime = programElement.startTime;
var timelineOffset = this.epgController.timelineOffset;

element.classList.add('focus');
programElement.classList.add('focus');
rowElement.classList.remove('hidden');
this._setTitlePadding({
setToNull: true
Expand Down Expand Up @@ -373,20 +342,15 @@
var row;
var timeOffset = this.visibleTimeOffset - this.epgController.timelineOffset;
var programElement;
var programTitleElement;
var programStartTime;
for(row = rowOffset; row < rowOffset + size; row++) {
if (this.epgController.programTable[timeOffset][row]) {
programElement =
this.epgController.programTable[timeOffset][row].element;
programTitleElement = programElement.querySelector('.title');
if (opts && opts.setToNull) {
programTitleElement.style.paddingLeft = null;
programElement.titlePadding = null;
} else {
programStartTime = parseInt(programElement.dataset.startTime, 10);
programTitleElement.style.paddingLeft =
EPG.COLUMN_WIDTH * (this.visibleTimeOffset - programStartTime) +
'rem';
programElement.titlePadding = EPG.COLUMN_WIDTH *
(this.visibleTimeOffset - programElement.startTime) + 'rem';
}
}
}
Expand Down Expand Up @@ -415,8 +379,8 @@
this.dateElement.textContent = dtf.localeFormat(now, timeFormat);
};

proto._onUnfocus = function epg__onUnfocus(element) {
element.classList.remove('focus');
proto._onUnfocus = function epg__onUnfocus(programElement) {
programElement.classList.remove('focus');
};

proto._onMove = function epg__onMove(key) {
Expand Down
90 changes: 90 additions & 0 deletions tv_apps/tv-epg/js/epg_program.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* global EPG */

'use strict';

window.EPGProgram = (function(exports) {
var proto = Object.create(HTMLElement.prototype);

proto.createdCallback = function() {
this._duration = 1;
this.dataset.duration = '1';
this.titleElement = document.createElement('DIV');
this.titleElement.classList.add('title');
this.appendChild(this.titleElement);

this.progressElement = document.createElement('DIV');
this.progressElement.classList.add('background-progress');
this.appendChild(this.progressElement);
};

Object.defineProperty(proto, 'progress', {
set: function(time) {
if (!this.startTime || !time) {
return;
}

var scaleX = (time - this.startTime) * EPG.COLUMN_WIDTH;
scaleX = scaleX / (this.duration * EPG.COLUMN_WIDTH - EPG.COLUMN_MARGIN);
scaleX = Math.min(scaleX, 1);
this.progressElement.style.transform = 'scaleX(' + scaleX + ')';
}
});

Object.defineProperty(proto, 'startTime', {
set: function(time) {
this._startTime = time;
},
get: function() {
return this._startTime;
}
});

Object.defineProperty(proto, 'title', {
set: function(title) {
this.titleElement.textContent = title;
}
});

Object.defineProperty(proto, 'titlePadding', {
set: function(padding) {
this.titleElement.style.paddingLeft = padding;
}
});

Object.defineProperty(proto, 'duration', {
set: function(duration) {
this._duration = duration;
this.dataset.duration = duration;
},
get: function() {
return this._duration;
}
});

proto.resetProgressElement = function(time) {
if (!this.startTime || !time) {
return;
}

if (this.startTime + this.duration <= time) {
this.fillProgress();
} else {
this.progressElement.classList.add('smooth');
}
this.appendChild(this.progressElement);
};

proto.fillProgress = function() {
this.progressElement.style.transform = 'scaleX(1)';
};

proto.hide = function() {
this.classList.add('hidden');
};

proto.show = function() {
this.classList.remove('hidden');
};

return document.registerElement('epg-program', { prototype: proto });
})(window);
85 changes: 85 additions & 0 deletions tv_apps/tv-epg/style/epg_program.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
epg-program {
display: inline-block;
position: relative;
width: 33.5rem;
font-size: 2.5rem;
line-height: 10.8rem;
overflow: hidden;
padding-bottom: 0.3rem;
padding-right: 0.3rem;
border-bottom: 0.1rem #5e5e5e solid;
margin-top: 0.3rem;
background-color: #404040;
height: 11.2rem;
background-clip: content-box;
box-sizing: border-box;
outline: none;
}

epg-program:after {
content: '';
position: absolute;
top: 0;
right: 0;
width: 0.1rem;
background-color: #5e5e5e;
height: 10.8rem;
}

epg-program.focus {
background-color: #00caf2;
}

epg-program.hidden {
display: none;
}

epg-program[data-duration="1"] {
width: 33.8rem;
}

epg-program[data-duration="2"] {
width: 67.6rem;
}

epg-program[data-duration="3"] {
width: 101.4rem;
}

epg-program[data-duration="4"] {
width: 135.2rem;
}

epg-program[data-duration="5"] {
width: 169rem;
}

epg-program[data-duration="6"] {
width: 202.8rem;
}

epg-program[data-duration="7"] {
width: 236.9rem;
}

epg-program > .title {
margin-left: 2rem;
overflow: hidden;
text-overflow: ellipsis;
width: calc(100% - 2rem);
box-sizing: border-box;
position: absolute;
}

epg-program > .background-progress {
position: absolute;
background-color: rgba(0, 0, 0, 0.2);
height: calc(100% - 0.3rem);
transform: scaleX(0);
transform-origin: 0;
width: calc(100% - 0.3rem);
}

epg-program > .background-progress.smooth {
transition: transform 0.3s;
}

0 comments on commit 748e20c

Please sign in to comment.