1 |
|
2 |
|
3 |
|
4 |
|
5 | import { ObservableJSON } from '@jupyterlab/observables';
|
6 | import { JSONExt } from '@lumino/coreutils';
|
7 | import { Signal } from '@lumino/signaling';
|
8 |
|
9 |
|
10 |
|
11 | export class AttachmentModel {
|
12 | |
13 |
|
14 |
|
15 | constructor(options) {
|
16 |
|
17 | this.trusted = false;
|
18 | this._changed = new Signal(this);
|
19 | this._raw = {};
|
20 | const data = Private.getData(options.value);
|
21 | this._data = new ObservableJSON({ values: data });
|
22 | this._rawData = data;
|
23 |
|
24 | const value = options.value;
|
25 | for (const key in value) {
|
26 |
|
27 | switch (key) {
|
28 | case 'data':
|
29 | break;
|
30 | default:
|
31 | this._raw[key] = Private.extract(value, key);
|
32 | }
|
33 | }
|
34 | }
|
35 | |
36 |
|
37 |
|
38 | get changed() {
|
39 | return this._changed;
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | dispose() {
|
45 | this._data.dispose();
|
46 | Signal.clearData(this);
|
47 | }
|
48 | |
49 |
|
50 |
|
51 | get data() {
|
52 | return this._rawData;
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get metadata() {
|
58 | return {};
|
59 | }
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | setData(options) {
|
68 | if (options.data) {
|
69 | this._updateObservable(this._data, options.data);
|
70 | this._rawData = options.data;
|
71 | }
|
72 | this._changed.emit(void 0);
|
73 | }
|
74 | |
75 |
|
76 |
|
77 | toJSON() {
|
78 | const attachment = {};
|
79 | for (const key in this._raw) {
|
80 | attachment[key] = Private.extract(this._raw, key);
|
81 | }
|
82 | return attachment;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | _updateObservable(observable, data) {
|
88 | const oldKeys = observable.keys();
|
89 | const newKeys = Object.keys(data);
|
90 |
|
91 | for (const key of oldKeys) {
|
92 | if (newKeys.indexOf(key) === -1) {
|
93 | observable.delete(key);
|
94 | }
|
95 | }
|
96 |
|
97 | for (const key of newKeys) {
|
98 | const oldValue = observable.get(key);
|
99 | const newValue = data[key];
|
100 | if (oldValue !== newValue) {
|
101 | observable.set(key, newValue);
|
102 | }
|
103 | }
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 | (function (AttachmentModel) {
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function getData(bundle) {
|
118 | return Private.getData(bundle);
|
119 | }
|
120 | AttachmentModel.getData = getData;
|
121 | })(AttachmentModel || (AttachmentModel = {}));
|
122 |
|
123 |
|
124 |
|
125 | var Private;
|
126 | (function (Private) {
|
127 | |
128 |
|
129 |
|
130 | function getData(bundle) {
|
131 | return convertBundle(bundle);
|
132 | }
|
133 | Private.getData = getData;
|
134 | |
135 |
|
136 |
|
137 | function getBundleOptions(options) {
|
138 | const data = getData(options.value);
|
139 | return { data };
|
140 | }
|
141 | Private.getBundleOptions = getBundleOptions;
|
142 | |
143 |
|
144 |
|
145 | function extract(value, key) {
|
146 | const item = value[key];
|
147 | if (item === undefined || JSONExt.isPrimitive(item)) {
|
148 | return item;
|
149 | }
|
150 | return JSONExt.deepCopy(item);
|
151 | }
|
152 | Private.extract = extract;
|
153 | |
154 |
|
155 |
|
156 | function convertBundle(bundle) {
|
157 | const map = Object.create(null);
|
158 | for (const mimeType in bundle) {
|
159 | map[mimeType] = extract(bundle, mimeType);
|
160 | }
|
161 | return map;
|
162 | }
|
163 | })(Private || (Private = {}));
|
164 |
|
\ | No newline at end of file |