UNPKG

4.64 kBJavaScriptView Raw
1/* -----------------------------------------------------------------------------
2| Copyright (c) Jupyter Development Team.
3| Distributed under the terms of the Modified BSD License.
4|----------------------------------------------------------------------------*/
5import { ObservableJSON } from '@jupyterlab/observables';
6import { JSONExt } from '@lumino/coreutils';
7import { Signal } from '@lumino/signaling';
8/**
9 * The default implementation of a notebook attachment model.
10 */
11export class AttachmentModel {
12 /**
13 * Construct a new attachment model.
14 */
15 constructor(options) {
16 // All attachments are untrusted
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 // Make a copy of the data.
24 const value = options.value;
25 for (const key in value) {
26 // Ignore data and metadata that were stripped.
27 switch (key) {
28 case 'data':
29 break;
30 default:
31 this._raw[key] = Private.extract(value, key);
32 }
33 }
34 }
35 /**
36 * A signal emitted when the attachment model changes.
37 */
38 get changed() {
39 return this._changed;
40 }
41 /**
42 * Dispose of the resources used by the attachment model.
43 */
44 dispose() {
45 this._data.dispose();
46 Signal.clearData(this);
47 }
48 /**
49 * The data associated with the model.
50 */
51 get data() {
52 return this._rawData;
53 }
54 /**
55 * The metadata associated with the model.
56 */
57 get metadata() {
58 return {};
59 }
60 /**
61 * Set the data associated with the model.
62 *
63 * #### Notes
64 * Depending on the implementation of the mime model,
65 * this call may or may not have deferred effects,
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 * Serialize the model to JSON.
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 * Update an observable JSON object using a readonly JSON object.
86 */
87 _updateObservable(observable, data) {
88 const oldKeys = observable.keys();
89 const newKeys = Object.keys(data);
90 // Handle removed keys.
91 for (const key of oldKeys) {
92 if (newKeys.indexOf(key) === -1) {
93 observable.delete(key);
94 }
95 }
96 // Handle changed data.
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 * The namespace for AttachmentModel statics.
108 */
109(function (AttachmentModel) {
110 /**
111 * Get the data for an attachment.
112 *
113 * @params bundle - A kernel attachment MIME bundle.
114 *
115 * @returns - The data for the payload.
116 */
117 function getData(bundle) {
118 return Private.getData(bundle);
119 }
120 AttachmentModel.getData = getData;
121})(AttachmentModel || (AttachmentModel = {}));
122/**
123 * The namespace for module private data.
124 */
125var Private;
126(function (Private) {
127 /**
128 * Get the data from a notebook attachment.
129 */
130 function getData(bundle) {
131 return convertBundle(bundle);
132 }
133 Private.getData = getData;
134 /**
135 * Get the bundle options given attachment model options.
136 */
137 function getBundleOptions(options) {
138 const data = getData(options.value);
139 return { data };
140 }
141 Private.getBundleOptions = getBundleOptions;
142 /**
143 * Extract a value from a JSONObject.
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 * Convert a mime bundle to mime data.
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//# sourceMappingURL=attachmentmodel.js.map
\No newline at end of file