|
8 | 8 |
|
9 | 9 | 'use strict';
|
10 | 10 |
|
11 |
| -var layoutAttributes = require('./layout_attributes'); |
12 | 11 |
|
13 | 12 | module.exports = function handleCategoryOrderDefaults(containerIn, containerOut, coerce) {
|
| 13 | + if(containerOut.type !== 'category') return; |
14 | 14 |
|
15 |
| - if(containerIn.type !== 'category') return; |
| 15 | + var arrayIn = containerIn.categoryarray, |
| 16 | + orderDefault; |
16 | 17 |
|
17 |
| - var validCategories = layoutAttributes.categoryorder.values; |
| 18 | + var isValidArray = (Array.isArray(arrayIn) && arrayIn.length); |
18 | 19 |
|
19 |
| - var propercategoryarray = Array.isArray(containerIn.categoryarray) && containerIn.categoryarray.length > 0; |
| 20 | + // override default 'categoryorder' value when non-empty array is supplied |
| 21 | + if(isValidArray) orderDefault = 'array'; |
20 | 22 |
|
21 |
| - if(validCategories.indexOf(containerIn.categoryorder) === -1 && propercategoryarray) { |
| 23 | + var order = coerce('categoryorder', orderDefault); |
22 | 24 |
|
23 |
| - // when unspecified or invalid, use the default, unless categoryarray implies 'array' |
24 |
| - coerce('categoryorder', 'array'); // promote to 'array' |
25 |
| - |
26 |
| - } else if(containerIn.categoryorder === 'array' && !propercategoryarray) { |
27 |
| - |
28 |
| - // when mode is 'array' but no list is given, revert to default |
29 |
| - |
30 |
| - containerIn.categoryorder = 'trace'; // revert to default |
31 |
| - coerce('categoryorder'); |
32 |
| - |
33 |
| - } else { |
34 |
| - |
35 |
| - // otherwise use the supplied mode, or the default one if unsupplied or invalid |
36 |
| - coerce('categoryorder'); |
| 25 | + // coerce 'categoryarray' only in array order case |
| 26 | + if(order === 'array') coerce('categoryarray'); |
37 | 27 |
|
| 28 | + // cannot set 'categoryorder' to 'array' with an invalid 'categoryarray' |
| 29 | + if(!isValidArray && order === 'array') { |
| 30 | + containerOut.categoryorder = 'trace'; |
38 | 31 | }
|
39 | 32 | };
|
0 commit comments