@singularsystems/neo-core 1.0.0-alpha.4 → 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # neo-core
2
2
 
3
- This project contains the core functionality of the neo client library. The code in this library is referenced by the neo-vue and neo-react projects.
3
+ This project contains the core functionality of the neo client library.
4
4
 
5
5
  ## Testing
6
6
 
@@ -10,7 +10,6 @@ export default class DatePicker {
10
10
  private headerAnimationHelper;
11
11
  private inputHasChanged;
12
12
  private canAutoSetDate;
13
- private suspendLayout;
14
13
  private input;
15
14
  private popup;
16
15
  private headerContainer;
@@ -65,6 +64,7 @@ export default class DatePicker {
65
64
  private previousClick;
66
65
  private nextClick;
67
66
  private moveCurrent;
67
+ private autoSetDate;
68
68
  private createAndAppend;
69
69
  destroy(): void;
70
70
  }
@@ -2,7 +2,6 @@ import DateUtils from '../../Dates/DateUtils';
2
2
  import AnimationHelper from '../AnimationHelper';
3
3
  import { YearsInYearView, ProcessedDatePickerOptions } from './DatePickerOptions';
4
4
  import PositionHelper from '../PositionHelper';
5
- import { Utils } from '../../Utils';
6
5
  // TODO: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
7
6
  // TODO: Allow input to be shown with inline picker, and specify position.
8
7
  var ViewType;
@@ -17,7 +16,6 @@ var DatePicker = /** @class */ (function () {
17
16
  this.isReadOnly = false;
18
17
  this.inputHasChanged = false;
19
18
  this.canAutoSetDate = false;
20
- this.suspendLayout = false;
21
19
  this.currentBindValue = null;
22
20
  this.boundDateNoTime = null;
23
21
  this.options = ProcessedDatePickerOptions.fromPartial(options || {});
@@ -139,15 +137,11 @@ var DatePicker = /** @class */ (function () {
139
137
  }
140
138
  };
141
139
  DatePicker.prototype.setBindProperty = function (dateValue, suspend) {
142
- var _this = this;
143
140
  if (dateValue) {
144
141
  dateValue = DateUtils.max(DateUtils.min(dateValue, this.options.maxDate), this.options.minDate);
145
142
  }
146
- if (suspend)
147
- this.suspendLayout = true;
143
+ this.currentBindValue = dateValue;
148
144
  this.bindProperty.value = dateValue;
149
- if (suspend)
150
- Utils.nextTick(function () { return _this.suspendLayout = false; });
151
145
  if (this.options.onDateSelected !== undefined) {
152
146
  this.options.onDateSelected(!suspend);
153
147
  }
@@ -191,14 +185,10 @@ var DatePicker = /** @class */ (function () {
191
185
  this.currentBindValue = bindProperty === null || bindProperty === void 0 ? void 0 : bindProperty.value;
192
186
  this.resetCurrentViewDate();
193
187
  if (this.options.alwaysShow) {
194
- if (!this.suspendLayout) {
195
- this.createComponents();
196
- }
197
- }
198
- else {
199
- this.setInputValue();
188
+ this.createComponents();
200
189
  }
201
190
  }
191
+ this.setInputValue();
202
192
  };
203
193
  DatePicker.prototype.resetCurrentViewDate = function () {
204
194
  this.setCurrentViewDate(this.bindProperty.value);
@@ -313,11 +303,13 @@ var DatePicker = /** @class */ (function () {
313
303
  var lastDayOfMonth = DateUtils.monthEnd(currentDate);
314
304
  this.prevButton.disabled = this.options.minDateNoTime.getTime() >= currentDate.getTime();
315
305
  if (this.canAutoSetDate && this.options.autoChangeDate == "start") {
316
- this.setBindProperty(currentDate, true);
306
+ //this.setCurrentViewDate(currentDate)
307
+ //this.setBindProperty(currentDate, true);
317
308
  }
318
309
  this.nextButton.disabled = this.options.maxDateNoTime.getTime() <= lastDayOfMonth.getTime();
319
310
  if (this.canAutoSetDate && this.options.autoChangeDate == "end") {
320
- this.setBindProperty(lastDayOfMonth, true);
311
+ //this.setCurrentViewDate(lastDayOfMonth)
312
+ //this.setBindProperty(lastDayOfMonth, true);
321
313
  }
322
314
  // Start with the first Sunday before the first of the month.
323
315
  currentDate.setDate(currentDate.getDate() - currentDate.getDay());
@@ -471,6 +463,7 @@ var DatePicker = /** @class */ (function () {
471
463
  if (this.currentViewType == ViewType.Days) {
472
464
  this.canAutoSetDate = true;
473
465
  this.currentViewDate.setMonth(this.currentViewDate.getMonth() + delta, 1);
466
+ this.autoSetDate(delta);
474
467
  }
475
468
  else {
476
469
  this.currentViewDate.setFullYear(this.currentViewDate.getFullYear() + delta * (this.currentViewType == ViewType.Months ? 1 : YearsInYearView));
@@ -478,6 +471,21 @@ var DatePicker = /** @class */ (function () {
478
471
  this.populateContentContainer(false, true);
479
472
  this.contentAnimationHelper.slide(delta, true);
480
473
  };
474
+ DatePicker.prototype.autoSetDate = function (delta) {
475
+ var autoDate = null;
476
+ if (this.options.autoChangeDate === "end") {
477
+ // Forward
478
+ autoDate = DateUtils.monthEnd(this.currentViewDate);
479
+ }
480
+ else if (this.options.autoChangeDate === "start") {
481
+ // Back
482
+ autoDate = this.currentViewDate;
483
+ }
484
+ if (autoDate) {
485
+ this.setBindProperty(autoDate, true);
486
+ this.setCurrentViewDate(this.bindProperty.value);
487
+ }
488
+ };
481
489
  DatePicker.prototype.createAndAppend = function (parent, elementType, className) {
482
490
  if (className === void 0) { className = ""; }
483
491
  var newElement = document.createElement(elementType);
@@ -33,7 +33,11 @@ export declare enum ErrorDisplayType {
33
33
  }
34
34
  export declare class ErrorHandler implements IErrorHandler {
35
35
  private notifications;
36
- constructor(notifications: INotificationHelper);
36
+ private blobType;
37
+ constructor(notifications: INotificationHelper, blobType?: {
38
+ new (blobParts?: BlobPart[] | undefined, options?: BlobPropertyBag | undefined): Blob;
39
+ prototype: Blob;
40
+ });
37
41
  showError(error: any, displayType: ErrorDisplayType): Promise<void>;
38
42
  parseError(error: any): Promise<IErrorDisplay>;
39
43
  protected getAxiosError(error: AxiosError, response?: AxiosResponse): Promise<IErrorDisplay>;
@@ -2,7 +2,6 @@ import { __awaiter, __decorate, __generator, __metadata, __param } from "tslib";
2
2
  import { injectable, inject } from 'inversify';
3
3
  import { ModalUtils } from './Modal/ModalUtils';
4
4
  import NeoTypes from '../AppServices/NeoTypes';
5
- import { Blob } from 'buffer';
6
5
  export var ErrorDisplayType;
7
6
  (function (ErrorDisplayType) {
8
7
  /** Show the error text in a modal. */
@@ -13,8 +12,10 @@ export var ErrorDisplayType;
13
12
  ErrorDisplayType[ErrorDisplayType["ThrowError"] = 2] = "ThrowError";
14
13
  })(ErrorDisplayType || (ErrorDisplayType = {}));
15
14
  export var ErrorHandler = /** @class */ (function () {
16
- function ErrorHandler(notifications) {
15
+ function ErrorHandler(notifications, blobType) {
16
+ if (blobType === void 0) { blobType = Blob; }
17
17
  this.notifications = notifications;
18
+ this.blobType = blobType;
18
19
  }
19
20
  ErrorHandler.prototype.showError = function (error, displayType) {
20
21
  return __awaiter(this, void 0, void 0, function () {
@@ -163,7 +164,7 @@ export var ErrorHandler = /** @class */ (function () {
163
164
  return __generator(this, function (_a) {
164
165
  switch (_a.label) {
165
166
  case 0:
166
- if (!(responseData instanceof Blob)) return [3 /*break*/, 2];
167
+ if (!(responseData instanceof this.blobType)) return [3 /*break*/, 2];
167
168
  return [4 /*yield*/, responseData.text()];
168
169
  case 1:
169
170
  errorText = _a.sent();
@@ -204,7 +205,7 @@ export var ErrorHandler = /** @class */ (function () {
204
205
  ErrorHandler = __decorate([
205
206
  injectable(),
206
207
  __param(0, inject(NeoTypes.UI.GlobalNotifications.symbol)),
207
- __metadata("design:paramtypes", [Object])
208
+ __metadata("design:paramtypes", [Object, Object])
208
209
  ], ErrorHandler);
209
210
  return ErrorHandler;
210
211
  }());
@@ -29,7 +29,7 @@ export declare abstract class BindableBase {
29
29
  * Must be called in the constructor, preferably directly after the super() call.
30
30
  * This method can be used as an alternative to the @NeoModel decorator.
31
31
  */
32
- protected makeObservable(): void;
32
+ protected makeObservable(options?: IMakeBindableOptions): void;
33
33
  /**
34
34
  * Makes the model bindable (and observable if it inherits from ModelBase) by creating its 'meta' object.
35
35
  * Must be called in the constructor, preferably directly after the super() call.
@@ -41,8 +41,11 @@ export var BindableBase = /** @class */ (function () {
41
41
  * Must be called in the constructor, preferably directly after the super() call.
42
42
  * This method can be used as an alternative to the @NeoModel decorator.
43
43
  */
44
- BindableBase.prototype.makeObservable = function () {
45
- this.makeBindable({ makeObservable: true });
44
+ BindableBase.prototype.makeObservable = function (options) {
45
+ var _a;
46
+ options !== null && options !== void 0 ? options : (options = {});
47
+ (_a = options.makeObservable) !== null && _a !== void 0 ? _a : (options.makeObservable = true);
48
+ this.makeBindable(options);
46
49
  };
47
50
  /**
48
51
  * Makes the model bindable (and observable if it inherits from ModelBase) by creating its 'meta' object.
@@ -43,5 +43,8 @@ export declare function OnChanged<T>(callback: (target: T) => (oldValue: any, ne
43
43
  export declare function Debounce(value?: boolean): (type: any, propertyKey: string) => void;
44
44
  /** Converts a getter into a computed. Note, when using NeoModel you do not need to do this. */
45
45
  export declare function Computed(isComputed?: boolean): (type: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
46
+ /** Specifies whether a method is a mobx action. By default all methods are actions.
47
+ * Use this decorator to suppress this behaviour (by passing in false), or change the default behaviour in the makeObservable options passed to makeObservable. */
48
+ export declare function Action(isAction?: boolean): (type: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
46
49
  /** Sets this property as the display member to be used in drop downs / radio lists etc. */
47
50
  export declare function DisplayMember(): (type: any, propertyKey: string) => void;
@@ -173,6 +173,13 @@ export function Computed(isComputed) {
173
173
  ensureTypeInfo(type.constructor).neo.addComputed(propertyKey, descriptor, isComputed);
174
174
  };
175
175
  }
176
+ /** Specifies whether a method is a mobx action. By default all methods are actions.
177
+ * Use this decorator to suppress this behaviour (by passing in false), or change the default behaviour in the makeObservable options passed to makeObservable. */
178
+ export function Action(isAction) {
179
+ return function (type, propertyKey, descriptor) {
180
+ ensureTypeInfo(type.constructor).neo.addMethod(propertyKey).isAction = isAction;
181
+ };
182
+ }
176
183
  /** Sets this property as the display member to be used in drop downs / radio lists etc. */
177
184
  export function DisplayMember() {
178
185
  return function (type, propertyKey) {
@@ -26,15 +26,20 @@ type RemoveChildren<T> = Pick<T, {
26
26
  export type PartialOrNull<T> = {
27
27
  [P in keyof T]?: T[P] | null;
28
28
  };
29
+ /** Removes ModelBase properties (isNew, isDirty etc), removes methods, and changes list types to arrays. */
29
30
  export type PlainObject<T> = TransformLists<NoModelBase<T>>;
31
+ /** PlainObject plus tracking state and entity identifier. */
30
32
  export type PlainTrackedObject<T> = PlainObject<T> & {
31
33
  trackingState?: Misc.TrackingState;
32
34
  entityIdentifier?: string;
33
35
  };
36
+ /** Plain object with no tracking state, and no lists. */
34
37
  export type PlainFlatObject<T> = Pick<T, Exclude<keyof T, keyof IModelBase> & keyof RemoveMethods<T> & keyof RemoveChildren<T>>;
38
+ /** Plain object where the meta property is not allowed. Used to accept any Plain* object. */
35
39
  export type PartialPlainObject<T> = Partial<PlainObject<T>> & {
36
40
  meta?: never;
37
41
  };
42
+ /** Plain object where the meta and tracking state properties are not allowed. Used to accept PlainObject and PlainFlatObject. */
38
43
  export type PartialPlainNonTrackedObject<T> = PartialPlainObject<T> & {
39
44
  trackingState?: never;
40
45
  };
@@ -49,6 +54,10 @@ export interface IMakeBindableOptions {
49
54
  * * If `undefined`, only types that inherit from `ModelBase` will be observable by default.
50
55
  */
51
56
  makeObservable?: boolean;
57
+ /**
58
+ * Specifies whether methods should be converted to mobx actions. This is `true` by default.
59
+ * Use `Attributes.Action()` to override this per methis. */
60
+ autoActions?: boolean;
52
61
  /**
53
62
  * If `true`, this instance will not be converted. In inheritence heirarchies, use this to ensure only the last derived type converts the instance.
54
63
  */
@@ -0,0 +1,8 @@
1
+ export default class MethodInfo {
2
+ name: string;
3
+ constructor(name: string);
4
+ /**
5
+ * Specifies whether the method should be converted to a mobx action.
6
+ */
7
+ isAction?: boolean;
8
+ }
@@ -0,0 +1,7 @@
1
+ var MethodInfo = /** @class */ (function () {
2
+ function MethodInfo(name) {
3
+ this.name = name;
4
+ }
5
+ return MethodInfo;
6
+ }());
7
+ export default MethodInfo;
@@ -1,7 +1,8 @@
1
1
  import { __decorate, __extends, __metadata } from "tslib";
2
2
  import * as ModelTraverser from './ModelGraphTraverser';
3
- import { Computed, NoTracking, Observable } from './Decorators';
3
+ import { Action, Computed, NoTracking, Observable } from './Decorators';
4
4
  import { JSFormatter } from '../Data/Serialiser';
5
+ import { ValidationRules } from '../Validation/ValidationRules';
5
6
  import { BindableBase } from './BindableBase';
6
7
  import { Misc } from '../';
7
8
  import { SerialiseType } from './Misc';
@@ -177,6 +178,12 @@ var ModelBase = /** @class */ (function (_super) {
177
178
  __metadata("design:type", Object),
178
179
  __metadata("design:paramtypes", [])
179
180
  ], ModelBase.prototype, "readyToSave", null);
181
+ __decorate([
182
+ Action(false),
183
+ __metadata("design:type", Function),
184
+ __metadata("design:paramtypes", [ValidationRules]),
185
+ __metadata("design:returntype", void 0)
186
+ ], ModelBase.prototype, "addBusinessRules", null);
180
187
  return ModelBase;
181
188
  }(BindableBase));
182
189
  export default ModelBase;
@@ -19,6 +19,5 @@ export declare abstract class ModelCreatorBase implements IModelCreatorBase {
19
19
  */
20
20
  protected ensureChildInfo(instance: {}, propertyInfo: PropertyInfo): void;
21
21
  protected addMethodInfo(methodName: string, method: Function): void;
22
- protected addProperty(propertyName: string, descriptor: PropertyDescriptor): void;
23
22
  abstract convertInstance(instance: {}, options: IMakeBindableOptions): {};
24
23
  }
@@ -5,6 +5,7 @@ import { RelationType } from './Misc';
5
5
  import { ChildList, ChildObject } from './Decorators';
6
6
  import ValueObject from './ValueObject';
7
7
  import { BindableBase } from './BindableBase';
8
+ var nonActionMethods = ["addBusinessRules", "toString"];
8
9
  export var ModelCreatorBase = /** @class */ (function () {
9
10
  function ModelCreatorBase() {
10
11
  this.alwaysInitialiseMembers = false;
@@ -49,10 +50,6 @@ export var ModelCreatorBase = /** @class */ (function () {
49
50
  if (this.typeInfo.inheritsBindableBase) {
50
51
  this.typeInfo.addComputed(propertyName, descriptor);
51
52
  }
52
- else {
53
- // for general objects, getters must explicitly define @Computed to become computed properties.
54
- this.addProperty(propertyName, descriptor);
55
- }
56
53
  }
57
54
  }
58
55
  }
@@ -82,8 +79,10 @@ export var ModelCreatorBase = /** @class */ (function () {
82
79
  }
83
80
  };
84
81
  ModelCreatorBase.prototype.addMethodInfo = function (methodName, method) {
85
- };
86
- ModelCreatorBase.prototype.addProperty = function (propertyName, descriptor) {
82
+ var methodInfo = this.typeInfo.addMethod(methodName);
83
+ if (nonActionMethods.indexOf(methodInfo.name) >= 0) {
84
+ methodInfo.isAction = false;
85
+ }
87
86
  };
88
87
  ModelCreatorBase = __decorate([
89
88
  injectable()
@@ -2,6 +2,7 @@ import { Rule } from '../Validation/Rules';
2
2
  import { PropertyInstanceMap } from './PropertyInstance';
3
3
  import PropertyInfo, { PropertyInfoMap } from './PropertyInfo';
4
4
  import { NeoModelConstructor, IModelBase, IMetaItem, IMakeBindableOptions } from './IModelBase';
5
+ import MethodInfo from './MethodInfo';
5
6
  export declare class NeoTypeInfo {
6
7
  private modelCreator;
7
8
  private objectConstructor;
@@ -11,6 +12,7 @@ export declare class NeoTypeInfo {
11
12
  private _metaItem;
12
13
  properties: PropertyInfo[];
13
14
  propertyMap: PropertyInfoMap;
15
+ methods: Map<string, MethodInfo>;
14
16
  children: PropertyInfo[];
15
17
  parentProperties: PropertyInfo[];
16
18
  parentProperty?: PropertyInfo;
@@ -24,6 +26,7 @@ export declare class NeoTypeInfo {
24
26
  private addPropertyInfo;
25
27
  getOrAddProperty(propertyName: string): PropertyInfo;
26
28
  addComputed(propertyName: string, descriptor: PropertyDescriptor, isComputed?: boolean): void;
29
+ addMethod(methodName: string): MethodInfo;
27
30
  addAttributeRule(propertyName: string, rule: Rule): void;
28
31
  /**
29
32
  * Ensures the type has all the information it needs in order to convert instances of this type.
@@ -7,7 +7,7 @@ import { ValidationRules } from '../Validation/ValidationRules';
7
7
  import ModelProxy from '../Validation/ModelProxy';
8
8
  import NeoTypes from '../AppServices/NeoTypes';
9
9
  import { RelationType } from './Misc';
10
- var ignoredPropertyNames = ["$mobx"];
10
+ import MethodInfo from './MethodInfo';
11
11
  var inversifyMetadataKey = "inversify:tagged";
12
12
  var hasConvertedSymbol = Symbol("hasConvertedModel");
13
13
  var NeoTypeInfo = /** @class */ (function () {
@@ -18,6 +18,7 @@ var NeoTypeInfo = /** @class */ (function () {
18
18
  this.properties = [];
19
19
  // Property info dictionary for easy lookup.
20
20
  this.propertyMap = {};
21
+ this.methods = new Map();
21
22
  // Properties of type List<T>, or marked with the ChildList or ChildObject decorator.
22
23
  this.children = [];
23
24
  // Properties marked with the parent decorator, multiple if a class can be child of more than one type.
@@ -80,6 +81,9 @@ var NeoTypeInfo = /** @class */ (function () {
80
81
  pi.isReadOnly = descriptor.set === undefined;
81
82
  pi.descriptor = descriptor;
82
83
  };
84
+ NeoTypeInfo.prototype.addMethod = function (methodName) {
85
+ return this.methods.getOrAdd(methodName, function () { return new MethodInfo(methodName); });
86
+ };
83
87
  NeoTypeInfo.prototype.addAttributeRule = function (propertyName, rule) {
84
88
  this.getOrAddProperty(propertyName).addAttributeRule(rule);
85
89
  };
@@ -99,13 +103,22 @@ var NeoTypeInfo = /** @class */ (function () {
99
103
  }
100
104
  this.modelCreator.initialise(this, this.objectConstructor);
101
105
  if (instance && this.modelCreator.shouldInitialiseAllMembers) {
102
- for (var _i = 0, _a = Object.getOwnPropertyNames(instance); _i < _a.length; _i++) {
106
+ var allProperties = __spreadArray([], this.properties, true);
107
+ var ownProperties = [];
108
+ for (var _i = 0, _a = Object.keys(instance); _i < _a.length; _i++) {
103
109
  var propertyName = _a[_i];
104
- if (ignoredPropertyNames.indexOf(propertyName) < 0) {
110
+ if (!(typeof instance[propertyName] === "function")) {
105
111
  var pi = this.getOrAddProperty(propertyName);
106
112
  pi.setupFromInstance(instance);
113
+ // Properties with decorators will be added before own properties.
114
+ // Mobx as of version 6 deletes properties, and re-adds them,
115
+ // causing future Object.keys evaulations to return the keys in the order they are passed to mobX.
116
+ // This will preserve the order of the keys as they were defined in the object.
117
+ allProperties.remove(pi);
118
+ ownProperties.push(pi);
107
119
  }
108
120
  }
121
+ this.properties = __spreadArray(__spreadArray([], ownProperties, true), allProperties, true);
109
122
  }
110
123
  if (instance)
111
124
  this.addRules(instance);
@@ -247,7 +260,13 @@ var NeoTypeInfo = /** @class */ (function () {
247
260
  else {
248
261
  instance[hasConvertedSymbol] = true;
249
262
  }
250
- return this.modelCreator.convertInstance(instance, options);
263
+ try {
264
+ return this.modelCreator.convertInstance(instance, options);
265
+ }
266
+ catch (e) {
267
+ console.error("Error converting model type " + instance.constructor.name);
268
+ throw e;
269
+ }
251
270
  }
252
271
  return instance;
253
272
  };
@@ -7,7 +7,6 @@ var PropertyInfo = /** @class */ (function () {
7
7
  this.attributeRules = [];
8
8
  /** Name to use on labels for this property */
9
9
  this.displayName = "";
10
- // TODO: noTrack and Observable should be combined and use an enum?
11
10
  /** True if the property does not cause the object to become dirty. */
12
11
  this.noTrack = false;
13
12
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@singularsystems/neo-core",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.0.0-beta.0",
4
4
  "description": "Core functionality for the Neo client library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",