| 1 |
/* Copyright 2015 Bloomberg Finance L.P. |
| 2 |
* |
| 3 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
* you may not use this file except in compliance with the License. |
| 5 |
* You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software |
| 10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
* See the License for the specific language governing permissions and |
| 13 |
* limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
import * as widgets from '@jupyter-widgets/base'; |
| 17 |
import { MarkModel } from './MarkModel'; |
| 18 |
import * as serialize from './serialize'; |
| 19 |
|
| 20 |
export class ImageModel extends MarkModel { |
| 21 |
defaults() { |
| 22 |
return { |
| 23 |
...MarkModel.prototype.defaults(), |
| 24 |
_model_name: 'ImageModel', |
| 25 |
_view_name: 'Image', |
| 26 |
pixelated: false, |
| 27 |
x: [0.0, 1.0], |
| 28 |
y: [0.0, 1.0], |
| 29 |
scales_metadata: { |
| 30 |
x: { orientation: 'horizontal', dimension: 'x' }, |
| 31 |
y: { orientation: 'vertical', dimension: 'y' }, |
| 32 |
}, |
| 33 |
}; |
| 34 |
} |
| 35 |
|
| 36 |
initialize(attributes, options) { |
| 37 |
super.initialize(attributes, options); |
| 38 |
this.on_some_change(['x', 'y'], this.update_data, this); |
| 39 |
this.on_some_change(['preserve_domain'], this.update_domains, this); |
| 40 |
this.update_data(); |
| 41 |
} |
| 42 |
|
| 43 |
update_data() { |
| 44 |
this.mark_data = { |
| 45 |
x: this.get('x'), |
| 46 |
y: this.get('y'), |
| 47 |
}; |
| 48 |
this.update_domains(); |
| 49 |
this.trigger('data_updated'); |
| 50 |
} |
| 51 |
|
| 52 |
update_domains() { |
| 53 |
if (!this.mark_data) { |
| 54 |
return; |
| 55 |
} |
| 56 |
const scales = this.get('scales'); |
| 57 |
const x_scale = scales.x; |
| 58 |
const y_scale = scales.y; |
| 59 |
|
| 60 |
if (x_scale) { |
| 61 |
if (!this.get('preserve_domain').x) { |
| 62 |
x_scale.compute_and_set_domain( |
| 63 |
this.mark_data['x'], |
| 64 |
this.model_id + '_x' |
| 65 |
); |
| 66 |
} else { |
| 67 |
x_scale.del_domain([], this.model_id + '_x'); |
| 68 |
} |
| 69 |
} |
| 70 |
if (y_scale) { |
| 71 |
if (!this.get('preserve_domain').y) { |
| 72 |
y_scale.compute_and_set_domain( |
| 73 |
this.mark_data['y'], |
| 74 |
this.model_id + '_y' |
| 75 |
); |
| 76 |
} else { |
| 77 |
y_scale.del_domain([], this.model_id + '_y'); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static serializers = { |
| 83 |
...MarkModel.serializers, |
| 84 |
image: { deserialize: widgets.unpack_models }, |
| 85 |
x: serialize.array_or_json_serializer, |
| 86 |
y: serialize.array_or_json_serializer, |
| 87 |
}; |
| 88 |
} |