Skip to content

Commit

Permalink
[Events] Implement basic Action entity (fixes #13).
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Jul 21, 2015
1 parent a75f6d5 commit 1b5bd1b
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 17 deletions.
28 changes: 28 additions & 0 deletions app/ts/core/actions/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface IAction {
name: string;
type: string;

perform(): Promise<boolean>;
}

export class Action implements IAction {
private _name: string;
private _type: string;

constructor(name: string, type: string) {
this._name = name;
this._type = type;
}

get name() {
return this._name;
}

get type() {
return this._type;
}

perform() {
return Promise.reject<boolean>(new Error('Not Implemented!'));
}
}
32 changes: 32 additions & 0 deletions app/ts/core/actions/broadcast-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Action } from 'core/actions/action';
import { IMessage } from 'core/events/message';
import { IMessageChannel } from 'core/events/message-channel';

export class BroadcastAction extends Action {
private _channel: IMessageChannel;
private _message: IMessage;

constructor(channel: IMessageChannel, message: IMessage) {
super('Broadcast', 'broadcast-action');

this._channel = channel;
this._message = message;
}

get channel() {
return this._channel;
}

get message() {
return this._message;
}

perform() {
try {
this._channel.send(this._message);
return Promise.resolve(true);
} catch(e) {
return Promise.reject<boolean>(e);
}
}
}
31 changes: 31 additions & 0 deletions app/ts/core/actions/change-property-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Action } from 'core/actions/action';
import { IProperty } from 'core/property';

export class ChangePropertyAction<TPropertyValue> extends Action {
private _property: IProperty<TPropertyValue>;
private _value: TPropertyValue;

constructor(property: IProperty<TPropertyValue>, value: TPropertyValue) {
super('Change Property', 'change-property-action');

this._property = property;
this._value = value;
}

get property() {
return this._property;
}

get value() {
return this._value;
}

perform() {
try {
this._property.setValue(this._value);
return Promise.resolve(true);
} catch(e) {
return Promise.reject<boolean>(e);
}
}
}
15 changes: 14 additions & 1 deletion app/ts/core/controls/base-control.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IProperty } from 'core/property';
import { IAction } from 'core/actions/action';
import ControlGroup from 'core/controls/control-group';

export default class BaseControl<TProperties> {
Expand All @@ -6,19 +8,22 @@ export default class BaseControl<TProperties> {
private _description: string;
private _group: ControlGroup;
private _properties: TProperties;
private _events: Array<IProperty<Array<IAction>>>;

constructor(
type: string,
name: string,
description: string,
groupType: string,
properties?: TProperties
properties?: TProperties,
events?: Array<IProperty<Array<IAction>>>
) {
this._type = type;
this._name = name;
this._description = description;
this._group = ControlGroup.get(groupType);
this._properties = properties;
this._events = events || [];
}

clone(): BaseControl<TProperties> {
Expand Down Expand Up @@ -64,4 +69,12 @@ export default class BaseControl<TProperties> {
get properties() {
return this._properties;
}

/**
* List of event names supported by control.
* @returns {Array<IProperty<Array<IAction>>>}
*/
get events() {
return this._events;
}
}
4 changes: 3 additions & 1 deletion app/ts/core/controls/visual/base-visual-control.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../../../../../typings/tsd.d.ts" />
import { IProperty } from 'core/property';
import { IAction } from 'core/actions/action';
import BaseControl from 'core/controls/base-control';
import { StyleRepository } from 'core/style-repository';

Expand All @@ -18,9 +19,10 @@ export class BaseVisualControl<TProperties>
name: string,
description: string,
properties?: TProperties,
events?: Array<IProperty<Array<IAction>>>,
styles?: { [key: string]: string; }
) {
super(type, name, description, 'visual', properties);
super(type, name, description, 'visual', properties, events);

Object.keys(styles || {}).forEach((styleKey) => {
let styleProperty = StyleRepository.getProperty(styleKey);
Expand Down
6 changes: 6 additions & 0 deletions app/ts/core/controls/visual/button-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
PropertyWithOptions
} from 'core/property';

import { IAction } from 'core/actions/action';

const BUTTON_TYPES = [
new Property<string>('Submit', 'submit'),
new Property<string>('Reset', 'reset'),
Expand Down Expand Up @@ -44,6 +46,10 @@ class ButtonControl extends BaseVisualControl<ButtonControlProperties> {
'Button',
'HTML Button',
properties || new ButtonControlProperties('[Text]', '[Title]'),
[
new Property<Array<IAction>>('Click', [], 'click'),
new Property<Array<IAction>>('Hover', [], 'hover'),
],
Object.assign({}, DEFAULT_STYLES, styles || {})
);
}
Expand Down
6 changes: 6 additions & 0 deletions app/ts/core/controls/visual/container-control.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference path="../../../../../typings/tsd.d.ts" />
import { Property } from 'core/property';
import { IAction } from 'core/actions/action';
import BaseControl from 'core/controls/base-control';
import { BaseVisualControl } from 'core/controls/visual/base-visual-control';

Expand All @@ -17,6 +19,10 @@ class ContainerControl extends BaseVisualControl<ContainerControlProperties> {
'Container',
'Component container',
properties || new ContainerControlProperties(),
[
new Property<Array<IAction>>('Click', [], 'click'),
new Property<Array<IAction>>('Hover', [], 'hover'),
],
styles
);
}
Expand Down
5 changes: 5 additions & 0 deletions app/ts/core/controls/visual/label-control.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="../../../../../typings/tsd.d.ts" />
import { BaseVisualControl } from 'core/controls/visual/base-visual-control';
import { IProperty, Property } from 'core/property';
import { IAction } from 'core/actions/action';

export class LabelControlProperties {
text: IProperty<string>;
Expand All @@ -24,6 +25,10 @@ export class LabelControl extends BaseVisualControl<LabelControlProperties> {
'Label',
'HTML Label',
properties || new LabelControlProperties('[Text]'),
[
new Property<Array<IAction>>('Click', [], 'click'),
new Property<Array<IAction>>('Hover', [], 'hover'),
],
Object.assign({}, DEFAULT_STYLES, styles || {})
);
}
Expand Down
5 changes: 5 additions & 0 deletions app/ts/core/controls/visual/range-control.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="../../../../../typings/tsd.d.ts" />
import { BaseVisualControl } from 'core/controls/visual/base-visual-control';
import { IProperty, Property } from 'core/property';
import { IAction } from 'core/actions/action';

class RangeControlProperties {
min: IProperty<number>;
Expand Down Expand Up @@ -31,6 +32,10 @@ class RangeControl extends BaseVisualControl<RangeControlProperties> {
'Number Range',
'Number Range',
properties || new RangeControlProperties(),
[
new Property<Array<IAction>>('Click', [], 'click'),
new Property<Array<IAction>>('Hover', [], 'hover'),
],
styles
);
}
Expand Down
16 changes: 1 addition & 15 deletions app/ts/core/events/message.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import { IMessageChannel } from 'core/events/message-channel';

export interface IMessage {
/**
* Message channel message belongs to.
* @type {IMessageChannel}
*/
channel: IMessageChannel;

/**
* Name of the message.
* @type {string}
Expand All @@ -21,20 +13,14 @@ export interface IMessage {
}

export class Message implements IMessage {
private _channel: IMessageChannel;
private _name: string;
private _data: any;

constructor(channel: IMessageChannel, name: string, data: any = null) {
this._channel = channel;
constructor(name: string, data: any = null) {
this._name = name;
this._data = data;
}

get channel() {
return this._channel;
}

get name() {
return this._name;
}
Expand Down

0 comments on commit 1b5bd1b

Please sign in to comment.