Skip to content

Instantly share code, notes, and snippets.

@sdluxeon
Created June 7, 2018 11:12
Show Gist options
  • Select an option

  • Save sdluxeon/1aa48296c321fe6a2ea50eb3f06f0016 to your computer and use it in GitHub Desktop.

Select an option

Save sdluxeon/1aa48296c321fe6a2ea50eb3f06f0016 to your computer and use it in GitHub Desktop.
// import { Angular2FontawesomeModule } from 'angular2-fontawesome/angular2-fontawesome';
// import { HttpClientModule} from '@angular/common/http';
// import { Pipe, PipeTransform } from '@angular/core';
// import { BrowserModule } from '@angular/platform-browser';
// import {FormsModule} from '@angular/forms';
// import { RouterModule } from '@angular/router';
// import {HttpModule} from '@angular/http';
// import {NgxPaginationModule} from 'ngx-pagination';
// import { AppComponent } from './app.component';
// import { AboutComponent } from './components/about.component';
// import {routing} from './app.routing';
// import { SiteHeaderTopComponent } from './site-header-top/site-header-top.component';
// import { LeftSidebarMenuComponent } from './left-sidebar-menu/left-sidebar-menu.component';
// import { MainComponent } from './main/main.component';
// import { HomePageComponent } from './home-page/home-page.component';
// import { LazyComponent } from './lazy/lazy.component';
// import { AggregateListingComponent } from './aggregate-listing/aggregate-listing.component';
// import { ProjectionListingComponent } from './projection-listing/projection-listing.component';
// import { ProjectionDetailComponent } from './projection-detail/projection-detail.component';
// import { ProjectionServiceService } from './services/projection-service.service';
//import { Reflector } from '../../node_modules/@angular/core/src/reflection/reflector';
// @NgModule({
// imports: [ BrowserModule, FormsModule, HttpModule, routing, Angular2FontawesomeModule, NgxPaginationModule, RouterModule, HttpClientModule ],
// declarations: [ AppComponent, AboutComponent, SiteHeaderTopComponent, LeftSidebarMenuComponent, MainComponent, HomePageComponent, AggregateListingComponent, ProjectionListingComponent, ProjectionDetailComponent ],
// providers: [ProjectionServiceService],
// bootstrap: [ AppComponent ],
// exports: [SiteHeaderTopComponent]
// })
const ANNOTATIONS = '__annotations__';
interface Type<T> extends Function { new (...args: any[]): T; }
function isType(v: any): v is Type<any> {
return typeof v === 'function';
}
function annotations(typeOrFunc: Type<any>): any[] {
if (!isType(typeOrFunc)) {
return [];
}
const parentCtor = getParentCtor(typeOrFunc);
const ownAnnotations = _ownAnnotations(typeOrFunc, parentCtor) || [];
const parentAnnotations = parentCtor !== Object ? annotations(parentCtor) : [];
return parentAnnotations.concat(ownAnnotations);
}
function _ownAnnotations(typeOrFunc: Type<any>, parentCtor: any): any[]|null {
// Prefer the direct API.
if ((<any>typeOrFunc).annotations && (<any>typeOrFunc).annotations !== parentCtor.annotations) {
let annotations = (<any>typeOrFunc).annotations;
if (typeof annotations === 'function' && annotations.annotations) {
annotations = annotations.annotations;
}
return annotations;
}
// API of tsickle for lowering decorators to properties on the class.
if ((<any>typeOrFunc).decorators && (<any>typeOrFunc).decorators !== parentCtor.decorators) {
return convertTsickleDecoratorIntoMetadata((<any>typeOrFunc).decorators);
}
// API for metadata created by invoking the decorators.
if (typeOrFunc.hasOwnProperty(ANNOTATIONS)) {
return (typeOrFunc as any)[ANNOTATIONS];
}
return null;
}
function getParentCtor(ctor: Function): Type<any> {
const parentProto = ctor.prototype ? Object.getPrototypeOf(ctor.prototype) : null;
const parentCtor = parentProto ? parentProto.constructor : null;
// Note: We always use `Object` as the null value
// to simplify checking later on.
return parentCtor || Object;
}
function convertTsickleDecoratorIntoMetadata(decoratorInvocations: any[]): any[] {
if (!decoratorInvocations) {
return [];
}
return decoratorInvocations.map(decoratorInvocation => {
const decoratorType = decoratorInvocation.type;
const annotationCls = decoratorType.annotationCls;
const annotationArgs = decoratorInvocation.args ? decoratorInvocation.args : [];
return new annotationCls(...annotationArgs);
});
}
var Reflection={
getAnnotation: annotations
}
export { Reflection };
import { NgModule, ReflectiveInjector } from '@angular/core';
import { Reflection } from './ng-reflection';
class MultitenantServiceRegistrations {
components: [{ component: any, tenant: string, path: string }];
constructor() {
}
getComponentTenant(component) {
if (!this.components)
return "default";
var found = this.components.filter(x => x.component == component);
if (found.length == 0)
return "default"
else
return found[0].tenant;
}
registerTenant(component, tenant) {
var registration = {
component: component,
tenant: tenant,
path: null,
}
if (!this.components) {
this.components = [registration];
}
else {
var current = this.components.filter(x => x.component == component);
if (current.length == 0)
this.components.push(registration);
else {
current[0].tenant = tenant;
}
}
}
registerPath(component, path) {
var registration = {
component: component,
tenant: "default",
path: path,
}
if (!this.components) {
this.components = [registration];
}
else {
var current = this.components.filter(x => x.component == component);
if (current.length == 0)
this.components.push(registration);
else {
current[0].path = path;
}
}
}
getRegistrations(): [{ component: any, tenant: string, path: string }] {
var components = [];
var tenant = this.getTenant();
this.components.forEach(element => {
if (element.tenant == tenant)
components.push(element);
});
return this.components;
}
getRegistrationsAsComponents(): any[] {
var components = [];
var tenant = this.getTenant();
this.components.forEach(element => {
if (element.tenant == tenant)
components.push(element);
});
return components;
}
compoennetSelctors: [{ selector: string, components: any[] }];
registerComponentSelector(component: any) {
var componentAnotation = Reflection.getAnnotation(component).filter(x => x.selector)[0];
var selector = componentAnotation.selector;
if (!this.compoennetSelctors) {
this.compoennetSelctors = [{ selector: selector, components: [component] }]
}
else {
var found = this.compoennetSelctors.filter(x => x.selector == selector);
if (found.length == 0)
this.compoennetSelctors.push({ selector: selector, components: [component] })
else
found[0].components.push(component);
}
}
getBootModules(allModules: any[]): any[] {
var currentTenenat = this.getTenant();
allModules.forEach(mod => {
var moduleTenant = this.getComponentTenant(mod);
this.registerTenant(mod, moduleTenant);
this.registerComponentSelector(mod);
})
this.compoennetSelctors.forEach(element => {
if (element.components.length > 1) {
element.components.forEach(c => {
if (this.getComponentTenant(c) != currentTenenat) {
var componentAnotation = Reflection.getAnnotation(c).filter(x => x.selector)[0];
componentAnotation.selector = this.getComponentTenant(c) + "-" + componentAnotation.selector;
}
});
}
});
return allModules;
}
getRoutes(): any[] {
var currentTenenat = this.getTenant();
var result = [];
var routes = {};
this.components.forEach(x => {
if (!routes[x.path]) {
routes[x.path]={ components :[]};
}
routes[x.path].components.push(x);
})
Object.keys(routes).forEach(function (key, index) {
console.log(this[key]);
}, routes);
for (var key in routes) {
var found = routes[key].components[0].component;
if (routes[key].components.length > 1) {
routes[key].components.forEach(c => {
if (c.tenant == currentTenenat) {
found = c.component;
}
});
}
result.push({ path: key, component: found });
}
console.log(result);
return result;
}
getTenant() {
if (window.location.toString().indexOf("gg") > 0) {
return "pruvit";
}
else
return "default";
}
}
var MultitenantService = (new MultitenantServiceRegistrations());
export function MultitenantModule(target) {
var ngModule = <NgModule>(Reflection.getAnnotation(target)[0]);
ngModule.declarations = MultitenantService.getBootModules(ngModule.declarations);
// var stupiiid=CompileReflector.prototype.annotations(target);
// console.log(stupiiid);
}
function Tenant(tenant: string) {
return <any>function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
MultitenantService.registerTenant(target, tenant);
};
}
function Path(path: string) {
return <any>function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
MultitenantService.registerPath(target, path);
};
}
export { MultitenantService };
export { Tenant };
export { Path };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment