UNPKG

4.91 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3var __extends = (this && this.__extends) || (function () {
4 var extendStatics = function (d, b) {
5 extendStatics = Object.setPrototypeOf ||
6 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
8 return extendStatics(d, b);
9 };
10 return function (d, b) {
11 extendStatics(d, b);
12 function __() { this.constructor = d; }
13 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14 };
15})();
16import { assign } from './utils';
17import { WidgetModel, WidgetView } from './widget';
18var StyleModel = /** @class */ (function (_super) {
19 __extends(StyleModel, _super);
20 function StyleModel() {
21 return _super !== null && _super.apply(this, arguments) || this;
22 }
23 StyleModel.prototype.defaults = function () {
24 var Derived = this.constructor;
25 return assign(_super.prototype.defaults.call(this), {
26 _model_name: 'StyleModel',
27 _view_name: 'StyleView',
28 }, Object.keys(Derived.styleProperties).reduce(function (obj, key) {
29 obj[key] = Derived.styleProperties[key].default;
30 return obj;
31 }, {}));
32 };
33 StyleModel.styleProperties = {};
34 return StyleModel;
35}(WidgetModel));
36export { StyleModel };
37var StyleView = /** @class */ (function (_super) {
38 __extends(StyleView, _super);
39 function StyleView() {
40 return _super !== null && _super.apply(this, arguments) || this;
41 }
42 /**
43 * Public constructor
44 */
45 StyleView.prototype.initialize = function (parameters) {
46 this._traitNames = [];
47 _super.prototype.initialize.call(this, parameters);
48 // Register the traits that live on the Python side
49 var ModelType = this.model.constructor;
50 for (var _i = 0, _a = Object.keys(ModelType.styleProperties); _i < _a.length; _i++) {
51 var key = _a[_i];
52 this.registerTrait(key);
53 }
54 // Set the initial styles
55 this.style();
56 };
57 /**
58 * Register a CSS trait that is known by the model
59 * @param trait
60 */
61 StyleView.prototype.registerTrait = function (trait) {
62 var _this = this;
63 this._traitNames.push(trait);
64 // Listen to changes, and set the value on change.
65 this.listenTo(this.model, 'change:' + trait, function (model, value) {
66 _this.handleChange(trait, value);
67 });
68 };
69 /**
70 * Handles when a trait value changes
71 */
72 StyleView.prototype.handleChange = function (trait, value) {
73 // should be synchronous so that we can measure later.
74 var parent = this.options.parent;
75 if (parent) {
76 var ModelType = this.model.constructor;
77 var styleProperties = ModelType.styleProperties;
78 var attribute = styleProperties[trait].attribute;
79 var selector = styleProperties[trait].selector;
80 var elements = selector ? parent.el.querySelectorAll(selector) : [parent.el];
81 if (value === null) {
82 for (var i = 0; i !== elements.length; ++i) {
83 elements[i].style.removeProperty(attribute);
84 }
85 }
86 else {
87 for (var i = 0; i !== elements.length; ++i) {
88 elements[i].style[attribute] = value;
89 }
90 }
91 }
92 else {
93 console.warn('Style not applied because a parent view does not exist');
94 }
95 };
96 /**
97 * Apply styles for all registered traits
98 */
99 StyleView.prototype.style = function () {
100 for (var _i = 0, _a = this._traitNames; _i < _a.length; _i++) {
101 var trait = _a[_i];
102 this.handleChange(trait, this.model.get(trait));
103 }
104 };
105 /**
106 * Remove the styling from the parent view.
107 */
108 StyleView.prototype.unstyle = function () {
109 var parent = this.options.parent;
110 var ModelType = this.model.constructor;
111 var styleProperties = ModelType.styleProperties;
112 this._traitNames.forEach(function (trait) {
113 if (parent) {
114 var attribute = styleProperties[trait].attribute;
115 var selector = styleProperties[trait].selector;
116 var elements = selector ? parent.el.querySelectorAll(selector) : [parent.el];
117 for (var i = 0; i !== elements.length; ++i) {
118 elements[i].style.removeProperty(attribute);
119 }
120 }
121 else {
122 console.warn('Style not removed because a parent view does not exist');
123 }
124 }, this);
125 };
126 return StyleView;
127}(WidgetView));
128export { StyleView };