Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed May 15, 2020
2 parents 91330aa + d2c5635 commit 282c0d1
Show file tree
Hide file tree
Showing 23 changed files with 445 additions and 392 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Signum Framework doesn't use any numeric versioning, since is distributed as sou

Whenever there are big changes worth to mention, we typicaly write it in the related commit. Here is the list of the relevant changes:

* [2020.05.14 Slimming Signum.React](/~https://github.com/signumsoftware/framework/commit/31c91dad34f251f0c728cad426d5f5db9e496261#comments)
* [2020.02.27 Updates package.json](/~https://github.com/signumsoftware/framework/commit/08f78128326fad5eaf80f184d67f76863f5aa8a9#comments)
* [2020.02.25 Typescript 3.8 and other changes](/~https://github.com/signumsoftware/framework/commit/8cdc9ab0ec5488dcfa65b2539bc7e784f0607f47#comments)
* [2020.02.14 Southwind inside of a Docker Container and running in Azure](/~https://github.com/signumsoftware/framework/commit/f9cc544a09cf1d80ae52ac659b57af06d8e730c5#comments)
Expand Down
155 changes: 155 additions & 0 deletions Signum.React/Scripts/AppContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import * as React from "react";
import { Route, Switch } from "react-router"
import * as H from "history"
import * as AppRelativeRoutes from "./AppRelativeRoutes";
import { IUserEntity, TypeEntity } from "./Signum.Entities.Basics";
import { Dic, classes, } from './Globals';
import { ImportRoute } from "./AsyncImport";
import { clearContextHeaders, ajaxGet, ajaxPost } from "./Services";
import { PseudoType, Type, getTypeName } from "./Reflection";
import { Entity, EntityPack, Lite, ModifiableEntity } from "./Signum.Entities";

Dic.skipClasses.push(React.Component);

export let currentUser: IUserEntity | undefined;
export function setCurrentUser(user: IUserEntity | undefined) {
currentUser = user;
}

export let history: H.History;
export function setCurrentHistory(h: H.History) {
history = h;
}

export let setTitle: (pageTitle?: string) => void;
export function setTitleFunction(titleFunction: (pageTitle?: string) => void) {
setTitle = titleFunction;
}

export function useTitle(title: string, deps?: readonly any[]) {
React.useEffect(() => {
setTitle(title);
return () => setTitle();
}, deps);
}

export function createAppRelativeHistory(): H.History {
var h = H.createBrowserHistory({});
AppRelativeRoutes.useAppRelativeBasename(h);
AppRelativeRoutes.useAppRelativeComputeMatch(Route);
AppRelativeRoutes.useAppRelativeComputeMatch(ImportRoute as any);
AppRelativeRoutes.useAppRelativeSwitch(Switch);
setCurrentHistory(h);
return h;
}


export const clearSettingsActions: Array<() => void> = [
clearContextHeaders,
];

export function clearAllSettings() {
clearSettingsActions.forEach(a => a());
}

export let resetUI: () => void = () => { };
export function setResetUI(reset: () => void) {
resetUI = reset;
}

export namespace Expander {
export let onGetExpanded: () => boolean;
export let onSetExpanded: (isExpanded: boolean) => void;

export function setExpanded(expanded: boolean): boolean {
let wasExpanded = onGetExpanded != null && onGetExpanded();;
if (onSetExpanded)
onSetExpanded(expanded);

return wasExpanded;
}
}

export function useExpand() {
React.useEffect(() => {
const wasExpanded = Expander.setExpanded(true);
return () => { Expander.setExpanded(wasExpanded); }
}, []);

}

export function pushOrOpenInTab(path: string, e: React.MouseEvent<any> | React.KeyboardEvent<any>) {
if ((e as React.MouseEvent<any>).button == 2)
return;

e.preventDefault();
if (e.ctrlKey || (e as React.MouseEvent<any>).button == 1)
window.open(toAbsoluteUrl(path));
else if (path.startsWith("http"))
window.location.href = path;
else
history.push(path);
}

export function toAbsoluteUrl(appRelativeUrl: string): string {
if (appRelativeUrl?.startsWith("~/"))
return window.__baseUrl + appRelativeUrl.after("~/");

var relativeCrappyUrl = history.location.pathname.beforeLast("/") + "/~/"; //In Link render ~/ is considered a relative url
if (appRelativeUrl?.startsWith(relativeCrappyUrl))
return window.__baseUrl + appRelativeUrl.after(relativeCrappyUrl);

if (appRelativeUrl.startsWith(window.__baseUrl) || appRelativeUrl.startsWith("http"))
return appRelativeUrl;

return appRelativeUrl;
}


declare global {
interface String {
formatHtml(...parameters: any[]): React.ReactElement<any>;
}

interface Array<T> {
joinCommaHtml(this: Array<T>, lastSeparator: string): React.ReactElement<any>;
}
}

String.prototype.formatHtml = function (this: string) {
const regex = /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g;

const args = arguments;

const parts = this.split(regex);

const result: (string | React.ReactElement<any>)[] = [];
for (let i = 0; i < parts.length - 4; i += 4) {
result.push(parts[i]);
result.push(args[parseInt(parts[i + 1])]);
}
result.push(parts[parts.length - 1]);

return React.createElement("span", undefined, ...result);
};

Array.prototype.joinCommaHtml = function (this: any[], lastSeparator: string) {
const args = arguments;

const result: (string | React.ReactElement<any>)[] = [];
for (let i = 0; i < this.length - 2; i++) {
result.push(this[i]);
result.push(", ");
}

if (this.length >= 2) {
result.push(this[this.length - 2]);
result.push(lastSeparator)
}

if (this.length >= 1) {
result.push(this[this.length - 1]);
}

return React.createElement("span", undefined, ...result);
}
9 changes: 5 additions & 4 deletions Signum.React/Scripts/AppRelativeRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import * as React from "react";
import { match, Route, RouterChildContext, matchPath, RouteProps, Switch, __RouterContext } from "react-router";
import * as H from "history";
import * as AppContext from './AppContext'
import * as Navigator from "./Navigator";

//monkey-patching prototypes to make use of this is implemented: /~https://github.com/ReactTraining/react-router/issues/5000

function fixBaseName<T>(baseFunction: (location: H.LocationDescriptorObject | string, state?: any) => T): (location: H.LocationDescriptorObject | string, state?: any) => T {
return (location, state) => {
if (typeof location === "string") {
return baseFunction(Navigator.toAbsoluteUrl(location), state);
return baseFunction(AppContext.toAbsoluteUrl(location), state);
} else {
location!.pathname = Navigator.toAbsoluteUrl(location!.pathname!);
location!.pathname = AppContext.toAbsoluteUrl(location!.pathname!);
return baseFunction(location, state);
}
};
Expand All @@ -28,7 +29,7 @@ export function useAppRelativeComputeMatch(RouteClass: typeof Route) {
(RouteClass.prototype as any).computeMatch = function computeMatch(this: Route, props: RouteProps, context: RouterChildContext<any>) {
let { path, ...p } = props;

const newPath = path && Navigator.toAbsoluteUrl(path as string);
const newPath = path && AppContext.toAbsoluteUrl(path as string);

return baseMatch.call(this, { path: newPath, ...p }, context);
};
Expand Down Expand Up @@ -60,7 +61,7 @@ export function useAppRelativeSwitch(SwitchClass: typeof Switch) {
const path = child.props.path ?? child.props.from;

match = path
? matchPath(location.pathname, { ...child.props, path: Navigator.toAbsoluteUrl(path as string) })
? matchPath(location.pathname, { ...child.props, path: AppContext.toAbsoluteUrl(path as string) })
: context.match;
}
});
Expand Down
10 changes: 5 additions & 5 deletions Signum.React/Scripts/AsyncImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export function ImportComponent({ onImportModule, componentProps }: ImportCompon
}

interface ImportRouteProps {
path?: string | string[];
exact?: boolean;
sensitive?: boolean;
path?: string | string[];
exact?: boolean;
sensitive?: boolean;
strict?: boolean;
location?: H.Location;
location?: H.Location;

onImportModule: () => Promise<ComponentModule>;

Expand All @@ -41,5 +41,5 @@ export function ImportRoute({ onImportModule, ...rest }: ImportRouteProps) {
{(props: RouteChildrenProps<any>) => <ImportComponent onImportModule={onImportModule} componentProps={props} />}
</Route>
);
}
}

Loading

0 comments on commit 282c0d1

Please sign in to comment.