rwd-table.js 28 KB

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