@domql/element 2.3.117 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/extend.js ADDED
@@ -0,0 +1,87 @@
1
+ 'use strict'
2
+
3
+ import { isFunction, exec, isString } from '@domql/utils'
4
+ import {
5
+ getExtendStack,
6
+ jointStacks,
7
+ cloneAndMergeArrayExtend,
8
+ deepMergeExtend,
9
+ replaceStringsWithComponents
10
+ } from './utils'
11
+
12
+ const ENV = process.env.NODE_ENV
13
+
14
+ /**
15
+ * Checks whether element has `extend` or is a part
16
+ * of parent's `childExtend` extend
17
+ */
18
+ export const applyExtend = (element, parent, options = {}) => {
19
+ if (isFunction(element)) element = exec(element, parent)
20
+
21
+ let { extend, props, context, __ref } = element
22
+
23
+ const COMPONENTS = (context && context.components) || options.components
24
+ if (isString(extend)) {
25
+ if (COMPONENTS && COMPONENTS[extend]) extend = COMPONENTS[extend]
26
+ else {
27
+ if (ENV !== 'test' || ENV !== 'development') {
28
+ console.warn('Extend is string but component was not found:', extend)
29
+ } extend = {}
30
+ }
31
+ }
32
+
33
+ const extendStack = getExtendStack(extend)
34
+
35
+ if (ENV !== 'test' || ENV !== 'development') delete element.extend
36
+
37
+ let childExtendStack = []
38
+ if (parent) {
39
+ element.parent = parent
40
+ // Assign parent attr to the element
41
+ if (!options.ignoreChildExtend) {
42
+ if (props && props.ignoreChildExtend) return
43
+
44
+ childExtendStack = getExtendStack(parent.childExtend)
45
+
46
+ // if (parent.childExtendRecursive && (props && !props.ignoreChildExtendRecursive)) {
47
+ if (parent.childExtendRecursive) {
48
+ const canExtendRecursive = !props?.ignoreChildExtendRecursive && element.key !== '__text'
49
+ if (canExtendRecursive) {
50
+ const childExtendRecursiveStack = getExtendStack(parent.childExtendRecursive)
51
+ // add error if childExtendRecursive contains element which goes to infinite loop
52
+ childExtendStack = childExtendStack.concat(childExtendRecursiveStack)
53
+ element.childExtendRecursive = parent.childExtendRecursive
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ const extendLength = extendStack.length
60
+ const childExtendLength = childExtendStack.length
61
+
62
+ let stack = []
63
+ if (extendLength && childExtendLength) {
64
+ stack = jointStacks(extendStack, childExtendStack)
65
+ } else if (extendLength) {
66
+ stack = extendStack
67
+ } else if (childExtendLength) {
68
+ stack = childExtendStack
69
+ } else if (!options.extend) return element
70
+
71
+ if (options.extend) {
72
+ const defaultOptionsExtend = getExtendStack(options.extend)
73
+ stack = [].concat(stack, defaultOptionsExtend)
74
+ }
75
+
76
+ if (__ref) __ref.__extend = stack
77
+ const findAndReplaceStrings = replaceStringsWithComponents(stack, COMPONENTS)
78
+ let mergedExtend = cloneAndMergeArrayExtend(findAndReplaceStrings)
79
+
80
+ const component = exec(element.component || mergedExtend.component, element)
81
+ if (component && COMPONENTS && COMPONENTS[component]) {
82
+ const componentExtend = cloneAndMergeArrayExtend(getExtendStack(COMPONENTS[component]))
83
+ mergedExtend = deepMergeExtend(componentExtend, mergedExtend)
84
+ }
85
+
86
+ return deepMergeExtend(element, mergedExtend)
87
+ }
package/index.js CHANGED
@@ -1,7 +1,17 @@
1
1
  'use strict'
2
2
 
3
- export * from '@domql/methods'
4
- export * from '@domql/iterate'
5
- export * from '@domql/update'
6
- export * from '@domql/create'
7
- export * from '@domql/set'
3
+ import { TREE } from './tree'
4
+ import create from './create'
5
+ import createNode from './node'
6
+ import define from './define'
7
+ import update from './update'
8
+ import set from './set'
9
+
10
+ export {
11
+ TREE,
12
+ create,
13
+ createNode,
14
+ define,
15
+ update,
16
+ set
17
+ }
package/iterate.js ADDED
@@ -0,0 +1,94 @@
1
+ 'use strict'
2
+
3
+ import { isObject, exec, isFunction, isNumber, isString } from '@domql/utils'
4
+ import { METHODS_EXL, checkIfKeyIsComponent, extendizeByKey, isVariant, overwrite } from './utils'
5
+ import { isMethod } from './methods'
6
+
7
+ export const throughInitialExec = (element, exclude = {}) => {
8
+ const { __ref: ref } = element
9
+ for (const param in element) {
10
+ if (exclude[param]) continue
11
+ const prop = element[param]
12
+ if (isFunction(prop) && !isMethod(param) && !isVariant(param)) {
13
+ ref.__exec[param] = prop
14
+ element[param] = prop(element, element.state)
15
+ // if (isComponent)
16
+ }
17
+ }
18
+ }
19
+
20
+ export const throughUpdatedExec = (element, options = { excludes: METHODS_EXL }) => {
21
+ const { __ref: ref } = element
22
+ const changes = {}
23
+
24
+ for (const param in ref.__exec) {
25
+ const prop = element[param]
26
+
27
+ const isDefinedParam = ref.__defineCache[param]
28
+ if (isDefinedParam) continue
29
+
30
+ const newExec = ref.__exec[param](element, element.state, element.context)
31
+ const execReturnsString = isString(newExec) || isNumber(newExec)
32
+ // if (prop && prop.node && execReturnsString) {
33
+ if (prop && prop.node && execReturnsString) {
34
+ overwrite(prop, { text: newExec }, options)
35
+ } else if (newExec !== prop) {
36
+ if (checkIfKeyIsComponent(param)) {
37
+ const { extend, ...newElem } = extendizeByKey(newExec, element, param)
38
+ overwrite(prop, newElem, options)
39
+ // } else {
40
+ // overwrite(prop, newExec, options)
41
+ } else {
42
+ ref.__cached[param] = changes[param] = prop
43
+ element[param] = newExec
44
+ }
45
+ }
46
+ }
47
+
48
+ return changes
49
+ }
50
+
51
+ export const throughInitialDefine = (element) => {
52
+ const { define, context, __ref: ref } = element
53
+
54
+ let defineObj = {}
55
+ const hasGlobalDefine = context && isObject(context.define)
56
+ if (isObject(define)) defineObj = { ...define }
57
+ if (hasGlobalDefine) defineObj = { ...defineObj, ...context.define }
58
+
59
+ for (const param in defineObj) {
60
+ let elementProp = element[param]
61
+
62
+ if (isFunction(elementProp) && !isMethod(param) && !isVariant(param)) {
63
+ ref.__exec[param] = elementProp
64
+ const execParam = elementProp = exec(elementProp, element)
65
+
66
+ if (execParam) {
67
+ elementProp = element[param] = execParam.parse ? execParam.parse() : execParam
68
+ ref.__defineCache[param] = elementProp
69
+ }
70
+ }
71
+
72
+ const execParam = defineObj[param](elementProp, element, element.state, element.context)
73
+ if (execParam) element[param] = execParam
74
+ }
75
+ return element
76
+ }
77
+
78
+ export const throughUpdatedDefine = (element) => {
79
+ const { context, define, __ref: ref } = element
80
+ const changes = {}
81
+
82
+ let obj = {}
83
+ if (isObject(define)) obj = { ...define }
84
+ if (isObject(context && context.define)) obj = { ...obj, ...context.define }
85
+
86
+ for (const param in obj) {
87
+ const execParam = ref.__exec[param]
88
+ if (execParam) ref.__defineCache[param] = execParam(element, element.state, element.context)
89
+ const cached = exec(ref.__defineCache[param], element)
90
+ const newExecParam = obj[param](cached, element, element.state, element.context)
91
+ if (newExecParam) element[param] = newExecParam
92
+ }
93
+ return changes
94
+ }
package/node.js ADDED
@@ -0,0 +1,84 @@
1
+ 'use strict'
2
+
3
+ import { exec, isFunction, isObject, isUndefined } from '@domql/utils'
4
+ import { applyEventsOnNode, triggerEventOn } from '@domql/event'
5
+ import { isMethod } from './methods'
6
+ import { cacheNode } from '@domql/node'
7
+
8
+ import create from './create'
9
+
10
+ import {
11
+ throughInitialDefine,
12
+ throughInitialExec
13
+ } from './iterate'
14
+ import { registry } from './mixins'
15
+ import { applyParam } from './applyParam'
16
+ import { isVariant } from './utils'
17
+ // import { defineSetter } from './methods'
18
+
19
+ const ENV = process.env.NODE_ENV
20
+
21
+ export const createNode = (element, options) => {
22
+ // create and assign a node
23
+ let { node, tag, __ref: ref } = element
24
+
25
+ let isNewNode
26
+
27
+ if (!node) {
28
+ isNewNode = true
29
+
30
+ if (!ref.__if) return element
31
+
32
+ if (tag === 'shadow') {
33
+ node = element.node = element.parent.node.attachShadow({ mode: 'open' })
34
+ } else node = element.node = cacheNode(element)
35
+
36
+ // trigger `on.attachNode`
37
+ triggerEventOn('attachNode', element, options)
38
+ }
39
+
40
+ // node.dataset // .key = element.key
41
+
42
+ if (ENV === 'test' || ENV === 'development' || options.alowRefReference) {
43
+ node.ref = element
44
+ if (isFunction(node.setAttribute)) node.setAttribute('key', element.key)
45
+ }
46
+
47
+ if (!ref.__if) return element
48
+
49
+ // iterate through all given params
50
+ if (element.tag !== 'string' || element.tag !== 'fragment') {
51
+ // iterate through define
52
+ throughInitialDefine(element)
53
+
54
+ // iterate through exec
55
+ throughInitialExec(element)
56
+
57
+ // apply events
58
+ if (isNewNode && isObject(element.on)) applyEventsOnNode(element)
59
+
60
+ for (const param in element) {
61
+ const prop = element[param]
62
+
63
+ if (
64
+ isUndefined(prop) ||
65
+ isMethod(param) ||
66
+ isVariant(param) ||
67
+ isObject(registry[param])
68
+ ) continue
69
+
70
+ const isElement = applyParam(param, element, options)
71
+ if (isElement) {
72
+ const { hasDefine, hasContextDefine } = isElement
73
+ if (element[param] && !hasDefine && !hasContextDefine) {
74
+ create(exec(prop, element), element, param, options)
75
+ }
76
+ }
77
+ }
78
+ }
79
+
80
+ // node.dataset.key = key
81
+ return element
82
+ }
83
+
84
+ export default createNode
package/package.json CHANGED
@@ -1,15 +1,37 @@
1
1
  {
2
2
  "name": "@domql/element",
3
- "version": "2.3.117",
4
- "main": "index.js",
3
+ "version": "2.4.0",
5
4
  "license": "MIT",
5
+ "type": "module",
6
+ "module": "index.js",
7
+ "main": "index.js",
8
+ "exports": {
9
+ ".": {
10
+ "kalduna": "./index.js",
11
+ "default": "./dist/cjs/index.js"
12
+ }
13
+ },
14
+ "source": "index.js",
15
+ "files": [
16
+ "*.js",
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "copy:package:cjs": "cp ../../build/package-cjs.json dist/cjs/package.json",
21
+ "build:esm": "npx esbuild *.js --target=es2019 --format=esm --outdir=dist/esm",
22
+ "build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
23
+ "build": "yarn build:cjs",
24
+ "prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
25
+ },
6
26
  "dependencies": {
7
- "@domql/create": "latest",
8
- "@domql/iterate": "latest",
9
- "@domql/methods": "latest",
10
- "@domql/set": "latest",
11
- "@domql/update": "latest"
27
+ "@domql/event": "latest",
28
+ "@domql/node": "^2.3.125",
29
+ "@domql/render": "^2.4.0",
30
+ "@domql/state": "latest",
31
+ "@domql/utils": "latest"
12
32
  },
13
- "gitHead": "a6da9bf1846e4a1e26017978cf52112b9d61d484",
14
- "source": "index.js"
33
+ "gitHead": "d01a7237a0065a5a439a03062064076c5fdc91b7",
34
+ "devDependencies": {
35
+ "@babel/core": "^7.12.0"
36
+ }
15
37
  }
package/set.js ADDED
@@ -0,0 +1,31 @@
1
+ 'use strict'
2
+
3
+ import { isEqualDeep } from '@domql/utils'
4
+
5
+ import create from './create'
6
+ import OPTIONS from './cache/options'
7
+ import { registry } from './mixins'
8
+ import { removeContent } from './mixins/content'
9
+
10
+ const set = function (params, options = {}, el) {
11
+ const element = el || this
12
+ const __contentRef = element.content && element.content.__ref
13
+
14
+ if (__contentRef && __contentRef.__cached && isEqualDeep(params, element.content)) return element
15
+ removeContent(element)
16
+
17
+ if (params) {
18
+ const { childExtend } = params
19
+ if (!childExtend && element.childExtend) params.childExtend = element.childExtend
20
+ create(params, element, 'content', {
21
+ ignoreChildExtend: true,
22
+ ...registry.defaultOptions,
23
+ ...OPTIONS.create,
24
+ ...options
25
+ })
26
+ }
27
+
28
+ return element
29
+ }
30
+
31
+ export default set
package/tree.js ADDED
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ import { document } from '@domql/utils'
4
+ import { report } from '@domql/report'
5
+
6
+ export const ROOT = {
7
+ key: ':root',
8
+ node: document ? document.body : report('DocumentNotDefined', document)
9
+ }
10
+
11
+ export const TREE = ROOT
package/update.js ADDED
@@ -0,0 +1,225 @@
1
+ 'use strict'
2
+
3
+ import { window, exec, isArray, isFunction, isNumber, isObject, isString, isUndefined, merge, overwriteDeep, createSnapshotId } from '@domql/utils'
4
+ import { applyEvent, triggerEventOn, triggerEventOnUpdate } from '@domql/event'
5
+ import { isMethod } from './methods'
6
+ import { updateProps } from './props'
7
+ import { createState } from '@domql/state'
8
+
9
+ import { METHODS_EXL, isVariant } from './utils'
10
+ import create from './create'
11
+ import { throughUpdatedDefine, throughUpdatedExec } from './iterate'
12
+ import { registry } from './mixins'
13
+ import { applyParam } from './applyParam'
14
+ import OPTIONS from './cache/options'
15
+
16
+ const snapshot = {
17
+ snapshotId: createSnapshotId
18
+ }
19
+
20
+ const UPDATE_DEFAULT_OPTIONS = {
21
+ stackChanges: false,
22
+ cleanExec: true,
23
+ preventRecursive: false,
24
+ currentSnapshot: false,
25
+ calleeElement: false,
26
+ excludes: METHODS_EXL
27
+ }
28
+
29
+ const update = function (params = {}, options = UPDATE_DEFAULT_OPTIONS) {
30
+ const element = this
31
+ const { parent, node, key } = element
32
+ const { excludes, preventInheritAtCurrentState } = options
33
+
34
+ if (preventInheritAtCurrentState && preventInheritAtCurrentState.__element === element) return
35
+ if (!excludes) merge(options, UPDATE_DEFAULT_OPTIONS)
36
+
37
+ let ref = element.__ref
38
+ if (!ref) ref = element.__ref = {}
39
+
40
+ const [snapshotOnCallee, calleeElement, snapshotHasUpdated] = captureSnapshot(element, options)
41
+ if (snapshotHasUpdated) return
42
+
43
+ if (isString(params) || isNumber(params)) {
44
+ params = { text: params }
45
+ }
46
+
47
+ const ifFails = checkIfOnUpdate(element, parent, options)
48
+ if (ifFails) return
49
+
50
+ const inheritState = inheritStateUpdates(element, options)
51
+ if (inheritState === false) return
52
+
53
+ if (ref.__if && !options.preventPropsUpdate) {
54
+ const hasParentProps = parent.props && (parent.props[key] || parent.props.childProps)
55
+ const hasFunctionInProps = ref.__props.filter(v => isFunction(v))
56
+ const props = params.props || hasParentProps || hasFunctionInProps.length
57
+ if (props) updateProps(props, element, parent)
58
+ }
59
+
60
+ if (!options.isForced) {
61
+ triggerEventOn('beforeClassAssign', element, options)
62
+ }
63
+
64
+ if (!options.preventInitUpdateListener) {
65
+ const initUpdateReturns = triggerEventOnUpdate('initUpdate', params, element, options)
66
+ if (initUpdateReturns === false) return element
67
+ }
68
+
69
+ const overwriteChanges = overwriteDeep(element, params, METHODS_EXL)
70
+ const execChanges = throughUpdatedExec(element, { ignore: UPDATE_DEFAULT_OPTIONS })
71
+ const definedChanges = throughUpdatedDefine(element)
72
+
73
+ if (options.stackChanges && element.__stackChanges) {
74
+ const stackChanges = merge(definedChanges, merge(execChanges, overwriteChanges))
75
+ element.__stackChanges.push(stackChanges)
76
+ }
77
+
78
+ if (!ref.__if) return false
79
+ if (!node) {
80
+ // return createNode(element, options)
81
+ return
82
+ }
83
+
84
+ for (const param in element) {
85
+ const prop = element[param]
86
+ const hasOnlyUpdateFalsy = options.onlyUpdate && (options.onlyUpdate !== param || !element.lookup(options.onlyUpdate))
87
+ const isInPreventUpdate = isArray(options.preventUpdate) && options.preventUpdate.includes(param)
88
+ const isInPreventDefineUpdate = isArray(options.preventDefineUpdate) && options.preventDefineUpdate.includes(param)
89
+
90
+ if (
91
+ isUndefined(prop) ||
92
+ hasOnlyUpdateFalsy ||
93
+ isInPreventUpdate ||
94
+ isInPreventDefineUpdate ||
95
+ options.preventDefineUpdate === true ||
96
+ options.preventDefineUpdate === param ||
97
+ (options.preventContentUpdate && param === 'content') ||
98
+ (options.preventStateUpdate && param) === 'state' ||
99
+ isMethod(param) || isObject(registry[param]) || isVariant(param)
100
+ ) continue
101
+ if (options.preventStateUpdate === 'once') options.preventStateUpdate = false
102
+
103
+ const isElement = applyParam(param, element, options)
104
+ if (isElement) {
105
+ const { hasDefine, hasContextDefine } = isElement
106
+ const canUpdate = isObject(prop) && !hasDefine && !hasContextDefine && !options.preventRecursive
107
+ if (!canUpdate) continue
108
+
109
+ const childUpdateCall = () => update.call(prop, params[prop], {
110
+ ...options,
111
+ currentSnapshot: snapshotOnCallee,
112
+ calleeElement
113
+ })
114
+
115
+ if ((element.props && element.props.lazyLoad) || options.lazyLoad) {
116
+ window.requestAnimationFrame(() => childUpdateCall())
117
+ } else childUpdateCall()
118
+ }
119
+ }
120
+
121
+ if (!options.preventUpdateListener) triggerEventOn('update', element, options)
122
+ }
123
+
124
+ const captureSnapshot = (element, options) => {
125
+ const ref = element.__ref
126
+
127
+ const { currentSnapshot, calleeElement } = options
128
+ const isCallee = calleeElement === element
129
+ if (!calleeElement || isCallee) {
130
+ const createdStanpshot = snapshot.snapshotId()
131
+ ref.__currentSnapshot = createdStanpshot
132
+ return [createdStanpshot, element]
133
+ }
134
+
135
+ const snapshotOnCallee = ref.__currentSnapshot
136
+ if (currentSnapshot < snapshotOnCallee) {
137
+ return [snapshotOnCallee, calleeElement, true]
138
+ }
139
+
140
+ return [snapshotOnCallee, calleeElement]
141
+ }
142
+
143
+ const checkIfOnUpdate = (element, parent, options) => {
144
+ if (!isFunction(element.if) || !element.state || !parent) return
145
+
146
+ const ref = element.__ref
147
+ const ifPassed = element.if(element, element.state, element.context, options)
148
+ const itWasFalse = ref.__if !== true
149
+
150
+ if (ifPassed) {
151
+ ref.__if = true
152
+ if (itWasFalse) {
153
+ delete element.__hash
154
+ delete element.extend
155
+ if (!ref.__hasRootState) {
156
+ delete element.state
157
+ }
158
+ if (ref.__state) {
159
+ element.state = ref.__state
160
+ }
161
+ const created = create(element, parent, element.key, OPTIONS.create)
162
+ if (options.preventUpdate !== true && element.on && isFunction(element.on.update)) {
163
+ applyEvent(element.on.update, created, created.state)
164
+ }
165
+ return created
166
+ }
167
+ } else if (element.node && !ifPassed) {
168
+ element.node.remove()
169
+ delete ref.__if
170
+ }
171
+ }
172
+
173
+ const inheritStateUpdates = (element, options) => {
174
+ const { __ref: ref } = element
175
+ const stateKey = ref.__state
176
+ const { parent, state } = element
177
+
178
+ if (options.preventpdateTriggerStateUpdate) return
179
+
180
+ if (!stateKey && !ref.__hasRootState) {
181
+ element.state = (parent && parent.state) || {}
182
+ return
183
+ }
184
+
185
+ const { isHoisted, execStateFunction, stateFunctionOverwrite } = options
186
+ const shouldForceStateUpdate = isFunction(stateKey) && (!isHoisted && execStateFunction && stateFunctionOverwrite)
187
+ if (shouldForceStateUpdate) {
188
+ const execState = exec(stateKey, element)
189
+ state.set(execState, {
190
+ ...options,
191
+ preventUpdate: true
192
+ })
193
+ return
194
+ }
195
+
196
+ const parentState = (parent && parent.state) || {}
197
+ const keyInParentState = parentState[stateKey]
198
+
199
+ if (!keyInParentState || options.preventInheritedStateUpdate) return
200
+
201
+ if (!options.preventInitStateUpdateListener) {
202
+ const initStateReturns = triggerEventOnUpdate('initStateUpdated', keyInParentState, element, options)
203
+ if (initStateReturns === false) return element
204
+ }
205
+
206
+ const newState = createStateUpdate(element, parent, options)
207
+
208
+ if (!options.preventStateUpdateListener) {
209
+ triggerEventOnUpdate('stateUpdated', newState.parse(), element, options)
210
+ }
211
+ }
212
+
213
+ const createStateUpdate = (element, parent, options) => {
214
+ const __stateChildren = element.state.__children
215
+ const newState = createState(element, parent)
216
+ element.state = newState
217
+ for (const child in __stateChildren) {
218
+ // check this for inherited states
219
+ if (newState[child]) newState.__children[child] = __stateChildren[child]
220
+ __stateChildren[child].parent = newState
221
+ }
222
+ return newState
223
+ }
224
+
225
+ export default update