Skip to content

Instantly share code, notes, and snippets.

View TheLarkInn's full-sized avatar
🦀
Getting Rusty

Sean Larkin TheLarkInn

🦀
Getting Rusty
View GitHub Profile
@TheLarkInn
TheLarkInn / bundle.umd.js
Created April 13, 2016 20:44
UMD Wrapped Webpack Lib
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["fun"] = factory();
else
root["fun"] = factory();
})(this, function() {
@TheLarkInn
TheLarkInn / rgbFadeLEDTessel.js
Last active April 17, 2016 03:23
Code for fading RGB via color map.
var five = require("johnny-five"),
TesselIO = require("tessel-io"),
board = new five.Board({
io: new TesselIO()
});
// Initial Red color to start with.
// Decent transitions are easier with RGB vs HEX.
var startingColorObject = {
r: 255,
@TheLarkInn
TheLarkInn / outside_event.plugin.ts
Created April 22, 2016 20:24
EventManagerPlugin extension to add to EVENT_MANAGER_PLUGINS for Angular2. Lets you handle clicking outside of target element.
import {DomEventsPlugin} from 'angular2/platform/common_dom';
// Have to pull DOM from src because platform/common_dom returns DOM as null.
// I believe its a TS bug.
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {Injectable} from 'angular2/core';
import {noop} from 'angular2/src/facade/lang';
@Injectable()
export class DOMOutsideEventPlugin extends DomEventsPlugin {
eventMap: Object = {
@TheLarkInn
TheLarkInn / bootStrapItNow.ts
Last active November 30, 2016 16:00
How to bundle up custom plugins with EVENT_MANAGER_PLGUINS
import {bootstrap} from 'angular2/platform/browser';
import {DIRECTIVES, PIPES, PROVIDERS, ENV_PROVIDERS, PLUGINS} from './platform/browser';
import {App, APP_PROVIDERS} from './app';
bootstrap(App, [
...PROVIDERS,
...ENV_PROVIDERS,
...DIRECTIVES,
...PIPES,
...APP_PROVIDERS,
@TheLarkInn
TheLarkInn / importingProvidersIntoYourAppViaBootstrap.ts
Created April 22, 2016 20:30
How to include providers custom and app providers in bootstrap function. Angular2
import {bootstrap} from 'angular2/platform/browser';
import {DIRECTIVES, PIPES, PROVIDERS, ENV_PROVIDERS, PLUGINS} from './platform/browser';
import {App, APP_PROVIDERS} from './app';
bootstrap(App, [
...PROVIDERS,
...ENV_PROVIDERS,
...DIRECTIVES,
...PIPES,
...APP_PROVIDERS,
@TheLarkInn
TheLarkInn / event_manager.ts
Created April 24, 2016 14:42
EventManagerPlugin class found in 'angular2/platform/dom/events'
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
import {Injectable, Inject, OpaqueToken} from 'angular2/src/core/di';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {ListWrapper} from 'angular2/src/facade/collection';
export const EVENT_MANAGER_PLUGINS: OpaqueToken =
CONST_EXPR(new OpaqueToken("EventManagerPlugins"));
@Injectable()
@TheLarkInn
TheLarkInn / dom_renderer_partial.ts
Created April 24, 2016 14:46
The listen and listenGlobal functions which call eventManager for listening to events.
listen(renderElement: any, name: string, callback: Function): Function {
return this._rootRenderer.eventManager.addEventListener(renderElement, name,
decoratePreventDefault(callback));
}
listenGlobal(target: string, name: string, callback: Function): Function {
return this._rootRenderer.eventManager.addGlobalEventListener(target, name,
decoratePreventDefault(callback));
}
@TheLarkInn
TheLarkInn / event_manager_finding_plugins.ts
Created April 24, 2016 14:52
A closer look at event_managers method for searching though its plugins to find valid eventplugins using the implemented supports() function.
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
var plugin = this._findPluginFor(eventName);
return plugin.addEventListener(element, eventName, handler);
}
addGlobalEventListener(target: string, eventName: string, handler: Function): Function {
var plugin = this._findPluginFor(eventName);
return plugin.addGlobalEventListener(target, eventName, handler);
}
@TheLarkInn
TheLarkInn / event_manager.d.ts
Created April 24, 2016 15:03
An empty EventManagerPlugin extension, accompanied by the EventManagerPlugins definition file so we can see how to implement it.
import { OpaqueToken } from 'angular2/src/core/di';
import { NgZone } from 'angular2/src/core/zone/ng_zone';
export declare const EVENT_MANAGER_PLUGINS: OpaqueToken;
export declare class EventManager {
private _zone;
private _plugins;
constructor(plugins: EventManagerPlugin[], _zone: NgZone);
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;
addGlobalEventListener(target: string, eventName: string, handler: Function): Function;
getZone(): NgZone;
@TheLarkInn
TheLarkInn / implementing_the_supports_function.ts
Created April 24, 2016 15:24
My implementation of the supports() function for our custom event.
getMultiEventArray(eventName: string): string[] {
return eventName.split(",")
.filter((item, index): boolean => { return item && item != '' })
}
supports(eventName: string): boolean {
return this.getMultiEventArray(eventName).length > 1
}