Skip to content

Commit

Permalink
#224: Shim the following APIs to supoprt the Redline widget:
Browse files Browse the repository at this point in the history
 - Fusion.ajaxRequest
 - [MapWidget].digitizeLine
  • Loading branch information
jumpinjackie committed May 9, 2017
1 parent c679408 commit 24dc14b
Showing 1 changed file with 87 additions and 14 deletions.
101 changes: 87 additions & 14 deletions src/containers/viewer-shim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import olGeom from "ol/geom/geometry";
import * as Constants from "../constants";
import * as Runtime from "../api/runtime";
import * as logger from "../utils/logger";
import { Client } from "../api/client";
import { MgError } from "../api/error";
import { RuntimeMap } from "../api/contracts/runtime-map";
import { FeatureSet, SelectedFeatureSet, QueryMapFeaturesResponse } from "../api/contracts/query";
import { RuntimeMapFeatureFlags } from "../api/request-builder";
import { RefreshMode, ReduxDispatch, IApplicationState, ICommand } from "../api/common";
import { RefreshMode, ReduxDispatch, IApplicationState, ICommand, ClientKind } from "../api/common";
import * as MapActions from "../actions/map";
import * as TaskPaneActions from "../actions/taskpane";
import * as LegendActions from "../actions/legend";
Expand All @@ -22,6 +23,7 @@ import { FormFrameShim } from "../components/form-frame-shim";
import { getCommand, DefaultCommands, CommandConditions } from "../api/registry/command";
import { Toaster, Position, Intent, IToaster } from "@blueprintjs/core";
import { tr } from "../api/i18n";
import { serialize } from "../api/builders/mapagent";

/**
* This class emulates a subset of the Fusion API. This represents the top-level object named "Fusion"
Expand All @@ -31,8 +33,64 @@ class FusionApiShim {
constructor(private parent: ViewerApiShim) {
this.Event = new FusionEventApiShim();
}
ajaxRequest(url: string, onSuccess: Function, onFailure: Function, parameters: any) {

getClient(): Client | undefined {
const { agentUri, agentKind } = this.parent.props;
if (agentUri && agentKind) {
return new Client(agentUri, agentKind);
}
return undefined;
}
ajaxRequest(url: string, options: any) { // onSuccess: Function, onFailure: Function, parameters: any) {
let reqUrl = `${Runtime.getFusionRoot()}/${url}`;
const client = this.getClient();
const resolve = options.onSuccess || ((res: any) => logger.debug(`No success handler defined for this operation`));
const fail = options.onFailure || options.onException || ((r: any, res: Error) => logger.error(res));
if (client) {
if (typeof(options.parameters) == 'string') {
reqUrl += "?" + options.parameters;
fetch(reqUrl, {
method: "GET"
}).then(res => {
if (!res.ok) {
const stat = res.statusText;
res.text().then(t => {
fail({
transport: {
responseText: t
}
}, new MgError(stat));
});
} else {
res.text().then(t => resolve({
responseText: t
}));
}
});
} else {
fetch(reqUrl, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
method: "POST",
body: serialize(options.parameters) //form
}).then(res => {
if (!res.ok) {
const stat = res.statusText;
res.text().then(t => {
fail({
transport: {
responseText: t
}
}, new MgError(stat));
});
} else {
res.text().then(t => resolve({
responseText: t
}));
}
});
}
}
}

getMapByName(name: string): FusionWidgetApiShim | undefined {
Expand Down Expand Up @@ -241,7 +299,7 @@ class FusionWidgetApiShim {
});
}
}
setSelection(xml: string, zoomTo: boolean): void {
setSelection(xml: string, zoomTo: boolean): void { //Map
const viewer = Runtime.getViewer();
if (viewer) {
//TODO: Support zoomTo
Expand All @@ -264,7 +322,7 @@ class FusionWidgetApiShim {
}
return layers;
}
isMapLoaded(): boolean {
isMapLoaded(): boolean { //Map
return true;
}
redraw(): void { //Map
Expand Down Expand Up @@ -292,7 +350,7 @@ class FusionWidgetApiShim {
get layerRoot(): FusionWidgetApiShim { //Map
return this;
}
findLayerByAttribute(name: string, value: string) {
findLayerByAttribute(name: string, value: string) { //Map
const map = this.parent.props.map;
if (map && map.Layer) {
const ml = map.Layer.filter(lyr => {
Expand All @@ -308,17 +366,20 @@ class FusionWidgetApiShim {
}
return null;
}
pixToGeoMeasure(tolerance: number) {
pixToGeoMeasure(tolerance: number) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
return tolerance * viewer.getResolution();
}
return 0.000001; //Pull some random number
}
drawMap(): void {
this.parent.Refresh();
}
registerForEvent(eventID: number, callback: Function): void { //Widget
this.parent.registerForEvent(eventID, callback);
}
deregisterForEvent(eventID: number, callback: Function): void {
deregisterForEvent(eventID: number, callback: Function): void { //Widget
this.parent.deregisterForEvent(eventID, callback);
}
static toOL2Circle(circ: olCircle) {
Expand All @@ -329,39 +390,47 @@ class FusionWidgetApiShim {
r: circ.getRadius()
};
}
digitizePoint(options: any, handler: FusionGeomDigitizer) {
digitizePoint(options: any, handler: FusionGeomDigitizer) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
viewer.digitizePoint(pt => {
handler(new OL2Geom(pt));
});
}
}
digitizeLineString(options: any, handler: FusionGeomDigitizer) {
digitizeLine(options: any, handler: FusionGeomDigitizer) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
viewer.digitizeLine(ln => {
handler(new OL2Geom(ln));
});
}
}
digitizeLineString(options: any, handler: FusionGeomDigitizer) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
viewer.digitizeLineString(lstr => {
handler(new OL2Geom(lstr));
});
}
}
digitizeRectangle(options: any, handler: FusionGeomDigitizer) {
digitizeRectangle(options: any, handler: FusionGeomDigitizer) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
viewer.digitizeRectangle(rect => {
handler(new OL2Rect(rect));
});
}
}
digitizePolygon(options: any, handler: FusionGeomDigitizer) {
digitizePolygon(options: any, handler: FusionGeomDigitizer) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
viewer.digitizePolygon(poly => {
handler(new OL2Geom(poly));
});
}
}
digitizeCircle(options: any, handler: FusionGeomDigitizer) {
digitizeCircle(options: any, handler: FusionGeomDigitizer) { //Map
const viewer = Runtime.getViewer();
if (viewer) {
viewer.digitizeCircle(circ => {
Expand Down Expand Up @@ -449,6 +518,8 @@ export interface IViewerApiShimProps {
export interface IViewerApiShimState {
map: RuntimeMap;
selectionSet: QueryMapFeaturesResponse;
agentUri: string;
agentKind: ClientKind;
}

export interface IViewerApiShimDispatch {
Expand All @@ -467,7 +538,9 @@ function mapStateToProps(state: IApplicationState): Partial<IViewerApiShimState>
}
return {
map: map,
selectionSet: selectionSet
selectionSet: selectionSet,
agentUri: state.config.agentUri,
agentKind: state.config.agentKind
};
}

Expand Down

0 comments on commit 24dc14b

Please sign in to comment.