UNPKG

adonisjs-livewire

Version:

A front-end framework for AdonisJS

82 lines (81 loc) 2.95 kB
import Computed from '../features/support_computed/computed.js'; import On from '../features/support_events/on.js'; import Lazy from '../features/support_lazy_loading/lazy.js'; import Locked from '../features/support_locked_properties/locked.js'; import Modelable from '../features/support_models/modelable.js'; import Layout from '../features/support_page_components/layout.js'; import Title from '../features/support_page_components/title.js'; import Url from '../features/support_query_string/url.js'; import Renderless from '../features/support_renderless/renderless.js'; export function title(value) { return function (constructor) { constructor.prototype.addDecorator(new Title(value)); }; } export function layout(name = 'main', props = {}) { return function (constructor) { constructor.prototype.addDecorator(new Layout(name, props)); }; } export function lazy(isolate = true) { return function (constructor) { constructor.prototype.addDecorator(new Lazy(isolate)); }; } export function computed(name) { return function (target, propertyKey, _descriptor) { target.addDecorator(new Computed(name || propertyKey, propertyKey)); }; } export function on(name) { return function (target, propertyKey) { target.addDecorator(new On(name || propertyKey, propertyKey)); }; } export function modelable() { return function (target, propertyKey) { target.addDecorator(new Modelable('wire:model', propertyKey)); }; } export function locked() { return function (target, propertyKey) { target.addDecorator(new Locked(propertyKey)); }; } export function url(as = null) { return function (target, propertyKey) { target.addDecorator(new Url(propertyKey, as)); }; } export function bind() { return function (target, propertyKey, descriptor) { const methodParams = Reflect.getMetadata('design:paramtypes', target, propertyKey); if (!methodParams) { return; } const functionString = descriptor.value.toString(); const match = functionString.match(/\(([^)]*)\)/); if (!match || !match[1]) { return; } const args = match[1].split(',').map((param) => param.trim()); const parentBindings = target['bindings']; if (!target.hasOwnProperty('bindings')) { Object.defineProperty(target, 'bindings', { value: parentBindings ? Object.assign({}, parentBindings) : {}, }); } target['bindings'][propertyKey] = target['bindings'][propertyKey] || []; methodParams.forEach((param, index) => { target['bindings'][propertyKey].push({ name: args[index], type: param, }); }); }; } export function renderless() { return function (target, propertyKey) { target.addDecorator(new Renderless()); }; }