Skip to content

Commit 13ca8a3

Browse files
committed
refactor: rename :group to :scope
1 parent 8e04852 commit 13ca8a3

File tree

8 files changed

+83
-83
lines changed

8 files changed

+83
-83
lines changed

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -342,26 +342,26 @@ If you want to use the variable across an element's attributes and events, you c
342342

343343
Like the example above, `:load` can be used to set the initial value of the variable.
344344

345-
### Group Variables
345+
### Scope Variables
346346

347-
Adding a `:group` attribute to an element will allow you to access its variables from its children using `group.` variables.
347+
Adding a `:scope` attribute to an element will allow you to access its variables from its children using `scope.` variables.
348348

349349
```html
350-
<!-- Group Element -->
351-
<div id="accordion" class="accordion" :group>
350+
<!-- Scope Element -->
351+
<div id="accordion" class="accordion" :scope>
352352
<!-- Children Elements -->
353353
<section
354354
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
355355
>
356356
<button
357-
:click="group.activeSection = 'about'"
357+
:click="scope.activeSection = 'about'"
358358
class="cursor-pointer font-bold p-4"
359359
>
360360
About Us
361361
</button>
362362
<div
363363
class="p-4 pt-2 overflow-hidden hidden"
364-
:class="group.activeSection =='about' ? 'block' : 'hidden'"
364+
:class="scope.activeSection =='about' ? 'block' : 'hidden'"
365365
>
366366
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
367367
eirmod.
@@ -372,14 +372,14 @@ Adding a `:group` attribute to an element will allow you to access its variables
372372
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
373373
>
374374
<button
375-
:click="group.activeSection = 'contact'"
375+
:click="scope.activeSection = 'contact'"
376376
class="cursor-pointer font-bold p-4"
377377
>
378378
Contact Us
379379
</button>
380380
<div
381381
class="p-4 pt-2 overflow-hidden"
382-
:class="group.activeSection =='contact' ? 'block' : 'hidden'"
382+
:class="scope.activeSection =='contact' ? 'block' : 'hidden'"
383383
>
384384
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
385385
eirmod.
@@ -388,17 +388,17 @@ Adding a `:group` attribute to an element will allow you to access its variables
388388

389389
<section
390390
class="grid transition-all border-gray-300 border rounded hover:bg-gray-100"
391-
:class="group.activeSection =='team' ? 'active' : ''"
391+
:class="scope.activeSection =='team' ? 'active' : ''"
392392
>
393393
<button
394-
:click="group.activeSection = 'team'"
394+
:click="scope.activeSection = 'team'"
395395
class="cursor-pointer font-bold p-4"
396396
>
397397
Team 3
398398
</button>
399399
<div
400400
class="p-4 pt-2 overflow-hidden"
401-
:class="group.activeSection =='team' ? 'block' : 'hidden'"
401+
:class="scope.activeSection =='team' ? 'block' : 'hidden'"
402402
>
403403
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
404404
eirmod.
@@ -407,10 +407,10 @@ Adding a `:group` attribute to an element will allow you to access its variables
407407
</div>
408408
```
409409

410-
You can set the default value of the group variables in the `:group` directive:
410+
You can set the default value of the scope variables in the `:scope` directive:
411411

412412
```html
413-
<div id="accordion" class="accordion" :group="activeSection = 'about'">
413+
<div id="accordion" class="accordion" :scope="activeSection = 'about'">
414414
<!-- ... -->
415415
</div>
416416
```

index.html

+10-10
Original file line numberDiff line numberDiff line change
@@ -1472,15 +1472,15 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
14721472
grid-template-rows: 0fr 1fr;
14731473
}
14741474
</style>
1475-
<div class="accordion" :group="activeSection = 'about'">
1475+
<div class="accordion" :scope="activeSection = 'about'">
14761476
<section
14771477
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
1478-
:class="group.activeSection == 'about' ? 'active' : ''"
1478+
:class="scope.activeSection == 'about' ? 'active' : ''"
14791479
>
14801480
<a
1481-
:click="group.activeSection = 'about'"
1481+
:click="scope.activeSection = 'about'"
14821482
class="cursor-pointer font-bold p-4"
1483-
:aria-expanded="group.activeSection == 'about'"
1483+
:aria-expanded="scope.activeSection == 'about'"
14841484
>
14851485
About Us
14861486
</a>
@@ -1494,12 +1494,12 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
14941494

14951495
<section
14961496
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
1497-
:class="group.activeSection =='contact' ? 'active' : ''"
1497+
:class="scope.activeSection =='contact' ? 'active' : ''"
14981498
>
14991499
<a
1500-
:click="group.activeSection = 'contact'"
1500+
:click="scope.activeSection = 'contact'"
15011501
class="cursor-pointer font-bold p-4"
1502-
:aria-expanded="group.activeSection == 'contact'"
1502+
:aria-expanded="scope.activeSection == 'contact'"
15031503
>
15041504
Contact Us
15051505
</a>
@@ -1513,11 +1513,11 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
15131513

15141514
<section
15151515
class="grid transition-all border-gray-300 border rounded hover:bg-gray-100"
1516-
:class="group.activeSection == 'team' ? 'active' : ''"
1516+
:class="scope.activeSection == 'team' ? 'active' : ''"
15171517
>
15181518
<a
1519-
:click="group.activeSection = 'team'"
1520-
:aria-expanded="group.activeSection == 'team'"
1519+
:click="scope.activeSection = 'team'"
1520+
:aria-expanded="scope.activeSection == 'team'"
15211521
class="cursor-pointer font-bold p-4"
15221522
>
15231523
Team 3

lib/entity.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ export class Entity {
2424

2525
if (this.base.debug) this.element.dataset.entityId = this.id
2626

27-
this.setAsGroup()
27+
this.setAsScope()
2828
}
2929

30-
setAsGroup() {
31-
if (!this.element.hasAttribute(':group')) return
32-
if (this.isGroup()) return
30+
setAsScope() {
31+
if (!this.element.hasAttribute(':scope')) return
32+
if (this.isScope()) return
3333

3434
this.uuid = this.id
3535
this.element.dataset['mini.uuid'] = this.uuid
3636
}
3737

38-
isGroup() {
38+
isScope() {
3939
return !!this.uuid
4040
}
4141

@@ -92,14 +92,14 @@ export class Entity {
9292
this.data.init()
9393
}
9494

95-
getGroup() {
96-
const groupNode = this.getClosestEl('data-mini.uuid')
95+
getScope() {
96+
const scopeNode = this.getClosestEl('data-mini.uuid')
9797

98-
if (groupNode == null) return { id: 'EntityDocument' }
98+
if (scopeNode == null) return { id: 'EntityDocument' }
9999

100100
const entities = Array.from(this.base.state.entities.values())
101101
const entity = entities.find(
102-
(e) => e.uuid == groupNode.dataset['mini.uuid']
102+
(e) => e.uuid == scopeNode.dataset['mini.uuid']
103103
)
104104

105105
return entity

lib/entity/attributes.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class Attributes {
1111
':checked',
1212
':each',
1313
':each.item',
14-
':group',
14+
':scope',
1515
]
1616
static FORBIDDEN_ATTRIBUTES = [':innerHTML', ':innerText']
1717

@@ -64,9 +64,9 @@ export class Attributes {
6464
const ids = {
6565
$: 'document-querySelector',
6666
el: `proxyWindow['${this.entity.id}${State.DISABLE_RE_RENDER_KEY}']`,
67-
group: this.entity.group
68-
? `proxyWindow['${this.entity.group.id}${
69-
!options.isGroup ? State.DISABLE_RE_RENDER_KEY : ''
67+
scope: this.entity.scope
68+
? `proxyWindow['${this.entity.scope.id}${
69+
!options.isScope ? State.DISABLE_RE_RENDER_KEY : ''
7070
}']`
7171
: undefined,
7272
...(options.ids || {}),
@@ -93,7 +93,7 @@ export class Attributes {
9393
}
9494

9595
async evaluate() {
96-
await this.evaluateAttribute(':group')
96+
await this.evaluateAttribute(':scope')
9797
await this.evaluateClass()
9898
await this.evaluateText()
9999
await this.evaluateValue()
@@ -109,7 +109,7 @@ export class Attributes {
109109

110110
async evaluateAttribute(attr) {
111111
if (!Attributes.isValidAttribute(attr, this.entity.element)) return
112-
if (attr === ':group') await this.evaluateGroup()
112+
if (attr === ':scope') await this.evaluateScope()
113113
else if (attr === ':class') await this.evaluateClass()
114114
else if (attr === ':text') await this.evaluateText()
115115
else if (attr === ':value') await this.evaluateValue()
@@ -148,29 +148,29 @@ export class Attributes {
148148
}
149149

150150
/*
151-
:group is a special attribute that acts as an :load event
151+
:scope is a special attribute that acts as an :load event
152152
when it has a given expr. Unlike other attributes, state updates
153-
inside :group will trigger re-renders.
153+
inside :scope will trigger re-renders.
154154
155155
NOTE: This should NOT be used in this.evaluate() because it will
156156
trigger an infinite loop.
157157
*/
158-
async evaluateGroup() {
159-
if (!this.entity.isGroup()) return
158+
async evaluateScope() {
159+
if (!this.entity.isScope()) return
160160

161-
const expr = this.entity.element.getAttribute(':group')
161+
const expr = this.entity.element.getAttribute(':scope')
162162
if (!expr) return
163163

164164
const ids = {}
165165

166-
this.entity.data.groupVariables.forEach((variable) => {
166+
this.entity.data.scopeVariables.forEach((variable) => {
167167
ids[variable] = `proxyWindow['${this.entity.id}'].${variable}`
168168
})
169169

170170
try {
171-
await this._interpret(expr, { isGroup: true, ids })
171+
await this._interpret(expr, { isScope: true, ids })
172172
} catch (error) {
173-
this._handleError(':group', expr, error)
173+
this._handleError(':scope', expr, error)
174174
}
175175
}
176176

lib/entity/data.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Data {
99
this.entity = entity
1010
this._variables = new Map() // key: variable, value: attributes
1111
this._attributes = new Map() // key: attribute, value: variables
12-
this.groupVariables = []
12+
this.scopeVariables = []
1313
}
1414

1515
get variables() {
@@ -54,15 +54,15 @@ export class Data {
5454
}
5555

5656
const lexer = new Lexer(expr)
57-
const isGroupAttr = attr === ':group'
57+
const isScopeAttr = attr === ':scope'
5858

5959
lexer.identifiers.forEach((variable) => {
6060
if (IGNORED_IDS.includes(variable)) return
6161

6262
const object = variable.split('.')[0]
6363
if (object in this.entity.state) return
6464

65-
if (isGroupAttr) this.groupVariables.push(variable)
65+
if (isScopeAttr) this.scopeVariables.push(variable)
6666
else this.add(variable, attr)
6767
})
6868
}
@@ -107,7 +107,7 @@ export class Data {
107107

108108
this.variables.forEach((variable) => {
109109
if (State.isElState(variable)) {
110-
this.entity.setAsGroup()
110+
this.entity.setAsScope()
111111

112112
if (window[entityID] == null)
113113
window[entityID] = state.create({}, entityID)
@@ -118,25 +118,25 @@ export class Data {
118118
const [_, varName] = variable.split('.')
119119
state.addEntityVariable(entityID, varName, entityID)
120120
}
121-
} else if (State.isGroupState(variable)) {
122-
if (this.entity.group == null)
123-
this.entity.group = this.entity.getGroup()
121+
} else if (State.isScopeState(variable)) {
122+
if (this.entity.scope == null)
123+
this.entity.scope = this.entity.getScope()
124124

125-
// Cases where group is not found:
126-
// - an each item with a :group directive being removed due to re-evaluation of :each attribute
127-
if (this.entity.group == null) return
125+
// Cases where scope is not found:
126+
// - an each item with a :scope directive being removed due to re-evaluation of :each attribute
127+
if (this.entity.scope == null) return
128128

129-
const groupID = this.entity.group?.id
129+
const scopeID = this.entity.scope?.id
130130

131-
if (window[groupID] == null) {
132-
window[groupID] = state.create({}, groupID)
131+
if (window[scopeID] == null) {
132+
window[scopeID] = state.create({}, scopeID)
133133
}
134134

135-
state.addVariable(groupID, entityID)
135+
state.addVariable(scopeID, entityID)
136136

137-
if (variable !== State.GROUP_STATE) {
137+
if (variable !== State.SCOPE_STATE) {
138138
const [_, varName] = variable.split('.')
139-
state.addEntityVariable(groupID, varName, entityID)
139+
state.addEntityVariable(scopeID, varName, entityID)
140140
}
141141
} else if (typeof window[variable] === 'function') {
142142
this.deleteVariable(variable)

lib/entity/events.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ export class Events {
440440
_attachVariableHelpers(attr) {
441441
const variables = []
442442
const elVariables = []
443-
const groupVariables = []
443+
const scopeVariables = []
444444

445445
this.entity.data.getAttributeVariables(attr).forEach((variable) => {
446446
const [_, object] = variable.split('.')
447447

448448
if (State.isElState(variable)) elVariables.push(object)
449-
else if (State.isGroupState(variable)) groupVariables.push(object)
449+
else if (State.isScopeState(variable)) scopeVariables.push(object)
450450
else variables.push(variable)
451451
})
452452

@@ -455,8 +455,8 @@ export class Events {
455455
state.attachVariableHelpers(variables)
456456
state.attachVariableHelpers(elVariables, this.entity.id)
457457

458-
if (this.entity.group)
459-
state.attachVariableHelpers(groupVariables, this.entity.group.id)
458+
if (this.entity.scope)
459+
state.attachVariableHelpers(scopeVariables, this.entity.scope.id)
460460
}
461461

462462
async _interpret(expr) {
@@ -467,10 +467,10 @@ export class Events {
467467
// "this" is set under the interpreter as bind context
468468
}
469469

470-
if (this.entity.group) ids.group = `proxyWindow['${this.entity.group.id}']`
470+
if (this.entity.scope) ids.scope = `proxyWindow['${this.entity.scope.id}']`
471471

472472
this.entity.data.variables.forEach((variable) => {
473-
if (State.isElState(variable) || State.isGroupState(variable)) return
473+
if (State.isElState(variable) || State.isScopeState(variable)) return
474474

475475
ids[variable] = `proxyWindow-${variable}`
476476
})

lib/generators/lexer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function getVariables(node) {
114114
export class Lexer {
115115
static debug = false
116116
static IGNORED_KEYS = ['event', 'window', 'document', 'console', 'Math']
117-
static ENTITY_KEYS = ['el', 'group']
117+
static ENTITY_KEYS = ['el', 'scope']
118118

119119
constructor(code) {
120120
this._code = code

0 commit comments

Comments
 (0)