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
// Example Use for the CellToolbar library
5
// add the following to your custom.js to load
6
// Celltoolbar UI for slideshow
7
8
// ```
9
// $.getScript('/static/js/celltoolbarpresets/example.js');
10
// ```
11
define([
12
'jquery',
13
'notebook/js/celltoolbar',
14
], function($, celltoolbar) {
15
"use strict";
16
17
var CellToolbar = celltoolbar.CellToolbar;
18
19
var example_preset = [];
20
21
var simple_button = function(div, cell) {
22
var button_container = $(div);
23
var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}});
24
var fun = function(value){
25
try{
26
if(value){
27
cell.code_mirror.setOption('readOnly','nocursor');
28
button.button('option','icons',{primary:'ui-icon-locked'});
29
} else {
30
cell.code_mirror.setOption('readOnly',false);
31
button.button('option','icons',{primary:'ui-icon-unlocked'});
32
}
33
} catch(e){}
34
35
};
36
fun(cell.metadata.ro);
37
button.click(function(){
38
var v = cell.metadata.ro;
39
var locked = !v;
40
cell.metadata.ro = locked;
41
fun(locked);
42
})
43
.css('height','16px')
44
.css('width','35px');
45
button_container.append(button);
46
};
47
48
CellToolbar.register_callback('example.lock',simple_button);
49
example_preset.push('example.lock');
50
51
var toggle_test = function(div, cell) {
52
var button_container = $(div);
53
var button = $('<div/>')
54
.button({label:String(cell.metadata.foo)}).
55
css('width','65px');
56
button.click(function(){
57
var v = cell.metadata.foo;
58
cell.metadata.foo = !v;
59
button.button("option","label",String(!v));
60
});
61
button_container.append(button);
62
};
63
64
CellToolbar.register_callback('example.toggle',toggle_test);
65
example_preset.push('example.toggle');
66
67
var checkbox_test = CellToolbar.utils.checkbox_ui_generator('Yes/No',
68
// setter
69
function(cell, value){
70
// we check that the slideshow namespace exist and create it if needed
71
if (cell.metadata.yn_test === undefined){cell.metadata.yn_test = {};}
72
// set the value
73
cell.metadata.yn_test.value = value;
74
},
75
//geter
76
function(cell){ var ns = cell.metadata.yn_test;
77
// if the slideshow namespace does not exist return `undefined`
78
// (will be interpreted as `false` by checkbox) otherwise
79
// return the value
80
return (ns === undefined)? undefined: ns.value;
81
}
82
);
83
84
85
CellToolbar.register_callback('example.checkbox',checkbox_test);
86
example_preset.push('example.checkbox');
87
88
var select_test = CellToolbar.utils.select_ui_generator([
89
["-" ,undefined ],
90
["Header Slide" ,"header_slide" ],
91
["Slide" ,"slide" ],
92
["Fragment" ,"fragment" ],
93
["Skip" ,"skip" ],
94
],
95
// setter
96
function(cell,value){
97
// we check that the slideshow namespace exist and create it if needed
98
if (cell.metadata.test === undefined){cell.metadata.test = {};}
99
// set the value
100
cell.metadata.test.slide_type = value;
101
},
102
//geter
103
function(cell){ var ns = cell.metadata.test;
104
// if the slideshow namespace does not exist return `undefined`
105
// (will be interpreted as `false` by checkbox) otherwise
106
// return the value
107
return (ns === undefined)? undefined: ns.slide_type;
108
});
109
110
CellToolbar.register_callback('example.select',select_test);
111
example_preset.push('example.select');
112
113
var simple_dialog = function(title,text){
114
var dlg = $('<div/>').attr('title',title)
115
.append($('<p/>').text(text));
116
$(dlg).dialog({
117
autoOpen: true,
118
height: 300,
119
width: 650,
120
modal: true,
121
close: function() {
122
/**
123
*cleanup on close
124
*/
125
$(this).remove();
126
}
127
});
128
};
129
130
var add_simple_dialog_button = function(div, cell) {
131
var help_text = ["This is the Metadata editting UI.",
132
"It heavily rely on plugin to work ",
133
"and is still under developpement. You shouldn't wait too long before",
134
" seeing some customisable buttons in those toolbar."
135
].join('\n');
136
var button_container = $(div);
137
var button = $('<div/>').button({label:'?'})
138
.click(function(){simple_dialog('help',help_text); return false;});
139
button_container.append(button);
140
};
141
142
var register = function (notebook) {
143
CellToolbar.register_callback('example.help',add_simple_dialog_button);
144
example_preset.push('example.help');
145
146
CellToolbar.register_preset('Example',example_preset, notebook);
147
console.log('Example extension for metadata editing loaded.');
148
};
149
return {'register': register};
150
});
151
152