1 | // Copyright (c) Jupyter Development Team.
|
2 | // Distributed under the terms of the Modified BSD License.
|
3 | /*-----------------------------------------------------------------------------
|
4 | | Copyright (c) 2014-2017, PhosphorJS Contributors
|
5 | |
|
6 | | Distributed under the terms of the BSD 3-Clause License.
|
7 | |
|
8 | | The full license is in the file LICENSE, distributed with this software.
|
9 | |----------------------------------------------------------------------------*/
|
10 |
|
11 | /**
|
12 | * An object which stores MIME data for general application use.
|
13 | *
|
14 | * #### Notes
|
15 | * This class does not attempt to enforce "correctness" of MIME types
|
16 | * and their associated data. Since this class is designed to transfer
|
17 | * arbitrary data and objects within the same application, it assumes
|
18 | * that the user provides correct and accurate data.
|
19 | */
|
20 | export class MimeData {
|
21 | /**
|
22 | * Get an array of the MIME types contained within the dataset.
|
23 | *
|
24 | * @returns A new array of the MIME types, in order of insertion.
|
25 | */
|
26 | types(): string[] {
|
27 | return this._types.slice();
|
28 | }
|
29 |
|
30 | /**
|
31 | * Test whether the dataset has an entry for the given type.
|
32 | *
|
33 | * @param mime - The MIME type of interest.
|
34 | *
|
35 | * @returns `true` if the dataset contains a value for the given
|
36 | * MIME type, `false` otherwise.
|
37 | */
|
38 | hasData(mime: string): boolean {
|
39 | return this._types.indexOf(mime) !== -1;
|
40 | }
|
41 |
|
42 | /**
|
43 | * Get the data value for the given MIME type.
|
44 | *
|
45 | * @param mime - The MIME type of interest.
|
46 | *
|
47 | * @returns The value for the given MIME type, or `undefined` if
|
48 | * the dataset does not contain a value for the type.
|
49 | */
|
50 | getData(mime: string): any | undefined {
|
51 | let i = this._types.indexOf(mime);
|
52 | return i !== -1 ? this._values[i] : undefined;
|
53 | }
|
54 |
|
55 | /**
|
56 | * Set the data value for the given MIME type.
|
57 | *
|
58 | * @param mime - The MIME type of interest.
|
59 | *
|
60 | * @param data - The data value for the given MIME type.
|
61 | *
|
62 | * #### Notes
|
63 | * This will overwrite any previous entry for the MIME type.
|
64 | */
|
65 | setData(mime: string, data: unknown): void {
|
66 | this.clearData(mime);
|
67 | this._types.push(mime);
|
68 | this._values.push(data);
|
69 | }
|
70 |
|
71 | /**
|
72 | * Remove the data entry for the given MIME type.
|
73 | *
|
74 | * @param mime - The MIME type of interest.
|
75 | *
|
76 | * #### Notes
|
77 | * This is a no-op if there is no entry for the given MIME type.
|
78 | */
|
79 | clearData(mime: string): void {
|
80 | let i = this._types.indexOf(mime);
|
81 | if (i !== -1) {
|
82 | this._types.splice(i, 1);
|
83 | this._values.splice(i, 1);
|
84 | }
|
85 | }
|
86 |
|
87 | /**
|
88 | * Remove all data entries from the dataset.
|
89 | */
|
90 | clear(): void {
|
91 | this._types.length = 0;
|
92 | this._values.length = 0;
|
93 | }
|
94 |
|
95 | private _types: string[] = [];
|
96 | private _values: any[] = [];
|
97 | }
|