diff options
Diffstat (limited to 'src/_contexts.scss')
| -rw-r--r-- | src/_contexts.scss | 114 |
1 files changed, 67 insertions, 47 deletions
diff --git a/src/_contexts.scss b/src/_contexts.scss index 9fe0e8c..8542056 100644 --- a/src/_contexts.scss +++ b/src/_contexts.scss | |||
| @@ -5,16 +5,18 @@ | |||
| 5 | /// It's an essential part for the BEM-related mixins. | 5 | /// It's an essential part for the BEM-related mixins. |
| 6 | /// | 6 | /// |
| 7 | /// If you want to create a new context, the easiest pattern is to create a new mixin and wrap | 7 | /// If you want to create a new context, the easiest pattern is to create a new mixin and wrap |
| 8 | /// the @content between a pair of iro-context-push and iro-context-pop. | 8 | /// the @content between a pair of push and pop. |
| 9 | /// From within the @content, you can access the context's data with iro-context-get. | 9 | /// From within the @content, you can access the context's data with get. |
| 10 | /// To make the compilation fail if a certain nesting order is violated, use | 10 | /// To make the compilation fail if a certain nesting order is violated, use |
| 11 | /// iro-context-assert-stack-must-contain and iro-context-assert-stack-must-not-contain. | 11 | /// assert-stack-must-contain and assert-stack-must-not-contain. |
| 12 | /// | 12 | /// |
| 13 | /// @group Contexts | 13 | /// @group Contexts |
| 14 | /// | 14 | /// |
| 15 | /// @access public | 15 | /// @access public |
| 16 | //// | 16 | //// |
| 17 | 17 | ||
| 18 | @use './functions'; | ||
| 19 | |||
| 18 | /// | 20 | /// |
| 19 | /// Map of all context stacks. | 21 | /// Map of all context stacks. |
| 20 | /// | 22 | /// |
| @@ -22,7 +24,7 @@ | |||
| 22 | /// | 24 | /// |
| 23 | /// @access private | 25 | /// @access private |
| 24 | /// | 26 | /// |
| 25 | $iro-context-stacks: (); | 27 | $stacks: (); |
| 26 | 28 | ||
| 27 | /// | 29 | /// |
| 28 | /// Create a new context stack. | 30 | /// Create a new context stack. |
| @@ -31,8 +33,8 @@ $iro-context-stacks: (); | |||
| 31 | /// | 33 | /// |
| 32 | /// @throw If context stack already exists | 34 | /// @throw If context stack already exists |
| 33 | /// | 35 | /// |
| 34 | @mixin iro-context-stack-create($stack-id) { | 36 | @mixin create($stack-id) { |
| 35 | $noop: iro-context-stack-create($stack-id); | 37 | $noop: create($stack-id); |
| 36 | } | 38 | } |
| 37 | 39 | ||
| 38 | /// | 40 | /// |
| @@ -40,12 +42,12 @@ $iro-context-stacks: (); | |||
| 40 | /// | 42 | /// |
| 41 | /// @param {string} $stack-id - ID of context stack | 43 | /// @param {string} $stack-id - ID of context stack |
| 42 | /// | 44 | /// |
| 43 | @function iro-context-stack-create($stack-id) { | 45 | @function create($stack-id) { |
| 44 | @if map-has-key($iro-context-stacks, $stack-id) { | 46 | @if map-has-key($stacks, $stack-id) { |
| 45 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 47 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 46 | } | 48 | } |
| 47 | 49 | ||
| 48 | $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: ())) !global; | 50 | $stacks: map-merge($stacks, ($stack-id: ())) !global; |
| 49 | 51 | ||
| 50 | @return null; | 52 | @return null; |
| 51 | } | 53 | } |
| @@ -55,8 +57,8 @@ $iro-context-stacks: (); | |||
| 55 | /// | 57 | /// |
| 56 | /// @param {string} $stack-id - ID of context stack | 58 | /// @param {string} $stack-id - ID of context stack |
| 57 | /// | 59 | /// |
| 58 | @mixin iro-context-stack-clear($stack-id) { | 60 | @mixin clear($stack-id) { |
| 59 | $noop: iro-context-stack-clear($stack-id); | 61 | $noop: clear($stack-id); |
| 60 | } | 62 | } |
| 61 | 63 | ||
| 62 | /// | 64 | /// |
| @@ -64,13 +66,13 @@ $iro-context-stacks: (); | |||
| 64 | /// | 66 | /// |
| 65 | /// @param {string} $stack-id - ID of context stack | 67 | /// @param {string} $stack-id - ID of context stack |
| 66 | /// | 68 | /// |
| 67 | @function iro-context-stack-clear($stack-id) { | 69 | @function clear($stack-id) { |
| 68 | @if not map-has-key($iro-context-stacks, $stack-id) { | 70 | @if not map-has-key($stacks, $stack-id) { |
| 69 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 71 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 70 | } | 72 | } |
| 71 | 73 | ||
| 72 | $context-stack: (); | 74 | $context-stack: (); |
| 73 | $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; | 75 | $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; |
| 74 | 76 | ||
| 75 | @return null; | 77 | @return null; |
| 76 | } | 78 | } |
| @@ -80,8 +82,8 @@ $iro-context-stacks: (); | |||
| 80 | /// | 82 | /// |
| 81 | /// @param {string} $stack-id - ID of context stack | 83 | /// @param {string} $stack-id - ID of context stack |
| 82 | /// | 84 | /// |
| 83 | @mixin iro-context-stack-delete($stack-id) { | 85 | @mixin delete($stack-id) { |
| 84 | $noop: iro-context-stack-delete($stack-id); | 86 | $noop: delete($stack-id); |
| 85 | } | 87 | } |
| 86 | 88 | ||
| 87 | /// | 89 | /// |
| @@ -89,12 +91,12 @@ $iro-context-stacks: (); | |||
| 89 | /// | 91 | /// |
| 90 | /// @param {string} $stack-id - ID of context stack | 92 | /// @param {string} $stack-id - ID of context stack |
| 91 | /// | 93 | /// |
| 92 | @function iro-context-stack-delete($stack-id) { | 94 | @function delete($stack-id) { |
| 93 | @if not map-has-key($iro-context-stacks, $stack-id) { | 95 | @if not map-has-key($stacks, $stack-id) { |
| 94 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 96 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 95 | } | 97 | } |
| 96 | 98 | ||
| 97 | $iro-context-stacks: map-remove($iro-context-stacks, $stack-id) !global; | 99 | $stacks: map-remove($stacks, $stack-id) !global; |
| 98 | 100 | ||
| 99 | @return null; | 101 | @return null; |
| 100 | } | 102 | } |
| @@ -106,8 +108,8 @@ $iro-context-stacks: (); | |||
| 106 | /// @param {string} $id - ID of new context | 108 | /// @param {string} $id - ID of new context |
| 107 | /// @param {any} $data [()] - Data that belongs to the context | 109 | /// @param {any} $data [()] - Data that belongs to the context |
| 108 | /// | 110 | /// |
| 109 | @mixin iro-context-push($stack-id, $id, $data: ()) { | 111 | @mixin push($stack-id, $id, $data: ()) { |
| 110 | $noop: iro-context-push($stack-id, $id, $data); | 112 | $noop: push($stack-id, $id, $data); |
| 111 | } | 113 | } |
| 112 | 114 | ||
| 113 | /// | 115 | /// |
| @@ -119,15 +121,15 @@ $iro-context-stacks: (); | |||
| 119 | /// | 121 | /// |
| 120 | /// @return {list} A list with two items: 1 = context id, 2 = context data | 122 | /// @return {list} A list with two items: 1 = context id, 2 = context data |
| 121 | /// | 123 | /// |
| 122 | @function iro-context-push($stack-id, $id, $data: ()) { | 124 | @function push($stack-id, $id, $data: ()) { |
| 123 | @if not map-has-key($iro-context-stacks, $stack-id) { | 125 | @if not map-has-key($stacks, $stack-id) { |
| 124 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 126 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 125 | } | 127 | } |
| 126 | 128 | ||
| 127 | $context: $id $data; | 129 | $context: $id $data; |
| 128 | $context-stack: map-get($iro-context-stacks, $stack-id); | 130 | $context-stack: map-get($stacks, $stack-id); |
| 129 | $context-stack: append($context-stack, $context); | 131 | $context-stack: append($context-stack, $context); |
| 130 | $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; | 132 | $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; |
| 131 | 133 | ||
| 132 | @return $context; | 134 | @return $context; |
| 133 | } | 135 | } |
| @@ -139,8 +141,8 @@ $iro-context-stacks: (); | |||
| 139 | /// | 141 | /// |
| 140 | /// @throw If context stack doesn't exist | 142 | /// @throw If context stack doesn't exist |
| 141 | /// | 143 | /// |
| 142 | @mixin iro-context-pop($stack-id) { | 144 | @mixin pop($stack-id) { |
| 143 | $noop: iro-context-pop($stack-id); | 145 | $noop: pop($stack-id); |
| 144 | } | 146 | } |
| 145 | 147 | ||
| 146 | /// | 148 | /// |
| @@ -150,12 +152,12 @@ $iro-context-stacks: (); | |||
| 150 | /// | 152 | /// |
| 151 | /// @return {list} A list with two items: 1 = context id, 2 = context data | 153 | /// @return {list} A list with two items: 1 = context id, 2 = context data |
| 152 | /// | 154 | /// |
| 153 | @function iro-context-pop($stack-id) { | 155 | @function pop($stack-id) { |
| 154 | @if not map-has-key($iro-context-stacks, $stack-id) { | 156 | @if not map-has-key($stacks, $stack-id) { |
| 155 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 157 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 156 | } | 158 | } |
| 157 | 159 | ||
| 158 | $context-stack: map-get($iro-context-stacks, $stack-id); | 160 | $context-stack: map-get($stacks, $stack-id); |
| 159 | 161 | ||
| 160 | @if length($context-stack) == 0 { | 162 | @if length($context-stack) == 0 { |
| 161 | @error 'Context stack "#{inspect($stack-id)}" is already empty.'; | 163 | @error 'Context stack "#{inspect($stack-id)}" is already empty.'; |
| @@ -166,10 +168,10 @@ $iro-context-stacks: (); | |||
| 166 | @if length($context-stack) == 1 { | 168 | @if length($context-stack) == 1 { |
| 167 | $context-stack: (); | 169 | $context-stack: (); |
| 168 | } @else { | 170 | } @else { |
| 169 | $context-stack: iro-list-slice($context-stack, 1, length($context-stack) - 1); | 171 | $context-stack: functions.list-slice($context-stack, 1, length($context-stack) - 1); |
| 170 | } | 172 | } |
| 171 | 173 | ||
| 172 | $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; | 174 | $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; |
| 173 | 175 | ||
| 174 | @return $popped-context; | 176 | @return $popped-context; |
| 175 | } | 177 | } |
| @@ -183,10 +185,16 @@ $iro-context-stacks: (); | |||
| 183 | /// | 185 | /// |
| 184 | /// @throw If assertion fails | 186 | /// @throw If assertion fails |
| 185 | /// | 187 | /// |
| 186 | @mixin iro-context-assert-stack-must-contain($stack-id, $context-ids, $check-head-only: false) { | 188 | @function assert-stack-must-contain($stack-id, $context-ids, $check-head-only: false) { |
| 187 | @if not iro-context-stack-contains($stack-id, $context-ids, $check-head-only) { | 189 | @if not contains($stack-id, $context-ids, $check-head-only) { |
| 188 | @error 'Must be called inside of contexts "#{inspect($context-ids)}".'; | 190 | @error 'Must be called inside of contexts "#{inspect($context-ids)}".'; |
| 189 | } | 191 | } |
| 192 | |||
| 193 | @return null; | ||
| 194 | } | ||
| 195 | |||
| 196 | @mixin assert-stack-must-contain($stack-id, $context-ids, $check-head-only: false) { | ||
| 197 | $noop: assert-stack-must-contain($stack-id, $context-ids, $check-head-only) | ||
| 190 | } | 198 | } |
| 191 | 199 | ||
| 192 | /// | 200 | /// |
| @@ -198,10 +206,16 @@ $iro-context-stacks: (); | |||
| 198 | /// | 206 | /// |
| 199 | /// @throw If assertion fails | 207 | /// @throw If assertion fails |
| 200 | /// | 208 | /// |
| 201 | @mixin iro-context-assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only: false) { | 209 | @function assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only: false) { |
| 202 | @if iro-context-stack-contains($stack-id, $context-ids, $check-head-only) { | 210 | @if contains($stack-id, $context-ids, $check-head-only) { |
| 203 | @error 'Must not be called inside of contexts "#{inspect($context-ids)}".'; | 211 | @error 'Must not be called inside of contexts "#{inspect($context-ids)}".'; |
| 204 | } | 212 | } |
| 213 | |||
| 214 | @return null; | ||
| 215 | } | ||
| 216 | |||
| 217 | @mixin assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only: false) { | ||
| 218 | $noop: assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only); | ||
| 205 | } | 219 | } |
| 206 | 220 | ||
| 207 | /// | 221 | /// |
| @@ -213,12 +227,12 @@ $iro-context-stacks: (); | |||
| 213 | /// | 227 | /// |
| 214 | /// @return {bool} `true` if the context stack contains one of the context IDs, otherwise `false` | 228 | /// @return {bool} `true` if the context stack contains one of the context IDs, otherwise `false` |
| 215 | /// | 229 | /// |
| 216 | @function iro-context-stack-contains($stack-id, $context-ids, $check-head-only: false) { | 230 | @function contains($stack-id, $context-ids, $check-head-only: false) { |
| 217 | @if not map-has-key($iro-context-stacks, $stack-id) { | 231 | @if not map-has-key($stacks, $stack-id) { |
| 218 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 232 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 219 | } | 233 | } |
| 220 | 234 | ||
| 221 | $context-stack: map-get($iro-context-stacks, $stack-id); | 235 | $context-stack: map-get($stacks, $stack-id); |
| 222 | 236 | ||
| 223 | @if length($context-stack) == 0 { | 237 | @if length($context-stack) == 0 { |
| 224 | @return false; | 238 | @return false; |
| @@ -247,10 +261,16 @@ $iro-context-stacks: (); | |||
| 247 | /// | 261 | /// |
| 248 | /// @throw If assertion fails | 262 | /// @throw If assertion fails |
| 249 | /// | 263 | /// |
| 250 | @mixin iro-context-assert-stack-count($stack-id, $max-count) { | 264 | @function assert-stack-count($stack-id, $max-count) { |
| 251 | @if iro-context-stack-count($stack-id) > $max-count { | 265 | @if count($stack-id) > $max-count { |
| 252 | @error 'Maximum context count "#{inspect($max-count)}" exceeded.'; | 266 | @error 'Maximum context count "#{inspect($max-count)}" exceeded.'; |
| 253 | } | 267 | } |
| 268 | |||
| 269 | @return null; | ||
| 270 | } | ||
| 271 | |||
| 272 | @mixin assert-stack-count($stack-id, $max-count) { | ||
| 273 | $noop: assert-stack-count($stack-id, $max-count); | ||
| 254 | } | 274 | } |
| 255 | 275 | ||
| 256 | /// | 276 | /// |
| @@ -260,12 +280,12 @@ $iro-context-stacks: (); | |||
| 260 | /// | 280 | /// |
| 261 | /// @return {number} The number of contexts | 281 | /// @return {number} The number of contexts |
| 262 | /// | 282 | /// |
| 263 | @function iro-context-stack-count($stack-id) { | 283 | @function count($stack-id) { |
| 264 | @if not map-has-key($iro-context-stacks, $stack-id) { | 284 | @if not map-has-key($stacks, $stack-id) { |
| 265 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 285 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 266 | } | 286 | } |
| 267 | 287 | ||
| 268 | $context-stack: map-get($iro-context-stacks, $stack-id); | 288 | $context-stack: map-get($stacks, $stack-id); |
| 269 | 289 | ||
| 270 | @return length($context-stack); | 290 | @return length($context-stack); |
| 271 | } | 291 | } |
| @@ -278,12 +298,12 @@ $iro-context-stacks: (); | |||
| 278 | /// | 298 | /// |
| 279 | /// @return {list} Null if no match was found, otherwise a list with two items: 1. context ID, 2. context data. | 299 | /// @return {list} Null if no match was found, otherwise a list with two items: 1. context ID, 2. context data. |
| 280 | /// | 300 | /// |
| 281 | @function iro-context-get($stack-id, $type-or-level: null) { | 301 | @function get($stack-id, $type-or-level: null) { |
| 282 | @if not map-has-key($iro-context-stacks, $stack-id) { | 302 | @if not map-has-key($stacks, $stack-id) { |
| 283 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 303 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
| 284 | } | 304 | } |
| 285 | 305 | ||
| 286 | $context-stack: map-get($iro-context-stacks, $stack-id); | 306 | $context-stack: map-get($stacks, $stack-id); |
| 287 | 307 | ||
| 288 | @if length($context-stack) == 0 { | 308 | @if length($context-stack) == 0 { |
| 289 | @return null; | 309 | @return null; |
