diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..04b9946 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +src/tables.intro.jquery.js +src/tables.intro.shoestring.js +src/tables.outro.js \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index 644ac15..b41cc2d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -127,7 +127,12 @@ module.exports = function(grunt) { } }, qunit: { - files: ['test/**/*.html'] + files: ['test-qunit/**/*.html'] + }, + run: { + ava: { + exec: "./node_modules/.bin/ava" + } }, watch: { src: { @@ -197,13 +202,13 @@ module.exports = function(grunt) { require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); // Default task. - grunt.registerTask('travis', ['qunit']); + grunt.registerTask('test', ['qunit', 'run:ava']); grunt.registerTask('concat-pre', ['concat:jsautoinit', 'concat:jsall', 'concat:jsjquery', 'concat:jsstack', 'concat:jsstackjquery', 'concat:cssall', 'concat:cssstack', 'concat:cssstackmixinpre']); grunt.registerTask('concat-post', ['concat:cssstackmixinpost']); grunt.registerTask('src', ['concat-pre', 'myth', 'concat-post', 'clean:dependencies', 'copy', 'clean:post']); grunt.registerTask('filesize', ['uglify', 'cssmin', 'bytesize', 'clean:post']); - grunt.registerTask('default', ['src', 'qunit', 'filesize']); + grunt.registerTask('default', ['src', 'test', 'filesize']); // Deploy grunt.registerTask('deploy', ['default', 'gh-pages']); diff --git a/README.md b/README.md index c88408c..c19c2f7 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ A set of plugins for responsive tables. * [Check All](#check-all) * [Internationalization i18n](#internationalization-i18n) * [Limitations](#limitations) -* [Run Tests](http://filamentgroup.github.io/tablesaw/test/tablesaw.html) +* [Run Tests](http://filamentgroup.github.io/tablesaw/test-qunit/tablesaw.html) * [Browser Support](#browser-support) ## Stack Mode @@ -363,7 +363,7 @@ Some of the more intrusive default styles have instead moved to opt-in classes y | `rowspan` | _Not yet supported_ ([#247](https://github.com/filamentgroup/tablesaw/issues/247)) | Supported | Supported | _Not yet supported_ ([#268](https://github.com/filamentgroup/tablesaw/issues/268)) | | `colspan` | Supported | Supported | Supported | Supported | -## [Tests](http://filamentgroup.github.io/tablesaw/test/tablesaw.html) +## [Tests](http://filamentgroup.github.io/tablesaw/test-qunit/tablesaw.html) ## Browser Support diff --git a/package.json b/package.json index c13cb95..a84be17 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,9 @@ "node": ">= 4" }, "scripts": { - "test": "./node_modules/.bin/grunt travis", + "test": "./node_modules/.bin/grunt test", "precommit": "lint-staged", - "prepush": "grunt qunit" + "prepush": "grunt test" }, "lint-staged": { "src/*.js": [ @@ -30,11 +30,31 @@ "git add" ] }, + "ava": { + "files": [ + "test/**/*.test.js" + ], + "require": [ + "babel-register", + "./test/helpers/setup-browser-env.js" + ], + "inherit": true, + "verbose": true + }, + "babel": { + "presets": [ + "@ava/stage-4" + ] + }, "main": "dist/tablesaw.js", "dependencies": { "shoestring": "^2.0.0" }, "devDependencies": { + "ava": "^0.23.0", + "babel-register": "^6.26.0", + "browser-env": "^3.2.2", + "console-advanced": "^1.0.7", "grunt": "^1.0.1", "grunt-bytesize": "~0.2.0", "grunt-contrib-clean": "~1.1.0", @@ -42,17 +62,18 @@ "grunt-contrib-concat": "~1.0.1", "grunt-contrib-copy": "~1.0.0", "grunt-contrib-cssmin": "^2.2.0", - "grunt-contrib-qunit": "~2.0.0", + "grunt-contrib-qunit": "^2.0.0", "grunt-contrib-uglify": "^3.0.1", "grunt-contrib-watch": "~1.0.0", "grunt-gh-pages": "~2.0.0", "grunt-myth": "~1.1.0", + "grunt-run": "^0.8.0", "husky": "^0.14.3", "jquery": "^3.1.0", "lint-staged": "^4.0.1", "matchdep": "~1.0.1", - "prettier": "^1.5.2", - "qunitjs": "^2.0.0" + "prettier": "^1.8.2", + "qunitjs": "^2.4.1" }, "optionalDependencies": { "javascript-natural-sort": "^0.7.1" diff --git a/src/tables.btnmarkup.js b/src/tables.btnmarkup.js index 771787f..766659b 100644 --- a/src/tables.btnmarkup.js +++ b/src/tables.btnmarkup.js @@ -22,7 +22,9 @@ if (sel) { // TODO next major version: remove .btn-select - $(this).addClass("btn-select tablesaw-btn-select")[pluginName]("_select", sel); + $(this) + .addClass("btn-select tablesaw-btn-select") + [pluginName]("_select", sel); } return oEl; }, @@ -90,4 +92,6 @@ // add methods $.extend($.fn[pluginName].prototype, methods); + + // TODO OOP this and add to Tablesaw object })(); diff --git a/src/tables.checkall.js b/src/tables.checkall.js index ab7c1f6..eea00a1 100644 --- a/src/tables.checkall.js +++ b/src/tables.checkall.js @@ -31,7 +31,9 @@ CheckAll.prototype._filterCells = function($checkboxes) { return $checkboxes .filter(function() { - return !$(this).closest("tr").is("[data-tablesaw-subrow],[data-tablesaw-ignorerow]"); + return !$(this) + .closest("tr") + .is("[data-tablesaw-subrow],[data-tablesaw-ignorerow]"); }) .find(this.checkboxSelector) .not(this.checkAllSelector); @@ -127,4 +129,6 @@ $(document).on(Tablesaw.events.create, function(e, tablesaw) { new CheckAll(tablesaw); }); + + Tablesaw.CheckAll = CheckAll; })(); diff --git a/src/tables.columntoggle.js b/src/tables.columntoggle.js index 925a2e1..00eafbb 100644 --- a/src/tables.columntoggle.js +++ b/src/tables.columntoggle.js @@ -136,15 +136,20 @@ if (self.set.length) { var index; - $(self.$popup).find("input[type='checkbox']").each(function(j) { - if (this === e.target) { - index = j; - return false; - } - }); + $(self.$popup) + .find("input[type='checkbox']") + .each(function(j) { + if (this === e.target) { + index = j; + return false; + } + }); $(self.set).each(function() { - var checkbox = $(this).data(data.key).$popup.find("input[type='checkbox']").get(index); + var checkbox = $(this) + .data(data.key) + .$popup.find("input[type='checkbox']") + .get(index); if (checkbox) { checkbox.checked = e.target.checked; onToggleCheckboxChange(checkbox); @@ -230,7 +235,11 @@ var invisibleColumns = 0; this.$menu.find("input").each(function() { var header = self.getHeaderFromCheckbox(this); - this.checked = self.tablesaw._$getCells(header).eq(0).css("display") === "table-cell"; + this.checked = + self.tablesaw + ._$getCells(header) + .eq(0) + .css("display") === "table-cell"; }); this.updateColspanCells(); @@ -260,13 +269,19 @@ $(document).on(Tablesaw.events.destroy, function(e, tablesaw) { if (tablesaw.mode === "columntoggle") { - $(tablesaw.table).data(data.key).destroy(); + $(tablesaw.table) + .data(data.key) + .destroy(); } }); $(document).on(Tablesaw.events.refresh, function(e, tablesaw) { if (tablesaw.mode === "columntoggle") { - $(tablesaw.table).data(data.key).refreshPriority(); + $(tablesaw.table) + .data(data.key) + .refreshPriority(); } }); + + Tablesaw.ColumnToggle = ColumnToggle; })(); diff --git a/src/tables.intro.jquery.js b/src/tables.intro.jquery.js index c1c7f9d..5367046 100644 --- a/src/tables.intro.jquery.js +++ b/src/tables.intro.jquery.js @@ -19,8 +19,10 @@ jQuery = require('jquery')(root); } } - factory(jQuery); - return jQuery; + return { + $: jQuery, + Tablesaw: factory(jQuery) + }; }; } else { // Browser globals diff --git a/src/tables.intro.shoestring.js b/src/tables.intro.shoestring.js index 5b5f628..4e2ecfb 100644 --- a/src/tables.intro.shoestring.js +++ b/src/tables.intro.shoestring.js @@ -19,8 +19,11 @@ shoestring = require('shoestring')(root); } } - factory(shoestring); - return shoestring; + + return { + $: shoestring, + Tablesaw: factory(shoestring) + }; }; } else { // Browser globals diff --git a/src/tables.js b/src/tables.js index 23bfb9f..90c1a7c 100644 --- a/src/tables.js +++ b/src/tables.js @@ -69,7 +69,10 @@ if (Tablesaw.mustard) { this.$table = $(element); // only one and are allowed, per the specification - this.$thead = this.$table.children().filter("thead").eq(0); + this.$thead = this.$table + .children() + .filter("thead") + .eq(0); // multiple are allowed, per the specification this.$tbody = this.$table.children().filter("tbody"); @@ -120,9 +123,12 @@ if (Tablesaw.mustard) { }; Table.prototype._getHeaderRows = function() { - return this.$thead.children().filter("tr").filter(function() { - return !$(this).is("[data-tablesaw-ignorerow]"); - }); + return this.$thead + .children() + .filter("tr") + .filter(function() { + return !$(this).is("[data-tablesaw-ignorerow]"); + }); }; Table.prototype._getRowIndex = function($row) { @@ -144,16 +150,18 @@ if (Tablesaw.mustard) { Table.prototype._$getCells = function(th) { var self = this; - return $(th).add(th.cells).filter(function() { - var $t = $(this); - var $row = $t.parent(); - var hasColspan = $t.is("[colspan]"); - // no subrows or ignored rows (keep cells in ignored rows that do not have a colspan) - return ( - !$row.is("[" + self.attributes.subrow + "]") && - (!$row.is("[" + self.attributes.ignorerow + "]") || !hasColspan) - ); - }); + return $(th) + .add(th.cells) + .filter(function() { + var $t = $(this); + var $row = $t.parent(); + var hasColspan = $t.is("[colspan]"); + // no subrows or ignored rows (keep cells in ignored rows that do not have a colspan) + return ( + !$row.is("[" + self.attributes.subrow + "]") && + (!$row.is("[" + self.attributes.ignorerow + "]") || !hasColspan) + ); + }); }; Table.prototype._getVisibleColspan = function() { @@ -188,9 +196,11 @@ if (Tablesaw.mustard) { }; Table.prototype.isCellInColumn = function(header, cell) { - return $(header).add(header.cells).filter(function() { - return this === cell; - }).length; + return $(header) + .add(header.cells) + .filter(function() { + return this === cell; + }).length; }; Table.prototype.updateColspanCells = function(cls, header, userAction) { @@ -273,7 +283,9 @@ if (Tablesaw.mustard) { Table.prototype.getRows = function() { var self = this; return this.$table.find("tr").filter(function() { - return $(this).closest("table").is(self.$table); + return $(this) + .closest("table") + .is(self.$table); }); }; @@ -397,7 +409,9 @@ if (Tablesaw.mustard) { var $anchor = this._getToolbarAnchor(); var $toolbar = this._getToolbar($anchor); if (!$toolbar.length) { - $toolbar = $("
").addClass(classes.toolbar).insertBefore($anchor); + $toolbar = $("
") + .addClass(classes.toolbar) + .insertBefore($anchor); } this.$toolbar = $toolbar; @@ -440,7 +454,10 @@ if (Tablesaw.mustard) { $doc.on("enhance.tablesaw", function(e) { // Cut the mustard if (Tablesaw.mustard) { - $(e.target).find(initSelector).filter(initFilterSelector)[pluginName](); + $(e.target) + .find(initSelector) + .filter(initFilterSelector) + [pluginName](); } }); @@ -468,4 +485,6 @@ if (Tablesaw.mustard) { }, 150); // must be less than the scrolling timeout above. } }); + + Tablesaw.Table = Table; })(); diff --git a/src/tables.minimap.js b/src/tables.minimap.js index b2be687..e66e122 100644 --- a/src/tables.minimap.js +++ b/src/tables.minimap.js @@ -9,6 +9,19 @@ var MiniMap = { attr: { init: "data-tablesaw-minimap" + }, + show: function(table) { + var mq = table.getAttribute(MiniMap.attr.init); + + if (mq === "") { + // value-less but exists + return true; + } else if (mq && "matchMedia" in win) { + // has a mq value + return win.matchMedia(mq).matches; + } + + return false; } }; @@ -26,13 +39,8 @@ $btns.appendTo(tblsaw.$toolbar); - function showMinimap($table) { - var mq = $table.attr(MiniMap.attr.init); - return !mq || (win.matchMedia && win.matchMedia(mq).matches); - } - function showHideNav() { - if (!showMinimap($table)) { + if (!MiniMap.show($table[0])) { $btns.css("display", "none"); return; } @@ -74,4 +82,7 @@ createMiniMap(tablesaw.$table); } }); + + // TODO OOP this better + Tablesaw.MiniMap = MiniMap; })(); diff --git a/src/tables.modeswitch.js b/src/tables.modeswitch.js index a4350b8..1ba8773 100644 --- a/src/tables.modeswitch.js +++ b/src/tables.modeswitch.js @@ -88,4 +88,6 @@ S.init(Tablesaw.table); } }); + + // TODO OOP this and add to Tablesaw object })(); diff --git a/src/tables.outro.js b/src/tables.outro.js index be4600a..dd3746d 100644 --- a/src/tables.outro.js +++ b/src/tables.outro.js @@ -1 +1,2 @@ + return Tablesaw; })); diff --git a/src/tables.sortable.js b/src/tables.sortable.js index 2841b4b..60112c6 100644 --- a/src/tables.sortable.js +++ b/src/tables.sortable.js @@ -69,7 +69,10 @@ $.each(h, function(i, col) { var b = $("