Skip to content

Commit

Permalink
Make slide number format configurable (gnab#130).
Browse files Browse the repository at this point in the history
  • Loading branch information
gnab committed Jun 29, 2014
1 parent 778d49f commit c4661aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/remark/models/slideshow.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Slideshow (events, options) {
self.getRatio = getOrDefault('ratio', '4:3');
self.getHighlightStyle = getOrDefault('highlightStyle', 'default');
self.getHighlightLanguage = getOrDefault('highlightLanguage', '');
self.getSlideNumberFormat = getOrDefault('slideNumberFormat', '%current% / %total%');

loadFromString(options.source);

Expand Down
17 changes: 16 additions & 1 deletion src/remark/views/slideView.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ SlideView.prototype.configureElements = function () {

self.numberElement = document.createElement('div');
self.numberElement.className = 'remark-slide-number';
self.numberElement.innerHTML = self.slide.number + ' / ' + self.slideshow.getSlides().length;
self.numberElement.innerHTML = formatSlideNumber(self.slide, self.slideshow);

self.contentElement.appendChild(self.numberElement);
self.element.appendChild(self.contentElement);
Expand All @@ -81,6 +81,21 @@ SlideView.prototype.configureElements = function () {
self.containerElement.appendChild(self.scalingElement);
};

function formatSlideNumber (slide, slideshow) {
var format = slideshow.getSlideNumberFormat()
, current = slide.number
, total = slideshow.getSlides().length
;

if (typeof format === 'function') {
return format(current, total);
}

return format
.replace('%current%', current)
.replace('%total%', total);
}

SlideView.prototype.scaleBackgroundImage = function (dimensions) {
var self = this
, styles = window.getComputedStyle(this.contentElement)
Expand Down
12 changes: 12 additions & 0 deletions test/remark/views/slideView_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('SlideView', function () {
, getSlides: function () { return []; }
, getLinks: function () { return {}; }
, getHighlightLanguage: function () { }
, getSlideNumberFormat: function () { return '%current% / %total%'; }
}
, scaler = {
dimensions: {width: 10, height: 10}
Expand Down Expand Up @@ -117,4 +118,15 @@ describe('SlideView', function () {
lines[0].innerHTML.should.equal(' line 2');
});
});

describe('slide numbering', function () {
it('should display according to format', function () {
var slide = new Slide(1, { content: [] })
, slideView = new SlideView(new EventEmitter(), slideshow, scaler, slide)
;

slideshow.getSlideNumberFormat = function () { return '%current% / %total%'; };
slideView.numberElement.innerHTML.should.equal('1 / 0');
});
});
});

0 comments on commit c4661aa

Please sign in to comment.