Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty space to be specified in template. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function(grunt) {
'src/Tile.js',
'src/Template.js',
'src/UniformTemplates.js',
'src/SmartArrangeTemplates.js',
'src/Grid.js'
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
// ensure that we have at least one column
numCols = Math.max(1, numCols);

var template = this.templateFactory.get(numCols, targetTiles);
var template = this.templateFactory.get(numCols, targetTiles, this.tiles);
if (!template) {

// fallback in case the default factory can't generate a good template
Expand Down
79 changes: 79 additions & 0 deletions src/SmartArrangeTemplates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// template provider which returns simple templates with 1x1 tiles
Tiles.SmartArrangeTemplates = {
get: function (numCols, targetTiles, tiles) {
var LABELS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
i, j, iLen, jLen, regExp, d, dx, match, strReplace, tileSegmentStr, strBuilder, templateArr, matchIndex,
DEFAULT_TILE_SIZE = { x: 1, y: 1 };

//helper function to build new line of template.
var buildTemplateLineSegment = function (n) {
strBuilder = [];
for (i = 0; i < n; i++) {
strBuilder.push('.');
}
strBuilder.push('*');
return strBuilder.join('');
};

//Fall back to uniform templates factory if tiles is null or empty.
if (!tiles || tiles.length === 0) {
return Tiles.UniformTemplates.get(numCols, targetTiles);
}

//Initialize template string.
var templateString = buildTemplateLineSegment(numCols);

for (var index = 0, tile; tile = tiles[index]; index++) {

d = tile.id.size || DEFAULT_TILE_SIZE;
dx = numCols < d.x ? numCols : d.x;

//Regular Expression use to see if current tile can fit within the current template.
regExp = new RegExp('(\\.{' + dx + '}.{' + (numCols - dx + 1) + '}){' + d.y + '}', 'g');

match = null;
//Append to template until we can fit the current tile.
while (!(match = templateString.match(regExp))) {
templateString += buildTemplateLineSegment(numCols);
}

//Once we find the section of the template that can fit the tile, insert the tile into template.
strReplace = match[0].split('');
tileSegmentStr = '';
strBuilder = [];

//Building the tile horizontal segment.
for (i = 0; i < dx; i++) {
strBuilder.push(LABELS[index % 26]);
}
tileSegmentStr = strBuilder.join('');

//Replace the empty space in template with tile segments.
for (i = 0, iLen = strReplace.length; i < iLen; i += numCols + 1) {
for (j = 0, jLen = tileSegmentStr.length; j < jLen; j++) {
strReplace[i + j] = tileSegmentStr[j];
}
}

//Rebuilding the template
templateArr = [];
matchIndex = templateString.indexOf(match[0]);

//Assure that no following small tiles can fit within this space to avoid tiles to be out of order.
templateArr.push(templateString.substring(0, matchIndex).replace(/\./g, '~'));

templateArr.push(strReplace.join(''));
templateArr.push(templateString.substring(matchIndex + match[0].length));

templateString = templateArr.join('');
}
templateString = templateString.replace(/\*/ig, '').replace(/(\.)/ig, '~');
//Convert template into json object.
var templateJson = [];
for (i = 0, iLen = templateString.length; i < iLen; i += numCols) {
templateJson.push(templateString.slice(i, i + numCols));
}

return Tiles.Template.fromJSON(templateJson);
}
};
Loading