Business intelligence made simple
Removed dependency on stupidtable.js [skip ci]
Andrew Kane5fa75ef41edb
diff --git a/app/assets/javascripts/blazer/application.js b/app/assets/javascripts/blazer/application.jsindex 23a59008..d4ffd402 100644--- a/app/assets/javascripts/blazer/application.js+++ b/app/assets/javascripts/blazer/application.js@@ -1,7 +1,6 @@//= require ./jquery//= require ./rails-ujs-//= require ./stupidtable-//= require ./stupidtable-custom-settings+//= require ./tablesort//= require ./jquery.stickytableheaders//= require ./selectize//= require ./highlight.mindiff --git a/app/assets/javascripts/blazer/stupidtable-custom-settings.js b/app/assets/javascripts/blazer/stupidtable-custom-settings.jsdeleted file mode 100644index a59dffb1..00000000--- a/app/assets/javascripts/blazer/stupidtable-custom-settings.js+++ /dev/null@@ -1,13 +0,0 @@-function removeCommas(string) {- return string.replace(/,/g, "")-}--// Remove commas for integers and floats to fix issues with sorting in the stupidtable plugin-var stupidtableCustomSettings = {- "int": function(a, b) {- return parseInt(removeCommas(a), 10) - parseInt(removeCommas(b), 10);- },- "float": function(a, b) {- return parseFloat(removeCommas(a)) - parseFloat(removeCommas(b));- }-}diff --git a/app/assets/javascripts/blazer/stupidtable.js b/app/assets/javascripts/blazer/stupidtable.jsdeleted file mode 100644index 8db085e5..00000000--- a/app/assets/javascripts/blazer/stupidtable.js+++ /dev/null@@ -1,281 +0,0 @@-// Stupid jQuery table plugin.--(function($) {- $.fn.stupidtable = function(sortFns) {- return this.each(function() {- var $table = $(this);- sortFns = sortFns || {};- sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns);- $table.data('sortFns', sortFns);- $table.stupidtable_build();-- $table.on("click.stupidtable", "thead th", function() {- $(this).stupidsort();- });-- // Sort th immediately if data-sort-onload="yes" is specified. Limit to- // the first one found - only one default sort column makes sense anyway.- var $th_onload_sort = $table.find("th[data-sort-onload=yes]").eq(0);- $th_onload_sort.stupidsort();- });- };-- // ------------------------------------------------------------------- // Default settings- // ------------------------------------------------------------------- $.fn.stupidtable.default_settings = {- should_redraw: function(sort_info){- return true;- },- will_manually_build_table: false- };- $.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"};- $.fn.stupidtable.default_sort_fns = {- "int": function(a, b) {- return parseInt(a, 10) - parseInt(b, 10);- },- "float": function(a, b) {- return parseFloat(a) - parseFloat(b);- },- "string": function(a, b) {- return a.toString().localeCompare(b.toString());- },- "string-ins": function(a, b) {- a = a.toString().toLocaleLowerCase();- b = b.toString().toLocaleLowerCase();- return a.localeCompare(b);- }- };-- // Allow specification of settings on a per-table basis. Call on a table- // jquery object. Call *before* calling .stuidtable();- $.fn.stupidtable_settings = function(settings) {- return this.each(function() {- var $table = $(this);- var final_settings = $.extend({}, $.fn.stupidtable.default_settings, settings);- $table.stupidtable.settings = final_settings;- });- };--- // Expects $("#mytable").stupidtable() to have already been called.- // Call on a table header.- $.fn.stupidsort = function(force_direction){- var $this_th = $(this);- var datatype = $this_th.data("sort") || null;-- // No datatype? Nothing to do.- if (datatype === null) {- return;- }-- var dir = $.fn.stupidtable.dir;- var $table = $this_th.closest("table");-- var sort_info = {- $th: $this_th,- $table: $table,- datatype: datatype- };--- // Bring in default settings if none provided- if(!$table.stupidtable.settings){- $table.stupidtable.settings = $.extend({}, $.fn.stupidtable.default_settings);- }-- sort_info.compare_fn = $table.data('sortFns')[datatype];- sort_info.th_index = calculateTHIndex(sort_info);- sort_info.sort_dir = calculateSortDir(force_direction, sort_info);-- $this_th.data("sort-dir", sort_info.sort_dir);- $table.trigger("beforetablesort", {column: sort_info.th_index, direction: sort_info.sort_dir, $th: $this_th});-- // More reliable method of forcing a redraw- $table.css("display");-- // Run sorting asynchronously on a timout to force browser redraw after- // `beforetablesort` callback. Also avoids locking up the browser too much.- setTimeout(function() {- if(!$table.stupidtable.settings.will_manually_build_table){- $table.stupidtable_build();- }- var table_structure = sortTable(sort_info);- var trs = getTableRowsFromTableStructure(table_structure, sort_info);-- if(!$table.stupidtable.settings.should_redraw(sort_info)){- return;- }- $table.children("tbody").append(trs);-- updateElementData(sort_info);- $table.trigger("aftertablesort", {column: sort_info.th_index, direction: sort_info.sort_dir, $th: $this_th});- $table.css("display");-- }, 10);- return $this_th;- };-- // Call on a sortable td to update its value in the sort. This should be the- // only mechanism used to update a cell's sort value. If your display value is- // different from your sort value, use jQuery's .text() or .html() to update- // the td contents, Assumes stupidtable has already been called for the table.- $.fn.updateSortVal = function(new_sort_val){- var $this_td = $(this);- if($this_td.is('[data-sort-value]')){- // For visual consistency with the .data cache- $this_td.attr('data-sort-value', new_sort_val);- }- $this_td.data("sort-value", new_sort_val);- return $this_td;- };--- $.fn.stupidtable_build = function(){- return this.each(function() {- var $table = $(this);- var table_structure = [];- var trs = $table.children("tbody").children("tr");- trs.each(function(index,tr) {-- // ====================================================================- // Transfer to using internal table structure- // ====================================================================- var ele = {- $tr: $(tr),- columns: [],- index: index- };-- $(tr).children('td').each(function(idx, td){- var sort_val = $(td).data("sort-value");-- // Store and read from the .data cache for display text only sorts- // instead of looking through the DOM every time- if(typeof(sort_val) === "undefined"){- var txt = $(td).text();- $(td).data('sort-value', txt);- sort_val = txt;- }- ele.columns.push(sort_val);- });- table_structure.push(ele);- });- $table.data('stupidsort_internaltable', table_structure);- });- };-- // ====================================================================- // Private functions- // ====================================================================- var sortTable = function(sort_info){- var table_structure = sort_info.$table.data('stupidsort_internaltable');- var th_index = sort_info.th_index;- var $th = sort_info.$th;-- var multicolumn_target_str = $th.data('sort-multicolumn');- var multicolumn_targets;- if(multicolumn_target_str){- multicolumn_targets = multicolumn_target_str.split(',');- }- else{- multicolumn_targets = [];- }- var multicolumn_th_targets = $.map(multicolumn_targets, function(identifier, i){- return get_th(sort_info.$table, identifier);- });-- table_structure.sort(function(e1, e2){- var multicolumns = multicolumn_th_targets.slice(0); // shallow copy- var diff = sort_info.compare_fn(e1.columns[th_index], e2.columns[th_index]);- while(diff === 0 && multicolumns.length){- var multicolumn = multicolumns[0];- var datatype = multicolumn.$e.data("sort");- var multiCloumnSortMethod = sort_info.$table.data('sortFns')[datatype];- diff = multiCloumnSortMethod(e1.columns[multicolumn.index], e2.columns[multicolumn.index]);- multicolumns.shift();- }- // Sort by position in the table if values are the same. This enforces a- // stable sort across all browsers. See https://bugs.chromium.org/p/v8/issues/detail?id=90- if (diff === 0)- return e1.index - e2.index;- else- return diff;-- });-- if (sort_info.sort_dir != $.fn.stupidtable.dir.ASC){- table_structure.reverse();- }- return table_structure;- };-- var get_th = function($table, identifier){- // identifier can be a th id or a th index number;- var $table_ths = $table.find('th');- var index = parseInt(identifier, 10);- var $th;- if(!index && index !== 0){- $th = $table_ths.siblings('#' + identifier);- index = $table_ths.index($th);- }- else{- $th = $table_ths.eq(index);- }- return {index: index, $e: $th};- };-- var getTableRowsFromTableStructure = function(table_structure, sort_info){- // Gather individual column for callbacks- var column = $.map(table_structure, function(ele, i){- return [[ele.columns[sort_info.th_index], ele.$tr, i]];- });-- /* Side effect */- sort_info.column = column;-- // Replace the content of tbody with the sorted rows. Strangely- // enough, .append accomplishes this for us.- return $.map(table_structure, function(ele) { return ele.$tr; });-- };-- var updateElementData = function(sort_info){- var $table = sort_info.$table;- var $this_th = sort_info.$th;- var sort_dir = $this_th.data('sort-dir');- var th_index = sort_info.th_index;--- // Reset siblings- $table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc");- $this_th.data("sort-dir", sort_dir).addClass("sorting-"+sort_dir);- };-- var calculateSortDir = function(force_direction, sort_info){- var sort_dir;- var $this_th = sort_info.$th;- var dir = $.fn.stupidtable.dir;-- if(!!force_direction){- sort_dir = force_direction;- }- else{- sort_dir = force_direction || $this_th.data("sort-default") || dir.ASC;- if ($this_th.data("sort-dir"))- sort_dir = $this_th.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC;- }- return sort_dir;- };-- var calculateTHIndex = function(sort_info){- var th_index = 0;- var base_index = sort_info.$th.index();- sort_info.$th.parents("tr").find("th").slice(0, base_index).each(function() {- var cols = $(this).attr("colspan") || 1;- th_index += parseInt(cols,10);- });- return th_index;- };--})(jQuery);diff --git a/app/assets/javascripts/blazer/tablesort.js b/app/assets/javascripts/blazer/tablesort.jsnew file mode 100644index 00000000..b1499b13--- /dev/null+++ b/app/assets/javascripts/blazer/tablesort.js@@ -0,0 +1,42 @@+function removeCommas(string) {+ return string.replace(/,/g, "")+}++function compareInt(a, b) {+ return parseInt(removeCommas(a), 10) - parseInt(removeCommas(b), 10)+}++function compareFloat(a, b) {+ return parseFloat(removeCommas(a)) - parseFloat(removeCommas(b))+}++function compareString(a, b) {+ return a.localeCompare(b, undefined, {sensitivity: "base"})+}++function tablesort(element) {+ let lastIndex = -1+ let reverse = false++ element.addEventListener("click", function (e) {+ const target = e.target.closest("th")+ if (target) {+ const columnIndex = Array.from(element.querySelectorAll("thead th")).indexOf(target)+ const sortType = target.getAttribute("data-sort")+ const compareFn = sortType == "int" ? compareInt : (sortType == "float" ? compareFloat : compareString)+ reverse = columnIndex != lastIndex ? false : !reverse++ const tbody = element.querySelector("tbody")+ const rows = Array.from(tbody.querySelectorAll("tr"))+ rows.sort(function (a, b) {+ const av = a.children[columnIndex].textContent+ const bv = b.children[columnIndex].textContent+ const r = compareFn(av, bv)+ return reverse ? -r : r+ })+ tbody.replaceChildren(...rows)++ lastIndex = columnIndex+ }+ })+}diff --git a/app/views/blazer/dashboards/show.html.erb b/app/views/blazer/dashboards/show.html.erbindex 2d8308b7..67e274a3 100644--- a/app/views/blazer/dashboards/show.html.erb+++ b/app/views/blazer/dashboards/show.html.erb@@ -47,7 +47,10 @@const chart = document.getElementById(chartId)runQuery(runData, function (data) {$(chart).html(data)- $(chart.querySelector("table")).stupidtable(stupidtableCustomSettings)+ const table = chart.querySelector("table")+ if (table) {+ tablesort(table)+ }}, function (message) {chart.classList.add("query-error")chart.textContent = messagediff --git a/app/views/blazer/queries/show.html.erb b/app/views/blazer/queries/show.html.erbindex e29de30e..7052a4e8 100644--- a/app/views/blazer/queries/show.html.erb+++ b/app/views/blazer/queries/show.html.erb@@ -51,7 +51,8 @@function showRun(data) {$(results).html(data)- $(results.querySelector("table")).stupidtable(stupidtableCustomSettings).stickyTableHeaders({fixedOffset: 60})+ tablesort(results.querySelector("table"))+ $(results.querySelector("table")).stickyTableHeaders({fixedOffset: 60})}function showError(message) {diff --git a/app/views/layouts/blazer/application.html.erb b/app/views/layouts/blazer/application.html.erbindex 8a8c1a2b..84099212 100644--- a/app/views/layouts/blazer/application.html.erb+++ b/app/views/layouts/blazer/application.html.erb@@ -7,7 +7,7 @@<%= favicon_link_tag "blazer/favicon.png" %><% if defined?(Propshaft::Railtie) && Rails.application.assets.is_a?(Propshaft::Assembly) %><%= stylesheet_link_tag "blazer/bootstrap-propshaft", "blazer/bootstrap", "blazer/selectize", "blazer/github", "blazer/daterangepicker", "blazer/application", nonce: true %>- <%= javascript_include_tag "blazer/jquery", "blazer/rails-ujs", "blazer/stupidtable", "blazer/stupidtable-custom-settings", "blazer/jquery.stickytableheaders", "blazer/selectize", "blazer/highlight.min", "blazer/moment", "blazer/moment-timezone-with-data", "blazer/daterangepicker", "blazer/chart.umd", "blazer/chartjs-adapter-date-fns.bundle", "blazer/chartkick", "blazer/mapkick.bundle", "blazer/ace/ace", "blazer/ace/ext-language_tools", "blazer/ace/theme-twilight", "blazer/ace/mode-sql", "blazer/ace/snippets/text", "blazer/ace/snippets/sql", "blazer/Sortable", "blazer/bootstrap", "blazer/routes", "blazer/queries", "blazer/fuzzysearch", "blazer/application", nonce: true %>+ <%= javascript_include_tag "blazer/jquery", "blazer/rails-ujs", "blazer/tablesort", "blazer/jquery.stickytableheaders", "blazer/selectize", "blazer/highlight.min", "blazer/moment", "blazer/moment-timezone-with-data", "blazer/daterangepicker", "blazer/chart.umd", "blazer/chartjs-adapter-date-fns.bundle", "blazer/chartkick", "blazer/mapkick.bundle", "blazer/ace/ace", "blazer/ace/ext-language_tools", "blazer/ace/theme-twilight", "blazer/ace/mode-sql", "blazer/ace/snippets/text", "blazer/ace/snippets/sql", "blazer/Sortable", "blazer/bootstrap", "blazer/routes", "blazer/queries", "blazer/fuzzysearch", "blazer/application", nonce: true %><% else %><%= stylesheet_link_tag "blazer/application", nonce: true %><%= javascript_include_tag "blazer/application", nonce: true %>diff --git a/licenses/LICENSE-stupidtable.txt b/licenses/LICENSE-stupidtable.txtdeleted file mode 100644index f177ee6c..00000000--- a/licenses/LICENSE-stupidtable.txt+++ /dev/null@@ -1,19 +0,0 @@-Copyright (c) 2012 Joseph McCullough--Permission is hereby granted, free of charge, to any person obtaining a copy-of this software and associated documentation files (the "Software"), to deal-in the Software without restriction, including without limitation the rights-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell-copies of the Software, and to permit persons to whom the Software is-furnished to do so, subject to the following conditions:--The above copyright notice and this permission notice shall be included in all-copies or substantial portions of the Software.--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE-SOFTWARE.