From 975e389e0af9fb057fe538b99c946e9c76d463dd Mon Sep 17 00:00:00 2001 From: Shuntaro Ogawa Date: Wed, 17 Dec 2014 23:48:48 +0900 Subject: [PATCH 1/3] rename test files. --- test/{jquery.gridder.html => jquery.gridster.html} | 0 test/{jquery.gridder_test.js => jquery.gridster_test.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename test/{jquery.gridder.html => jquery.gridster.html} (100%) rename test/{jquery.gridder_test.js => jquery.gridster_test.js} (100%) diff --git a/test/jquery.gridder.html b/test/jquery.gridster.html similarity index 100% rename from test/jquery.gridder.html rename to test/jquery.gridster.html diff --git a/test/jquery.gridder_test.js b/test/jquery.gridster_test.js similarity index 100% rename from test/jquery.gridder_test.js rename to test/jquery.gridster_test.js From 913462f190a8b885e7982f64ff7ed1ac0525f19f Mon Sep 17 00:00:00 2001 From: Shuntaro Ogawa Date: Wed, 17 Dec 2014 23:50:30 +0900 Subject: [PATCH 2/3] add element "#qunit-testresult" --- test/jquery.gridster.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jquery.gridster.html b/test/jquery.gridster.html index 1750f0a4..5937526f 100644 --- a/test/jquery.gridster.html +++ b/test/jquery.gridster.html @@ -61,7 +61,7 @@

- +
From 0ad0b3bd9218d15e8f1391108a98b0ccdfa9d27f Mon Sep 17 00:00:00 2001 From: Shuntaro Ogawa Date: Thu, 18 Dec 2014 03:29:48 +0900 Subject: [PATCH 3/3] Convert to integer type of property value when sorting. --- src/jquery.gridster.js | 30 ++++++++++++ test/jquery.gridster_test.js | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/jquery.gridster.js b/src/jquery.gridster.js index 7bac9118..a23d90c3 100755 --- a/src/jquery.gridster.js +++ b/src/jquery.gridster.js @@ -153,6 +153,28 @@ Gridster.defaults = defaults; Gridster.generated_stylesheets = []; + /** + * Convert properties to Integer + * + * @param {Object} + * @return {Objct} Returns the converted object. + */ + function convInt(obj) { + var props = ['col', 'row', 'size_x', 'size_y']; + var tmp = {}; + for (var i=0, len=props.length; i b.row) { return 1; } @@ -189,6 +213,8 @@ */ Gridster.sort_by_row_and_col_asc = function(widgets) { widgets = widgets.sort(function(a, b) { + a = convInt(a); + b = convInt(b); if (a.row > b.row || a.row === b.row && a.col > b.col) { return 1; } @@ -209,6 +235,8 @@ */ Gridster.sort_by_col_asc = function(widgets) { widgets = widgets.sort(function(a, b) { + a = convInt(a); + b = convInt(b); if (a.col > b.col) { return 1; } @@ -229,6 +257,8 @@ */ Gridster.sort_by_row_desc = function(widgets) { widgets = widgets.sort(function(a, b) { + a = convInt(a); + b = convInt(b); if (a.row + a.size_y < b.row + b.size_y) { return 1; } diff --git a/test/jquery.gridster_test.js b/test/jquery.gridster_test.js index 2df25e77..6a68e634 100644 --- a/test/jquery.gridster_test.js +++ b/test/jquery.gridster_test.js @@ -27,6 +27,13 @@ this.el = $('#qunit-fixture').find(".wrapper ul"); + this.serialization = [ + { name: "A", col: "1", row: "1", size_x: "2", size_y: "2" }, + { name: "B", col: "4", row: "1", size_x: "1", size_y: "2" }, + { name: "C", col: "10", row: "10", size_x: "10", size_y: "10" }, + { name: "D", col: "3", row: "1", size_x: "1", size_y: "1" }, + { name: "E", col: "2", row: "3", size_x: "3", size_y: "1" } + ]; } }); @@ -35,4 +42,87 @@ // strictEqual(this.el, this.el.gridster(), 'should be chaninable'); // }); + test('Gridster.sort_by_row_asc', function(assert) { + var sorted = Gridster.sort_by_row_asc(this.serialization); + + var result = pickup(sorted, 'name').join(','); + var expected = 'A,B,D,E,C'; + assert.equal(result, expected); + }); + + test('Gridster.sort_by_row_and_col_asc', function(assert) { + var sorted = Gridster.sort_by_row_and_col_asc(this.serialization); + + var result = pickup(sorted, 'name').join(','); + var expected = 'A,D,B,E,C'; + assert.equal(result, expected); + }); + + test('Gridster.sort_by_col_asc', function(assert) { + var sorted = Gridster.sort_by_col_asc(this.serialization); + + var result = pickup(sorted, 'name').join(','); + var expected = 'A,E,D,B,C'; + assert.equal(result, expected); + }); + + test('Gridster.sort_by_row_desc', function(assert) { + var sorted = Gridster.sort_by_row_desc(this.serialization); + + var result = pickup(sorted, 'name').join(','); + var expected = 'C,E,A,B,D'; + assert.equal(result, expected); + }); + + // erros + test('Throws not exists property', function(assert) { + assert.throws(function() { + var data = [{row:1, size_x:1, size_y:1},{col:2,row:1,size_x:1,size_y:1}]; + Gridster.sort_by_row_asc(data); + }, + Error, + 'raise error not exists required property' + ); + }); + + test('Throws invalid type of value', function(assert) { + // inconvertible types + assert.throws(function() { + Gridster.sort_by_row_asc([{col:"AAA", row:1, size_x:1, size_y:1},{col:2,row:1,size_x:1,size_y:1}]); + }, + Error, + 'raise error inconvertible types' + ); + + // null + assert.throws(function() { + Gridster.sort_by_row_asc([{col:null, row:1, size_x:1, size_y:1},{col:2,row:1,size_x:1,size_y:1}]); + }, + Error, + 'raise error value is null' + ); + + // array + assert.throws(function() { + Gridster.sort_by_row_asc([{col:[1,2,3], row:1, size_x:1, size_y:1},{col:2,row:1,size_x:1,size_y:1}]); + }, + Error, + 'raise error value is array' + ); + + // object + assert.throws(function() { + Gridster.sort_by_row_asc([{col:{k:1}, row:1, size_x:1, size_y:1},{col:2,row:1,size_x:1,size_y:1}]); + }, + Error, + 'raise error value is object' + ); + }); + + // helper + function pickup(data, prop) { + return data.map(function(elm) { + return elm[prop]; + }); + } }(jQuery));