example.js
150 lines
| 5.5 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17217 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
// Example Use for the CellToolbar library | ||||
// add the following to your custom.js to load | ||||
// Celltoolbar UI for slideshow | ||||
// ``` | ||||
// $.getScript('/static/js/celltoolbarpresets/example.js'); | ||||
// ``` | ||||
define([ | ||||
'jquery', | ||||
'notebook/js/celltoolbar', | ||||
], function($, celltoolbar) { | ||||
Matthias BUSSONNIER
|
r9057 | "use strict"; | ||
Jonathan Frederic
|
r17217 | var CellToolbar = celltoolbar.CellToolbar; | ||
Matthias BUSSONNIER
|
r9057 | |||
Matthias BUSSONNIER
|
r9083 | var example_preset = []; | ||
Matthias BUSSONNIER
|
r9057 | var simple_button = function(div, cell) { | ||
var button_container = $(div); | ||||
var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}}); | ||||
var fun = function(value){ | ||||
try{ | ||||
if(value){ | ||||
Jonathan Frederic
|
r17217 | cell.code_mirror.setOption('readOnly','nocursor'); | ||
button.button('option','icons',{primary:'ui-icon-locked'}); | ||||
Matthias BUSSONNIER
|
r9057 | } else { | ||
Jonathan Frederic
|
r17217 | cell.code_mirror.setOption('readOnly',false); | ||
button.button('option','icons',{primary:'ui-icon-unlocked'}); | ||||
Matthias BUSSONNIER
|
r9057 | } | ||
} catch(e){} | ||||
Jonathan Frederic
|
r17217 | }; | ||
fun(cell.metadata.ro); | ||||
Matthias BUSSONNIER
|
r9057 | button.click(function(){ | ||
var v = cell.metadata.ro; | ||||
var locked = !v; | ||||
cell.metadata.ro = locked; | ||||
Jonathan Frederic
|
r17217 | fun(locked); | ||
Matthias BUSSONNIER
|
r9057 | }) | ||
.css('height','16px') | ||||
.css('width','35px'); | ||||
button_container.append(button); | ||||
Jonathan Frederic
|
r17217 | }; | ||
Matthias BUSSONNIER
|
r9057 | |||
Matthias BUSSONNIER
|
r9064 | CellToolbar.register_callback('example.lock',simple_button); | ||
Matthias BUSSONNIER
|
r9057 | example_preset.push('example.lock'); | ||
var toggle_test = function(div, cell) { | ||||
Jonathan Frederic
|
r17217 | var button_container = $(div); | ||
Matthias BUSSONNIER
|
r9070 | var button = $('<div/>') | ||
.button({label:String(cell.metadata.foo)}). | ||||
css('width','65px'); | ||||
Matthias BUSSONNIER
|
r9057 | button.click(function(){ | ||
var v = cell.metadata.foo; | ||||
cell.metadata.foo = !v; | ||||
button.button("option","label",String(!v)); | ||||
Jonathan Frederic
|
r17217 | }); | ||
Matthias BUSSONNIER
|
r9057 | button_container.append(button); | ||
Jonathan Frederic
|
r17217 | }; | ||
Matthias BUSSONNIER
|
r9057 | |||
Matthias BUSSONNIER
|
r9064 | CellToolbar.register_callback('example.toggle',toggle_test); | ||
Matthias BUSSONNIER
|
r9057 | example_preset.push('example.toggle'); | ||
Matthias BUSSONNIER
|
r9072 | var checkbox_test = CellToolbar.utils.checkbox_ui_generator('Yes/No', | ||
// setter | ||||
function(cell, value){ | ||||
// we check that the slideshow namespace exist and create it if needed | ||||
Jonathan Frederic
|
r17217 | if (cell.metadata.yn_test === undefined){cell.metadata.yn_test = {};} | ||
Matthias BUSSONNIER
|
r9072 | // set the value | ||
Jonathan Frederic
|
r17217 | cell.metadata.yn_test.value = value; | ||
Matthias BUSSONNIER
|
r9072 | }, | ||
//geter | ||||
function(cell){ var ns = cell.metadata.yn_test; | ||||
// if the slideshow namespace does not exist return `undefined` | ||||
// (will be interpreted as `false` by checkbox) otherwise | ||||
// return the value | ||||
Jonathan Frederic
|
r17217 | return (ns === undefined)? undefined: ns.value; | ||
Matthias BUSSONNIER
|
r9072 | } | ||
); | ||||
Matthias BUSSONNIER
|
r9057 | |||
Matthias BUSSONNIER
|
r9064 | CellToolbar.register_callback('example.checkbox',checkbox_test); | ||
Matthias BUSSONNIER
|
r9057 | example_preset.push('example.checkbox'); | ||
Matthias BUSSONNIER
|
r9072 | var select_test = CellToolbar.utils.select_ui_generator([ | ||
["-" ,undefined ], | ||||
["Header Slide" ,"header_slide" ], | ||||
["Slide" ,"slide" ], | ||||
["Fragment" ,"fragment" ], | ||||
["Skip" ,"skip" ], | ||||
], | ||||
// setter | ||||
function(cell,value){ | ||||
// we check that the slideshow namespace exist and create it if needed | ||||
Jonathan Frederic
|
r17217 | if (cell.metadata.test === undefined){cell.metadata.test = {};} | ||
Matthias BUSSONNIER
|
r9072 | // set the value | ||
Jonathan Frederic
|
r17217 | cell.metadata.test.slide_type = value; | ||
Matthias BUSSONNIER
|
r9072 | }, | ||
//geter | ||||
function(cell){ var ns = cell.metadata.test; | ||||
// if the slideshow namespace does not exist return `undefined` | ||||
// (will be interpreted as `false` by checkbox) otherwise | ||||
// return the value | ||||
Jonathan Frederic
|
r17217 | return (ns === undefined)? undefined: ns.slide_type; | ||
Matthias BUSSONNIER
|
r9057 | }); | ||
Matthias BUSSONNIER
|
r9064 | CellToolbar.register_callback('example.select',select_test); | ||
Matthias BUSSONNIER
|
r9057 | example_preset.push('example.select'); | ||
Matthias BUSSONNIER
|
r9067 | var simple_dialog = function(title,text){ | ||
var dlg = $('<div/>').attr('title',title) | ||||
Jonathan Frederic
|
r17217 | .append($('<p/>').text(text)); | ||
Matthias BUSSONNIER
|
r9067 | $(dlg).dialog({ | ||
autoOpen: true, | ||||
height: 300, | ||||
width: 650, | ||||
modal: true, | ||||
close: function() { | ||||
Jonathan Frederic
|
r19176 | /** | ||
*cleanup on close | ||||
*/ | ||||
Matthias BUSSONNIER
|
r9067 | $(this).remove(); | ||
} | ||||
}); | ||||
Jonathan Frederic
|
r17217 | }; | ||
Matthias BUSSONNIER
|
r9067 | |||
var add_simple_dialog_button = function(div, cell) { | ||||
var help_text = ["This is the Metadata editting UI.", | ||||
"It heavily rely on plugin to work ", | ||||
"and is still under developpement. You shouldn't wait too long before", | ||||
" seeing some customisable buttons in those toolbar." | ||||
Jonathan Frederic
|
r17217 | ].join('\n'); | ||
var button_container = $(div); | ||||
Matthias BUSSONNIER
|
r9067 | var button = $('<div/>').button({label:'?'}) | ||
Jonathan Frederic
|
r17217 | .click(function(){simple_dialog('help',help_text); return false;}); | ||
Matthias BUSSONNIER
|
r9067 | button_container.append(button); | ||
Jonathan Frederic
|
r17217 | }; | ||
Matthias BUSSONNIER
|
r9067 | |||
Matthias BUSSONNIER
|
r17454 | var register = function (notebook) { | ||
Jonathan Frederic
|
r17217 | CellToolbar.register_callback('example.help',add_simple_dialog_button); | ||
example_preset.push('example.help'); | ||||
Matthias BUSSONNIER
|
r9057 | |||
Matthias BUSSONNIER
|
r17454 | CellToolbar.register_preset('Example',example_preset, notebook); | ||
Jonathan Frederic
|
r17217 | console.log('Example extension for metadata editing loaded.'); | ||
}; | ||||
return {'register': register}; | ||||
}); | ||||