jails-js 5.9.0 → 6.0.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.
@@ -1,5 +1,10 @@
1
+
1
2
  const textarea = document.createElement('textarea')
2
3
 
4
+ export const g = {
5
+ scope: {}
6
+ }
7
+
3
8
  export const decodeHTML = (text) => {
4
9
  textarea.innerHTML = text
5
10
  return textarea.value
@@ -13,36 +18,17 @@ export const rAF = (fn) => {
13
18
  }
14
19
 
15
20
  export const uuid = () => {
16
- return 'xxxxxxxx'.replace(/[xy]/g, (c) => {
17
- const r = Math.random() * 8 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
18
- return v.toString(8)
19
- })
21
+ return Math.random().toString(36).substring(2, 9)
20
22
  }
21
23
 
22
24
  export const dup = (o) => {
23
25
  return JSON.parse(JSON.stringify(o))
24
26
  }
25
27
 
26
- // http://crockford.com/javascript/memory/leak.html
27
- export const purge = (d) => {
28
- var a = d.attributes, i, l, n
29
- if (a) {
30
- for (i = a.length - 1; i >= 0; i -= 1) {
31
- n = a[i].name
32
- if (typeof d[n] === 'function') {
33
- d[n] = null
34
- }
35
- }
36
- }
37
- a = d.childNodes
38
- if (a) {
39
- l = a.length
40
- for (i = 0; i < l; i += 1) {
41
- purge(d.childNodes[i])
42
- }
43
- }
44
- }
45
-
46
28
  export const safe = (execute, val) => {
47
- try{return execute()}catch(err){return val || ''}
29
+ try{
30
+ return execute()
31
+ }catch(err){
32
+ return val || ''
33
+ }
48
34
  }
@@ -1,3 +1,4 @@
1
+
1
2
  const topics: any = {}
2
3
  const _async: any = {}
3
4
 
@@ -17,4 +18,3 @@ export const subscribe = (name, method) => {
17
18
  topics[name] = topics[name].filter( fn => fn != method )
18
19
  }
19
20
  }
20
-
package/tsconfig.json CHANGED
@@ -1,5 +1,8 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "declaration": true,
4
+ "emitDeclarationOnly": true,
5
+ "outDir": "dist",
3
6
  /* Visit https://aka.ms/tsconfig to read more about this file */
4
7
 
5
8
  /* Projects */
package/types.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ type EventCallback = ( Event: Event, data?:any ) => void
2
+
3
+ export declare const templateConfig: (options: any) => void;
4
+ export declare const register: (name: string, module: Module, dependencies?: any) => void
5
+ export declare const start: (target?: HTMLElement) => void;
6
+ export declare const subscribe:( subject: string, callback: (data:any) => void ) => Function
7
+ export declare const publish: ( subject: string, data :any ) => void
8
+
9
+ export type Component = {
10
+
11
+ elm: HTMLElement
12
+ dependencies: any
13
+
14
+ state : {
15
+ protected( props: Array<string> ): Array<string>
16
+ set( data: object ) : Promise<any>
17
+ set( callback: ( state: object ) => any ) : Promise<any>
18
+ get() : object
19
+ getRaw() : object
20
+ }
21
+
22
+ main( mainArgs: ( t: Component ) => void ): void
23
+
24
+ publish( name: string, value: any ) : void
25
+
26
+ subscribe( name: string, value: Function ) : Function
27
+
28
+ unmount( callback: () => void ) : void
29
+
30
+ on( eventName: string, selector: string | EventCallback, callback?: EventCallback ): void
31
+
32
+ emit( eventName: string, data: any ) : void
33
+
34
+ off( eventName: string, callback: () => void ): void
35
+
36
+ trigger( eventName: string, selector :string, data: any ): void
37
+
38
+ innerHTML( target: HTMLElement | string, html?: string ) : void
39
+ }
40
+
41
+ export type Model = {
42
+ [key: string] : object
43
+ }
44
+
45
+ export type View = ( state: object ) => object
46
+
47
+ export type Template = ({ elm: HTMLElement, children: string }) => string
package/vite.config.ts CHANGED
@@ -9,8 +9,10 @@ export default defineConfig({
9
9
  lib: {
10
10
  name: 'jails',
11
11
  entry: path.resolve('src', 'index.ts'),
12
- formats: ['umd'],
13
- fileName: format => `jails.js`
12
+ formats: ['umd', 'es'],
13
+ fileName: (format) => {
14
+ return format == 'umd'? 'jails.js': 'index.js'
15
+ }
14
16
  },
15
17
  rollupOptions: {
16
18
  output: {
package/index.d.ts DELETED
@@ -1,65 +0,0 @@
1
- type EventCallback = ( Event: Event, data?:any ) => void
2
-
3
- declare const html = ( value :string ) => string
4
-
5
- declare const _default: {
6
- publish( subject: string, data :any )
7
- subscribe( subject: string, callback: ((data: any ) => void) )
8
- register(name: string, module: Module, dependencies?: any): void
9
- start( target?:Element): void
10
- templateConfig( options: any ): void
11
- }
12
-
13
- export default _default
14
- export const html
15
-
16
- export type Module = {
17
- default: ((component:Component) => Promise<void> | void )
18
- Model?: Model
19
- View?: View
20
- }
21
-
22
- export type Component = {
23
-
24
- elm: HTMLElement
25
- dependencies: any
26
-
27
- state : {
28
- set( data: object ) : Promise
29
- set( callback: ( state: object ) => any ) : Promise
30
- get() : object
31
- getRaw() : object
32
- }
33
-
34
- main( mainArgs: ( t: Component ) => Array<Function> | void ): void
35
-
36
- publish( name: string, value: any ) : void
37
-
38
- subscribe( name: string, value: Function ) : Function
39
-
40
- template( data: object ) : void
41
-
42
- unmount( callback: () => void ) : void
43
-
44
- onupdate( callback: () => void ) : void
45
-
46
- on( eventName: string, selector: string | EventCallback, callback?: EventCallback ): void
47
-
48
- emit( eventName: string, data: any ) : void
49
-
50
- off( eventName: string, callback: () => void ): void
51
-
52
- trigger( eventName: string, selector :string, data: any ): void
53
-
54
- render( data: object ) : void
55
-
56
- innerHTML( target: HTMLElement | string, html: string? ) : void
57
- }
58
-
59
- export type Model = {
60
- [key: string] : object
61
- }
62
-
63
- export type View = ( state: object ) => object
64
-
65
-
package/logo.svg DELETED
@@ -1,29 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <svg width="71px" height="72px" viewBox="0 0 71 72" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
- <!-- Generator: Sketch 55.2 (78181) - https://sketchapp.com -->
4
- <title>logo</title>
5
- <desc>Created with Sketch.</desc>
6
- <defs>
7
- <linearGradient x1="0%" y1="0%" x2="100%" y2="0%" id="linearGradient-1">
8
- <stop stop-color="#2D388A" offset="0%"></stop>
9
- <stop stop-color="#00AEEF" offset="100%"></stop>
10
- </linearGradient>
11
- <linearGradient x1="0%" y1="0%" x2="100%" y2="0%" id="linearGradient-2">
12
- <stop stop-color="#2D388A" offset="0%"></stop>
13
- <stop stop-color="#00AEEF" offset="100%"></stop>
14
- </linearGradient>
15
- </defs>
16
- <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
17
- <g id="logo" fill-rule="nonzero">
18
- <g id="SvgjsG1008">
19
- <g id="SvgjsG1009">
20
- <g id="Group" transform="translate(0.194727, 0.886526)">
21
- <path d="M0.244508781,35.2445089 L8.13267043,43.1877401 L43.1348733,7.94543398 L35.2445088,0.00220278181 L0.244508781,35.2445089 Z M8.13267043,41.6259678 L1.79746995,35.2445089 L35.2445089,1.56397508 L41.5819122,7.94543398 L8.13267043,41.6259678 Z" id="Shape" fill="url(#linearGradient-1)"></path>
22
- <path d="M13.800428,48.8951478 L21.6907925,56.8361762 L56.6885898,21.5938701 L48.7982254,13.6506389 L13.800428,48.8951478 Z M21.6907925,55.2744039 L15.3533892,48.8951478 L48.7982254,15.2124112 L55.1356286,21.5938701 L21.6907925,55.2744039 Z" id="Shape" fill="url(#linearGradient-2)"></path>
23
- <path d="M70.244509,35.2445089 L62.3563474,27.3034805 L27.3541445,62.5457866 L35.2445089,70.4890178 L70.244509,35.2445089 Z M62.3563474,28.86305 L68.6915479,35.2445089 L35.2445089,68.9272455 L28.9071057,62.5457866 L62.3563474,28.86305 Z" id="Shape" fill="url(#linearGradient-1)"></path>
24
- </g>
25
- </g>
26
- </g>
27
- </g>
28
- </g>
29
- </svg>
package/src/transpile.ts DELETED
@@ -1,79 +0,0 @@
1
- import { uuid } from './utils'
2
-
3
- const parser = new DOMParser()
4
-
5
- export default function Transpile(html, config) {
6
-
7
- const regexTags = new RegExp(`\\${config.tags[0]}(.+?)\\${config.tags[1]}`, 'g')
8
- const virtual = parser.parseFromString(html.replace(/<\/?template[^>]*>/g, ''), 'text/html')
9
-
10
- virtual.querySelectorAll('[html-for], [html-if], [html-inner], [html-class], [html-model]').forEach((element) => {
11
-
12
- const htmlForeach = element.getAttribute('html-foreach')
13
- const htmlFor = element.getAttribute('html-for')
14
- const htmlIf = element.getAttribute('html-if')
15
- const htmlInner = element.getAttribute('html-inner')
16
- const htmlClass = element.getAttribute('html-class')
17
- const forEachInstruction = htmlFor || htmlForeach
18
-
19
- if ( forEachInstruction ) {
20
- const selector = htmlFor? 'html-for': 'html-foreach'
21
- const split = forEachInstruction.match(/(.*)\sin\s(.*)/) || ''
22
- const varname = split[1]
23
- const object = split[2]
24
-
25
- element.removeAttribute(selector)
26
- element.setAttribute('scope', '')
27
- const script = document.createElement('script')
28
-
29
- script.dataset.scope = ''
30
- script.type = 'text/html'
31
- script.text = `%%_= $scope _%%`
32
-
33
- element.appendChild( script )
34
-
35
- const open = document.createTextNode(`%%_(function(){ var $index = 0; for(var $key in safe(function(){ return ${object} }) ){ var ${varname} = ${object}[$key]; var $scope = JSON.stringify({ '${varname}':${varname}, $index: $index, $key:$key }); _%%`)
36
- const close = document.createTextNode(`%%_ $index++; } })() _%%`)
37
- wrap(open, element, close)
38
- }
39
- if (htmlIf) {
40
- element.removeAttribute('html-if')
41
- const open = document.createTextNode(`%%_ if ( safe(function(){ return ${htmlIf} }) ){ _%%`)
42
- const close = document.createTextNode(`%%_ } _%%`)
43
- wrap(open, element, close)
44
- }
45
- if (htmlInner) {
46
- element.removeAttribute('html-inner')
47
- element.innerHTML = `%%_=${htmlInner}_%%`
48
- }
49
- if (htmlClass) {
50
- element.removeAttribute('html-class')
51
- element.className = (element.className + ` %%_=${htmlClass}_%%`).trim()
52
- }
53
- })
54
-
55
- return (
56
- virtual.body.innerHTML
57
- .replace(regexTags, '%%_=$1_%%')
58
- // Booleans
59
- // https://meiert.com/en/blog/boolean-attributes-of-html/
60
- .replace(/html-(allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|inert|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|selected)=\"(.*?)\"/g, `%%_if(safe(function(){ return $2 })){_%%$1%%_}_%%`)
61
- // The rest
62
- .replace(/html-(.*?)=\"(.*?)\"/g, (all, key, value) => {
63
- if (key === 'key' || key === 'model' || key == 'scope') {
64
- return all
65
- }
66
- if (value) {
67
- value = value.replace(/^{|}$/g, '')
68
- return `${key}="%%_=safe(function(){ return ${value} })_%%"`
69
- } else {
70
- return all
71
- }
72
- })
73
- )
74
- }
75
-
76
- const wrap = (open, node, close) => {
77
- node.parentNode?.insertBefore(open, node)
78
- node.parentNode?.insertBefore(close, node.nextSibling)
79
- }
@@ -1,86 +0,0 @@
1
- const customEvent = (() => {
2
- return ('CustomEvent' in window && typeof window.CustomEvent === 'function')
3
- ? (name, data) => new CustomEvent(name, data)
4
- : (name, data) => {
5
- const newEvent = document.createEvent('CustomEvent')
6
- newEvent.initCustomEvent(name, true, true, data)
7
- return newEvent
8
- }
9
- })()
10
-
11
- const handler = (node, ev) => {
12
- return function (e) {
13
- const scope = this
14
- const detail = e.detail || {}
15
- node.__events[ev].forEach(o => {
16
- o.handler.apply(scope, [e].concat(detail.args))
17
- })
18
- }
19
- }
20
-
21
- const removeListener = (node, ev) => {
22
- if (node.__events[ev] && node.__events[ev].listener) {
23
- node.removeEventListener(
24
- ev,
25
- node.__events[ev].listener,
26
- (ev == 'focus' || ev == 'blur' || ev == 'mouseenter' || ev == 'mouseleave'))
27
- delete node.__events[ev]
28
- }
29
- }
30
-
31
- const delegate = (node, selector, callback) => {
32
- return function (e) {
33
-
34
- const element = this
35
- const detail = e.detail || {}
36
-
37
- let parent = e.target
38
-
39
- while (parent) {
40
- if (parent.matches(selector)) {
41
- e.delegateTarget = parent
42
- callback.apply(element, [e].concat(detail.args))
43
- }
44
- if (parent === node) break
45
- parent = parent.parentNode
46
- }
47
- }
48
- }
49
-
50
- export const on = (node, ev, selectorOrCallback, callback) => {
51
-
52
- node.__events = node.__events || {}
53
- node.__events[ev] = (node.__events[ev] || [])
54
-
55
- if (!node.__events[ev].length) {
56
- const fn = handler(node, ev)
57
- node.addEventListener(
58
- ev,
59
- fn,
60
- (ev == 'focus' || ev == 'blur' || ev == 'mouseenter' || ev == 'mouseleave'))
61
- node.__events[ev].listener = fn
62
- }
63
-
64
- if (selectorOrCallback.call) {
65
- node.__events[ev].push({ handler: selectorOrCallback, callback: selectorOrCallback })
66
- } else {
67
- node.__events[ev].push({ handler: delegate(node, selectorOrCallback, callback), callback })
68
- }
69
- }
70
-
71
- export const off = (node, ev, fn) => {
72
-
73
- if (fn && node.__events[ev] && node.__events[ev].length) {
74
- var old = node.__events[ev]
75
- node.__events[ev] = node.__events[ev].filter(function (o) { return o.callback != fn; })
76
- node.__events[ev].listener = old.listener
77
- if (!node.__events[ev].length)
78
- removeListener(node, ev)
79
- } else {
80
- removeListener(node, ev)
81
- }
82
- }
83
-
84
- export const trigger = (node, name, args) => {
85
- node.dispatchEvent(customEvent(name, { bubbles: true, detail: args }))
86
- }
@@ -1,41 +0,0 @@
1
- //https://github.com/polleverywhere/hmr-custom-element
2
-
3
- export const register = (module, tag, clazz) => {
4
- // when not in a HMR env, we should be doing the correct thing
5
- // and let the browser throw an error because we're importing this
6
- // more than once for some reason
7
- if (!module.hot) {
8
- try {
9
- customElements.define(tag, clazz)
10
- } catch (e) {
11
- console.error(`Failed to register element: ${tag}`, e)
12
- }
13
-
14
- return
15
- }
16
- // hot module reload the elements
17
- //
18
- // if we're in a HMR env, we're going to be reloaded so
19
- // don't register the elements again
20
- if (!customElements.get(tag)) {
21
- try {
22
- customElements.define(tag, clazz)
23
- } catch (e) {
24
- console.error(`Failed to register element in HMR environment: ${tag}`, e)
25
- }
26
- }
27
-
28
- module.hot.accept((e) => {
29
- console.error('HMR error', e)
30
- })
31
-
32
- if (module.hot.status && module.hot.status() === 'apply') {
33
- // find each instance and swap the prototype for the new one and re-render
34
- document.querySelectorAll(tag).forEach((node) => {
35
- // Swap prototype of instance with new one
36
- Object.setPrototypeOf(node, clazz.prototype)
37
- // re-render
38
- node.connectedCallback()
39
- })
40
- }
41
- }