Skip to content

Commit

Permalink
Merge pull request #4139 from TerriaJS/mobx-dragdrop-file-fix
Browse files Browse the repository at this point in the history
Mobx dragdrop file fix
  • Loading branch information
KeyboardSounds authored Apr 20, 2020
2 parents c4406c2 + 2d9e8d6 commit 2517704
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 183 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Change Log
==========

### MobX Development

* Fixed DragDropFile and `createCatalogItemFromFileOrUrl` which wasn't enabled/working in mobx, added tests for `createCatalogItemFromFileOrUrl` and renamed `createCatalogItemFromUrl` to `createUrlRefernceFromUrl`.
* Fixed bug in StratumOrder where `sortBottomToTop` would sort strata in the wrong order.
* Allow member re-ordering via GroupMixin's `moveMemberToIndex`
* Fixed a bug where `updateModelFromJson` would ignore its `replaceStratum` parameter.
Expand Down
2 changes: 1 addition & 1 deletion lib/Core/loadBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Resource from "terriajs-cesium/Source/Core/Resource";

export default function loadBlob(
urlOrResource: string,
headers: any
headers?: any
): Promise<Blob> {
return makeRealPromise(
Resource.fetchBlob({ url: urlOrResource, headers: headers })
Expand Down
13 changes: 9 additions & 4 deletions lib/Models/CsvCatalogItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import StratumOrder from "./StratumOrder";
import Terria from "./Terria";
import AutoRefreshingMixin from "../ModelMixins/AutoRefreshingMixin";
import isDefined from "../Core/isDefined";
import { BaseModel } from "./Model";

// Types of CSVs:
// - Points - Latitude and longitude columns or address
Expand Down Expand Up @@ -44,8 +45,12 @@ export default class CsvCatalogItem extends TableMixin(

private _csvFile?: File;

constructor(id: string | undefined, terria: Terria) {
super(id, terria);
constructor(
id: string | undefined,
terria: Terria,
sourceReference?: BaseModel
) {
super(id, terria, sourceReference);
this.strata.set(
automaticTableStylesStratumName,
new TableAutomaticStylesStratum(this)
Expand Down Expand Up @@ -126,10 +131,10 @@ export default class CsvCatalogItem extends TableMixin(
protected forceLoadTableData(): Promise<string[][]> {
if (this.csvString !== undefined) {
return Csv.parseString(this.csvString, true);
} else if (this.url !== undefined) {
return Csv.parseUrl(proxyCatalogItemUrl(this, this.url, "1d"), true);
} else if (this._csvFile !== undefined) {
return Csv.parseFile(this._csvFile, true);
} else if (this.url !== undefined) {
return Csv.parseUrl(proxyCatalogItemUrl(this, this.url, "1d"), true);
} else {
return Promise.reject(
new TerriaError({
Expand Down
8 changes: 6 additions & 2 deletions lib/Models/GtfsCatalogItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,12 @@ export default class GtfsCatalogItem extends AsyncMappableMixin(
return new ModelGraphics(options);
}

constructor(id: string | undefined, terria: Terria) {
super(id, terria);
constructor(
id: string | undefined,
terria: Terria,
sourceReference?: BaseModel
) {
super(id, terria, sourceReference);
// We should only poll when our map items have consumers
onBecomeObserved(this, "mapItems", () => {
this.disposer = reaction(
Expand Down
12 changes: 6 additions & 6 deletions lib/Models/UrlReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ModelTraits from "../Traits/ModelTraits";
import UrlReferenceTraits from "../Traits/UrlReferenceTraits";
import StratumOrder from "./StratumOrder";
import CatalogMemberMixin from "../ModelMixins/CatalogMemberMixin";
import { mapping } from "./createCatalogItemFromUrl";
import { mapping } from "./createUrlReferenceFromUrl";
import updateModelFromJson from "./updateModelFromJson";

const urlRecordStratum = "url-record";
Expand Down Expand Up @@ -40,7 +40,7 @@ export default class UrlReference extends UrlMixin(
return Promise.resolve(undefined);
}

const target = UrlReference.createCatalogItemFromUrlReference(
const target = UrlReference.createUrlReferenceFromUrlReference(
this,
this.uniqueId,
this.url,
Expand All @@ -51,7 +51,7 @@ export default class UrlReference extends UrlMixin(
return Promise.resolve(target);
}

private static createCatalogItemFromUrlReference(
private static createUrlReferenceFromUrlReference(
sourceReference: BaseModel,
id: string,
url: string,
Expand All @@ -68,7 +68,7 @@ export default class UrlReference extends UrlMixin(
(mapping[index].matcher && !mapping[index].matcher(url)) ||
(mapping[index].requiresLoad && !allowLoad)
) {
return UrlReference.createCatalogItemFromUrlReference(
return UrlReference.createUrlReferenceFromUrlReference(
sourceReference,
id,
url,
Expand All @@ -85,7 +85,7 @@ export default class UrlReference extends UrlMixin(
);

if (item === undefined) {
return UrlReference.createCatalogItemFromUrlReference(
return UrlReference.createUrlReferenceFromUrlReference(
sourceReference,
id,
url,
Expand All @@ -105,7 +105,7 @@ export default class UrlReference extends UrlMixin(
.loadMetadata()
.then(() => item)
.catch(e => {
return UrlReference.createCatalogItemFromUrlReference(
return UrlReference.createUrlReferenceFromUrlReference(
sourceReference,
id,
url,
Expand Down
2 changes: 1 addition & 1 deletion lib/Models/addUserFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import TerriaError from "../Core/TerriaError";
import isDefined from "../Core/isDefined";

interface FileType {
value: String;
value: string;
}

export default function addUserFiles(
Expand Down
42 changes: 21 additions & 21 deletions lib/Models/createCatalogItemFromFileOrUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import i18next from "i18next";
import { runInAction } from "mobx";
import isDefined from "../Core/isDefined";
import TerriaError from "../Core/TerriaError";
import CatalogMemberMixin from "../ModelMixins/CatalogMemberMixin";
import ViewState from "../ReactViewModels/ViewState";
import CatalogMemberFactory from "./CatalogMemberFactory";
import CommonStrata from "./CommonStrata";
import createCatalogItemFromUrl from "./createCatalogItemFromUrl";
import createUrlReferenceFromUrl from "./createUrlReferenceFromUrl";
import { BaseModel } from "./Model";
import Terria from "./Terria";
import upsertModelFromJson from "./upsertModelFromJson";
import ReferenceMixin from "../ModelMixins/ReferenceMixin";

export default function createCatalogItemFromFileOrUrl(
terria: Terria,
viewState: ViewState,
fileOrUrl: File | string,
dataType: any,
confirmConversion: boolean
dataType?: string,
confirmConversion: boolean = false
): Promise<BaseModel | undefined> {
dataType = isDefined(dataType) ? dataType : "auto";

Expand All @@ -30,8 +30,7 @@ export default function createCatalogItemFromFileOrUrl(
}

if (dataType === "auto") {
return createCatalogItemFromUrl(name, terria, isUrl).then(newItem => {
//##Doesn't work for file uploads
return createUrlReferenceFromUrl(name, terria, isUrl).then(newItem => {
if (!isDefined(newItem)) {
return tryConversionService(name, terria, viewState, confirmConversion);
} else {
Expand Down Expand Up @@ -160,29 +159,30 @@ function getConfirmation(
});
}

function loadItem(newCatalogItem: BaseModel, fileOrUrl: File | string) {
async function loadItem(
newCatalogItem: BaseModel,
fileOrUrl: File | string
): Promise<BaseModel> {
if (
ReferenceMixin.is(newCatalogItem) &&
newCatalogItem.target !== undefined
) {
return loadItem(newCatalogItem.target, fileOrUrl);
}

if (typeof fileOrUrl === "string") {
newCatalogItem.setTrait(CommonStrata.user, "url", fileOrUrl);
} else {
if (hasFileInput(newCatalogItem)) {
newCatalogItem.setFileInput(fileOrUrl);
}
// TODO
// newCatalogItem.dataSourceUrl = fileOrUrl.name;
// newCatalogItem.dataUrlType = "local";
} else if (hasFileInput(newCatalogItem)) {
newCatalogItem.setFileInput(fileOrUrl);
}

if (CatalogMemberMixin.isMixedInto(newCatalogItem)) {
return newCatalogItem.loadMetadata().then(() => newCatalogItem);
} else {
return Promise.resolve(newCatalogItem);
}
return newCatalogItem;
}

interface HasFileInput extends BaseModel {
export interface HasFileInput extends BaseModel {
setFileInput(file: File): void;
}

function hasFileInput(model: BaseModel): model is HasFileInput {
export function hasFileInput(model: BaseModel): model is HasFileInput {
return "setFileInput" in model;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,13 @@ import Terria from "./Terria";
import CommonStrata from "./CommonStrata";
import upsertModelFromJson from "./upsertModelFromJson";
import CatalogMemberFactory from "./CatalogMemberFactory";
import { BaseModel } from "./Model";
import UrlReference from "./UrlReference";

export default function createCatalogItemFromUrl(
export default function createUrlReferenceFromUrl(
url: string,
terria: Terria,
allowLoad: boolean,
_index?: number
): Promise<BaseModel | undefined> {
return createCatalogItemFromUrlWithOptions(
url,
terria,
allowLoad,
{},
_index
);
}

export function createCatalogItemFromUrlWithOptions(
url: string,
terria: Terria,
allowLoad: boolean,
options: {
id?: string;
},
_index?: number
): Promise<BaseModel | undefined> {
allowLoad: boolean
): Promise<UrlReference | undefined> {
const item = upsertModelFromJson(
CatalogMemberFactory,
terria,
Expand All @@ -39,7 +19,7 @@ export function createCatalogItemFromUrlWithOptions(
type: UrlReference.type,
name: url,
url: url,
localId: options.id || url,
localId: url,
allowLoad: allowLoad
}
);
Expand All @@ -66,7 +46,7 @@ interface MappingEntry {

export const mapping: MappingEntry[] = [];

createCatalogItemFromUrl.register = function(
createUrlReferenceFromUrl.register = function(
matcher: Matcher,
type: string,
requiresLoad?: boolean
Expand Down
Loading

0 comments on commit 2517704

Please sign in to comment.