UNPKG

15.1 kBTypeScriptView Raw
1/**
2 * Base functionality
3*/
4/**
5 * ============================================================================
6 * IMPORTS
7 * ============================================================================
8 * @hidden
9 */
10import { IClone } from "./utils/Clone";
11import { List, ListTemplate } from "./utils/List";
12import { OrderedListTemplate, SortedListTemplate } from "./utils/SortedList";
13import { Dictionary, DictionaryTemplate } from "./utils/Dictionary";
14import { IDisposer } from "./utils/Disposer";
15import { EventDispatcher, AMEvent } from "./utils/EventDispatcher";
16import { Adapter } from "./utils/Adapter";
17import { ITheme } from "../themes/ITheme";
18import { Ordering } from "./utils/Order";
19import * as $type from "./utils/Type";
20/**
21 * Provides base functionality for all derivative objects, like generating ids,
22 * handling cache, etc.
23 */
24export declare class BaseObject implements IClone<BaseObject>, IDisposer {
25 /**
26 * A unique ID for this object.
27 *
28 * Generated on first access by `uid()` getter.
29 */
30 protected _uid: $type.Optional<string>;
31 /**
32 * Indicates if this object has already been deleted. Any
33 * destruction/disposal code should take this into account when deciding
34 * wheter to run potentially costly disposal operations if they already have
35 * been run.
36 */
37 protected _disposed: boolean;
38 /**
39 * List of IDisposer which will be disposed when the BaseObject is disposed.
40 */
41 protected _disposers: Array<IDisposer>;
42 /**
43 * User-defined id of the object.
44 */
45 protected _id: $type.Optional<string>;
46 /**
47 * Holds a universal mapping collection, so that elements and their children
48 * can create and look up all kinds of relations between id and object.
49 */
50 protected _map: $type.Optional<Dictionary<string, any>>;
51 /**
52 * Holds mapping for objects referenced by id in JSON config that are not yet
53 * available at processing time.
54 */
55 protected _delayedMap: $type.Optional<Dictionary<string, any>>;
56 /**
57 * The theme used by this object.
58 */
59 protected _themes: $type.Optional<ITheme[]>;
60 /**
61 * A list of objects that are clones of this object. An object needs to
62 * maintain a list of its clones so that properties can be re-applied to
63 * clones whenever property on the object they were cloned from changes.
64 */
65 protected _clones: $type.Optional<List<this>>;
66 /**
67 * Reference to the original object this object was cloned from. We need to
68 * keep this so we can disassociate it from source object when this object
69 * is disposed.
70 */
71 clonedFrom: $type.Optional<this>;
72 /**
73 * A class name for the object.
74 *
75 * This property is used by deriving classes to identify which class it is.
76 * We could derive the class name from the object itself, however method of
77 * doing so is too costly, so we are relying on this property to quickly
78 * access type of class.
79 *
80 * @ignore Exclude from docs
81 */
82 protected _className: $type.Optional<string>;
83 /**
84 * [cloneId description]
85 *
86 * @todo Needs description
87 * @ignore Exclude from docs
88 */
89 cloneId: $type.Optional<string>;
90 /**
91 * Holds processing error list.
92 */
93 protected _processingErrors: string[];
94 /**
95 * Constructor
96 * * Sets class name
97 */
98 constructor();
99 protected debug(): void;
100 /**
101 * Returns object's internal unique ID.
102 *
103 * @return Unique ID
104 */
105 readonly uid: string;
106 /**
107 * Sets the user-defined id of the element.
108 *
109 * @param value Id
110 */
111 /**
112 * @return Id
113 */
114 id: $type.Optional<string>;
115 /**
116 * Returns a universal collection for mapping ids with objects.
117 *
118 * @ignore Exclude from docs
119 * @return Map collection
120 */
121 readonly map: Dictionary<string, any>;
122 /**
123 * Returns mapping for objects referenced by id in JSON config that are not yet
124 * available at processing time.
125 *
126 * @ignore Exclude from docs
127 * @return Map collection
128 */
129 readonly delayedMap: Dictionary<string, any>;
130 /**
131 * Logs an id and property of the target element that is not yet available
132 * for later assignment.
133 *
134 * @ignore
135 * @param property Property to set
136 * @param id ID of the target element
137 */
138 addDelayedMap(property: string, id: string): void;
139 /**
140 * Processes delayed JSON config items.
141 *
142 * @ignore
143 */
144 processDelayedMap(): void;
145 /**
146 * Applies properties from all assigned themes.
147 *
148 * @ignore Exclude from docs
149 */
150 applyTheme(): void;
151 /**
152 * A list of themes to be used for this element.
153 *
154 * @ignore Exclude from docs
155 * @param value An array of themes
156 */
157 /**
158 * @ignore Exclude from docs
159 * @return An array of themes
160 */
161 themes: $type.Optional<ITheme[]>;
162 /**
163 * Returns a list of themes that should be applied to this element. It could
164 * either be a list of themes set explicitly on this element, or system-wide.
165 *
166 * @return List of themes
167 */
168 getCurrentThemes(): ITheme[];
169 /**
170 * Returns if this object has been already been disposed.
171 *
172 * @return Is disposed?
173 */
174 isDisposed(): boolean;
175 /**
176 * Destroys this object and all related data.
177 */
178 dispose(): void;
179 /**
180 * Adds an IDisposer, which will be disposed when this object is disposed.
181 *
182 * @param target Object to dispose
183 * @ignore Exclude from docs
184 */
185 addDisposer(target: IDisposer): void;
186 /**
187 * Disposes disposable object and removes it from `_disposers`.
188 *
189 * @param target Object to dispose
190 * @ignore Exclude from docs
191 */
192 removeDispose(target: IDisposer): void;
193 /**
194 * Makes a copy of this object and returns the clone. Try to avoid cloning complex objects like chart, create new instances if you need them.
195 *
196 * @param cloneId An id to use for clone (if not set a unique id will be generated)
197 * @returns Clone
198 */
199 clone<A extends this>(cloneId?: string): this;
200 /**
201 * Returns a collection of object's clones.
202 *
203 * @ignore Exclude from docs
204 * @return Clones
205 */
206 readonly clones: List<this>;
207 /**
208 * Copies all properties and related data from different element.
209 *
210 * @param object Source element
211 */
212 copyFrom(object: this): void;
213 /**
214 * Element's class name. (a class that was used to instantiate the element)
215 *
216 * @ignore Exclude from docs
217 * @param value Class name
218 */
219 /**
220 * @ignore Exclude from docs
221 * @return Class name
222 */
223 className: $type.Optional<string>;
224 /**
225 * Caches value in object's cache.
226 *
227 * @ignore Exclude from docs
228 * @param key Key
229 * @param value Value
230 * @param ttl TTL in seconds
231 */
232 setCache(key: string, value: any, ttl?: number): void;
233 /**
234 * Retrieves cached value.
235 *
236 * If optional second padarameter is specified, it will return that value
237 * if cache is not available or is expired.
238 *
239 * @ignore Exclude from docs
240 * @param key Key
241 * @param value Value to return if cache is not available
242 * @return Value
243 */
244 getCache(key: string, value?: any): any;
245 /**
246 * Clears object's local cache.
247 *
248 * @ignore Exclude from docs
249 */
250 clearCache(): void;
251 /**
252 * Creates [[Disposer]] for `setTimeout` function call. This ensures that all
253 * timeouts created by the object will be cleared when object itself is
254 * disposed.
255 *
256 * @ignore Exclude from docs
257 * @param fn Callback function
258 * @param delay Timeout (ms)
259 * @return Disposer for timeout
260 */
261 setTimeout(fn: () => void, delay: number): IDisposer;
262 /**
263 * Creates [[Disposer]] for `setInterval` function call. This ensures that all
264 * timeouts created by the object will be cleared when object itself is
265 * disposed.
266 *
267 * @ignore Exclude from docs
268 * @param fn Callback function
269 * @param delay Timeout (ms)
270 * @return Disposer for timeout
271 */
272 setInterval(fn: () => void, delay: number): IDisposer;
273 /**
274 * ==========================================================================
275 * JSON-BASED CONFIG PROCESSING
276 * ==========================================================================
277 * @hidden
278 */
279 /**
280 * Use this property to set JSON-based config. When set, triggers processing
281 * routine, which will go through all properties, and try to apply values,
282 * create instances, etc.
283 *
284 * Use this with caution, as it is a time-consuming process. It's used for
285 * initialchart setup only, not routine operations.
286 *
287 * @param json JSON config
288 */
289 config: object;
290 /**
291 * Processes the JSON config.
292 *
293 * @param json JSON config
294 * @ignore Exclude from docs
295 */
296 protected processConfig(config?: object): void;
297 /**
298 * Tries to detect if value is color or percent and converts to proper object
299 * if necessary.
300 *
301 * Returns the same source value if no color/percent detected
302 *
303 * @param value Source value
304 * @return Converted value
305 */
306 protected maybeColorOrPercent(value: any): any;
307 protected processAdapters(item: Adapter<any, any>, config: any): void;
308 protected processEvents(item: EventDispatcher<any>, config: any): void;
309 /**
310 * Processes JSON config for a [[DictionaryTemplate]] item.
311 *
312 * @todo Description
313 * @param item Item
314 * @param config Config
315 */
316 protected processDictionaryTemplate(item: DictionaryTemplate<any, any>, config: any): void;
317 /**
318 * Processes JSON config for a [[Dictionary]] item.
319 *
320 * @todo Description
321 * @param item Item
322 * @param config Config
323 */
324 protected processDictionary(item: Dictionary<any, any>, config: any): void;
325 /**
326 * Processes [[ListTemplate]].
327 *
328 * @param configValue Config value
329 * @param item Item
330 */
331 protected processListTemplate(configValue: any, item: ListTemplate<any>): void;
332 /**
333 * Processes [[OrdererListTemplate]] or [[SortedListTemplate]].
334 *
335 * @param configValue Config value
336 * @param item Item
337 */
338 protected processOrderedTemplate(configValue: any, item: OrderedListTemplate<any> | SortedListTemplate<any>): void;
339 /**
340 * Processes [[List]].
341 *
342 * @param configValue Config value
343 * @param item Item
344 */
345 protected processList(configValue: any, item: List<any>, parent?: any): void;
346 /**
347 * This function is used to sort element's JSON config properties, so that
348 * some properties that absolutely need to be processed last, can be put at
349 * the end.
350 *
351 * @ignore Exclude from docs
352 * @param a Element 1
353 * @param b Element 2
354 * @return Sorting number
355 */
356 protected configOrder(a: string, b: string): Ordering;
357 /**
358 * Checks if field should be just assigned as is, without any checking when
359 * processing JSON config.
360 *
361 * Extending functions can override this function to do their own checks.
362 *
363 * @param field Field name
364 * @return Assign as is?
365 */
366 protected asIs(field: string): boolean;
367 /**
368 * Checks if field needs to be converted to function, if it is specified
369 * as string.
370 *
371 * @param field Field name
372 * @return Assign as function?
373 */
374 protected asFunction(field: string): boolean;
375 /**
376 * Creates a relevant class instance if such class definition exists.
377 *
378 * @ignore Exclude from docs
379 * @param className Class name
380 * @return Instance
381 */
382 protected createClassInstance(className: string): Object;
383 /**
384 * Creates a class instance for a config entry using it's type. (as set in
385 * `type` property)
386 *
387 * @ignore Exclude from docs
388 * @param config Config part
389 * @return Instance
390 */
391 protected createEntryInstance(config: any): any;
392 /**
393 * Determines config object type.
394 *
395 * @ignore Exclude from docs
396 * @param config Config part
397 * @return Type
398 */
399 protected getConfigEntryType(config: any): any;
400 /**
401 * Checks if this element has a property.
402 *
403 * @ignore Exclude from docs
404 * @param prop Property name
405 * @return Has property?
406 */
407 protected hasProperty(prop: string): boolean;
408 /**
409 * Checkes whether JSON key is a reserved keyword.
410 *
411 * @param key Key
412 * @return Reserved
413 */
414 protected isReserved(key: string): boolean;
415 /**
416 * A list of errors that happened during JSON processing.
417 *
418 * @return Errors
419 */
420 protected readonly processingErrors: string[];
421}
422/**
423 * Defines events for [[BaseObjectEvents]].
424 */
425export interface IBaseObjectEvents {
426}
427/**
428 * A version of [[BaseObject]] with events properties and methods.
429 * Classes that use [[EventDispatcher]] should extend this instead of
430 * [[BaseObject]] directly.
431 */
432export declare class BaseObjectEvents extends BaseObject {
433 /**
434 * Constructor
435 */
436 constructor();
437 _events: IBaseObjectEvents;
438 /**
439 * An [[EventDispatcher]] instance
440 * @ignore
441 */
442 _eventDispatcher: EventDispatcher<AMEvent<this, this["_events"]>>;
443 /**
444 * An [[EventDispatcher]] instance
445 */
446 readonly events: EventDispatcher<AMEvent<this, this["_events"]>>;
447 /**
448 * Dispatches an event using own event dispatcher. Will automatically
449 * populate event data object with event type and target (this element).
450 * It also checks if there are any handlers registered for this sepecific
451 * event.
452 *
453 * @param eventType Event type (name)
454 * @param data Data to pass into event handler(s)
455 */
456 dispatch<Key extends keyof this["_events"]>(eventType: Key, data?: any): void;
457 /**
458 * Works like `dispatch`, except event is triggered immediately, without
459 * waiting for the next frame cycle.
460 *
461 * @param eventType Event type (name)
462 * @param data Data to pass into event handler(s)
463 */
464 dispatchImmediately<Key extends keyof this["_events"]>(eventType: Key, data?: any): void;
465 /**
466 * Copies all parameters from another [[Sprite]].
467 *
468 * @param source Source object
469 */
470 copyFrom(source: this): void;
471}