clean up js files

This commit is contained in:
Adam Bradley
2015-07-03 13:51:34 -05:00
parent 6ab57f916d
commit 90d6564c10
20 changed files with 277 additions and 1099 deletions

View File

@ -1,181 +0,0 @@
import {
RegExp,
RegExpWrapper,
RegExpMatcherWrapper,
StringWrapper,
isPresent,
isBlank,
BaseException,
normalizeBlank
} from 'angular2/src/facade/lang';
import {
Map,
MapWrapper,
StringMap,
StringMapWrapper,
List,
ListWrapper
} from 'angular2/src/facade/collection';
class ContinuationSegment {
generate(params) {
return '';
}
}
class StaticSegment {
constructor(string) {
this.name = '';
this.regex = escapeRegex(string);
}
generate(params) {
return this.string;
}
}
class DynamicSegment {
constructor(name) {
this.regex = "([^/]+)";
}
generate(params) {
if (!StringMapWrapper.contains(params, this.name)) {
throw new BaseException(
`Route generator for '${this.name}' was not included in parameters passed.`)
}
return normalizeBlank(StringMapWrapper.get(params, this.name));
}
}
class StarSegment {
constructor(name) {
this.regex = "(.+)";
}
generate(params) {
return normalizeBlank(StringMapWrapper.get(params, this.name));
}
}
var paramMatcher = RegExpWrapper.create("^:([^\/]+)$");
var wildcardMatcher = RegExpWrapper.create("^\\*([^\/]+)$");
function parsePathString(route: string) {
// normalize route as not starting with a "/". Recognition will
// also normalize.
if (StringWrapper.startsWith(route, "/")) {
route = StringWrapper.substring(route, 1);
}
var segments = splitBySlash(route);
var results = [];
var specificity = 0;
// The "specificity" of a path is used to determine which route is used when multiple routes match
// a URL.
// Static segments (like "/foo") are the most specific, followed by dynamic segments (like
// "/:id"). Star segments
// add no specificity. Segments at the start of the path are more specific than proceeding ones.
// The code below uses place values to combine the different types of segments into a single
// integer that we can
// sort later. Each static segment is worth hundreds of points of specificity (10000, 9900, ...,
// 200), and each
// dynamic segment is worth single points of specificity (100, 99, ... 2).
if (segments.length > 98) {
throw new BaseException(`'${route}' has more than the maximum supported number of segments.`);
}
var limit = segments.length - 1;
for (var i = 0; i <= limit; i++) {
var segment = segments[i], match;
if (isPresent(match = RegExpWrapper.firstMatch(paramMatcher, segment))) {
results.push(new DynamicSegment(match[1]));
specificity += (100 - i);
} else if (isPresent(match = RegExpWrapper.firstMatch(wildcardMatcher, segment))) {
results.push(new StarSegment(match[1]));
} else if (segment == '...') {
if (i < limit) {
// TODO (matsko): setup a proper error here `
throw new BaseException(`Unexpected "..." before the end of the path for "${route}".`);
}
results.push(new ContinuationSegment());
} else if (segment.length > 0) {
results.push(new StaticSegment(segment));
specificity += 100 * (100 - i);
}
}
return {segments: results, specificity};
}
function splitBySlash(url: string): List<string> {
return url.split('/');
}
// represents something like '/foo/:bar'
export class PathRecognizer {
constructor(path) {
this.segments = [];
// TODO: use destructuring assignment
// see https://github.com/angular/ts2dart/issues/158
var parsed = parsePathString(path);
var specificity = parsed['specificity'];
var segments = parsed['segments'];
var regexString = '^';
ListWrapper.forEach(segments, (segment) => {
if (segment instanceof ContinuationSegment) {
this.terminal = false;
} else {
regexString += '/' + segment.regex;
}
});
if (this.terminal) {
regexString += '$';
}
this.regex = RegExpWrapper.create(regexString);
this.segments = segments;
this.specificity = specificity;
}
parseParams(url) {
var params = StringMapWrapper.create();
var urlPart = url;
for (var i = 0; i < this.segments.length; i++) {
var segment = this.segments[i];
if (segment instanceof ContinuationSegment) {
continue;
}
var match = RegExpWrapper.firstMatch(RegExpWrapper.create('/' + segment.regex), urlPart);
urlPart = StringWrapper.substring(urlPart, match[0].length);
if (segment.name.length > 0) {
StringMapWrapper.set(params, segment.name, match[1]);
}
}
return params;
}
generate(params) {
return ListWrapper.join(
ListWrapper.map(this.segments, (segment) => '/' + segment.generate(params)), '');
}
}
var specialCharacters = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'];
var escapeRe = RegExpWrapper.create('(\\' + specialCharacters.join('|\\') + ')', 'g');
function escapeRegex(string): string {
return StringWrapper.replaceAllMapped(string, escapeRe, (match) => { return "\\" + match; });
}

View File

@ -1,169 +0,0 @@
import {
RegExp,
RegExpWrapper,
RegExpMatcherWrapper,
StringWrapper,
isPresent,
isBlank,
BaseException,
normalizeBlank
} from 'angular2/src/facade/lang';
import * as util from '../util/util';
import {PathRecognizer} from './path-recognizer';
export class IonicRouter {
constructor(config) {
this._routes = {};
this._viewCtrls = [];
this.config(config);
}
app(app) {
this._app = app;
}
config(config) {
for (let routeName in config) {
this.addRoute(routeName, config[routeName]);
}
}
addRoute(routeName, routeConfig) {
if (routeName && routeConfig && routeConfig.path) {
this._routes[routeName] = new Route(routeName, routeConfig)
}
}
init() {
let rootViewCtrl = this.activeViewController();
if (rootViewCtrl) {
let matchedRoute = this.match( this.getCurrentPath() ) || this.otherwise();
this.push(rootViewCtrl, matchedRoute);
}
}
match(path) {
let matchedRoute = null;
let routeMatch = null;
let highestSpecifity = 0;
for (let routeName in this._routes) {
routeMatch = this._routes[routeName].match(path);
if (routeMatch.match && (!matchedRoute || routeMatch.specificity > highestSpecifity)) {
matchedRoute = this._routes[routeName];
highestSpecifity = routeMatch.specificity;
}
}
return matchedRoute;
}
otherwise(val) {
if (arguments.length) {
this._otherwise = val;
} else if (this._otherwise) {
return this._routes[this._otherwise];
}
}
push(viewCtrl, route) {
let self = this;
function run() {
self._app.zone().run(() => {
viewCtrl.push(route.cls);
});
}
if (viewCtrl && route) {
if (route.cls) {
run();
} else if (route.module) {
System.import(route.module).then(m => {
if (m) {
route.cls = m[route.name];
run();
}
});
}
}
}
stateChange(activeView) {
if (activeView && activeView.ComponentType) {
let routeConfig = activeView.ComponentType.route;
if (routeConfig) {
let matchedRoute = this.match(routeConfig.path);
if (matchedRoute) {
this.updateState(matchedRoute);
}
}
}
}
updateState(route) {
let newPath = route.path;
if (window.location.hash !== '#' + newPath) {
console.log('updateState', newPath);
window.location.hash = newPath;
}
}
addViewController(viewCtrl) {
this._viewCtrls.push(viewCtrl);
}
activeViewController() {
if (this._viewCtrls.length) {
return this._viewCtrls[ this._viewCtrls.length - 1 ];
}
return null;
}
getCurrentPath() {
let hash = window.location.hash;
// Grab the path without the leading hash
return hash.slice(1);
}
}
export class Routable {
constructor(cls, routeConfig) {
cls.route = routeConfig;
}
}
class Route {
constructor(name, routeConfig) {
this.name = name;
this.cls = null;
util.extend(this, routeConfig);
this.recognizer = new PathRecognizer(this.path);
}
match(matchPath) {
let routeMatch = new RouteMatch(this, matchPath);
if (routeMatch) {
return routeMatch;
}
return false;
}
}
class RouteMatch {
constructor(route, matchPath) {
this.route = route;
this.specificity = route.recognizer.specificity;
this.match = RegExpWrapper.firstMatch(route.recognizer.regex, matchPath);
}
}