Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: TS rewrite #203

Merged
merged 11 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/UI/AbstractPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class AbstractPresenter extends NellaPresenter

abstract public function createComponentGrid(): DataGrid;

public function changeStatus(int $id, string $newStatus): void
public function changeStatus($id, string $newStatus): void
{
if (in_array($newStatus, ['active', 'inactive', 'deleted'], true)) {
$data = ['status' => $newStatus];
Expand Down
3 changes: 2 additions & 1 deletion assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
@import 'bootstrap/dist/css/bootstrap.css';
@import 'prismjs/themes/prism-okaidia.css';

@import './../datagrid/datagrid.css';
@import './../datagrid/assets/datagrid.css';
@import 'vanillajs-datepicker/css/datepicker-bs5.css';

/* App styles */
@import './styles.css';
3 changes: 0 additions & 3 deletions assets/datagrid/@types/global.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions assets/datagrid/@types/types.d.ts

This file was deleted.

14 changes: 0 additions & 14 deletions assets/datagrid/_TODO.ts

This file was deleted.

1 change: 1 addition & 0 deletions assets/datagrid/ajax/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./naja";
135 changes: 135 additions & 0 deletions assets/datagrid/ajax/naja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import type {Naja} from "naja";
import type {
Ajax,
RequestParams,
Payload,
DatagridPayload,
EventDetail,
EventListener,
Response as AjaxResponse,
BaseRequestParams as AjaxBaseRequestParams,
BeforeEventDetail as BaseBeforeEventDetail,
ErrorEventDetail as BaseErrorEventDetail,
InteractEventDetail as BaseInteractEventDetail,
SuccessEventDetail as BaseSuccessEventDetail,
AjaxEventMap as BaseAjaxEventMap,
} from "@datagrid/types";
import {Datagrid} from "@datagrid/datagrid";
import {BeforeEvent, SuccessEvent, ErrorEvent, Payload as NajaPayload} from "naja/dist/Naja";
import {InteractionEvent} from "naja/dist/core/UIHandler";

export interface BaseRequestParams extends AjaxBaseRequestParams, Request {
url: string;
method: string;
}

export interface BeforeEventDetail<D = {}> extends BaseBeforeEventDetail<D> {
params: EventDetail<BeforeEvent> & RequestParams<D>;
}

export interface InteractEventDetail<
E extends HTMLElement = HTMLElement
> extends BaseInteractEventDetail<E>, EventDetail<InteractionEvent> {
element: E;
}

export interface SuccessEventDetail<
P = DatagridPayload
> extends BaseSuccessEventDetail<P, Response>, EventDetail<SuccessEvent> {
params: BaseRequestParams;
payload: Payload<P> & NajaPayload;
response: AjaxResponse & Response;
}

export interface ErrorEventDetail<
E extends Error = Error,
> extends BaseErrorEventDetail<E, Response>, EventDetail<ErrorEvent> {
params: BaseRequestParams;
response: (AjaxResponse & Response) | undefined;
error: E;
}

export interface AjaxEventMap extends BaseAjaxEventMap {
before: CustomEvent<BeforeEventDetail>;
interact: CustomEvent<InteractEventDetail>;
snippetUpdate: CustomEvent<InteractEventDetail>;
success: CustomEvent<SuccessEventDetail>;
error: CustomEvent<ErrorEventDetail>;
}

export class NajaAjax<C extends Naja = Naja, G extends Datagrid = Datagrid> extends EventTarget implements Ajax<C, G> {
constructor(public client: C) {
if (!client.VERSION || client.VERSION < 2) {
throw new Error("NajaAjax supports Naja 2 and higher" + (client.VERSION ? `(version ${client.VERSION} provided)` : ''))
}
super();
}

onInit() {
this.client.addEventListener('before', (e) => {
return this.dispatch('before', {
params: e.detail
});
})

this.client.uiHandler.addEventListener('interaction', (e) => {
if (!(e.detail.element instanceof HTMLElement)) {
throw new Error("Element is not an instanceof HTMLElement");
}

return this.dispatch('interact', {
...e.detail,
element: e.detail.element as HTMLElement // Naja's event has a type of HTMLElement
})
})


this.client.addEventListener('success', (e) => {
return this.dispatch('success', {
...e.detail,
params: e.detail.request,
payload: e.detail.payload as Payload
});
})

this.client.addEventListener('error', (e) => {
return this.dispatch('error', {
...e.detail,
params: e.detail.request,
response: e.detail.response,
});
})

return this;
}

async request<D = {}, P = DatagridPayload>(args: RequestParams<D>): Promise<P> {
return await this.client.makeRequest(args.method, args.url, args.data) as P;
}

async submitForm<E extends HTMLFormElement = HTMLFormElement, P = Payload>(element: E): Promise<P> {
return await this.client.uiHandler.submitForm(element) as P;
}

dispatch<
K extends string, M extends BaseAjaxEventMap = AjaxEventMap
>(type: K, detail: K extends keyof M ? EventDetail<M[K]> : any, options?: boolean): boolean {
return this.dispatchEvent(new CustomEvent(type, {detail}));
}

declare addEventListener: <K extends keyof M, M extends BaseAjaxEventMap = AjaxEventMap>(
type: K,
listener: EventListener<this, M[K]>,
options?: boolean | AddEventListenerOptions
) => void;

declare removeEventListener: <K extends keyof M, M extends BaseAjaxEventMap = AjaxEventMap>(
type: K,
listener: EventListener<this, M[K]>,
options?: boolean | AddEventListenerOptions
) => void;

declare dispatchEvent: <K extends string, M extends BaseAjaxEventMap = AjaxEventMap>(
event: K extends keyof M ? M[K] : CustomEvent
) => boolean;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/* .datagrid--slide-toggle > td > div {
-webkit-transition: height .25s ease;
-o-transition: height .25s ease;
transition: height .25s ease;
overflow: hidden;
} */

.datagrid--content-row:not(.is-active) > td /*> div*/ {
display: none;
}

@keyframes edited {
0% {
background-color: #A6E2A9
Expand Down Expand Up @@ -225,22 +236,6 @@
display: table
}

.datagrid table tbody tr.row-item-detail {
display: none
}

.datagrid table tbody tr.row-item-detail.toggled {
display: table-row
}

.datagrid table tbody tr.row-item-detail .item-detail-content {
display: none
}

.datagrid table tbody tr.row-item-detail-helper {
display: none
}

.datagrid table tbody tr .datagrid-inline-edit .form-control {
margin: -3px;
padding-bottom: 4px;
Expand Down
Loading