Skip to content

Commit

Permalink
Added jQuery support namespace stratum.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxovaiko committed Jun 23, 2018
1 parent 3137323 commit 2acb0d8
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 124 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
- [Future updates](https://github.com/zaxoavoki/stratum.js#future-updates)
- [Copyright and license](https://github.com/zaxoavoki/stratum.js#copyright-and-license)

### How to use?

- You can download it with [stratum.js CDN](https://cdn.rawgit.com/zaxoavoki/stratum.js/31373231/dist/stratum.min.js);
- Use npm catalog;

### Future updates

- Add padding and margin gap between columns;
- Add older browsers' supports;
- Rewrite code using basic jQuery plugin rules;
- Add this package to the [npm](https://www.npmjs.com/) catalog;

### Copyright and license
Expand Down
6 changes: 6 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
<!-- Include jQuery first and stratum.js -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="../dist/stratum.min.js"></script>
<script>
$('[data-grid]').stratum({
// padding: 20,
// columns: 3
});
</script>

</body>
</html>
172 changes: 105 additions & 67 deletions dist/stratum.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,77 +91,115 @@
"use strict";


/*
* Stratum.js
* Grid layout library
* MIT License
* by Volodia Zakhovaiko
*/

$(document).ready(function () {

// Get all grids
var grid = $('[data-grid]');

// Count of the columns
var columns = grid.data('grid');

// Set default padding gap
var padding = 15;

// Set default value if user does not set count of the columns
if (columns === undefined || !columns) {
columns = 3;
}

grid.css({
position: 'relative'
});

// Array with item's height and width
var items = [];
// Initial values: left and top position
var top = 0,
left = -100 / columns;

//
//----- Main cycle: there we will check current coordinates -----//
grid.children().each(function (i) {

$(this).wrap('<div class="grid-item"></div>div>');

// Set left position for every nth block
i % columns === 0 ? left = 0 : left += 100 / columns;

// Set width and left position the first
$(this).parent().css({
position: 'absolute',
width: 100 / columns + '%',
left: left + '%',
padding: padding
});
(function ($) {

$.fn.stratum = function (options) {

var settings = $.extend({
padding: 15,
columns: 3
}, options);

var grid = $(this);

// Count of the columns and default padding gap
var columns = settings.columns;
var padding = settings.padding;

// Iterate and reformat each matched element
return this.each(function () {

// Initialize stratum function
function init() {
grid.css({
position: 'relative'
});

// Array with items height
var items = [];
var top = 0;
var left = -100 / columns;

//
// Main cycle: there we will check current coordinates
grid.children().each(function (i, obj) {

var gridItem = $(obj);

if (gridItem.attr('class') !== "grid_item") {
gridItem.wrap('<div class="grid_item"></div>');
}

i % columns === 0 ? left = 0 : left += 100 / columns;

// Set width and others CSS rules
gridItem.parent().css({
position: 'absolute',
width: 100 / columns + '%',
left: left + '%',
padding: padding
});

// Save height value in main array to check top positions
items.push(gridItem.parent().outerHeight(true));

// Set top position
if (items[i - columns] === undefined) {
top = 0;
} else {
var itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
}
}

// Update and set new position values
gridItem.parent().css({
top: top
});
});
}

// Save height value in main array to check top positions
items.push($(this).parent().outerHeight(true));

// Set top position
if (items[i - columns] === undefined) {
top = 0;
} else {
var itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
function resize() {
var items = [],
top = 0;

grid.children().each(function (i, obj) {

var gridItem = $(obj);

// Save height value in main array to check top positions
items.push(gridItem.outerHeight(true));

// Set top position
if (items[i - columns] === undefined) {
top = 0;
} else {
var itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
}
}

// Update and set new position values
gridItem.css({
top: top
});
});
}
}

// Update and set new position values
$(this).parent().css({
top: top
$(window).on('load', function () {
init();
}).on('resize', function () {
resize();
});
});
});
});
};
})(jQuery);

/***/ })
/******/ ]);
2 changes: 1 addition & 1 deletion dist/stratum.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"build": "webpack --config webpack.config.js",
"watch": "webpack --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/zaxoavoki/stratum.js"
},
"author": "Volodia Zakhovaiko",
"license": "MIT",
"devDependencies": {
Expand Down
152 changes: 97 additions & 55 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,112 @@
/*
* Stratum.js
* Grid layout library
* MIT License
* by Volodia Zakhovaiko
*/
(function ($) {

$(document).ready(function () {
$.fn.stratum = function (options) {

// Get all grids
let grid = $('[data-grid]');
let settings = $.extend({
padding: 15,
columns: 3
}, options);

// Count of the columns
let columns = grid.data('grid');
let grid = $(this);

// Set default padding gap
let padding = 15;
// Count of the columns and default padding gap
let columns = settings.columns;
let padding = settings.padding;

// Set default value if user does not set count of the columns
if (columns === undefined || !columns) {
columns = 3;
}
// Iterate and reformat each matched element
return this.each(function () {

grid.css({
position: 'relative'
});
// Initialize stratum function
function init() {
grid.css({
position: 'relative'
});

// Array with item's height and width
let items = [];
// Initial values: left and top position
let top = 0, left = -100 / columns;
// Array with items height
let items = [];
let top = 0;
let left = -100 / columns;

//
//----- Main cycle: there we will check current coordinates -----//
grid.children().each(function (i) {
//
// Main cycle: there we will check current coordinates
grid.children().each(function (i, obj) {

$(this).wrap('<div class="grid-item"></div>div>');
let gridItem = $(obj);

// Set left position for every nth block
i % columns === 0 ? left = 0 : left += 100 / columns;
if (gridItem.attr('class') !== "grid_item") {
gridItem.wrap('<div class="grid_item"></div>');
}

// Set width and left position the first
$(this).parent().css({
position : 'absolute',
width : 100 / columns + '%',
left: left + '%',
padding: padding
});
i % columns === 0 ? left = 0 : left += 100 / columns;

// Set width and others CSS rules
gridItem.parent().css({
position: 'absolute',
width: 100 / columns + '%',
left: left + '%',
padding: padding
});

// Save height value in main array to check top positions
items.push(gridItem.parent().outerHeight(true));

// Set top position
if (items[i - columns] === undefined) {
top = 0;
} else {
let itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
}
}

// Update and set new position values
gridItem.parent().css({
top: top
});
});
}

function resize() {

// Save height value in main array to check top positions
items.push($(this).parent().outerHeight(true));

// Set top position
if (items[i - columns] === undefined) {
top = 0;
} else {
let itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
// TODO: Create setTopPosition function;

let items = [], top = 0;

grid.children().each(function (i, obj) {

let gridItem = $(obj);

// Save height value in main array to check top positions
items.push(gridItem.outerHeight(true));

// Set top position
if (items[i - columns] === undefined) {
top = 0;
} else {
let itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
}
}

// Update and set new position values
gridItem.css({
top: top
});
});
}
}

// Update and set new position values
$(this).parent().css({
top: top
$(window).on('load', function () {
init();
}).on('resize', function () {
resize();
});
});
});
});
};

}(jQuery));

0 comments on commit 2acb0d8

Please sign in to comment.