|
2 | 2 | * Responsive Bootstrap Toolkit |
3 | 3 | * Author: Maciej Gurban |
4 | 4 | * License: MIT |
5 | | - * Version: 2.3.0 (2015-02-15) |
| 5 | + * Version: 2.4.0 (2015-04-12) |
6 | 6 | * Origin: https://github.com/maciej-gurban/responsive-bootstrap-toolkit |
7 | 7 | */ |
8 | 8 | ;var ResponsiveBootstrapToolkit = (function($){ |
9 | 9 |
|
10 | | - // Methods and properties |
| 10 | + // Internal methods |
| 11 | + var internal = { |
| 12 | + |
| 13 | + /** |
| 14 | + * Determines whether passed string is a parsable expression |
| 15 | + */ |
| 16 | + isAnExpression: function( str ) { |
| 17 | + return (str.charAt(0) == '<' || str.charAt(0) == '>'); |
| 18 | + }, |
| 19 | + |
| 20 | + /** |
| 21 | + * Splits the expression in into <|> [=] alias |
| 22 | + */ |
| 23 | + splitExpression: function( str ) { |
| 24 | + // Used operator |
| 25 | + var operator = (str.charAt(0) == '<' || str.charAt(0) == '>') ? str.charAt(0) : false; |
| 26 | + // Include breakpoints equal to X? |
| 27 | + var orEqual = (str.charAt(1) == '=') ? true : false |
| 28 | + |
| 29 | + // Cast to boolean, then to integer |
| 30 | + var index = (!!operator ? 1 : 0) + (orEqual ? 1 : 0); |
| 31 | + |
| 32 | + /** |
| 33 | + * The remaining part of the expression, after the operator, will be treated as |
| 34 | + * breakpoint name to compare with |
| 35 | + */ |
| 36 | + var breakpointName = str.slice(index); |
| 37 | + |
| 38 | + return { |
| 39 | + operator: operator, |
| 40 | + orEqual: orEqual, |
| 41 | + breakpointName: breakpointName |
| 42 | + }; |
| 43 | + }, |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns true if currently active breakpoint matches the expression |
| 47 | + */ |
| 48 | + isAnyActive: function( breakpoints ) { |
| 49 | + var found = false; |
| 50 | + $.each(breakpoints, function( index, alias ) { |
| 51 | + // Once first breakpoint matches, return true and break the out of the loop |
| 52 | + if( self.breakpoints[ alias ].is(':visible') ) { |
| 53 | + found = true; |
| 54 | + return false; |
| 55 | + } |
| 56 | + }); |
| 57 | + return found; |
| 58 | + }, |
| 59 | + |
| 60 | + /** |
| 61 | + * Determines whether current breakpoint matches the expression given |
| 62 | + */ |
| 63 | + isMatchingExpression: function( string ) { |
| 64 | + |
| 65 | + var expression = internal.splitExpression( string ); |
| 66 | + |
| 67 | + // Get names of all breakpoints |
| 68 | + var breakpointList = Object.keys(self.breakpoints); |
| 69 | + |
| 70 | + // Get index of sought breakpoint in the list |
| 71 | + var pos = breakpointList.indexOf( expression.breakpointName ); |
| 72 | + |
| 73 | + // Breakpoint found |
| 74 | + if( pos !== -1 ) { |
| 75 | + |
| 76 | + var start = 0; |
| 77 | + var end = 0; |
| 78 | + |
| 79 | + /** |
| 80 | + * Parsing viewport.is('<=md') we interate from smallest breakpoint ('xs') and end |
| 81 | + * at 'md' breakpoint, indicated in the expression, |
| 82 | + * That makes: start = 0, end = 2 (index of 'md' breakpoint) |
| 83 | + * |
| 84 | + * Parsing viewport.is('<md') we start at index 'xs' breakpoint, and end at |
| 85 | + * 'sm' breakpoint, one before 'md'. |
| 86 | + * Which makes: start = 0, end = 1 |
| 87 | + */ |
| 88 | + if( expression.operator == '<' ) { |
| 89 | + start = 0; |
| 90 | + end = expression.orEqual ? ++pos : pos; |
| 91 | + } |
| 92 | + /** |
| 93 | + * Parsing viewport.is('>=sm') we interate from breakpoint 'sm' and end at the end |
| 94 | + * of breakpoint list. |
| 95 | + * That makes: start = 1, end = undefined |
| 96 | + * |
| 97 | + * Parsing viewport.is('>sm') we start at breakpoint 'md' and end at the end of |
| 98 | + * breakpoint list. |
| 99 | + * Which makes: start = 2, end = undefined |
| 100 | + */ |
| 101 | + if( expression.operator == '>' ) { |
| 102 | + start = expression.orEqual ? pos : ++pos; |
| 103 | + end = undefined; |
| 104 | + } |
| 105 | + |
| 106 | + var acceptedBreakpoints = breakpointList.slice(start, end); |
| 107 | + |
| 108 | + return internal.isAnyActive( acceptedBreakpoints ); |
| 109 | + |
| 110 | + } |
| 111 | + }, |
| 112 | + |
| 113 | + }; |
| 114 | + |
| 115 | + // Public methods and properties |
11 | 116 | var self = { |
12 | 117 |
|
13 | 118 | /** |
|
16 | 121 | interval: 300, |
17 | 122 |
|
18 | 123 | /** |
19 | | - * Breakpoint aliases |
| 124 | + * Breakpoint aliases, listed from smallest to biggest |
20 | 125 | */ |
21 | 126 | breakpoints: { |
22 | 127 | 'xs': $('<div class="device-xs visible-xs"></div>').appendTo('body'), |
|
33 | 138 | /** |
34 | 139 | * Returns true if current breakpoint matches passed alias |
35 | 140 | */ |
36 | | - is: function( alias ) { |
37 | | - return self.breakpoints[alias].is(':visible'); |
| 141 | + is: function( str ) { |
| 142 | + |
| 143 | + if( internal.isAnExpression( str ) ) { |
| 144 | + return internal.isMatchingExpression( str ); |
| 145 | + } |
| 146 | + |
| 147 | + return self.breakpoints[ str ].is(':visible'); |
38 | 148 | }, |
39 | 149 |
|
40 | 150 | /** |
|
0 commit comments