feather-ts
Version:
Lightweight javascript framework with REST, routing, bindings, message hub and event delegation (under 10kb gzipped)
314 lines (313 loc) • 12.1 kB
TypeScript
declare module feather.functions {
import FnOne = feather.types.FnOne;
import StringFactory = feather.types.StringFactory;
const compose: <U>(fns: FnOne[]) => any;
function isFunction(functionToCheck: any): boolean;
const isDef: (x: any) => boolean;
const isUndef: (x: any) => boolean;
const strFactory: (x: string | StringFactory) => string;
}
declare module feather.arrays {
import Widget = feather.core.Widget;
import Hook = feather.annotations.Hook;
import BindProperties = feather.observe.BindProperties;
interface ArrayListener<T> {
sort(indices: number[]): any;
splice(start: number, deleteCount: number, addedItems: T[], deletedItems: T[]): any;
}
function from<T>(object: any): T[];
function removeFromArray(arr: any[], elements: any[]): void;
let notifyListeners: (source: any[]) => void;
const range: (start: number, end: number) => number[];
const observeArray: <T>(arr: T[], listener: ArrayListener<T>) => void;
function defaultArrayListener(widget: Widget, arr: Widget[], hook: Hook, conf: BindProperties, filterFactory: Function): ArrayListener<Widget>;
}
declare module feather.objects {
import TypedMap = feather.types.TypedMap;
import ObjectChange = feather.types.ObjectChange;
const isObject: (obj: any) => boolean;
const values: <T>(data: TypedMap<T>) => T[];
function deepValue(obj: {}, path: string): any;
function setDeepValue(obj: {}, path: string, value: any): any;
function collectAnnotationsFromArray<T, P extends Object>(map: WeakMap<P, T[]>, start: P): T[];
/**
* deep merge objects and if some values are arrays, merge those accordingly
*/
const merge: (a: any, b: any) => any;
type NestedMapLike<V> = WeakMap<ObjectConstructor, TypedMap<V>> | Map<ObjectConstructor, TypedMap<V>>;
function collectAnnotationsFromTypeMap<T, P extends Object>(map: NestedMapLike<T>, start: P): TypedMap<T>;
const pathCallbacks: WeakMap<any, TypedMap<ObjectChange[]>>;
const createObjectPropertyListener: (obj: {}, path: string, callback: ObjectChange) => void;
const ensure: <T>(map: WeakMap<{}, T> | Map<{}, T>, obj: any, defaultValue: any) => T;
}
declare module feather.dom {
import ValidRoot = feather.types.ValidRoot;
function selectorMatches(el: ValidRoot, selector: string): boolean;
function allChildNodes(doc: Node): Element[];
function allTextNodes(doc: Node): Node[];
}
declare module feather.types {
type Primitive = string | number | boolean | Array<SimpleMap>;
type ValidRoot = Element | DocumentFragment;
type SimpleMap = {
[key: string]: any;
};
type TypedMap<T> = {
[key: string]: T;
};
type ObjectChange = (val: any) => void;
type Callback = (path: string) => void;
type OldNewCallback<T> = (newVal?: T, oldVal?: T) => void;
type FnOne = <A1, R>(arg: A1) => R;
type StringFactory = () => string;
}
declare module feather.strings {
function format(str: string, obj: any): string;
function namedRegexMatch(text: any, regex: any, matchNames: any): any;
}
declare module feather.observe {
import Subscribable = feather.hub.Subscribable;
import TypedMap = feather.types.TypedMap;
const getPath: (obj: Subscribable, property: string) => string;
const maybeStore: (parent: Subscribable, property: string, conf: BindProperties, value: any, isArray: boolean) => void;
const loadLocalStorageValue: (binders: WeakMap<any, TypedMap<BindProperties>>, context: Observable) => void;
const Write: (arrayName: string) => (proto: Observable, method: string) => void;
const Read: (arrayName: string) => (proto: Observable, method: string) => void;
}
declare module feather.boot {
import Constructable = feather.core.Constructable;
import ValidRoot = feather.types.ValidRoot;
import Widget = feather.core.Widget;
interface Blueprint {
selector: string;
attributes?: string[];
singleton?: boolean;
}
class ComponentInfo implements Blueprint {
selector: string;
singleton: boolean;
attributes: string[];
component: Constructable;
constructor(selector: string, singleton: boolean, attributes: string[], component: Constructable);
}
class WidgetFactory {
static widgetRegistry: ComponentInfo[];
static singletonRegistry: Widget[];
static start(scope?: ValidRoot, parentWidget?: Widget): void;
static initComponents(nodes: HTMLElement[], info: ComponentInfo, parentWidget?: Widget): void;
static register(info: Blueprint, component: any): void;
}
}
declare module feather {
let start: () => void;
}
declare module feather.media {
interface MediaConfig {
query: string;
method: string;
}
class MediaQueryAware {
attachMediaListeners(): void;
cleanUp(): void;
}
const Media: (query: string) => (proto: MediaQueryAware, method: string) => void;
}
declare module feather.annotations {
import Widget = feather.core.Widget;
import SimpleMap = feather.types.SimpleMap;
import ComponentInfo = feather.boot.ComponentInfo;
const selfClosingTags: RegExp;
const openTags = "$1></$2>";
enum HookType {
CLASS = 0,
PROPERTY = 1,
ATTRIBUTE = 2,
TEXT = 3,
}
class Hook {
node: Element;
type: HookType;
attribute: string;
property: string;
transformFns: string[];
constructor(node: Element, type: HookType, attribute: string, property: string, transformFns: string[]);
hasMethods: () => boolean;
}
class HookInfo {
nodePosition: number;
type: HookType;
curly: string;
attribute: string;
property: string;
transformFns: string[];
constructor(nodePosition: number, type: HookType, curly: string, attribute?: string, property?: string, transformFns?: string[]);
}
interface ParsedTemplate {
doc: Node;
first: Element;
hooks: Hook[];
components: Component[];
}
interface Component {
info: ComponentInfo;
nodes: HTMLElement[];
}
interface PreComponent {
info: ComponentInfo;
nodes: number[];
}
class PreparsedTemplate {
node: Node;
hookInfos: feather.annotations.HookInfo[];
hookMap: SimpleMap;
preComponents: PreComponent[];
constructor(node: Node, hookInfos: feather.annotations.HookInfo[], hookMap: SimpleMap, preComponents: PreComponent[]);
asParsedTemplate(): ParsedTemplate;
}
const getFragment: (html: string) => DocumentFragment;
function getPreparsedTemplate(templateStr: string): PreparsedTemplate;
class TemplateFactory {
static getTemplate(widget: Widget, name: string): ParsedTemplate;
static warmUp: () => void;
}
let Template: (name?: string) => (proto: Widget, method: string) => void;
}
declare module feather.event {
import MediaQueryAware = feather.media.MediaQueryAware;
enum Scope {
Direct = 0,
Delegate = 1,
}
interface EventConfig {
event: string;
scope?: Scope;
selector?: string;
preventDefault?: boolean;
bubble?: boolean;
}
interface Handler extends EventConfig {
method: string;
}
type HandlersRegistry = {
[s: number]: WeakMap<EventAware, Handler[]>;
};
type HandlersMap = {
[s: number]: Handler[];
};
let addListener: (el: Element, event: string, listener: EventListenerOrEventListenerObject) => void;
class EventAware extends MediaQueryAware {
element: Element;
attachEvents(): void;
handlers: (scope: Scope) => HandlersMap;
attachDirect(handlerMap: HandlersMap): void;
attachDelegates(handlerMap: HandlersMap): void;
eventRegistered(context: any, event: string, handler: Handler[], scope: Scope): void;
cleanUp(): void;
}
let On: (ec: EventConfig) => (proto: EventAware, method: string) => void;
}
declare module feather.hub {
import TypedMap = feather.types.TypedMap;
import EventAware = feather.event.EventAware;
const subscribers: WeakMap<any, TypedMap<string[]>>;
abstract class Subscribable extends EventAware {
parentWidget: Subscribable;
childWidgets: Array<Subscribable>;
triggerUp(event: string, data?: any): void;
triggerSingleton(event: string, data?: any): void;
triggerDown(event: string, data?: any): void;
private trigger(event, data?);
}
let Subscribe: (event: string, animationFrame?: boolean) => (proto: Subscribable, method: string, desc: PropertyDescriptor) => void;
}
declare module feather.routing {
import TypedMap = feather.types.TypedMap;
import Widget = feather.core.Widget;
import Subscribable = feather.hub.Subscribable;
const namedMatch: (pattern: string, input: string) => TypedMap<string>;
abstract class RouteAware extends Subscribable {
currentRoute: () => string;
initRoutes(): void;
route: (path: string) => void;
cleanUp(): void;
}
const runRoutes: () => void;
const Route: (route: string) => (proto: Widget, method: string) => void;
}
declare module feather.observe {
import RouteAware = feather.routing.RouteAware;
import Hook = feather.annotations.Hook;
interface BindProperties {
templateName?: string;
localStorage?: boolean;
bequeath?: boolean;
html?: boolean;
affectsArrays?: string[];
property?: string;
}
class Observable extends RouteAware {
attachHooks(hooks: Hook[], parent?: any): void;
cleanUp(): void;
}
const Bind: (props?: BindProperties) => (proto: Observable, property: string) => void;
const Computed: (...props: string[]) => (proto: Observable, property: string) => void;
interface Serializer {
write?: string;
read?: string;
}
}
declare module feather.annotations {
import Blueprint = feather.boot.Blueprint;
const Construct: (blueprint: Blueprint) => (target: any) => void;
}
declare module feather.xhr {
import TypedMap = feather.types.TypedMap;
import Widget = feather.core.Widget;
import StringFactory = feather.types.StringFactory;
type MethodValue = 'GET' | 'POST' | 'DELETE' | 'PUT';
const Method: {
GET: MethodValue;
POST: MethodValue;
DELETE: MethodValue;
PUT: MethodValue;
};
interface RestConfig {
url: string;
method?: MethodValue;
timeout?: number;
async?: boolean;
responseFilter?: (data: string) => any;
requestFilter?: (data: string) => any;
progress?: (ev: ProgressEvent) => any;
withCredentials?: boolean;
body?: string;
headers?: TypedMap<string | StringFactory>;
}
const defaultRestConfig: RestConfig;
let sendRequest: (conf: RestConfig, success: (data: any) => void, error: (err: string | Event, xhr?: XMLHttpRequest) => void) => Promise<any>;
let Rest: (params: RestConfig) => (proto: Widget, method: string, desc: PropertyDescriptor) => void;
}
declare module feather.core {
import Observable = feather.observe.Observable;
import ParsedTemplate = feather.annotations.ParsedTemplate;
interface Initializable {
}
interface Constructable {
new (): Initializable;
}
type StringGenerator = () => string;
enum RenderPlacement {
append = 0,
prepend = 1,
replace = 2,
}
abstract class Widget extends Observable implements Initializable {
element: Element;
id?: string | StringGenerator;
bindToElement(element: Element): void;
protected render(templateName?: string, placement?: RenderPlacement): void;
getParsed(templateName: string): ParsedTemplate;
init(element: Element): void;
cleanUp(): void;
}
}