maishu-chitu
Advanced tools
Comparing version 1.0.4 to 1.2.0
300
chitu.d.ts
@@ -33,3 +33,3 @@ declare namespace chitu { | ||
fileBasePath: string; | ||
backFail: Callback<Application, {}>; | ||
backFail: Callback<Application, null>; | ||
constructor(); | ||
@@ -40,3 +40,3 @@ protected parseRouteString(routeString: string): RouteData; | ||
readonly pages: Array<Page>; | ||
protected createPage(routeData: RouteData): Page; | ||
protected createPage(routeData: RouteData, actionArguments: any): Page; | ||
protected createPageElement(routeData: chitu.RouteData): HTMLElement; | ||
@@ -46,2 +46,3 @@ protected hashchange(): void; | ||
getPage(name: string): Page; | ||
private getPageByRouteString(routeString); | ||
showPage(routeString: string, args?: any): Page; | ||
@@ -91,12 +92,219 @@ setLocationHash(routeString: string): void; | ||
function Callbacks<S, A>(): Callback<S, A>; | ||
function fireCallback<S, A>(callback: Callback<S, A>, sender: S, args: A): void; | ||
class ValueStore<T> { | ||
private funcs; | ||
private _value; | ||
constructor(value?: T); | ||
add(func: (value: T) => any): (args: T) => any; | ||
remove(func: (value: T) => any): void; | ||
fire(value: T): void; | ||
value: T; | ||
} | ||
} | ||
declare namespace chitu { | ||
interface PageActionConstructor { | ||
new (args: Page): any; | ||
interface PageDisplayConstructor { | ||
new (app: Application): PageDisplayer; | ||
} | ||
interface PageConstructor { | ||
new (args: PageParams): Page; | ||
interface PageDisplayer { | ||
show(page: Page): Promise<any>; | ||
hide(page: Page): Promise<any>; | ||
} | ||
interface PageParams { | ||
app: Application; | ||
routeData: RouteData; | ||
element: HTMLElement; | ||
displayer: PageDisplayer; | ||
previous?: Page; | ||
actionArguments: any; | ||
} | ||
class Page { | ||
private animationTime; | ||
private num; | ||
private _element; | ||
private _previous; | ||
private _app; | ||
private _routeData; | ||
private _displayer; | ||
private _actionArguments; | ||
static tagName: string; | ||
error: Callback<Page, Error>; | ||
load: Callback<this, null>; | ||
loadComplete: Callback<this, null>; | ||
showing: Callback<this, null>; | ||
shown: Callback<this, null>; | ||
hiding: Callback<this, null>; | ||
hidden: Callback<this, null>; | ||
closing: Callback<this, null>; | ||
closed: Callback<this, null>; | ||
constructor(params: PageParams); | ||
private on_load(); | ||
private on_loadComplete(); | ||
private on_showing(); | ||
private on_shown(); | ||
private on_hiding(); | ||
private on_hidden(); | ||
private on_closing(); | ||
private on_closed(); | ||
show(): Promise<any>; | ||
hide(): Promise<any>; | ||
close(): Promise<any>; | ||
createService<T>(type: ServiceConstructor): Service; | ||
readonly element: HTMLElement; | ||
previous: Page; | ||
readonly routeData: RouteData; | ||
readonly name: string; | ||
private loadPageAction(); | ||
reload(): Promise<void>; | ||
} | ||
} | ||
interface PageActionConstructor { | ||
new (page: chitu.Page): any; | ||
} | ||
interface PageConstructor { | ||
new (args: chitu.PageParams): chitu.Page; | ||
} | ||
declare class PageDisplayerImplement implements chitu.PageDisplayer { | ||
show(page: chitu.Page): Promise<void>; | ||
hide(page: chitu.Page): Promise<void>; | ||
} | ||
interface ServiceError extends Error { | ||
method?: string; | ||
} | ||
declare function ajax<T>(url: string, options: RequestInit): Promise<T>; | ||
declare namespace chitu { | ||
interface ServiceConstructor { | ||
new (): Service; | ||
} | ||
abstract class Service { | ||
error: Callback<Service, Error>; | ||
static settings: { | ||
ajaxTimeout: number; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
constructor(); | ||
ajax<T>(url: string, options: RequestInit): Promise<T>; | ||
getByJson<T>(url: string, data?: any): Promise<T>; | ||
postByJson<T>(url: string, data?: Object): Promise<T>; | ||
deleteByJson<T>(url: string, data?: Object): Promise<T>; | ||
putByJson<T>(url: string, data?: Object): Promise<T>; | ||
get<T>(url: string, data?: any): Promise<T>; | ||
post<T>(url: string, data?: any): Promise<T>; | ||
put<T>(url: string, data?: any): Promise<T>; | ||
delete<T>(url: string, data?: any): Promise<T>; | ||
private ajaxByForm<T>(url, data, method); | ||
private ajaxByJSON<T>(url, data, method); | ||
} | ||
} | ||
declare namespace chitu { | ||
function combinePath(path1: string, path2: string): string; | ||
function loadjs(path: any): Promise<any>; | ||
} | ||
declare module "maishu-chitu" { | ||
export = chitu; | ||
} | ||
declare namespace chitu { | ||
class RouteData { | ||
private _parameters; | ||
private path_string; | ||
private path_spliter_char; | ||
private path_contact_char; | ||
private param_spliter; | ||
private name_spliter_char; | ||
private _pathBase; | ||
private _pageName; | ||
private _actionPath; | ||
private _routeString; | ||
private _loadCompleted; | ||
constructor(basePath: string, routeString: string, pathSpliterChar?: string); | ||
parseRouteString(): void; | ||
private pareeUrlQuery(query); | ||
readonly basePath: string; | ||
readonly values: any; | ||
readonly pageName: string; | ||
readonly routeString: string; | ||
readonly actionPath: string; | ||
readonly loadCompleted: boolean; | ||
} | ||
class Application { | ||
pageCreated: Callback<Application, Page>; | ||
protected pageType: PageConstructor; | ||
protected pageDisplayType: PageDisplayConstructor; | ||
private _runned; | ||
private zindex; | ||
private page_stack; | ||
private cachePages; | ||
fileBasePath: string; | ||
backFail: Callback<Application, null>; | ||
constructor(); | ||
protected parseRouteString(routeString: string): RouteData; | ||
private on_pageCreated(page); | ||
readonly currentPage: Page; | ||
readonly pages: Array<Page>; | ||
protected createPage(routeData: RouteData, actionArguments: any): Page; | ||
protected createPageElement(routeData: chitu.RouteData): HTMLElement; | ||
protected hashchange(): void; | ||
run(): void; | ||
getPage(name: string): Page; | ||
private getPageByRouteString(routeString); | ||
showPage(routeString: string, args?: any): Page; | ||
setLocationHash(routeString: string): void; | ||
private closeCurrentPage(); | ||
private clearPageStack(); | ||
redirect(routeString: string, args?: any): Page; | ||
back(args?: any): void; | ||
} | ||
} | ||
declare namespace chitu { | ||
class Errors { | ||
static argumentNull(paramName: string): Error; | ||
static modelFileExpecteFunction(script: any): Error; | ||
static paramTypeError(paramName: string, expectedType: string): Error; | ||
static paramError(msg: string): Error; | ||
static viewNodeNotExists(name: any): Error; | ||
static pathPairRequireView(index: any): Error; | ||
static notImplemented(name: any): Error; | ||
static routeExists(name: any): Error; | ||
static noneRouteMatched(url: any): Error; | ||
static emptyStack(): Error; | ||
static canntParseUrl(url: string): Error; | ||
static canntParseRouteString(routeString: string): Error; | ||
static routeDataRequireController(): Error; | ||
static routeDataRequireAction(): Error; | ||
static viewCanntNull(): Error; | ||
static createPageFail(pageName: string): Error; | ||
static actionTypeError(pageName: string): Error; | ||
static canntFindAction(pageName: any): Error; | ||
static exportsCanntNull(pageName: string): void; | ||
static scrollerElementNotExists(): Error; | ||
static resourceExists(resourceName: string, pageName: string): Error; | ||
} | ||
} | ||
declare namespace chitu { | ||
class Callback<S, A> { | ||
private funcs; | ||
constructor(); | ||
add(func: (sender: S, args: A) => any): void; | ||
remove(func: (sender: S, args: A) => any): void; | ||
fire(sender: S, args: A): void; | ||
} | ||
function Callbacks<S, A>(): Callback<S, A>; | ||
class ValueStore<T> { | ||
private funcs; | ||
private _value; | ||
constructor(value?: T); | ||
add(func: (value: T) => any): (args: T) => any; | ||
remove(func: (value: T) => any): void; | ||
fire(value: T): void; | ||
value: T; | ||
} | ||
} | ||
declare namespace chitu { | ||
interface PageDisplayConstructor { | ||
@@ -115,2 +323,3 @@ new (app: Application): PageDisplayer; | ||
previous?: Page; | ||
actionArguments: any; | ||
} | ||
@@ -125,22 +334,26 @@ class Page { | ||
private _displayer; | ||
private _actionArguments; | ||
static tagName: string; | ||
allowCache: boolean; | ||
load: Callback<this, any>; | ||
showing: Callback<this, {}>; | ||
shown: Callback<this, {}>; | ||
hiding: Callback<this, {}>; | ||
hidden: Callback<this, {}>; | ||
closing: Callback<this, {}>; | ||
closed: Callback<this, {}>; | ||
error: Callback<Page, Error>; | ||
load: Callback<this, null>; | ||
loadComplete: Callback<this, null>; | ||
showing: Callback<this, null>; | ||
shown: Callback<this, null>; | ||
hiding: Callback<this, null>; | ||
hidden: Callback<this, null>; | ||
closing: Callback<this, null>; | ||
closed: Callback<this, null>; | ||
constructor(params: PageParams); | ||
on_load(args: any): void; | ||
on_showing(): void; | ||
on_shown(): void; | ||
on_hiding(): void; | ||
on_hidden(): void; | ||
on_closing(): void; | ||
on_closed(): void; | ||
private on_load(); | ||
private on_loadComplete(); | ||
private on_showing(); | ||
private on_shown(); | ||
private on_hiding(); | ||
private on_hidden(); | ||
private on_closing(); | ||
private on_closed(); | ||
show(): Promise<any>; | ||
hide(): Promise<any>; | ||
close(): Promise<any>; | ||
createService<T extends Service>(type: ServiceConstructor<T>): T; | ||
readonly element: HTMLElement; | ||
@@ -153,6 +366,43 @@ previous: Page; | ||
} | ||
class PageDisplayerImplement implements PageDisplayer { | ||
show(page: Page): Promise<void>; | ||
hide(page: Page): Promise<void>; | ||
} | ||
interface PageActionConstructor { | ||
new (page: chitu.Page): any; | ||
} | ||
interface PageConstructor { | ||
new (args: chitu.PageParams): chitu.Page; | ||
} | ||
declare class PageDisplayerImplement implements chitu.PageDisplayer { | ||
show(page: chitu.Page): Promise<void>; | ||
hide(page: chitu.Page): Promise<void>; | ||
} | ||
interface ServiceError extends Error { | ||
method?: string; | ||
} | ||
declare function ajax<T>(url: string, options: RequestInit): Promise<T>; | ||
declare namespace chitu { | ||
interface ServiceConstructor<T> { | ||
new (): T; | ||
} | ||
abstract class Service { | ||
error: Callback<Service, Error>; | ||
static settings: { | ||
ajaxTimeout: number; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
constructor(); | ||
ajax<T>(url: string, options: RequestInit): Promise<T>; | ||
getByJson<T>(url: string, data?: any): Promise<T>; | ||
postByJson<T>(url: string, data?: Object): Promise<T>; | ||
deleteByJson<T>(url: string, data?: Object): Promise<T>; | ||
putByJson<T>(url: string, data?: Object): Promise<T>; | ||
get<T>(url: string, data?: any): Promise<T>; | ||
post<T>(url: string, data?: any): Promise<T>; | ||
put<T>(url: string, data?: any): Promise<T>; | ||
delete<T>(url: string, data?: any): Promise<T>; | ||
private ajaxByForm<T>(url, data, method); | ||
private ajaxByJSON<T>(url, data, method); | ||
} | ||
} | ||
@@ -159,0 +409,0 @@ |
460
chitu.es5.js
@@ -127,3 +127,3 @@ (function(factory) { | ||
this.pageType = chitu.Page; | ||
this.pageDisplayType = chitu.PageDisplayerImplement; | ||
this.pageDisplayType = PageDisplayerImplement; | ||
this._runned = false; | ||
@@ -145,7 +145,9 @@ this.page_stack = new Array(); | ||
value: function on_pageCreated(page) { | ||
return chitu.fireCallback(this.pageCreated, this, page); | ||
return this.pageCreated.fire(this, page); | ||
} | ||
}, { | ||
key: 'createPage', | ||
value: function createPage(routeData) { | ||
value: function createPage(routeData, actionArguments) { | ||
var _this = this; | ||
var previous_page = this.pages[this.pages.length - 1]; | ||
@@ -160,4 +162,12 @@ var element = this.createPageElement(routeData); | ||
displayer: displayer, | ||
element: element | ||
element: element, | ||
actionArguments: actionArguments | ||
}); | ||
var page_onclosed = function page_onclosed(sender) { | ||
_this.page_stack = _this.page_stack.filter(function (o) { | ||
return o != sender; | ||
}); | ||
page.closed.remove(page_onclosed); | ||
}; | ||
page.closed.add(page_onclosed); | ||
this.on_pageCreated(page); | ||
@@ -188,6 +198,9 @@ return page; | ||
if (location.hash.length > 1) routeString = location.hash.substr(1); | ||
var routeData = this.parseRouteString(routeString); | ||
var page = this.getPage(routeData.pageName); | ||
var page = this.getPageByRouteString(routeString); | ||
var previousPageIndex = this.page_stack.length - 2; | ||
this.showPage(routeString); | ||
if (page != null && this.page_stack.indexOf(page) == previousPageIndex) { | ||
this.closeCurrentPage(); | ||
} else { | ||
this.showPage(routeString); | ||
} | ||
} | ||
@@ -197,3 +210,3 @@ }, { | ||
value: function run() { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -204,3 +217,3 @@ if (this._runned) return; | ||
window.addEventListener('hashchange', function () { | ||
_this.hashchange(); | ||
_this2.hashchange(); | ||
}); | ||
@@ -219,2 +232,11 @@ this._runned = true; | ||
}, { | ||
key: 'getPageByRouteString', | ||
value: function getPageByRouteString(routeString) { | ||
for (var i = this.page_stack.length - 1; i >= 0; i--) { | ||
var page = this.page_stack[i]; | ||
if (page != null && page.routeData.routeString == routeString) return page; | ||
} | ||
return null; | ||
} | ||
}, { | ||
key: 'showPage', | ||
@@ -230,6 +252,3 @@ value: function showPage(routeString, args) { | ||
if (page == null) { | ||
page = this.createPage(routeData); | ||
if (page.allowCache) { | ||
this.cachePages[routeData.pageName] = page; | ||
} | ||
page = this.createPage(routeData, args); | ||
} | ||
@@ -264,8 +283,4 @@ if (page == this.currentPage) { | ||
var page = this.page_stack.pop(); | ||
if (page.allowCache) { | ||
page.hide(); | ||
} else { | ||
page.close(); | ||
if (this.cachePages[page.name]) this.cachePages[page.name] = null; | ||
} | ||
page.close(); | ||
if (this.cachePages[page.name]) this.cachePages[page.name] = null; | ||
if (this.currentPage != null) this.setLocationHash(this.currentPage.routeData.routeString); | ||
@@ -293,3 +308,3 @@ } | ||
if (this.page_stack.length == 0) { | ||
chitu.fireCallback(this.backFail, this, {}); | ||
this.backFail.fire(this, null); | ||
} | ||
@@ -428,3 +443,3 @@ } | ||
value: function actionTypeError(pageName) { | ||
var msg = 'The action in page \'' + pageName + '\' is expect as function or Class.'; | ||
var msg = 'The action in page \'' + pageName + '\' is expect as function.'; | ||
return new Error(msg); | ||
@@ -508,6 +523,46 @@ } | ||
chitu.Callbacks = Callbacks; | ||
function fireCallback(callback, sender, args) { | ||
callback.fire(sender, args); | ||
} | ||
chitu.fireCallback = fireCallback; | ||
var ValueStore = function () { | ||
function ValueStore(value) { | ||
_classCallCheck(this, ValueStore); | ||
this.funcs = new Array(); | ||
this._value = value; | ||
} | ||
_createClass(ValueStore, [{ | ||
key: "add", | ||
value: function add(func) { | ||
this.funcs.push(func); | ||
return func; | ||
} | ||
}, { | ||
key: "remove", | ||
value: function remove(func) { | ||
this.funcs = this.funcs.filter(function (o) { | ||
return o != func; | ||
}); | ||
} | ||
}, { | ||
key: "fire", | ||
value: function fire(value) { | ||
this.funcs.forEach(function (o) { | ||
return o(value); | ||
}); | ||
} | ||
}, { | ||
key: "value", | ||
get: function get() { | ||
return this._value; | ||
}, | ||
set: function set(value) { | ||
this._value = value; | ||
this.fire(value); | ||
} | ||
}]); | ||
return ValueStore; | ||
}(); | ||
chitu.ValueStore = ValueStore; | ||
})(chitu || (chitu = {})); | ||
@@ -553,4 +608,5 @@ //# sourceMappingURL=Extends.js.map | ||
this.animationTime = 300; | ||
this.allowCache = false; | ||
this.error = chitu.Callbacks(); | ||
this.load = chitu.Callbacks(); | ||
this.loadComplete = chitu.Callbacks(); | ||
this.showing = chitu.Callbacks(); | ||
@@ -567,2 +623,3 @@ this.shown = chitu.Callbacks(); | ||
this._displayer = params.displayer; | ||
this._actionArguments = params.actionArguments; | ||
this.loadPageAction(); | ||
@@ -573,9 +630,14 @@ } | ||
key: 'on_load', | ||
value: function on_load(args) { | ||
return chitu.fireCallback(this.load, this, args); | ||
value: function on_load() { | ||
return this.load.fire(this, null); | ||
} | ||
}, { | ||
key: 'on_loadComplete', | ||
value: function on_loadComplete() { | ||
return this.loadComplete.fire(this, null); | ||
} | ||
}, { | ||
key: 'on_showing', | ||
value: function on_showing() { | ||
return chitu.fireCallback(this.showing, this, {}); | ||
return this.showing.fire(this, null); | ||
} | ||
@@ -585,3 +647,3 @@ }, { | ||
value: function on_shown() { | ||
return chitu.fireCallback(this.shown, this, {}); | ||
return this.shown.fire(this, null); | ||
} | ||
@@ -591,3 +653,3 @@ }, { | ||
value: function on_hiding() { | ||
return chitu.fireCallback(this.hiding, this, {}); | ||
return this.hiding.fire(this, null); | ||
} | ||
@@ -597,3 +659,3 @@ }, { | ||
value: function on_hidden() { | ||
return chitu.fireCallback(this.hidden, this, {}); | ||
return this.hidden.fire(this, null); | ||
} | ||
@@ -603,3 +665,3 @@ }, { | ||
value: function on_closing() { | ||
return chitu.fireCallback(this.closing, this, {}); | ||
return this.closing.fire(this, null); | ||
} | ||
@@ -609,3 +671,3 @@ }, { | ||
value: function on_closed() { | ||
return chitu.fireCallback(this.closed, this, {}); | ||
return this.closed.fire(this, null); | ||
} | ||
@@ -644,6 +706,20 @@ }, { | ||
}, { | ||
key: 'createService', | ||
value: function createService(type) { | ||
var _this4 = this; | ||
var service = new type(); | ||
service.error.add(function (ender, error) { | ||
_this4.error.fire(_this4, error); | ||
}); | ||
return service; | ||
} | ||
}, { | ||
key: 'loadPageAction', | ||
value: function loadPageAction() { | ||
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var routeData, url, actionResult, actionName, action, args; | ||
var _this5 = this; | ||
var routeData, url, actionResult, actionName, action, _actionResult; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -682,19 +758,31 @@ while (1) { | ||
if (!(typeof action == 'function')) { | ||
_context.next = 16; | ||
_context.next = 19; | ||
break; | ||
} | ||
if (action['prototype'] != null) new action(this);else action(this); | ||
_context.next = 17; | ||
break; | ||
if (!(action['prototype'] != null)) { | ||
_context.next = 15; | ||
break; | ||
} | ||
case 16: | ||
throw chitu.Errors.actionTypeError(routeData.pageName); | ||
case 17: | ||
args = {}; | ||
case 15: | ||
_actionResult = action(this); | ||
this.on_load(args); | ||
if (_actionResult.then != null && _actionResult.catch != null) { | ||
_actionResult.then(function () { | ||
return _this5.on_loadComplete(); | ||
}); | ||
} | ||
_context.next = 20; | ||
break; | ||
case 19: | ||
throw chitu.Errors.actionTypeError(routeData.pageName); | ||
case 20: | ||
this.on_load(); | ||
case 21: | ||
case 'end': | ||
@@ -742,34 +830,282 @@ return _context.stop(); | ||
chitu.Page = Page; | ||
})(chitu || (chitu = {})); | ||
var PageDisplayerImplement = function () { | ||
function PageDisplayerImplement() { | ||
_classCallCheck(this, PageDisplayerImplement); | ||
var PageDisplayerImplement = function () { | ||
function PageDisplayerImplement() { | ||
_classCallCheck(this, PageDisplayerImplement); | ||
} | ||
_createClass(PageDisplayerImplement, [{ | ||
key: 'show', | ||
value: function show(page) { | ||
page.element.style.display = 'block'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'none'; | ||
} | ||
return Promise.resolve(); | ||
} | ||
}, { | ||
key: 'hide', | ||
value: function hide(page) { | ||
page.element.style.display = 'none'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'block'; | ||
} | ||
return Promise.resolve(); | ||
} | ||
}]); | ||
_createClass(PageDisplayerImplement, [{ | ||
key: 'show', | ||
value: function show(page) { | ||
page.element.style.display = 'block'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'none'; | ||
return PageDisplayerImplement; | ||
}(); | ||
//# sourceMappingURL=Page.js.map | ||
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var __awaiter = undefined && undefined.__awaiter || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : new P(function (resolve) { | ||
resolve(result.value); | ||
}).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
function _ajax(url, options) { | ||
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var response, responseText, p, text, textObject, isJSONContextType, err, travelJSON; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
travelJSON = function travelJSON(result) { | ||
var datePattern = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/; | ||
if (typeof result === 'string' && value.match(this.datePattern)) { | ||
return new Date(result); | ||
} | ||
var stack = new Array(); | ||
stack.push(result); | ||
while (stack.length > 0) { | ||
var item = stack.pop(); | ||
for (var key in item) { | ||
var value = item[key]; | ||
if (value == null) continue; | ||
if (value instanceof Array) { | ||
for (var i = 0; i < value.length; i++) { | ||
stack.push(value[i]); | ||
} | ||
continue; | ||
} | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) == 'object') { | ||
stack.push(value); | ||
continue; | ||
} | ||
if (typeof value == 'string' && value.match(datePattern)) { | ||
item[key] = new Date(value); | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
_context.next = 3; | ||
return fetch(url, options); | ||
case 3: | ||
response = _context.sent; | ||
responseText = response.text(); | ||
p = void 0; | ||
if (typeof responseText == 'string') { | ||
p = new Promise(function (reslove, reject) { | ||
reslove(responseText); | ||
}); | ||
} else { | ||
p = responseText; | ||
} | ||
_context.next = 9; | ||
return responseText; | ||
case 9: | ||
text = _context.sent; | ||
textObject = void 0; | ||
isJSONContextType = (response.headers.get('content-type') || '').indexOf('json') >= 0; | ||
if (isJSONContextType) { | ||
textObject = JSON.parse(text); | ||
textObject = travelJSON(textObject); | ||
} else { | ||
textObject = text; | ||
} | ||
if (!(response.status >= 300)) { | ||
_context.next = 20; | ||
break; | ||
} | ||
err = new Error(); | ||
err.method = options.method; | ||
err.name = '' + response.status; | ||
err.message = isJSONContextType ? textObject.Message || textObject.message : textObject; | ||
err.message = err.message || response.statusText; | ||
throw err; | ||
case 20: | ||
return _context.abrupt('return', textObject); | ||
case 21: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
return Promise.resolve(); | ||
} | ||
}, _callee, this); | ||
})); | ||
} | ||
var chitu; | ||
(function (chitu) { | ||
var Service = function () { | ||
function Service() { | ||
_classCallCheck(this, Service); | ||
this.error = chitu.Callbacks(); | ||
} | ||
_createClass(Service, [{ | ||
key: 'ajax', | ||
value: function ajax(url, options) { | ||
var _this = this; | ||
options.headers = Object.assign(Service.settings.headers || {}, options.headers || {}); | ||
return new Promise(function (reslove, reject) { | ||
var timeId = void 0; | ||
if (options.method == 'get') { | ||
timeId = setTimeout(function () { | ||
var err = new Error(); | ||
err.name = 'timeout'; | ||
err.message = '网络连接超时'; | ||
reject(err); | ||
_this.error.fire(_this, err); | ||
clearTimeout(timeId); | ||
}, Service.settings.ajaxTimeout * 1000); | ||
} | ||
_ajax(url, options).then(function (data) { | ||
reslove(data); | ||
if (timeId) clearTimeout(timeId); | ||
}).catch(function (err) { | ||
reject(err); | ||
_this.error.fire(_this, err); | ||
if (timeId) clearTimeout(timeId); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: 'hide', | ||
value: function hide(page) { | ||
page.element.style.display = 'none'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'block'; | ||
key: 'getByJson', | ||
value: function getByJson(url, data) { | ||
console.assert(url.indexOf('?') < 0); | ||
if (data) { | ||
url = url + '?' + JSON.stringify(data); | ||
} | ||
return Promise.resolve(); | ||
return this.ajaxByJSON(url, null, 'get'); | ||
} | ||
}, { | ||
key: 'postByJson', | ||
value: function postByJson(url, data) { | ||
return this.ajaxByJSON(url, data, 'post'); | ||
} | ||
}, { | ||
key: 'deleteByJson', | ||
value: function deleteByJson(url, data) { | ||
return this.ajaxByJSON(url, data, 'delete'); | ||
} | ||
}, { | ||
key: 'putByJson', | ||
value: function putByJson(url, data) { | ||
return this.ajaxByJSON(url, data, 'put'); | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get(url, data) { | ||
data = data || {}; | ||
var urlParams = ''; | ||
for (var key in data) { | ||
urlParams = urlParams + ('&' + key + '=' + data[key]); | ||
} | ||
console.assert(url.indexOf('?') < 0); | ||
url = url + '?' + urlParams; | ||
var options = { | ||
method: 'get' | ||
}; | ||
return this.ajax(url, options); | ||
} | ||
}, { | ||
key: 'post', | ||
value: function post(url, data) { | ||
return this.ajaxByForm(url, data, 'post'); | ||
} | ||
}, { | ||
key: 'put', | ||
value: function put(url, data) { | ||
return this.ajaxByForm(url, data, 'put'); | ||
} | ||
}, { | ||
key: 'delete', | ||
value: function _delete(url, data) { | ||
return this.ajaxByForm(url, data, 'delete'); | ||
} | ||
}, { | ||
key: 'ajaxByForm', | ||
value: function ajaxByForm(url, data, method) { | ||
var form = new FormData(); | ||
for (var key in data) { | ||
form.append(key, data[key]); | ||
} | ||
return this.ajax(url, { body: form, method: method }); | ||
} | ||
}, { | ||
key: 'ajaxByJSON', | ||
value: function ajaxByJSON(url, data, method) { | ||
var headers = {}; | ||
headers['content-type'] = 'application/json'; | ||
var body = void 0; | ||
if (data) body = JSON.stringify(data); | ||
var options = { | ||
headers: headers, | ||
body: body, | ||
method: method | ||
}; | ||
return this.ajax(url, options); | ||
} | ||
}]); | ||
return PageDisplayerImplement; | ||
return Service; | ||
}(); | ||
chitu.PageDisplayerImplement = PageDisplayerImplement; | ||
Service.settings = { | ||
ajaxTimeout: 30, | ||
headers: {} | ||
}; | ||
chitu.Service = Service; | ||
})(chitu || (chitu = {})); | ||
//# sourceMappingURL=Page.js.map | ||
//# sourceMappingURL=Service.js.map | ||
@@ -776,0 +1112,0 @@ 'use strict'; |
314
chitu.js
@@ -90,3 +90,3 @@ (function(factory) { | ||
this.pageType = chitu.Page; | ||
this.pageDisplayType = chitu.PageDisplayerImplement; | ||
this.pageDisplayType = PageDisplayerImplement; | ||
this._runned = false; | ||
@@ -103,3 +103,3 @@ this.page_stack = new Array(); | ||
on_pageCreated(page) { | ||
return chitu.fireCallback(this.pageCreated, this, page); | ||
return this.pageCreated.fire(this, page); | ||
} | ||
@@ -114,3 +114,3 @@ get currentPage() { | ||
} | ||
createPage(routeData) { | ||
createPage(routeData, actionArguments) { | ||
let previous_page = this.pages[this.pages.length - 1]; | ||
@@ -125,4 +125,10 @@ let element = this.createPageElement(routeData); | ||
displayer, | ||
element | ||
element, | ||
actionArguments | ||
}); | ||
let page_onclosed = (sender) => { | ||
this.page_stack = this.page_stack.filter(o => o != sender); | ||
page.closed.remove(page_onclosed); | ||
}; | ||
page.closed.add(page_onclosed); | ||
this.on_pageCreated(page); | ||
@@ -150,6 +156,10 @@ return page; | ||
routeString = location.hash.substr(1); | ||
var routeData = this.parseRouteString(routeString); | ||
var page = this.getPage(routeData.pageName); | ||
var page = this.getPageByRouteString(routeString); | ||
let previousPageIndex = this.page_stack.length - 2; | ||
this.showPage(routeString); | ||
if (page != null && this.page_stack.indexOf(page) == previousPageIndex) { | ||
this.closeCurrentPage(); | ||
} | ||
else { | ||
this.showPage(routeString); | ||
} | ||
} | ||
@@ -174,2 +184,10 @@ run() { | ||
} | ||
getPageByRouteString(routeString) { | ||
for (var i = this.page_stack.length - 1; i >= 0; i--) { | ||
var page = this.page_stack[i]; | ||
if (page != null && page.routeData.routeString == routeString) | ||
return page; | ||
} | ||
return null; | ||
} | ||
showPage(routeString, args) { | ||
@@ -185,6 +203,3 @@ if (!routeString) | ||
if (page == null) { | ||
page = this.createPage(routeData); | ||
if (page.allowCache) { | ||
this.cachePages[routeData.pageName] = page; | ||
} | ||
page = this.createPage(routeData, args); | ||
} | ||
@@ -217,10 +232,5 @@ if (page == this.currentPage) { | ||
var page = this.page_stack.pop(); | ||
if (page.allowCache) { | ||
page.hide(); | ||
} | ||
else { | ||
page.close(); | ||
if (this.cachePages[page.name]) | ||
this.cachePages[page.name] = null; | ||
} | ||
page.close(); | ||
if (this.cachePages[page.name]) | ||
this.cachePages[page.name] = null; | ||
if (this.currentPage != null) | ||
@@ -241,3 +251,3 @@ this.setLocationHash(this.currentPage.routeData.routeString); | ||
if (this.page_stack.length == 0) { | ||
chitu.fireCallback(this.backFail, this, {}); | ||
this.backFail.fire(this, null); | ||
} | ||
@@ -316,3 +326,3 @@ } | ||
static actionTypeError(pageName) { | ||
let msg = `The action in page '${pageName}' is expect as function or Class.`; | ||
let msg = `The action in page '${pageName}' is expect as function.`; | ||
return new Error(msg); | ||
@@ -360,6 +370,26 @@ } | ||
chitu.Callbacks = Callbacks; | ||
function fireCallback(callback, sender, args) { | ||
callback.fire(sender, args); | ||
class ValueStore { | ||
constructor(value) { | ||
this.funcs = new Array(); | ||
this._value = value; | ||
} | ||
add(func) { | ||
this.funcs.push(func); | ||
return func; | ||
} | ||
remove(func) { | ||
this.funcs = this.funcs.filter(o => o != func); | ||
} | ||
fire(value) { | ||
this.funcs.forEach(o => o(value)); | ||
} | ||
get value() { | ||
return this._value; | ||
} | ||
set value(value) { | ||
this._value = value; | ||
this.fire(value); | ||
} | ||
} | ||
chitu.fireCallback = fireCallback; | ||
chitu.ValueStore = ValueStore; | ||
})(chitu || (chitu = {})); | ||
@@ -380,4 +410,5 @@ | ||
this.animationTime = 300; | ||
this.allowCache = false; | ||
this.error = chitu.Callbacks(); | ||
this.load = chitu.Callbacks(); | ||
this.loadComplete = chitu.Callbacks(); | ||
this.showing = chitu.Callbacks(); | ||
@@ -394,24 +425,28 @@ this.shown = chitu.Callbacks(); | ||
this._displayer = params.displayer; | ||
this._actionArguments = params.actionArguments; | ||
this.loadPageAction(); | ||
} | ||
on_load(args) { | ||
return chitu.fireCallback(this.load, this, args); | ||
on_load() { | ||
return this.load.fire(this, null); | ||
} | ||
on_loadComplete() { | ||
return this.loadComplete.fire(this, null); | ||
} | ||
on_showing() { | ||
return chitu.fireCallback(this.showing, this, {}); | ||
return this.showing.fire(this, null); | ||
} | ||
on_shown() { | ||
return chitu.fireCallback(this.shown, this, {}); | ||
return this.shown.fire(this, null); | ||
} | ||
on_hiding() { | ||
return chitu.fireCallback(this.hiding, this, {}); | ||
return this.hiding.fire(this, null); | ||
} | ||
on_hidden() { | ||
return chitu.fireCallback(this.hidden, this, {}); | ||
return this.hidden.fire(this, null); | ||
} | ||
on_closing() { | ||
return chitu.fireCallback(this.closing, this, {}); | ||
return this.closing.fire(this, null); | ||
} | ||
on_closed() { | ||
return chitu.fireCallback(this.closed, this, {}); | ||
return this.closed.fire(this, null); | ||
} | ||
@@ -437,2 +472,9 @@ show() { | ||
} | ||
createService(type) { | ||
let service = new type(); | ||
service.error.add((ender, error) => { | ||
this.error.fire(this, error); | ||
}); | ||
return service; | ||
} | ||
get element() { | ||
@@ -468,5 +510,7 @@ return this._element; | ||
if (action['prototype'] != null) | ||
new action(this); | ||
else | ||
action(this); | ||
throw chitu.Errors.actionTypeError(routeData.pageName); | ||
let actionResult = action(this); | ||
if (actionResult.then != null && actionResult.catch != null) { | ||
actionResult.then(() => this.on_loadComplete()); | ||
} | ||
} | ||
@@ -476,4 +520,3 @@ else { | ||
} | ||
let args = {}; | ||
this.on_load(args); | ||
this.on_load(); | ||
}); | ||
@@ -487,19 +530,190 @@ } | ||
chitu.Page = Page; | ||
class PageDisplayerImplement { | ||
show(page) { | ||
page.element.style.display = 'block'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'none'; | ||
})(chitu || (chitu = {})); | ||
class PageDisplayerImplement { | ||
show(page) { | ||
page.element.style.display = 'block'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'none'; | ||
} | ||
return Promise.resolve(); | ||
} | ||
hide(page) { | ||
page.element.style.display = 'none'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'block'; | ||
} | ||
return Promise.resolve(); | ||
} | ||
} | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
function ajax(url, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let response = yield fetch(url, options); | ||
let responseText = response.text(); | ||
let p; | ||
if (typeof responseText == 'string') { | ||
p = new Promise((reslove, reject) => { | ||
reslove(responseText); | ||
}); | ||
} | ||
else { | ||
p = responseText; | ||
} | ||
let text = yield responseText; | ||
let textObject; | ||
let isJSONContextType = (response.headers.get('content-type') || '').indexOf('json') >= 0; | ||
if (isJSONContextType) { | ||
textObject = JSON.parse(text); | ||
textObject = travelJSON(textObject); | ||
} | ||
else { | ||
textObject = text; | ||
} | ||
if (response.status >= 300) { | ||
let err = new Error(); | ||
err.method = options.method; | ||
err.name = `${response.status}`; | ||
err.message = isJSONContextType ? (textObject.Message || textObject.message) : textObject; | ||
err.message = err.message || response.statusText; | ||
throw err; | ||
} | ||
return textObject; | ||
function travelJSON(result) { | ||
const datePattern = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/; | ||
if (typeof result === 'string' && value.match(this.datePattern)) { | ||
return new Date(result); | ||
} | ||
return Promise.resolve(); | ||
var stack = new Array(); | ||
stack.push(result); | ||
while (stack.length > 0) { | ||
var item = stack.pop(); | ||
for (var key in item) { | ||
var value = item[key]; | ||
if (value == null) | ||
continue; | ||
if (value instanceof Array) { | ||
for (var i = 0; i < value.length; i++) { | ||
stack.push(value[i]); | ||
} | ||
continue; | ||
} | ||
if (typeof value == 'object') { | ||
stack.push(value); | ||
continue; | ||
} | ||
if (typeof value == 'string' && value.match(datePattern)) { | ||
item[key] = new Date(value); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
hide(page) { | ||
page.element.style.display = 'none'; | ||
if (page.previous != null) { | ||
page.previous.element.style.display = 'block'; | ||
}); | ||
} | ||
var chitu; | ||
(function (chitu) { | ||
class Service { | ||
constructor() { | ||
this.error = chitu.Callbacks(); | ||
} | ||
ajax(url, options) { | ||
options.headers = Object.assign(Service.settings.headers || {}, options.headers || {}); | ||
return new Promise((reslove, reject) => { | ||
let timeId; | ||
if (options.method == 'get') { | ||
timeId = setTimeout(() => { | ||
let err = new Error(); | ||
err.name = 'timeout'; | ||
err.message = '网络连接超时'; | ||
reject(err); | ||
this.error.fire(this, err); | ||
clearTimeout(timeId); | ||
}, Service.settings.ajaxTimeout * 1000); | ||
} | ||
ajax(url, options) | ||
.then(data => { | ||
reslove(data); | ||
if (timeId) | ||
clearTimeout(timeId); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
this.error.fire(this, err); | ||
if (timeId) | ||
clearTimeout(timeId); | ||
}); | ||
}); | ||
} | ||
getByJson(url, data) { | ||
console.assert(url.indexOf('?') < 0); | ||
if (data) { | ||
url = url + '?' + JSON.stringify(data); | ||
} | ||
return Promise.resolve(); | ||
return this.ajaxByJSON(url, null, 'get'); | ||
} | ||
postByJson(url, data) { | ||
return this.ajaxByJSON(url, data, 'post'); | ||
} | ||
deleteByJson(url, data) { | ||
return this.ajaxByJSON(url, data, 'delete'); | ||
} | ||
putByJson(url, data) { | ||
return this.ajaxByJSON(url, data, 'put'); | ||
} | ||
get(url, data) { | ||
data = data || {}; | ||
let urlParams = ''; | ||
for (let key in data) { | ||
urlParams = urlParams + `&${key}=${data[key]}`; | ||
} | ||
console.assert(url.indexOf('?') < 0); | ||
url = url + '?' + urlParams; | ||
let options = { | ||
method: 'get', | ||
}; | ||
return this.ajax(url, options); | ||
} | ||
post(url, data) { | ||
return this.ajaxByForm(url, data, 'post'); | ||
} | ||
put(url, data) { | ||
return this.ajaxByForm(url, data, 'put'); | ||
} | ||
delete(url, data) { | ||
return this.ajaxByForm(url, data, 'delete'); | ||
} | ||
ajaxByForm(url, data, method) { | ||
var form = new FormData(); | ||
for (let key in data) { | ||
form.append(key, data[key]); | ||
} | ||
return this.ajax(url, { body: form, method }); | ||
} | ||
ajaxByJSON(url, data, method) { | ||
let headers = {}; | ||
headers['content-type'] = 'application/json'; | ||
let body; | ||
if (data) | ||
body = JSON.stringify(data); | ||
let options = { | ||
headers, | ||
body, | ||
method | ||
}; | ||
return this.ajax(url, options); | ||
} | ||
} | ||
chitu.PageDisplayerImplement = PageDisplayerImplement; | ||
Service.settings = { | ||
ajaxTimeout: 30, | ||
headers: {} | ||
}; | ||
chitu.Service = Service; | ||
})(chitu || (chitu = {})); | ||
@@ -506,0 +720,0 @@ |
{ | ||
"name": "maishu-chitu", | ||
"version": "1.0.4", | ||
"author": "mai.shu", | ||
"version": "1.2.0", | ||
"devDependencies": { | ||
@@ -15,22 +14,3 @@ "babel-preset-es2015": "^6.18.0", | ||
"grunt-ts": "~5.5.1" | ||
}, | ||
"description": "采用TypeScript编写的轻量级单页面类库,帮助开发人员编写易维护的大规模项目。采用面向对象,事件驱动的编程模式。", | ||
"main": "chitu.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"dependencies": { | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ansiboy/chitu.git" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ansiboy/chitu/issues" | ||
}, | ||
"homepage": "https://github.com/ansiboy/chitu#readme" | ||
} | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
82909
2152
1
2
1
2