Comparing version 0.1.1 to 0.1.2
@@ -40,9 +40,11 @@ function _define_property(obj, key, value) { | ||
} | ||
constructor(page, layoutData){ | ||
super(layoutData); | ||
_define_property(this, "root", true); | ||
constructor(data, page){ | ||
super(data); | ||
_define_property(this, "page", void 0); | ||
_define_property(this, "path", ""); | ||
_define_property(this, "isLayout", void 0); | ||
_define_property(this, "path", void 0); | ||
this.page = page; | ||
this.isLayout = true; | ||
this.path = ""; | ||
} | ||
} |
@@ -0,1 +1,14 @@ | ||
function _define_property(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
import { View } from "./View"; | ||
@@ -6,2 +19,7 @@ export class Page extends View { | ||
} | ||
constructor(data, sharedData){ | ||
super(data); | ||
_define_property(this, "sharedData", void 0); | ||
this.sharedData = sharedData; | ||
} | ||
} |
@@ -15,2 +15,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-return */ function _define_property(obj, key, value) { | ||
import { View } from "./View"; | ||
import { Page } from "./Page"; | ||
class Rune { | ||
@@ -33,2 +34,29 @@ set(element, instance, Constructor) { | ||
} | ||
_getPageByParentView(currentView) { | ||
do { | ||
if (currentView instanceof Page) { | ||
return currentView; | ||
} | ||
}while (currentView = currentView.parentView); | ||
} | ||
getPage(currentView) { | ||
let page; | ||
if (currentView) { | ||
page = this._getPageByParentView(currentView); | ||
} | ||
if (!page && typeof window !== "undefined") { | ||
const element = document.querySelector("body [data-rune]"); | ||
if (element) { | ||
page = this.getUnknownView(element); | ||
} | ||
} | ||
if (page && page instanceof Page) { | ||
return page; | ||
} else { | ||
throw Error("rune.Page not found."); | ||
} | ||
} | ||
getSharedData(currentView) { | ||
return this.getPage(currentView).sharedData; | ||
} | ||
constructor(){ | ||
@@ -35,0 +63,0 @@ _define_property(this, "_weakMap", new WeakMap()); |
@@ -77,3 +77,3 @@ function _define_property(obj, key, value) { | ||
_addRuneAttrs(html) { | ||
if (this.root) return html; | ||
if (this.isLayout) return html; | ||
const { startTag, startTagName } = this._matchStartTag(html); | ||
@@ -115,6 +115,6 @@ const runeDataset = 'data-rune="'.concat(this, '" data-rune-parent="').concat(this.parentView, '"'); | ||
super(); | ||
_define_property(this, "root", false); | ||
_define_property(this, "_data", void 0); | ||
_define_property(this, "parentView", null); | ||
_define_property(this, "subViewsFromTemplate", []); | ||
_define_property(this, "isLayout", false); | ||
_define_property(this, "renderCount", 0); | ||
@@ -121,0 +121,0 @@ _define_property(this, "_currentHtml", null); |
@@ -50,9 +50,11 @@ "use strict"; | ||
} | ||
constructor(page, layoutData){ | ||
super(layoutData); | ||
_define_property(this, "root", true); | ||
constructor(data, page){ | ||
super(data); | ||
_define_property(this, "page", void 0); | ||
_define_property(this, "path", ""); | ||
_define_property(this, "isLayout", void 0); | ||
_define_property(this, "path", void 0); | ||
this.page = page; | ||
this.isLayout = true; | ||
this.path = ""; | ||
} | ||
} |
@@ -12,2 +12,15 @@ "use strict"; | ||
const _View = require("./View"); | ||
function _define_property(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
class Page extends _View.View { | ||
@@ -17,2 +30,7 @@ hydrateFromSSR() { | ||
} | ||
constructor(data, sharedData){ | ||
super(data); | ||
_define_property(this, "sharedData", void 0); | ||
this.sharedData = sharedData; | ||
} | ||
} |
@@ -12,2 +12,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-return */ "use strict"; | ||
const _View = require("./View"); | ||
const _Page = require("./Page"); | ||
function _define_property(obj, key, value) { | ||
@@ -43,2 +44,29 @@ if (key in obj) { | ||
} | ||
_getPageByParentView(currentView) { | ||
do { | ||
if (currentView instanceof _Page.Page) { | ||
return currentView; | ||
} | ||
}while (currentView = currentView.parentView); | ||
} | ||
getPage(currentView) { | ||
let page; | ||
if (currentView) { | ||
page = this._getPageByParentView(currentView); | ||
} | ||
if (!page && typeof window !== "undefined") { | ||
const element = document.querySelector("body [data-rune]"); | ||
if (element) { | ||
page = this.getUnknownView(element); | ||
} | ||
} | ||
if (page && page instanceof _Page.Page) { | ||
return page; | ||
} else { | ||
throw Error("rune.Page not found."); | ||
} | ||
} | ||
getSharedData(currentView) { | ||
return this.getPage(currentView).sharedData; | ||
} | ||
constructor(){ | ||
@@ -45,0 +73,0 @@ _define_property(this, "_weakMap", new WeakMap()); |
@@ -107,3 +107,3 @@ "use strict"; | ||
_addRuneAttrs(html) { | ||
if (this.root) return html; | ||
if (this.isLayout) return html; | ||
const { startTag, startTagName } = this._matchStartTag(html); | ||
@@ -145,6 +145,6 @@ const runeDataset = 'data-rune="'.concat(this, '" data-rune-parent="').concat(this.parentView, '"'); | ||
super(); | ||
_define_property(this, "root", false); | ||
_define_property(this, "_data", void 0); | ||
_define_property(this, "parentView", null); | ||
_define_property(this, "subViewsFromTemplate", []); | ||
_define_property(this, "isLayout", false); | ||
_define_property(this, "renderCount", 0); | ||
@@ -151,0 +151,0 @@ _define_property(this, "_currentHtml", null); |
{ | ||
"name": "rune-ts", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Rune Core Library", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -5,9 +5,10 @@ import { html, VirtualView } from './VirtualView'; | ||
export class Layout<T extends object> extends VirtualView<T> { | ||
override root = true; | ||
page: Page<object>; | ||
override readonly isLayout: boolean = true; | ||
path = ''; | ||
constructor(page: Page<object>, layoutData: T) { | ||
super(layoutData); | ||
this.page = page; | ||
constructor( | ||
data: T, | ||
public page: Page<object>, | ||
) { | ||
super(data); | ||
} | ||
@@ -14,0 +15,0 @@ |
import { View } from './View'; | ||
export class Page<T extends object> extends View<T> { | ||
constructor( | ||
data: T, | ||
public sharedData?: Record<string, any>, | ||
) { | ||
super(data); | ||
} | ||
override hydrateFromSSR(): this { | ||
@@ -5,0 +12,0 @@ return super.hydrateFromSSR(document.querySelector(`body [data-rune="${this}"]`)!); |
@@ -6,2 +6,4 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { View } from './View'; | ||
import { Page } from './Page'; | ||
import type { VirtualView } from './VirtualView'; | ||
@@ -41,2 +43,32 @@ type Constructor = new (...args: any) => any; | ||
} | ||
protected _getPageByParentView(currentView: VirtualView<object>) { | ||
do { | ||
if (currentView instanceof Page) { | ||
return currentView as Page<object>; | ||
} | ||
} while ((currentView = currentView.parentView!)); | ||
} | ||
getPage(currentView: VirtualView<object>): Page<object> { | ||
let page: View<object> | undefined; | ||
if (currentView) { | ||
page = this._getPageByParentView(currentView); | ||
} | ||
if (!page && typeof window !== 'undefined') { | ||
const element = document.querySelector(`body [data-rune]`); | ||
if (element) { | ||
page = this.getUnknownView(element); | ||
} | ||
} | ||
if (page && page instanceof Page) { | ||
return page as Page<object>; | ||
} else { | ||
throw Error('rune.Page not found.'); | ||
} | ||
} | ||
getSharedData(currentView: VirtualView<object>): Record<string, any> | undefined { | ||
return this.getPage(currentView).sharedData; | ||
} | ||
} | ||
@@ -43,0 +75,0 @@ |
@@ -6,3 +6,2 @@ import { _escape } from './lib/_escape'; | ||
export class VirtualView<T extends object> extends Base { | ||
root = false; | ||
private readonly _data: T; | ||
@@ -13,2 +12,3 @@ | ||
readonly isLayout: boolean = false; | ||
renderCount = 0; | ||
@@ -51,3 +51,3 @@ protected _currentHtml: string | null = null; | ||
private _addRuneAttrs(html: string): string { | ||
if (this.root) return html; | ||
if (this.isLayout) return html; | ||
const { startTag, startTagName } = this._matchStartTag(html); | ||
@@ -58,4 +58,4 @@ const runeDataset = `data-rune="${this}" data-rune-parent="${this.parentView}"`; | ||
: startTag.includes("class='") | ||
? html.replace("class='", `${runeDataset} class='${this} `) | ||
: html.replace(`<${startTagName}`, `<${startTagName} ${runeDataset} class="${this}"`); | ||
? html.replace("class='", `${runeDataset} class='${this} `) | ||
: html.replace(`<${startTagName}`, `<${startTagName} ${runeDataset} class="${this}"`); | ||
return html; | ||
@@ -153,6 +153,6 @@ } | ||
: templateVal instanceof Html | ||
? make(templateVal, virtualView) | ||
: templateVal instanceof UnsafeHtml | ||
? templateVal.toString() | ||
: _escape(templateVal as string); | ||
? make(templateVal, virtualView) | ||
: templateVal instanceof UnsafeHtml | ||
? templateVal.toString() | ||
: _escape(templateVal as string); | ||
} | ||
@@ -159,0 +159,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
245182
4574