aboutsummaryrefslogtreecommitdiff
path: root/_sass/minimal-mistakes/vendor/breakpoint/_parsers.scss
blob: f0b053feebc1b08f3a0a66d3b9f8c293574ad380 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//////////////////////////////
// Import Parser Pieces
//////////////////////////////
@import "parsers/query";
@import "parsers/single";
@import "parsers/double";
@import "parsers/triple";
@import "parsers/resolution";

$Memo-Exists: function-exists(memo-get) and function-exists(memo-set);

//////////////////////////////
// Breakpoint Function
//////////////////////////////
@function breakpoint($query, $contexts...) {
  $run: true;
  $return: ();

  // Grab the Memo Output if Memoization can be a thing
  @if $Memo-Exists {
    $return: memo-get(breakpoint, breakpoint $query $contexts);

    @if $return != null {
      $run: false;
    }
  }

  @if not $Memo-Exists or $run {
    // Internal Variables
    $query-string: '';
    $query-fallback: false;
    $return: ();

    // Reserve Global Private Breakpoint Context
    $holder-context: $private-breakpoint-context-holder;
    $holder-query-count: $private-breakpoint-query-count;

    // Reset Global Private Breakpoint Context
    $private-breakpoint-context-holder: () !global;
    $private-breakpoint-query-count: 0 !global;


    // Test to see if it's a comma-separated list
    $or-list: if(list-separator($query) == 'comma', true, false);


    @if ($or-list == false and breakpoint-get('legacy syntax') == false) {
      $query-string: breakpoint-parse($query);
    }
    @else {
      $length: length($query);

      $last: nth($query, $length);
      $query-fallback: breakpoint-no-query($last);

      @if ($query-fallback != false) {
        $length: $length - 1;
      }

      @if (breakpoint-get('legacy syntax') == true) {
        $mq: ();

        @for $i from 1 through $length {
          $mq: append($mq, nth($query, $i), comma);
        }

        $query-string: breakpoint-parse($mq);
      }
      @else {
        $query-string: '';
        @for $i from 1 through $length {
          $query-string: $query-string + if($i == 1, '', ', ') + breakpoint-parse(nth($query, $i));
        }
      }
    }

    $return: ('query': $query-string,
        'fallback': $query-fallback,
        'context holder': $private-breakpoint-context-holder,
        'query count': $private-breakpoint-query-count
    );
    @if length($contexts) > 0 and nth($contexts, 1) != false {
      @if $query-fallback != false {
        $context-setter: private-breakpoint-set-context('no-query', $query-fallback);
      }
      $context-map: ();
      @each $context in $contexts {
        $context-map: map-merge($context-map, ($context: breakpoint-get-context($context)));
      }
      $return: map-merge($return, (context: $context-map));
    }

    // Reset Global Private Breakpoint Context
    $private-breakpoint-context-holder: () !global;
    $private-breakpoint-query-count: 0 !global;

    @if $Memo-Exists {
      $holder: memo-set(breakpoint, breakpoint $query $contexts, $return);
    }
  }

  @return $return;
}

//////////////////////////////
// General Breakpoint Parser
//////////////////////////////
@function breakpoint-parse($query) {
  // Increase number of 'and' queries
  $private-breakpoint-query-count: $private-breakpoint-query-count + 1 !global;

  // Set up Media Type
  $query-print: '';

  $force-all: ((breakpoint-get('force all media type') == true) and (breakpoint-get('default media') == 'all'));
  $empty-media: true;
  @if ($force-all == true) or (breakpoint-get('default media') != 'all') {
    // Force the print of the default media type if (force all is true and default media type is all) or (default media type is not all)
    $query-print: breakpoint-get('default media');
    $empty-media: false;
  }


  $query-resolution: false;

  $query-holder: breakpoint-parse-query($query);



  // Loop over each parsed out query and write it to $query-print
  $first: true;

  @each $feature in $query-holder {
    $length: length($feature);

    // Parse a single feature
    @if ($length == 1) {
      // Feature is currently a list, grab the actual value
      $feature: nth($feature, 1);

      // Media Type must by convention be the first item, so it's safe to flat override $query-print, which right now should only be the default media type
      @if (breakpoint-is-media($feature)) {
        @if ($force-all == true) or ($feature != 'all') {
          // Force the print of the default media type if (force all is true and default media type is all) or (default media type is not all)
          $query-print: $feature;
          $empty-media: false;

          // Set Context
          $context-setter: private-breakpoint-set-context(media, $query-print);
        }
      }
      @else {
        $parsed: breakpoint-parse-single($feature, $empty-media, $first);
        $query-print: '#{$query-print} #{$parsed}';
        $first: false;
      }
    }
    // Parse a double feature
    @else if ($length == 2) {
      @if (breakpoint-is-resolution($feature) != false) {
        $query-resolution: $feature;
      }
      @else {
        $parsed: null;
        // If it's a string/number pair,
        // we check to see if one is a single-string value,
        // then we parse it as a normal double
        $alpha: nth($feature, 1);
        $beta: nth($feature, 2);
        @if breakpoint-single-string($alpha) or breakpoint-single-string($beta) {
          $parsed: breakpoint-parse-single($alpha, $empty-media, $first);
          $query-print: '#{$query-print} #{$parsed}';
          $first: false;
          $parsed: breakpoint-parse-single($beta, $empty-media, $first);
          $query-print: '#{$query-print} #{$parsed}';
        }
        @else {
          $parsed: breakpoint-parse-double($feature, $empty-media, $first);
          $query-print: '#{$query-print} #{$parsed}';
          $first: false;
        }
      }
    }
    // Parse a triple feature
    @else if ($length == 3) {
      $parsed: breakpoint-parse-triple($feature, $empty-media, $first);
      $query-print: '#{$query-print} #{$parsed}';
      $first: false;
    }

  }

  @if ($query-resolution != false) {
    $query-print: breakpoint-build-resolution($query-print, $query-resolution, $empty-media, $first);
  }

  // Loop through each feature that's been detected so far and append 'false' to the the value list to increment their counters
  @each $f, $v in $private-breakpoint-context-holder {
    $v-holder: $v;
    $length: length($v-holder);
    @if length($v-holder) < $private-breakpoint-query-count {
      @for $i from $length to $private-breakpoint-query-count {
        @if $f == 'media' {
          $v-holder: append($v-holder, breakpoint-get('default media'));
        }
        @else {
          $v-holder: append($v-holder, false);
        }
      }
    }
    $private-breakpoint-context-holder: map-merge($private-breakpoint-context-holder, ($f: $v-holder)) !global;
  }

  @return $query-print;
}