diff options
author | Michał Zimniewicz <michzimny@users.noreply.github.com> | 2018-02-04 18:10:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-04 18:10:43 +0100 |
commit | 46652656fc42df252cd8cadc82ced2f6e002bdc3 (patch) | |
tree | 8a536c03fe3a01dc2514129c3ad3a24e853ce900 /sklady/tdd.js | |
parent | 64b94f2831022d09f7a5025139c857e0d0517ce7 (diff) | |
parent | 4e91dd3b096afbd8e05fa052001589308e29405b (diff) |
Merge pull request #5 from emkael/single-table-visualization
Wyróżnianie rozkładów pojedynczego stołu
Diffstat (limited to 'sklady/tdd.js')
-rw-r--r-- | sklady/tdd.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/sklady/tdd.js b/sklady/tdd.js new file mode 100644 index 0000000..bc75356 --- /dev/null +++ b/sklady/tdd.js @@ -0,0 +1,74 @@ +var TDD = { + + rowSelector: 'body > table > tbody > tr', + rowHeaderClass: 'tdd-header', + + findTable: function(element) { + var row = element.closest(TDD.rowSelector); + var headerRow = row; + while (headerRow.length && !headerRow.hasClass(TDD.rowHeaderClass)) { + headerRow = headerRow.prev(TDD.rowSelector); + } + var rows = []; + if (headerRow) { + rows.push(headerRow[0]); + headerRow = headerRow.next(TDD.rowSelector); + while (headerRow.length && !headerRow.hasClass(TDD.rowHeaderClass)) { + rows.push(headerRow[0]); + headerRow = headerRow.next(TDD.rowSelector); + } + } + return $(rows); + }, + + highlightTable: function(elem) { + TDD.findTable(elem).addClass('specified'); + }, + + hoverTable: function(ev) { + TDD.findTable($(ev.currentTarget)).addClass('hovered'); + }, + + unhoverTable: function(ev) { + TDD.findTable($(ev.currentTarget)).removeClass('hovered'); + }, + + switchTable: function(ev) { + var header = TDD.findTable($(ev.currentTarget)).find('h4[id]'); + location.hash = header.attr('id'); + ev.stopPropagation(); + }, + + detectReferer: function() { + var regex = document.referrer.match(/\d+t(\d+)-\d+\.htm/); + if (regex) { + return regex[1]; + } + return undefined; + }, + + bindEvents: function() { + $('tr').hover(TDD.hoverTable, TDD.unhoverTable); + $('tr').click(TDD.switchTable); + $(window).on('hashchange', function() { + var table = $(location.hash); + if (table.length) { + $('.specified').removeClass('specified'); + TDD.highlightTable(table); + } else { + var tableNo = TDD.detectReferer(); + if (tableNo) { + location.hash = '#table-' + tableNo; + } else { + $('h4[id]').each(function() { TDD.highlightTable($(this)); }); + } + } + }); + } + +}; + +$(document).ready(function() { + TDD.bindEvents(); + $(window).trigger('hashchange'); +}); |