Skip to content

Commit

Permalink
refactor(titleManager) next level button in titleManager
Browse files Browse the repository at this point in the history
  • Loading branch information
stared committed Jul 3, 2016
1 parent a3a9d96 commit f3bfbf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
30 changes: 11 additions & 19 deletions js/game_board.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ export class GameBoard {

this.game = game;
this.svg = svg;
this.blinkSvg = blinkSvg;

this.titleManager = new TitleManager(
this.svg.select('.title-bar .title-text'),
this.svg.select('.subtitle-bar'),
this.svg.select('.title-bar .level-number'));
this.svg.select('.title-bar .level-number'),
this.svg.select('.navigation-controls'),
blinkSvg
);
this.titleManager.activateNextLevelButton(() => this.loadNextLevel());

this.popupManager = popupManager;
this.storage = storage;

Expand All @@ -61,8 +65,6 @@ export class GameBoard {

this.boardControls = svg.select('.board-controls');
this.activateBoardControls();
this.navigationControls = svg.select('.navigation-controls');
this.activateNavigationControls();

this.loadLevel(levelId);
this.tileHelper = new TileHelper(svg, this.bareBoard, this.game);
Expand Down Expand Up @@ -125,11 +127,8 @@ export class GameBoard {
absorptionDuration
);
}
// Show next level button
this.navigationControls.select('.next-level').classed('hidden', false);
// Show the blink SVG
this.blinkSvg.classed('hidden', false);

this.titleManager.showNextLevelButton(true);
this.storage.setLevelIsWon(level.id, true);
this.saveProgress();
this.progressPearls.update();
Expand All @@ -153,7 +152,7 @@ export class GameBoard {
// Hack: bareBoard SVG sets its viewBox - use that information to set
// the viewBox of blinking SVG
// TODO(pathes): more elegant mechanism
this.blinkSvg.attr('viewBox', this.svg.attr('viewBox'));
this.titleManager.blinkSvg.attr('viewBox', this.svg.attr('viewBox'));
this.stock.elementCount(this.bareBoard.level);
this.stock.drawStock();
}
Expand Down Expand Up @@ -272,12 +271,6 @@ export class GameBoard {
.on('mouseover', () => gameBoard.titleManager.displayMessage('DOWNLOAD LEVEL AS JSON'));
}

activateNavigationControls() {
const navigationControls = this.navigationControls;
navigationControls.select('.next-level')
.on('click', () => this.nextLevel());
}

downloadCurrentLevel() {
const levelJSON = stringify(this.bareBoard.exportBoard(), {maxLength: 100, indent: 2});
const fileName = _.kebabCase(`${this.bareBoard.level.name}_${(new Date()).toISOString()}`) + '.json';
Expand Down Expand Up @@ -330,12 +323,11 @@ export class GameBoard {
this.reset();
this.progressPearls.update();

// Show next level button?
this.navigationControls.select('.next-level').classed('hidden', !this.bareBoard.alreadyWon);
this.blinkSvg.classed('hidden', !this.bareBoard.alreadyWon);
this.titleManager.showNextLevelButton(this.bareBoard.alreadyWon);

}

nextLevel() {
loadNextLevel() {
if (this.bareBoard.level && this.bareBoard.level.next) {
this.loadLevel(this.bareBoard.level.next);
}
Expand Down
18 changes: 17 additions & 1 deletion js/title_manager.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/*global window:false*/
import {displayMessageTimeout} from './config';

// TODO(migdal): passing that many selectors is nasty - refactor
export class TitleManager {
constructor(titleElem, subtitleElem, levelNumberElem) {
constructor(titleElem, subtitleElem, levelNumberElem, navigationControls, blinkSvg) {
this.titleElem = titleElem;
this.subtitleElem = subtitleElem;
this.messageElem = this.subtitleElem.select('.subtitle-message');
this.levelNumberElem = levelNumberElem;
this.defaultMessage = '';
this.navigationControls = navigationControls;
this.blinkSvg = blinkSvg;
}

setTitle(title) {
Expand Down Expand Up @@ -39,4 +42,17 @@ export class TitleManager {
.text(this.defaultMessage);
}
}

activateNextLevelButton(nextLevelCallback) {
const navigationControls = this.navigationControls;
navigationControls.select('.next-level')
.on('click', nextLevelCallback);
}

showNextLevelButton(ifShow) {
// Show next level button?
this.navigationControls.select('.next-level').classed('hidden', !ifShow);
this.blinkSvg.classed('hidden', !ifShow);
}

}

0 comments on commit f3bfbf4

Please sign in to comment.