|
| 1 | +interface Rule { |
| 2 | + selector: string; |
| 3 | + rule: any; |
| 4 | +} |
| 5 | + |
| 6 | +type Condition = { |
| 7 | + query: string; |
| 8 | + rules: Array<Rule>; |
| 9 | + children: ConditionalRuleset; |
| 10 | +}; |
| 11 | + |
| 12 | +export class ConditionalRuleset { |
| 13 | + ruleset: Array<Condition>; |
| 14 | + /** |
| 15 | + * Stores information about where conditions must in relation to other conditions |
| 16 | + * |
| 17 | + * e.g. mobile -> tablet, desktop |
| 18 | + */ |
| 19 | + precedenceLookup: Map<string, Set<String>>; |
| 20 | + |
| 21 | + constructor() { |
| 22 | + this.ruleset = []; |
| 23 | + this.precedenceLookup = new Map(); |
| 24 | + } |
| 25 | + |
| 26 | + findOrCreateCondition(conditionQuery: string) { |
| 27 | + let targetCondition = this.ruleset.find( |
| 28 | + (cond) => cond.query === conditionQuery, |
| 29 | + ); |
| 30 | + |
| 31 | + if (!targetCondition) { |
| 32 | + // No target condition so create one |
| 33 | + targetCondition = { |
| 34 | + query: conditionQuery, |
| 35 | + rules: [], |
| 36 | + children: new ConditionalRuleset(), |
| 37 | + }; |
| 38 | + this.ruleset.push(targetCondition); |
| 39 | + } |
| 40 | + |
| 41 | + return targetCondition; |
| 42 | + } |
| 43 | + |
| 44 | + getConditionalRulesetByPath(conditionPath: Array<string>) { |
| 45 | + let currRuleset: ConditionalRuleset = this; |
| 46 | + |
| 47 | + for (const query of conditionPath) { |
| 48 | + const condition = currRuleset.findOrCreateCondition(query); |
| 49 | + |
| 50 | + currRuleset = condition.children; |
| 51 | + } |
| 52 | + |
| 53 | + return currRuleset; |
| 54 | + } |
| 55 | + |
| 56 | + addRule(rule: Rule, conditionQuery: string, conditionPath: Array<string>) { |
| 57 | + const ruleset = this.getConditionalRulesetByPath(conditionPath); |
| 58 | + const targetCondition = ruleset.findOrCreateCondition(conditionQuery); |
| 59 | + |
| 60 | + if (!targetCondition) { |
| 61 | + throw new Error('Failed to add conditional rule'); |
| 62 | + } |
| 63 | + |
| 64 | + targetCondition.rules.push(rule); |
| 65 | + } |
| 66 | + |
| 67 | + addConditionPrecedence( |
| 68 | + conditionPath: Array<string>, |
| 69 | + conditionOrder: Array<string>, |
| 70 | + ) { |
| 71 | + const ruleset = this.getConditionalRulesetByPath(conditionPath); |
| 72 | + |
| 73 | + for (let i = 0; i < conditionOrder.length; i++) { |
| 74 | + const condition = conditionOrder[i]; |
| 75 | + |
| 76 | + const conditionPrecedence = |
| 77 | + ruleset.precedenceLookup.get(condition) ?? new Set(); |
| 78 | + |
| 79 | + for (const lowerPrecedenceCondition of conditionOrder.slice(i + 1)) { |
| 80 | + conditionPrecedence.add(lowerPrecedenceCondition); |
| 81 | + } |
| 82 | + |
| 83 | + ruleset.precedenceLookup.set(condition, conditionPrecedence); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + isCompatible(incomingRuleset: ConditionalRuleset) { |
| 88 | + for (const [ |
| 89 | + condition, |
| 90 | + orderPrecedence, |
| 91 | + ] of this.precedenceLookup.entries()) { |
| 92 | + for (const lowerPrecedenceCondition of orderPrecedence) { |
| 93 | + if ( |
| 94 | + incomingRuleset.precedenceLookup |
| 95 | + .get(lowerPrecedenceCondition as string) |
| 96 | + ?.has(condition) |
| 97 | + ) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Check that children are compatible |
| 104 | + for (const { query, children } of incomingRuleset.ruleset) { |
| 105 | + const matchingCondition = this.ruleset.find( |
| 106 | + (cond) => cond.query === query, |
| 107 | + ); |
| 108 | + |
| 109 | + if ( |
| 110 | + matchingCondition && |
| 111 | + !matchingCondition.children.isCompatible(children) |
| 112 | + ) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + merge(incomingRuleset: ConditionalRuleset) { |
| 121 | + // Merge rulesets into one array |
| 122 | + for (const { query, rules, children } of incomingRuleset.ruleset) { |
| 123 | + const matchingCondition = this.ruleset.find( |
| 124 | + (cond) => cond.query === query, |
| 125 | + ); |
| 126 | + |
| 127 | + if (matchingCondition) { |
| 128 | + matchingCondition.rules.push(...rules); |
| 129 | + |
| 130 | + matchingCondition.children.merge(children); |
| 131 | + } else { |
| 132 | + this.ruleset.push({ query, rules, children }); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Merge order precendeces |
| 137 | + for (const [ |
| 138 | + condition, |
| 139 | + incomingOrderPrecedence, |
| 140 | + ] of incomingRuleset.precedenceLookup.entries()) { |
| 141 | + const orderPrecendence = |
| 142 | + this.precedenceLookup.get(condition) ?? new Set(); |
| 143 | + |
| 144 | + this.precedenceLookup.set( |
| 145 | + condition, |
| 146 | + new Set([...orderPrecendence, ...incomingOrderPrecedence]), |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Merge another ConditionalRuleset into this one if they are compatible |
| 153 | + * |
| 154 | + * @returns true if successful, false if the ruleset is incompatible |
| 155 | + */ |
| 156 | + mergeIfCompatible(incomingRuleset: ConditionalRuleset) { |
| 157 | + if (!this.isCompatible(incomingRuleset)) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + this.merge(incomingRuleset); |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + sort() { |
| 167 | + this.ruleset.sort((a, b) => { |
| 168 | + const aWeights = this.precedenceLookup.get(a.query); |
| 169 | + |
| 170 | + if (aWeights?.has(b.query)) { |
| 171 | + // A is higher precedence |
| 172 | + return -1; |
| 173 | + } |
| 174 | + |
| 175 | + const bWeights = this.precedenceLookup.get(b.query); |
| 176 | + |
| 177 | + if (bWeights?.has(a.query)) { |
| 178 | + // B is higher precedence |
| 179 | + return 1; |
| 180 | + } |
| 181 | + |
| 182 | + return 0; |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + renderToObj() { |
| 187 | + // Sort rulesets according to required rule order |
| 188 | + this.sort(); |
| 189 | + |
| 190 | + const target: any = {}; |
| 191 | + |
| 192 | + for (const { query, rules, children } of this.ruleset) { |
| 193 | + target[query] = {}; |
| 194 | + |
| 195 | + for (const rule of rules) { |
| 196 | + target[query][rule.selector] = rule.rule; |
| 197 | + } |
| 198 | + |
| 199 | + Object.assign(target[query], children.renderToObj()); |
| 200 | + } |
| 201 | + |
| 202 | + return target; |
| 203 | + } |
| 204 | +} |
0 commit comments