BigW Consortium Gitlab

breakpoints.js 1.99 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, one-var, no-var, one-var-declaration-per-line, quotes, no-shadow, prefer-arrow-callback, prefer-template, consistent-return, no-return-assign, new-parens, no-param-reassign, max-len */
2

3 4
var Breakpoints = (function() {
  var BreakpointInstance, instance;
Fatih Acet committed
5

6
  function Breakpoints() {}
Fatih Acet committed
7

8
  instance = null;
Fatih Acet committed
9

10 11
  BreakpointInstance = (function() {
    var BREAKPOINTS;
Fatih Acet committed
12

13
    BREAKPOINTS = ["xs", "sm", "md", "lg"];
Fatih Acet committed
14

15 16 17
    function BreakpointInstance() {
      this.setup();
    }
Fatih Acet committed
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32
    BreakpointInstance.prototype.setup = function() {
      var allDeviceSelector, els;
      allDeviceSelector = BREAKPOINTS.map(function(breakpoint) {
        return ".device-" + breakpoint;
      });
      if ($(allDeviceSelector.join(",")).length) {
        return;
      }
      // Create all the elements
      els = $.map(BREAKPOINTS, function(breakpoint) {
        return "<div class='device-" + breakpoint + " visible-" + breakpoint + "'></div>";
      });
      return $("body").append(els.join(''));
    };
Fatih Acet committed
33

34 35 36 37 38 39 40
    BreakpointInstance.prototype.visibleDevice = function() {
      var allDeviceSelector;
      allDeviceSelector = BREAKPOINTS.map(function(breakpoint) {
        return ".device-" + breakpoint;
      });
      return $(allDeviceSelector.join(",")).filter(":visible");
    };
Fatih Acet committed
41

42 43 44 45 46 47 48 49 50 51
    BreakpointInstance.prototype.getBreakpointSize = function() {
      var $visibleDevice;
      $visibleDevice = this.visibleDevice;
      // TODO: Consider refactoring in light of turbolinks removal.
      // the page refreshed via turbolinks
      if (!$visibleDevice().length) {
        this.setup();
      }
      $visibleDevice = this.visibleDevice();
      return $visibleDevice.attr("class").split("visible-")[1];
Fatih Acet committed
52 53
    };

54
    return BreakpointInstance;
Fatih Acet committed
55 56
  })();

57 58 59 60 61 62 63 64
  Breakpoints.get = function() {
    return instance != null ? instance : instance = new BreakpointInstance;
  };

  return Breakpoints;
})();

$(() => { window.bp = Breakpoints.get(); });
Fatih Acet committed
65

66
window.Breakpoints = Breakpoints;