| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { unpack_models } from '@jupyter-widgets/base'; |
| 4 |
import { CoreWidgetModel } from './widget_core'; |
| 5 |
export class DirectionalLinkModel extends CoreWidgetModel { |
| 6 |
defaults() { |
| 7 |
return Object.assign(Object.assign({}, super.defaults()), { target: undefined, source: undefined, _model_name: 'DirectionalLinkModel' }); |
| 8 |
} |
| 9 |
initialize(attributes, options) { |
| 10 |
super.initialize(attributes, options); |
| 11 |
this.on('change', this.updateBindings, this); |
| 12 |
this.updateBindings(); |
| 13 |
} |
| 14 |
updateValue(sourceModel, sourceAttr, targetModel, targetAttr) { |
| 15 |
if (this._updating) { |
| 16 |
return; |
| 17 |
} |
| 18 |
this._updating = true; |
| 19 |
try { |
| 20 |
if (targetModel) { |
| 21 |
targetModel.set(targetAttr, sourceModel.get(sourceAttr)); |
| 22 |
targetModel.save_changes(); |
| 23 |
} |
| 24 |
} |
| 25 |
finally { |
| 26 |
this._updating = false; |
| 27 |
} |
| 28 |
} |
| 29 |
updateBindings() { |
| 30 |
this.cleanup(); |
| 31 |
[this.sourceModel, this.sourceAttr] = this.get('source') || [null, null]; |
| 32 |
[this.targetModel, this.targetAttr] = this.get('target') || [null, null]; |
| 33 |
if (this.sourceModel) { |
| 34 |
this.listenTo(this.sourceModel, 'change:' + this.sourceAttr, () => { |
| 35 |
this.updateValue(this.sourceModel, this.sourceAttr, this.targetModel, this.targetAttr); |
| 36 |
}); |
| 37 |
this.updateValue(this.sourceModel, this.sourceAttr, this.targetModel, this.targetAttr); |
| 38 |
this.listenToOnce(this.sourceModel, 'destroy', this.cleanup); |
| 39 |
} |
| 40 |
if (this.targetModel) { |
| 41 |
this.listenToOnce(this.targetModel, 'destroy', this.cleanup); |
| 42 |
} |
| 43 |
} |
| 44 |
cleanup() { |
| 45 |
// Stop listening to 'change' and 'destroy' events of the source and target |
| 46 |
if (this.sourceModel) { |
| 47 |
this.stopListening(this.sourceModel, 'change:' + this.sourceAttr, undefined); |
| 48 |
this.stopListening(this.sourceModel, 'destroy', undefined); |
| 49 |
} |
| 50 |
if (this.targetModel) { |
| 51 |
this.stopListening(this.targetModel, 'destroy', undefined); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
DirectionalLinkModel.serializers = Object.assign(Object.assign({}, CoreWidgetModel.serializers), { target: { deserialize: unpack_models }, source: { deserialize: unpack_models } }); |
| 56 |
export class LinkModel extends DirectionalLinkModel { |
| 57 |
defaults() { |
| 58 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'LinkModel' }); |
| 59 |
} |
| 60 |
updateBindings() { |
| 61 |
super.updateBindings(); |
| 62 |
if (this.targetModel) { |
| 63 |
this.listenTo(this.targetModel, 'change:' + this.targetAttr, () => { |
| 64 |
this.updateValue(this.targetModel, this.targetAttr, this.sourceModel, this.sourceAttr); |
| 65 |
}); |
| 66 |
} |
| 67 |
} |
| 68 |
cleanup() { |
| 69 |
super.cleanup(); |
| 70 |
if (this.targetModel) { |
| 71 |
this.stopListening(this.targetModel, 'change:' + this.targetAttr, undefined); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
//# sourceMappingURL=widget_link.js.map |