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