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

Added Row/Column limit feature in Vertical/Horizontal layouts #40

Open
wants to merge 4 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
9 changes: 9 additions & 0 deletions src/js/layout/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ function ($) {
}

return intersection;
},

/**
* ### @isLimitable
* Returns whether rows/colums in layout are limitable
* If yes, should implement a setLimit method
*/
isLimitable : function() {
return false;
}
};

Expand Down
75 changes: 57 additions & 18 deletions src/js/layout/horizontal.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,31 @@ function ($, util, BaseLayout) {
getDimensions: function (sprites, defaults) {
var width = 0;
var height = 0;

$.map(sprites, function (sprite) {

var cols = this.columnlimit || 0,
rows = 0,
minwidth = 0,
i = 0,
j =0;

if(cols>0 && cols<sprites.length) {
rows = Math.ceil(sprites.length/cols);
for(i=0; i<rows; i++) {
for(j=0; j<cols; j++) {
if(sprites[i*cols+j]) {
minwidth += sprites[i*cols+j].width;
height += sprites[i*cols+j].height;
}
}
width = Math.max(width, minwidth);
minwidth = 0;
}
}
else $.map(sprites, function (sprite) {
height = sprite.height > height ? sprite.height : height;
width += sprite.width;
});

return {
width: width || defaults.width,
height: height || defaults.height
Expand All @@ -76,28 +95,48 @@ function ($, util, BaseLayout) {
var pass = 0;
var x = 0;
var y = 0;

while (pass++ < this.settings.maxPass) {
for (x = 0; x <= dimensions.width - sprite.width; x++) {
sprite.x = x;
sprite.y = y;

intersection = this.intersection(sprite, placed);

if (!intersection) {
placed.push(sprite);
sprite.show();
return true;
}

x = intersection.x + intersection.width - 1;
for (y = 0; y <= dimensions.height - sprite.height; y++) {
for (x = 0; x <= dimensions.width - sprite.width; x++) {
sprite.x = x;
sprite.y = y;

intersection = this.intersection(sprite, placed);

if (!intersection) {
placed.push(sprite);
sprite.show();
return true;
}

x = intersection.x + intersection.width - 1;
}

y = intersection.y + intersection.height - 1;
}

dimensions.width += sprite.width;
dimensions.height += sprite.height;
}

return false;
},

/**
* ### @isLimitable
* Returns whether rows/colums in layout are limitable
*/
isLimitable : function() {
return true;
},

/**
* ### @setLimit
* Limit the number of columns
*/
setLimit : function(limit) {
this.columnlimit = limit;
}
});

Expand Down
69 changes: 54 additions & 15 deletions src/js/layout/vertical.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,30 @@ function ($, util, BaseLayout) {
var width = 0;
var height = 0;

$.map(sprites, function (sprite) {
var rows = this.rowlimit || 0,
cols = 0,
minheight = 0,
i = 0,
j =0;

if(rows>0 && rows<sprites.length) {
cols = Math.ceil(sprites.length/rows);
for(i=0; i<cols; i++) {
for(j=0; j<rows; j++) {
if(sprites[i*rows+j]) {
minheight += sprites[i*rows+j].height;
width += sprites[i*rows+j].width;
}
}
height = Math.max(height, minheight);
minheight = 0;
}
}
else $.map(sprites, function (sprite) {
width = sprite.width > width ? sprite.width : width;
height += sprite.height;
});

console.log(width, height, sprites.length, this.rowlimit);
return {
width: width || defaults.width,
height: height || defaults.height
Expand All @@ -76,28 +95,48 @@ function ($, util, BaseLayout) {
var pass = 0;
var x = 0;
var y = 0;

console.log(sprite.name);
while (pass++ < this.settings.maxPass) {
for (y = 0; y <= dimensions.height - sprite.height; y++) {
sprite.x = x;
sprite.y = y;

intersection = this.intersection(sprite, placed);

if (!intersection) {
placed.push(sprite);
sprite.show();
return true;
for (x = 0; x <= dimensions.width - sprite.width; x++) {
for (y = 0; y <= dimensions.height - sprite.height; y++) {
sprite.x = x;
sprite.y = y;

intersection = this.intersection(sprite, placed);
console.log("-",$(intersection).attr("name"));
if (!intersection) {
placed.push(sprite);
sprite.show();
return true;
}

y = intersection.y + intersection.height - 1;
}

y = intersection.y + intersection.height - 1;
x = intersection.x + intersection.width - 1;
}

dimensions.width += sprite.width;
dimensions.height += sprite.height;
}

return false;
},

/**
* ### @isLimitable
* Returns whether rows/colums in layout are limitable
*/
isLimitable : function() {
return true;
},

/**
* ### @setLimit
* Limit the number of rows
*/
setLimit : function(limit) {
this.rowlimit = limit;
}
});

Expand Down
20 changes: 20 additions & 0 deletions src/js/manager/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ function ($, CompactLayout, VerticalLayout, HorizontalLayout) {

this.manager = new Manager();
},
/**
* ### @isLimitable
* Returns whether working layout manager is limitable
*
* @param null
*/
isLimitable: function () {
return this.manager.isLimitable();
},
/**
* ### @limitRows
* Set the working layout manager instance by type
*
* @param {number} limit value for the limit
*/
setLimit: function (limit) {
if(this.manager.isLimitable()) {
this.manager.setLimit(limit);
}
},

/**
* ### @getDimensions
Expand Down
26 changes: 24 additions & 2 deletions src/js/module/stitches.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function($, Modernizr, store, util, templates, fileManager, layoutManager, style
});
layoutManager.set(this.settings.layout);
stylesheetManager.set(this.settings.stylesheet);
this.updateProps();
},

/**
Expand Down Expand Up @@ -286,10 +287,18 @@ function($, Modernizr, store, util, templates, fileManager, layoutManager, style
"change": function (e) {
var $checked = this.$element.find("input[name=layout]:checked");
var value = $checked.val();

this.source.layout = value;
layoutManager.set(value);

self.updateSettings();
layoutManager.setLimit(
this.$element.find("input[name=limit]").val());
}
},
limit: {
"change": function (e) {
var value = $(e.currentTarget).val();
self.settings.limit = value;
layoutManager.setLimit(value);
self.updateSettings();
}
},
Expand Down Expand Up @@ -416,6 +425,7 @@ function($, Modernizr, store, util, templates, fileManager, layoutManager, style
// update ui
this.showOverlay();
this.canvas.reset();
this.updateProps();

// store settings
if (store && !store.disabled) {
Expand All @@ -442,6 +452,17 @@ function($, Modernizr, store, util, templates, fileManager, layoutManager, style
hideOverlay: function (e) {
this.$overlay.fadeOut("fast");
},

/**
* ### @updateProps
* Update Dependent Properties
*
* @param {event} e The event object
*/
updateProps: function (e) {
this.$element.find("input[name=limit]")
.prop('disabled', !layoutManager.isLimitable());
},

/**
* ### @openAbout
Expand Down Expand Up @@ -793,6 +814,7 @@ function($, Modernizr, store, util, templates, fileManager, layoutManager, style

// update settings
layoutManager.set(this.settings.layout);
layoutManager.setLimit(this.settings.limit);
stylesheetManager.set(this.settings.stylesheet);
this.updateSettings();

Expand Down
8 changes: 8 additions & 0 deletions src/templates/stitches.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@
</label>
</div>
</div>


<div class="control-group">
<label class="control-label">Fixed Limit</label>
<div class="controls">
<input name="limit" disabled type="number" placeholder="Limit of rows/columns...">
</div>
</div>

<div class="control-group">
<label class="control-label">CSS/LESS</label>
Expand Down