forked from jupyter-widgets/ipywidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_button.js
97 lines (87 loc) · 2.74 KB
/
widget_button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';
var widget = require('./widget');
var _ = require('underscore');
var ButtonModel = widget.DOMWidgetModel.extend({
defaults: _.extend({}, widget.DOMWidgetModel.prototype.defaults, {
description: '',
tooltip: '',
disabled: false,
icon: '',
button_style: '',
_view_name: 'ButtonView',
_model_name: 'ButtonModel'
})
});
var ButtonView = widget.DOMWidgetView.extend({
initialize: function() {
/**
* Called when view is instantiated.
*/
this.setElement(document.createElement('button'));
ButtonView.__super__.initialize.apply(this, arguments);
},
render: function() {
/**
* Called when view is rendered.
*/
this.el.className = 'jupyter-widgets widget-button';
this.listenTo(
this.model,
'change:button_style',
this.update_button_style,
this
);
this.update_button_style();
this.update(); // Set defaults.
},
update: function() {
/**
* Update the contents of this view
*
* Called when the model is changed. The model may have been
* changed by another view or by a state update from the back-end.
*/
this.el.disabled = this.model.get('disabled');
this.el.setAttribute('title', this.model.get('tooltip'));
var description = this.model.get('description');
var icon = this.model.get('icon');
if (description.trim().length || icon.trim().length) {
this.el.textContent = '';
if (icon.trim().length) {
var i = document.createElement('i');
i.classList.add('fa');
i.classList.add('fa-' + icon);
this.el.appendChild(i);
}
this.el.appendChild(document.createTextNode(description));
}
return ButtonView.__super__.update.apply(this);
},
update_button_style: function() {
var class_map = {
primary: ['mod-primary'],
success: ['mod-success'],
info: ['mod-info'],
warning: ['mod-warning'],
danger: ['mod-danger']
};
this.update_mapped_classes(class_map, 'button_style');
},
events: {
// Dictionary of events and their handlers.
'click': '_handle_click'
},
_handle_click: function(event) {
/**
* Handles when the button is clicked.
*/
event.preventDefault();
this.send({event: 'click'});
}
});
module.exports = {
ButtonView: ButtonView,
ButtonModel: ButtonModel
};