Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'base/js/namespace',
6
'jquery'
7
], function(IPython, $) {
8
"use strict";
9
10
/**
11
* A generic toolbar on which one can add button
12
* @class ToolBar
13
* @constructor
14
* @param {Dom_object} selector
15
*/
16
var ToolBar = function (selector, options) {
17
this.selector = selector;
18
this.actions = (options||{}).actions;
19
if (this.selector !== undefined) {
20
this.element = $(selector);
21
this.style();
22
}
23
};
24
25
ToolBar.prototype._pseudo_actions={};
26
27
28
ToolBar.prototype.construct = function (config) {
29
for(var k=0; k<config.length; k++) {
30
this.add_buttons_group(config[k][0],config[k][1]);
31
}
32
};
33
34
/**
35
* Add a group of button into the current toolbar.
36
*
37
* Use a [dict of [list of action name]] to trigger
38
* on click to the button
39
*
40
* @example
41
*
42
* ... todo, maybe use a list of list to keep ordering.
43
*
44
* [
45
* [
46
* [
47
* action_name_1,
48
* action_name_2,
49
* action_name_3,
50
* ],
51
* optional_group_name
52
* ],
53
* ...
54
* ]
55
*
56
* For backward compatibility this also support the
57
* old methods of adding a button directly bound to callbacks:
58
* @example
59
* # deprecate, do not use
60
* IPython.toolbar.add_buttons_group([
61
* {
62
* label:'my button',
63
* icon:'icon-hdd',
64
* callback:function(){alert('hoho')},
65
* id : 'my_button_id', // this is optional
66
* },
67
* {
68
* label:'my second button',
69
* icon:'icon-play',
70
* callback:function(){alert('be carefull I cut')}
71
* }
72
* ],
73
* "my_button_group_id"
74
* )
75
*
76
* @method add_buttons_group
77
* @param list {List}
78
* List of button of the group, with the following paramter for each :
79
* @param list.label {string} text to show on button hover
80
* @param list.icon {string} icon to choose from [Font Awesome](http://fortawesome.github.io/Font-Awesome)
81
* @param list.callback {function} function to be called on button click
82
* @param [list.id] {String} id to give to the button
83
* @param [group_id] {String} optionnal id to give to the group
84
*
85
*
86
* for private usage, the key can also be strings starting with '<' and ending with '>' to inject custom element that cannot
87
* be bound to an action.
88
*
89
*/
90
// TODO JUPYTER:
91
// get rid of legacy code that handle things that are not actions.
92
ToolBar.prototype.add_buttons_group = function (list, group_id) {
93
// handle custom call of pseudoaction binding.
94
if(typeof(list) === 'string' && list.slice(0,1) === '<' && list.slice(-1) === '>'){
95
var _pseudo_action;
96
try{
97
_pseudo_action = list.slice(1,-1);
98
this.element.append(this._pseudo_actions[_pseudo_action].call(this));
99
} catch (e) {
100
console.warn('ouch, calling ', _pseudo_action, 'does not seem to work...:', e);
101
}
102
return ;
103
}
104
var that = this;
105
var btn_group = $('<div/>').addClass("btn-group");
106
if( group_id !== undefined ) {
107
btn_group.attr('id',group_id);
108
}
109
for(var i=0; i < list.length; i++) {
110
111
// IIFE because javascript don't have loop scope so
112
// action_name would otherwise be the same on all iteration
113
// of the loop
114
(function(i,list){
115
var el = list[i];
116
var action_name;
117
var action;
118
if(typeof(el) === 'string'){
119
action = that.actions.get(el);
120
action_name = el;
121
122
}
123
var button = $('<button/>')
124
.addClass('btn btn-default')
125
.attr("title", el.label||action.help)
126
.append(
127
$("<i/>").addClass(el.icon||(action||{icon:'fa-exclamation-triangle'}).icon).addClass('fa')
128
);
129
var id = el.id;
130
if( id !== undefined ){
131
button.attr('id',id);
132
}
133
button.attr('data-jupyter-action', action_name);
134
var fun = el.callback|| function(){
135
that.actions.call(action_name);
136
};
137
button.click(fun);
138
btn_group.append(button);
139
})(i,list);
140
// END IIFE
141
}
142
$(this.selector).append(btn_group);
143
};
144
145
ToolBar.prototype.style = function () {
146
this.element.addClass('toolbar');
147
};
148
149
/**
150
* Show and hide toolbar
151
* @method toggle
152
*/
153
ToolBar.prototype.toggle = function () {
154
this.element.toggle();
155
};
156
157
// Backwards compatibility.
158
IPython.ToolBar = ToolBar;
159
160
return {'ToolBar': ToolBar};
161
});
162
163