Skip to content
This repository has been archived by the owner on Nov 22, 2019. It is now read-only.

Commit

Permalink
Getting sources related commands to work with undo manager
Browse files Browse the repository at this point in the history
- removed the AddSourceCommand
- removed the RemoveSourceCommand
- for each added or removed source the connection name and path is being kept in the AddSourcesCommand and RemoveSourcesCommand
- CommandFactory methods now return either the request Command or an Error object if the command cannot be created
  • Loading branch information
elvisisking committed Jun 26, 2018
1 parent 64aa36b commit ea0c79e
Show file tree
Hide file tree
Showing 14 changed files with 386 additions and 439 deletions.
42 changes: 12 additions & 30 deletions ngapp/src/app/dataservices/shared/view.model.ts
Expand Up @@ -110,7 +110,9 @@ export class View {
* @param {SchemaNode} sourceToAdd the source being added
*/
public addSource( sourceToAdd: SchemaNode ): void {
const index = this.sources.findIndex( ( source ) => source.getName() === sourceToAdd.getName() );
const index = this.sources.findIndex( ( source ) =>
source.getConnectionName() === sourceToAdd.getConnectionName() && source.getPath() === sourceToAdd.getPath()
);

if ( index === -1 ) {
this.sources.push( sourceToAdd );
Expand All @@ -131,46 +133,26 @@ export class View {
}

/**
* @param {SchemaNode | string} sourceToRemove the source or the ID of the source being removed
* @param {SchemaNode} sourceToRemove the schema node of the source being removed
*/
public removeSource( sourceToRemove: SchemaNode | string ): void {
let sourceName: string;

if ( typeof sourceToRemove === "string" ) {
sourceName = sourceToRemove as string;
} else {
const source = sourceToRemove as SchemaNode;
sourceName = source.getName();
}

const index = this.sources.findIndex( ( source ) => source.getName() === sourceName );
public removeSource( sourceToRemove: SchemaNode ): void {
const index = this.sources.findIndex( ( source ) =>
source.getConnectionName() === sourceToRemove.getConnectionName() && source.getPath() === sourceToRemove.getPath() );

if ( index !== -1 ) {
this.sources.splice( index, 1 );
}
}

/**
* @param {SchemaNode} sourcesToRemove the sources being removed
* @param {SchemaNode} sourcesToRemove the schema nodes of the sources being removed
*/
public removeSources( sourcesToRemove: SchemaNode[] | string[] ): void {
public removeSources( sourcesToRemove: SchemaNode[] ): void {
const self = this;

if ( sourcesToRemove && sourcesToRemove.length !== 0 ) {
if ( typeof sourcesToRemove[ 0 ] === "string" ) {
const ids = sourcesToRemove as string[];

ids.forEach( ( source ) => {
self.removeSource( source );
} );
} else {
const sources = sourcesToRemove as SchemaNode[];

sources.forEach( ( source ) => {
self.removeSource( source );
} );
}
}
sourcesToRemove.forEach( ( source ) => {
self.removeSource( source );
} );
}

/**
Expand Down

This file was deleted.

Expand Up @@ -15,8 +15,11 @@
* limitations under the License.
*/

import { SchemaNode } from "@connections/shared/schema-node.model";
import { ViewEditorI18n } from "@dataservices/virtualization/view-editor/view-editor-i18n";
import { Command } from "@dataservices/virtualization/view-editor/command/command";
import { CommandUtils } from "@dataservices/virtualization/view-editor/command/command-utils";
import { RemoveSourcesCommand } from "@dataservices/virtualization/view-editor/command/remove-sources-command";

export class AddSourcesCommand extends Command {

Expand All @@ -28,33 +31,35 @@ export class AddSourcesCommand extends Command {
public static readonly id = "AddSourcesCommand";

/**
* The name of the command argument whose value is the IDs of the sources that are being added.
* The name of the command argument whose value is the sources that are being added.
*
* @type {string}
*/
public static readonly addedSourcesIds = "addedSourcesIds";
public static readonly addedSources = CommandUtils.sources;

/**
* @param {string} addedSourcesIds the IDs of the sources being added (cannot be `null` or empty)
* @param {string | SchemaNode} addedSources the JSON representation of the sources or the schema nodes of the sources
* being added (cannot be `null` or empty)
*/
public constructor( addedSourcesIds: string[] ) {
public constructor( addedSources: string | SchemaNode[] ) {
super( AddSourcesCommand.id, ViewEditorI18n.addSourcesCommandName );

// concatenate IDs using comma as delimiter
let args = "";
let firstTime = true;
let arg: string;

addedSourcesIds.forEach( ( id ) => {
if ( firstTime ) {
firstTime = false;
} else {
args += Command.idsDelimiter;
}
if ( typeof addedSources === "string" ) {
arg = addedSources as string;
} else {
arg = CommandUtils.toJsonValue( addedSources as SchemaNode[] );
}

args += id;
} );
this._args.set( AddSourcesCommand.addedSources, arg );
}

this._args.set( AddSourcesCommand.addedSourcesIds, args );
/**
* @returns {{}[] | Error} an array of { connectionName: string, path: string } objects or an error if unable to parse
*/
public decodeSourcesArg(): {}[] | Error {
return CommandUtils.parseSourcesArg( this.getArg( RemoveSourcesCommand.removedSources ) as string );
}

}

0 comments on commit ea0c79e

Please sign in to comment.