aboutsummaryrefslogtreecommitdiffstats
path: root/guides/assets/javascripts/responsive-tables.js
diff options
context:
space:
mode:
authorYoshiyuki Hirano <yhirano@me.com>2018-04-21 17:59:30 +0900
committerYoshiyuki Hirano <yhirano@me.com>2018-04-21 20:59:45 +0900
commit419adbf1a50d1b3009353d46c2c54eb7ea9f31c3 (patch)
tree7b4505a5f1d5d1d22cfbfe907cd64cca58bb33df /guides/assets/javascripts/responsive-tables.js
parentf6e3ca515df97088315eef8905bf2bf3ec8ebbcc (diff)
downloadrails-419adbf1a50d1b3009353d46c2c54eb7ea9f31c3.tar.gz
rails-419adbf1a50d1b3009353d46c2c54eb7ea9f31c3.tar.bz2
rails-419adbf1a50d1b3009353d46c2c54eb7ea9f31c3.zip
:scissors: jQuery for Rails Guides
* Rewrite with Vanilla JS * Confirmed with Chrome, Safari, Firefox
Diffstat (limited to 'guides/assets/javascripts/responsive-tables.js')
-rw-r--r--guides/assets/javascripts/responsive-tables.js74
1 files changed, 37 insertions, 37 deletions
diff --git a/guides/assets/javascripts/responsive-tables.js b/guides/assets/javascripts/responsive-tables.js
index 8554a1343b..3c90ba758c 100644
--- a/guides/assets/javascripts/responsive-tables.js
+++ b/guides/assets/javascripts/responsive-tables.js
@@ -1,43 +1,43 @@
-$(document).ready(function() {
+(function() {
+ "use strict";
var switched = false;
- $("table").not(".syntaxhighlighter").addClass("responsive");
+
+ document.querySelectorAll(":not(.syntaxhighlighter)>table").forEach(function(element) {
+ element.classList.add("responsive");
+ });
+
var updateTables = function() {
- if (($(window).width() < 767) && !switched ){
+ if (document.documentElement.clientWidth < 767 && !switched) {
switched = true;
- $("table.responsive").each(function(i, element) {
- splitTable($(element));
- });
- return true;
- }
- else if (switched && ($(window).width() > 767)) {
+ document.querySelectorAll("table.responsive").forEach(splitTable);
+ } else {
switched = false;
- $("table.responsive").each(function(i, element) {
- unsplitTable($(element));
- });
+ document.querySelectorAll(".table-wrapper table.responsive").forEach(unsplitTable);
}
- };
-
- $(window).load(updateTables);
- $(window).bind("resize", updateTables);
-
-
- function splitTable(original)
- {
- original.wrap("<div class='table-wrapper' />");
-
- var copy = original.clone();
- copy.find("td:not(:first-child), th:not(:first-child)").css("display", "none");
- copy.removeClass("responsive");
-
- original.closest(".table-wrapper").append(copy);
- copy.wrap("<div class='pinned' />");
- original.wrap("<div class='scrollable' />");
- }
-
- function unsplitTable(original) {
- original.closest(".table-wrapper").find(".pinned").remove();
- original.unwrap();
- original.unwrap();
- }
+ }
+
+ document.addEventListener("DOMContentLoaded", updateTables);
+ window.addEventListener("resize", updateTables);
+
+ var splitTable = function(original) {
+ wrap(original, createElement("div", "table-wrapper"));
+
+ var $copy = original.cloneNode(true);
+ $copy.querySelectorAll("td:not(:first-child), th:not(:first-child)").forEach(function(element) {
+ element.style.display = "none";
+ });
+ $copy.classList.remove("responsive");
+
+ original.parentNode.append($copy);
+ wrap($copy, createElement("div", "pinned"))
+ wrap(original, createElement("div", "scrollable"));
+ }
-});
+ var unsplitTable = function(original) {
+ document.querySelectorAll(".table-wrapper .pinned").forEach(function(element) {
+ element.parentNode.removeChild(element);
+ });
+ unwrap(original.parentNode);
+ unwrap(original);
+ }
+}).call(this);