rwd-table.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*!
  2. * Responsive Tables v5.3.2 (http://gergeo.se/RWD-Table-Patterns)
  3. * This is an awesome solution for responsive tables with complex data.
  4. * Authors: Nadan Gergeo <nadan@blimp.se> (www.blimp.se), Lucas Wiener <lucas@blimp.se> & "Maggie Wachs (www.filamentgroup.com)"
  5. * Licensed under MIT (https://github.com/nadangergeo/RWD-Table-Patterns/blob/master/LICENSE-MIT)
  6. */
  7. (function ($) {
  8. 'use strict';
  9. // RESPONSIVE TABLE CLASS DEFINITION
  10. // ==========================
  11. var ResponsiveTable = function(element, options) {
  12. // console.time('init');
  13. var that = this;
  14. this.options = options;
  15. this.$tableWrapper = null; //defined later in wrapTable
  16. this.$tableScrollWrapper = $(element); //defined later in wrapTable
  17. this.$table = $(element).find('table');
  18. if(this.$table.length !== 1) {
  19. throw new Error('Exactly one table is expected in a .table-responsive div.');
  20. }
  21. //apply pattern option as data-attribute, in case it was set via js
  22. this.$tableScrollWrapper.attr('data-pattern', this.options.pattern);
  23. //if the table doesn't have a unique id, give it one.
  24. //The id will be a random hexadecimal value, prefixed with id.
  25. //Used for triggers with displayAll button.
  26. this.id = this.$table.prop('id') || this.$tableScrollWrapper.prop('id') || 'id' + Math.random().toString(16).slice(2);
  27. this.$tableClone = null; //defined farther down
  28. this.$stickyTableHeader = null; //defined farther down
  29. //good to have - for easy access
  30. this.$thead = this.$table.find('thead');
  31. this.$hdrCells = this.$thead.find("tr").first().find('th');
  32. this.$bodyRows = this.$table.find('tbody, tfoot').find('tr');
  33. //toolbar and buttons
  34. this.$btnToolbar = null; //defined farther down
  35. this.$dropdownGroup = null; //defined farther down
  36. this.$dropdownBtn = null; //defined farther down
  37. this.$dropdownContainer = null; //defined farther down
  38. this.$displayAllBtn = null; //defined farther down
  39. this.$focusGroup = null; //defined farther down
  40. this.$focusBtn = null; //defined farther down
  41. //misc
  42. this.displayAllTrigger = 'display-all-' + this.id + '.responsive-table';
  43. this.idPrefix = this.id + '-col-';
  44. this.headerColIndices = {};
  45. this.headerRowIndices = {};
  46. // Setup table
  47. // -------------------------
  48. //wrap table
  49. this.wrapTable();
  50. //create toolbar with buttons
  51. this.createButtonToolbar();
  52. //Build header indices mapping (for colspans in header)
  53. this.buildHeaderCellIndices();
  54. // Setup cells
  55. // -------------------------
  56. //setup header
  57. this.setupTableHeader();
  58. //setup standard cells
  59. this.setupBodyRows();
  60. //create sticky table head
  61. if(this.options.stickyTableHeader){
  62. this.createStickyTableHeader();
  63. }
  64. // hide toggle button if the list is empty
  65. if(this.$dropdownContainer.is(':empty')){
  66. this.$dropdownGroup.hide();
  67. }
  68. // Event binding
  69. // -------------------------
  70. // on orientchange, resize and displayAllBtn-click
  71. $(window).bind('orientationchange resize ' + this.displayAllTrigger, function(){
  72. //update the inputs' checked status
  73. that.$dropdownContainer.find('input').trigger('updateCheck');
  74. //update colspan and visibility of spanning cells
  75. $.proxy(that.updateSpanningCells(), that);
  76. }).trigger('resize');
  77. // console.timeEnd('init');
  78. };
  79. ResponsiveTable.DEFAULTS = {
  80. pattern: 'priority-columns',
  81. stickyTableHeader: true,
  82. fixedNavbar: '.navbar-fixed-top', // Is there a fixed navbar? The stickyTableHeader needs to know about it!
  83. addDisplayAllBtn: true, // should it have a display-all button?
  84. addFocusBtn: true, // should it have a focus button?
  85. focusBtnIcon: 'glyphicon glyphicon-screenshot',
  86. mainContainer: window,
  87. i18n: {
  88. focus : 'Focus',
  89. display : 'Display',
  90. displayAll: 'Display all'
  91. }
  92. };
  93. // Wrap table
  94. ResponsiveTable.prototype.wrapTable = function() {
  95. this.$tableScrollWrapper.wrap('<div class="table-wrapper"/>');
  96. this.$tableWrapper = this.$tableScrollWrapper.parent();
  97. };
  98. // Create toolbar with buttons
  99. ResponsiveTable.prototype.createButtonToolbar = function() {
  100. var that = this;
  101. this.$btnToolbar = $('[data-responsive-table-toolbar="' + this.id + '"]').addClass('btn-toolbar');
  102. if(this.$btnToolbar.length === 0) {
  103. this.$btnToolbar = $('<div class="btn-toolbar" />');
  104. }
  105. this.$dropdownGroup = $('<div class="btn-group dropdown-btn-group pull-right" />');
  106. this.$dropdownBtn = $('<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">' + this.options.i18n.display + '</button>');
  107. this.$dropdownContainer = $('<ul class="dropdown-menu"/>');
  108. // Focus btn
  109. if(this.options.addFocusBtn) {
  110. // Create focus btn group
  111. this.$focusGroup = $('<div class="btn-group focus-btn-group" />');
  112. // Create focus btn
  113. this.$focusBtn = $('<button type="button" class="btn btn-sm btn-default">' + this.options.i18n.focus + '</button>');
  114. if(this.options.focusBtnIcon) {
  115. this.$focusBtn.prepend('<span class="' + this.options.focusBtnIcon + '"></span> ');
  116. }
  117. // Add btn to group
  118. this.$focusGroup.append(this.$focusBtn);
  119. // Add focus btn to toolbar
  120. this.$btnToolbar.append(this.$focusGroup);
  121. // bind click on focus btn
  122. this.$focusBtn.click(function(){
  123. $.proxy(that.activateFocus(), that);
  124. });
  125. // bind click on rows
  126. this.$bodyRows.click(function(){
  127. $.proxy(that.focusOnRow($(this)), that);
  128. });
  129. }
  130. // Display-all btn
  131. if(this.options.addDisplayAllBtn) {
  132. // Create display-all btn
  133. this.$displayAllBtn = $('<button type="button" class="btn btn-sm btn-default">' + this.options.i18n.displayAll + '</button>');
  134. // Add display-all btn to dropdown-btn-group
  135. this.$dropdownGroup.append(this.$displayAllBtn);
  136. if (this.$table.hasClass('display-all')) {
  137. // add 'btn-primary' class to btn to indicate that display all is activated
  138. this.$displayAllBtn.addClass('btn-primary');
  139. }
  140. // bind click on display-all btn
  141. this.$displayAllBtn.click(function(){
  142. $.proxy(that.displayAll(null, true), that);
  143. });
  144. }
  145. //add dropdown btn and menu to dropdown-btn-group
  146. this.$dropdownGroup.append(this.$dropdownBtn).append(this.$dropdownContainer);
  147. //add dropdown group to toolbar
  148. this.$btnToolbar.append(this.$dropdownGroup);
  149. // add toolbar above table
  150. // this.$tableScrollWrapper.before(this.$btnToolbar);
  151. };
  152. ResponsiveTable.prototype.clearAllFocus = function() {
  153. this.$bodyRows.removeClass('unfocused');
  154. this.$bodyRows.removeClass('focused');
  155. };
  156. ResponsiveTable.prototype.activateFocus = function() {
  157. // clear all
  158. this.clearAllFocus();
  159. if(this.$focusBtn){
  160. this.$focusBtn.toggleClass('btn-primary');
  161. }
  162. this.$table.toggleClass('focus-on');
  163. };
  164. ResponsiveTable.prototype.focusOnRow = function(row) {
  165. // only if activated (.i.e the table has the class focus-on)
  166. if(this.$table.hasClass('focus-on')) {
  167. var alreadyFocused = $(row).hasClass('focused');
  168. // clear all
  169. this.clearAllFocus();
  170. if(!alreadyFocused) {
  171. this.$bodyRows.addClass('unfocused');
  172. $(row).addClass('focused');
  173. }
  174. }
  175. };
  176. /**
  177. * @param activate Forces the displayAll to be active or not. If anything else than bool, it will not force the state so it will toggle as normal.
  178. * @param trigger Bool to indicate if the displayAllTrigger should be triggered.
  179. */
  180. ResponsiveTable.prototype.displayAll = function(activate, trigger) {
  181. if(this.$displayAllBtn){
  182. // add 'btn-primary' class to btn to indicate that display all is activated
  183. this.$displayAllBtn.toggleClass('btn-primary', activate);
  184. }
  185. this.$table.toggleClass('display-all', activate);
  186. if(this.$tableClone){
  187. this.$tableClone.toggleClass('display-all', activate);
  188. }
  189. if(trigger) {
  190. $(window).trigger(this.displayAllTrigger);
  191. }
  192. };
  193. ResponsiveTable.prototype.preserveDisplayAll = function() {
  194. var displayProp = 'table-cell';
  195. if($('html').hasClass('lt-ie9')){
  196. displayProp = 'inline';
  197. }
  198. $(this.$table).find('th, td').css('display', displayProp);
  199. if(this.$tableClone){
  200. $(this.$tableClone).find('th, td').css('display', displayProp);
  201. }
  202. };
  203. ResponsiveTable.prototype.createStickyTableHeader = function() {
  204. var that = this;
  205. //clone table head
  206. that.$tableClone = that.$table.clone();
  207. //replace ids
  208. that.$tableClone.prop('id', this.id + '-clone');
  209. that.$tableClone.find('[id]').each(function() {
  210. $(this).prop('id', $(this).prop('id') + '-clone');
  211. });
  212. // wrap table clone (this is our "sticky table header" now)
  213. that.$tableClone.wrap('<div class="sticky-table-header"/>');
  214. that.$stickyTableHeader = that.$tableClone.parent();
  215. // give the sticky table header same height as original
  216. that.$stickyTableHeader.css('height', that.$thead.height() + 2);
  217. //insert sticky table header
  218. that.$table.before(that.$stickyTableHeader);
  219. // bind scroll on mainContainer with updateStickyTableHeader
  220. $(this.options.mainContainer).bind('scroll', function(){
  221. $.proxy(that.updateStickyTableHeader(), that);
  222. });
  223. // bind resize on window with updateStickyTableHeader
  224. $(window).bind('resize', function(e){
  225. $.proxy(that.updateStickyTableHeader(), that);
  226. });
  227. $(that.$tableScrollWrapper).bind('scroll', function(){
  228. $.proxy(that.updateStickyTableHeader(), that);
  229. });
  230. // determine what solution to use for rendereing sticky table head (aboslute/fixed).
  231. that.useFixedSolution = !isIOS() || (getIOSVersion() >= 8);
  232. //add class for rendering solution
  233. if(that.useFixedSolution) {
  234. that.$tableScrollWrapper.addClass('fixed-solution');
  235. } else {
  236. that.$tableScrollWrapper.addClass('absolute-solution');
  237. }
  238. };
  239. // Help function for sticky table header
  240. ResponsiveTable.prototype.updateStickyTableHeader = function() {
  241. var that = this,
  242. top = 0,
  243. offsetTop = that.$table.offset().top,
  244. scrollTop = $(this.options.mainContainer).scrollTop() -1, //-1 to accomodate for top border
  245. maxTop = that.$table.height() - that.$stickyTableHeader.height(),
  246. rubberBandOffset = (scrollTop + $(this.options.mainContainer).height()) - $(document).height(),
  247. navbarHeight = 0;
  248. //Is there a fixed navbar?
  249. if($(that.options.fixedNavbar).length) {
  250. var $navbar = $(that.options.fixedNavbar).first();
  251. navbarHeight = $navbar.height();
  252. scrollTop = scrollTop + navbarHeight;
  253. }
  254. var shouldBeVisible;
  255. if(this.options.mainContainer === window) {
  256. shouldBeVisible = (scrollTop > offsetTop) && (scrollTop < offsetTop + that.$table.height());
  257. } else {
  258. shouldBeVisible = (offsetTop <= 0) && (-offsetTop < that.$table.height());
  259. }
  260. // console.log('offsetTop:' + offsetTop);
  261. // console.log('scrollTop:' + scrollTop);
  262. // console.log('tableHeight:' + that.$table.height());
  263. // console.log('shouldBeVisible:' + shouldBeVisible);
  264. if(that.useFixedSolution) { //fixed solution
  265. that.$stickyTableHeader.scrollLeft(that.$tableScrollWrapper.scrollLeft());
  266. // Calculate top property value (-1 to accomodate for top border)
  267. top = navbarHeight - 1;
  268. // When the user is about to scroll past the table, move sticky table head up
  269. if(this.options.mainContainer === window && ((scrollTop - offsetTop) > maxTop)){
  270. top -= ((scrollTop - offsetTop) - maxTop);
  271. that.$stickyTableHeader.addClass('border-radius-fix');
  272. } else if(this.options.mainContainer !== window && ((- offsetTop) > maxTop)){
  273. top -= ((- offsetTop) - maxTop);
  274. that.$stickyTableHeader.addClass('border-radius-fix');
  275. } else {
  276. that.$stickyTableHeader.removeClass('border-radius-fix');
  277. }
  278. if (shouldBeVisible) {
  279. //show sticky table header and update top and width.
  280. that.$stickyTableHeader.css({ 'visibility': 'visible', 'top': top + 'px', 'width': that.$tableScrollWrapper.innerWidth() + 'px'});
  281. //no more stuff to do - return!
  282. return;
  283. } else {
  284. //hide sticky table header and reset width
  285. that.$stickyTableHeader.css({'visibility': 'hidden', 'width': 'auto' });
  286. }
  287. } else { // alternate method
  288. //animation duration
  289. var animationDuration = 400;
  290. // Calculate top property value (-1 to accomodate for top border)
  291. if(this.options.mainContainer === window) {
  292. top = scrollTop - offsetTop - 1;
  293. } else {
  294. top = -offsetTop - 1;
  295. // console.log('top:' + top);
  296. }
  297. // Make sure the sticky table header doesn't slide up/down too far.
  298. if(top < 0) {
  299. top = 0;
  300. } else if (top > maxTop) {
  301. top = maxTop;
  302. }
  303. // Accomandate for rubber band effect
  304. if(this.options.mainContainer === window) {
  305. if(rubberBandOffset > 0) {
  306. top = top - rubberBandOffset;
  307. }
  308. }
  309. if (shouldBeVisible) {
  310. //show sticky table header (animate repositioning)
  311. that.$stickyTableHeader.css({ 'visibility': 'visible' });
  312. that.$stickyTableHeader.animate({ 'top': top + 'px' }, animationDuration);
  313. // hide original table head
  314. that.$thead.css({ 'visibility': 'hidden' });
  315. } else {
  316. that.$stickyTableHeader.animate({ 'top': '0' }, animationDuration, function(){
  317. // show original table head
  318. that.$thead.css({ 'visibility': 'visible' });
  319. // hide sticky table head
  320. that.$stickyTableHeader.css({ 'visibility': 'hidden' });
  321. });
  322. }
  323. }
  324. };
  325. // Setup header cells
  326. ResponsiveTable.prototype.setupTableHeader = function() {
  327. var that = this;
  328. // for each header column
  329. that.$hdrCells.each(function(i){
  330. var $th = $(this),
  331. id = $th.prop('id'),
  332. thText = $th.text();
  333. // assign an id to each header, if none is in the markup
  334. if (!id) {
  335. id = that.idPrefix + i;
  336. $th.prop('id', id);
  337. }
  338. if(thText === ''){
  339. thText = $th.attr('data-col-name');
  340. }
  341. // create the hide/show toggle for the current column
  342. if ( $th.is('[data-priority]') && $th.data('priority') !== -1 ) {
  343. var $toggle = $('<li class="checkbox-row"><input type="checkbox" name="toggle-'+id+'" id="toggle-'+id+'" value="'+id+'" /> <label for="toggle-'+id+'">'+ thText +'</label></li>');
  344. var $checkbox = $toggle.find('input');
  345. that.$dropdownContainer.append($toggle);
  346. $toggle.click(function(){
  347. // console.log("cliiiick!");
  348. $checkbox.prop('checked', !$checkbox.prop('checked'));
  349. $checkbox.trigger('change');
  350. });
  351. //Freakin' IE fix
  352. if ($('html').hasClass('lt-ie9')) {
  353. $checkbox.click(function() {
  354. $(this).trigger('change');
  355. });
  356. }
  357. $toggle.find('label').click(function(event){
  358. event.stopPropagation();
  359. });
  360. $toggle.find('input')
  361. .click(function(event){
  362. event.stopPropagation();
  363. })
  364. .change(function(){ // bind change event on checkbox
  365. var $checkbox = $(this),
  366. val = $checkbox.val(),
  367. //all cells under the column, including the header and its clone
  368. $cells = that.$tableWrapper.find('#' + val + ', #' + val + '-clone, [data-columns~='+ val +']');
  369. //if display-all is on - save state and carry on
  370. if(that.$table.hasClass('display-all')){
  371. //save state
  372. $.proxy(that.preserveDisplayAll(), that);
  373. //remove display all class
  374. that.$table.removeClass('display-all');
  375. if(that.$tableClone){
  376. that.$tableClone.removeClass('display-all');
  377. }
  378. //switch off button
  379. that.$displayAllBtn.removeClass('btn-primary');
  380. }
  381. // loop through the cells
  382. $cells.each(function(){
  383. var $cell = $(this);
  384. // is the checkbox checked now?
  385. if ($checkbox.is(':checked')) {
  386. // if the cell was already visible, it means its original colspan was >1
  387. // so let's increment the colspan
  388. // This should not be done for th's in thead.
  389. if(!$cell.closest("thead").length && $cell.css('display') !== 'none'){
  390. // make sure new colspan value does not exceed original colspan value
  391. var newColSpan = Math.min(parseInt($cell.prop('colSpan')) + 1, $cell.attr('data-org-colspan'));
  392. // update colspan
  393. $cell.prop('colSpan', newColSpan);
  394. }
  395. // show cell
  396. $cell.show();
  397. }
  398. // checkbox has been unchecked
  399. else {
  400. // decrement colSpan if it's not 1 (because colSpan should not be 0)
  401. // This should not be done for th's in thead.
  402. if(!$cell.closest("thead").length && parseInt($cell.prop('colSpan'))>1){
  403. $cell.prop('colSpan', parseInt($cell.prop('colSpan')) - 1);
  404. }
  405. // otherwise, hide the cell
  406. else {
  407. $cell.hide();
  408. }
  409. }
  410. });
  411. })
  412. .bind('updateCheck', function(){
  413. if ( $th.css('display') !== 'none') {
  414. $(this).prop('checked', true);
  415. }
  416. else {
  417. $(this).prop('checked', false);
  418. }
  419. });
  420. } // end if
  421. }); // end hdrCells loop
  422. if(!$.isEmptyObject(this.headerRowIndices)) {
  423. that.setupRow(this.$thead.find("tr:eq(1)"), this.headerRowIndices);
  424. }
  425. };
  426. // Setup body rows
  427. // assign matching "data-columns" attributes to the associated cells "(cells with colspan>1 has multiple columns).
  428. ResponsiveTable.prototype.setupBodyRows = function() {
  429. var that = this;
  430. // for each body rows
  431. that.$bodyRows.each(function(){
  432. that.setupRow($(this), that.headerColIndices);
  433. });
  434. };
  435. ResponsiveTable.prototype.setupRow = function($row, indices) {
  436. var that = this;
  437. //check if it's already set up
  438. if($row.data('setup')){
  439. // don't do anything
  440. return;
  441. } else {
  442. $row.data('setup', true);
  443. }
  444. var idStart = 0;
  445. // for each cell
  446. $row.find('th, td').each(function(){
  447. var $cell = $(this);
  448. var columnsAttr = '';
  449. var colSpan = $cell.prop('colSpan');
  450. $cell.attr('data-org-colspan', colSpan);
  451. // if colSpan is more than 1
  452. if(colSpan > 1) {
  453. //give it the class 'spn-cell';
  454. $cell.addClass('spn-cell');
  455. }
  456. // loop through columns that the cell spans over
  457. for (var k = idStart; k < (idStart + colSpan); k++) {
  458. // add column id
  459. columnsAttr = columnsAttr + ' ' + that.idPrefix + indices[k];
  460. // get column header
  461. var $colHdr = that.$table.find('#' + that.idPrefix + indices[k]);
  462. // copy data-priority attribute from column header
  463. var dataPriority = $colHdr.attr('data-priority');
  464. if (dataPriority) { $cell.attr('data-priority', dataPriority); }
  465. }
  466. //remove whitespace in begining of string.
  467. columnsAttr = columnsAttr.substring(1);
  468. //set attribute to cell
  469. $cell.attr('data-columns', columnsAttr);
  470. //increment idStart with the current cells colSpan.
  471. idStart = idStart + colSpan;
  472. });
  473. };
  474. ResponsiveTable.prototype.buildHeaderCellIndices = function() {
  475. var that = this;
  476. var rowspansBeforeIndex = {};
  477. this.headerColIndices = {};
  478. this.headerRowIndices = {};
  479. var colPadding = 0;
  480. var rowPadding = 0;
  481. this.$thead.find("tr").first().find('th').each(function(i){
  482. var $th = $(this);
  483. var colSpan = $th.prop('colSpan');
  484. var rowSpan = $th.prop("rowSpan");
  485. for(var index = 0; index < colSpan; index++) {
  486. that.headerColIndices[colPadding + i + index] = i;
  487. if(colPadding + i + index >= 0) {
  488. rowspansBeforeIndex[colPadding + i + index - rowPadding] = rowPadding;
  489. }
  490. }
  491. if(rowSpan > 1) {
  492. rowPadding++;
  493. }
  494. colPadding += colSpan - 1;
  495. });
  496. if(this.$thead.find("tr").length > 2) {
  497. throw new Error("This plugin doesnt support more than two rows in thead.");
  498. }
  499. if(this.$thead.find("tr").length === 2) {
  500. var $row = $(this.$thead.find("tr")[1]);
  501. $row.find("th").each(function(cellIndex) {
  502. that.headerRowIndices[cellIndex] = that.headerColIndices[rowspansBeforeIndex[cellIndex] + cellIndex];
  503. });
  504. }
  505. };
  506. // Run this after the content in tbody has changed
  507. ResponsiveTable.prototype.update = function() {
  508. this.$bodyRows = this.$table.find('tbody, tfoot').find('tr');
  509. this.setupBodyRows();
  510. // Remove old tbody clone from Tableclone
  511. this.$tableClone.find('tbody, tfoot').remove();
  512. // Make new clone of tbody
  513. var $tbodyClone = this.$table.find('tbody, tfoot').clone();
  514. //replace ids
  515. $tbodyClone.find('[id]').each(function() {
  516. $(this).prop('id', $(this).prop('id') + '-clone');
  517. });
  518. // Append new clone to tableClone
  519. $tbodyClone.appendTo(this.$tableClone);
  520. // Make sure columns visibility is in sync,
  521. // by triggering a (non-changing) change event on all checkboxes
  522. this.$dropdownContainer.find('input').trigger('change');
  523. // ¯\(°_o)/¯ I dunno if this is needed
  524. // this.updateSpanningCells();
  525. };
  526. // Update colspan and visibility of spanning cells
  527. ResponsiveTable.prototype.updateSpanningCells = function() {
  528. var that = this;
  529. // iterate through cells with class 'spn-cell'
  530. that.$table.find('.spn-cell').each( function(){
  531. var $cell = $(this);
  532. var columnsAttr = $cell.attr('data-columns').split(' ');
  533. var colSpan = columnsAttr.length;
  534. var numOfHidden = 0;
  535. for (var i = 0; i < colSpan; i++) {
  536. if($('#' + columnsAttr[i]).css('display')==='none'){
  537. numOfHidden++;
  538. }
  539. }
  540. // if one of the columns that the cell belongs to is visible then show the cell
  541. if(numOfHidden !== colSpan){
  542. $cell.show();
  543. } else {
  544. $cell.hide(); //just in case
  545. }
  546. // console.log('numOfHidden: ' + numOfHidden);
  547. // console.log("new colSpan:" +Math.max((colSpan - numOfHidden),1));
  548. //update colSpan to match number of visible columns that i belongs to
  549. $cell.prop('colSpan',Math.max((colSpan - numOfHidden),1));
  550. });
  551. };
  552. // RESPONSIVE TABLE PLUGIN DEFINITION
  553. // ===========================
  554. var old = $.fn.responsiveTable;
  555. $.fn.responsiveTable = function (option) {
  556. return this.each(function () {
  557. var $this = $(this);
  558. var data = $this.data('responsiveTable');
  559. var options = $.extend({}, ResponsiveTable.DEFAULTS, $this.data(), typeof option === 'object' && option);
  560. if(options.pattern === '') {
  561. return;
  562. }
  563. if (!data) {
  564. $this.data('responsiveTable', (data = new ResponsiveTable(this, options)));
  565. }
  566. if (typeof option === 'string') {
  567. data[option]();
  568. }
  569. });
  570. };
  571. $.fn.responsiveTable.Constructor = ResponsiveTable;
  572. // RESPONSIVE TABLE NO CONFLICT
  573. // =====================
  574. $.fn.responsiveTable.noConflict = function () {
  575. $.fn.responsiveTable = old;
  576. return this;
  577. };
  578. // RESPONSIVE TABLE DATA-API
  579. // ==================
  580. $(document).on('ready.responsive-table.data-api', function () {
  581. $('.table-responsive[data-pattern]').each(function () {
  582. var $tableScrollWrapper = $(this);
  583. $tableScrollWrapper.responsiveTable($tableScrollWrapper.data());
  584. });
  585. });
  586. // DROPDOWN
  587. // ==========================
  588. // Prevent dropdown from closing when toggling checkbox
  589. $(document).on('click.dropdown.data-api', '.dropdown-menu .checkbox-row', function (e) {
  590. e.stopPropagation();
  591. });
  592. // FEATURE DETECTION (instead of Modernizr)
  593. // ==========================
  594. // media queries
  595. function mediaQueriesSupported() {
  596. return (typeof window.matchMedia !== 'undefined' || typeof window.msMatchMedia !== 'undefined' || typeof window.styleMedia !== 'undefined');
  597. }
  598. // touch
  599. function hasTouch() {
  600. return 'ontouchstart' in window;
  601. }
  602. // Checks if current browser is on IOS.
  603. function isIOS() {
  604. return !!(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i));
  605. }
  606. // Gets iOS version number. If the user is not on iOS, the function returns 0.
  607. function getIOSVersion() {
  608. if(isIOS()){
  609. var iphone_version = parseFloat(('' + (/CPU.*OS ([0-9_]{1,5})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0,''])[1]).replace('undefined', '3_2').replace('_', '.').replace('_', ''));
  610. return iphone_version;
  611. } else {
  612. return 0;
  613. }
  614. }
  615. $(document).ready(function() {
  616. // Change `no-js` to `js`
  617. $('html').removeClass('no-js').addClass('js');
  618. // Add mq/no-mq class to html
  619. if(mediaQueriesSupported()) {
  620. $('html').addClass('mq');
  621. } else {
  622. $('html').addClass('no-mq');
  623. }
  624. // Add touch/no-touch class to html
  625. if(hasTouch()) {
  626. $('html').addClass('touch');
  627. } else {
  628. $('html').addClass('no-touch');
  629. }
  630. });
  631. })(jQuery);