Skip to content

Instantly share code, notes, and snippets.

View brakmic's full-sized avatar
🏠

Harris Brakmić brakmic

🏠
View GitHub Profile
@brakmic
brakmic / vr_module_reducer.ts
Created March 14, 2017 14:56
vr module reducer
const vrModuleReducer: ActionReducer<IVrModule[]> = (state: IVrModule[] =
initialState, action: Action) => {
switch (action.type) {
case VR_MODULE_REMOVED:
return _.filter(state, (mod) => mod.id === action.payload.id);
case VR_MODULE_ADDED:
return _.concat(state, action.payload);
default:
return state;
}
@brakmic
brakmic / vr_module_interface.ts
Created March 14, 2017 09:54
IVrModule interface
interface IVrModule {
id: string;
name: string;
type: VrModuleType;
markup: string;
scripts?: string[];
}
@brakmic
brakmic / append_script_method.ts
Created March 12, 2017 14:00
helper method for script insertion
private appendScript(host: HTMLElement, data: string, index: number) {
this.ngZone.runOutsideAngular(() => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'utf-8';
script.id = this.vrScriptPrefix + index;
script.defer = true;
script.async = true;
script.text = data;
host.appendChild(script);
@brakmic
brakmic / create_component_factory.ts
Created March 12, 2017 13:35
helper function for dynamic component creation in Angular 2
export function createComponentFactory(compiler: Compiler,
metadata: Component):
Promise<ComponentFactory<any>> {
const cmpClass = class DynamicComponent {};
const decoratedCmp = Component(metadata)(cmpClass);
@NgModule({
imports: [ CommonModule ],
declarations: [decoratedCmp],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ] })
@brakmic
brakmic / create_component_dynamically.ts
Last active March 12, 2017 13:26
dynamic creation of Angular components
const body = document.getElementsByTagName('body')[0];
// if the markup has changed remove the existing vr-module scripts first
Promise.resolve(_.each(this.activeScripts, (script, index) => {
const oldNode = body.removeChild(document.getElementById(this.vrScriptPrefix + index));
})).then(() => {
// clean up the view-container reference...
this.activeScripts = [];
_.each(cmp.scripts, (script, idx) => {
this.appendScript(body, script, Number(idx))
});
@brakmic
brakmic / vrwrapper.component.html
Created March 12, 2017 13:06
wrapper component HTML structure
<vr-element [dynamicComponent]="dynamicComponent"></vr-element>
@brakmic
brakmic / route_params_subscription.ts
Last active March 12, 2017 13:27
reacting to route changes in vrwrappercomponent
// Each time the route changes take the ID and instantiate the appropriate vr element.
// If the element contains a script-array load them too.
// Check shared/directives/vr-element.directive.ts for more info regarding
// dynamic instantiation of vr elements.
this.routeSubscription = this.route.params.subscribe((params) => {
const id = params['id'];
const mod = _.find(this.availableModules, (m) => m.id === id);
if (!_.isNil(mod)) {
this.dynamicComponent = {
html: mod ? mod.markup : undefined,
@brakmic
brakmic / child_routes_vrwrapper.module.ts
Created March 12, 2017 12:55
child routes for vrwrapper-module
import { Routes } from '@angular/router';
import { VrWrapperComponent } from './wrapper.component';
export const WRAPPER_ROUTES: Routes = [
{ path: '',
component: VrWrapperComponent
}
];
@brakmic
brakmic / vrwrappermodule_route.ts
Created March 12, 2017 12:52
routing target - vr-wrapper-module.ts
export const APP_ROUTES: Routes = [
{ path: '',
children: [
{ path: '', loadChildren: './shared/wrapper#VrWrapperModule' },
{ path: ':area', loadChildren: './shared/wrapper#VrWrapperModule' }
]
}
];
@brakmic
brakmic / click_handler_vr_module.ts
Created March 12, 2017 12:47
click handler for module selection
/**
* Click handler for vr module selections (sidebar on the left)
* Check app.routes.ts for more info on angular2 routing
*
* @private
* @param {*} event
*/
private onVrModuleSelected(event: any) {
const params = {
id: event.module.id