##// END OF EJS Templates
Fixed cell toolbars
Jonathan Frederic -
Show More
@@ -4,8 +4,7
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'notebook/js/textcell',
7 ], function(IPython, $) {
8 ], function(IPython, $, textcell) {
9 "use strict";
8 "use strict";
10
9
11 var CellToolbar = function (options) {
10 var CellToolbar = function (options) {
@@ -176,13 +175,13 define([
176 * CellToolbar.register_preset('foo.foo_preset1', ['foo.c1', 'foo.c2', 'foo.c5'])
175 * CellToolbar.register_preset('foo.foo_preset1', ['foo.c1', 'foo.c2', 'foo.c5'])
177 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
176 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
178 */
177 */
179 CellToolbar.register_preset = function(name, preset_list) {
178 CellToolbar.register_preset = function(name, preset_list, notebook, events) {
180 CellToolbar._presets[name] = preset_list;
179 CellToolbar._presets[name] = preset_list;
181 this.events.trigger('preset_added.CellToolbar', {name: name});
180 events.trigger('preset_added.CellToolbar', {name: name});
182 // When "register_callback" is called by a custom extension, it may be executed after notebook is loaded.
181 // When "register_callback" is called by a custom extension, it may be executed after notebook is loaded.
183 // In that case, activate the preset if needed.
182 // In that case, activate the preset if needed.
184 if (this.notebook && this.notebook.metadata && this.notebook.metadata.celltoolbar === name)
183 if (notebook && notebook.metadata && notebook.metadata.celltoolbar === name)
185 this.activate_preset(name);
184 CellToolbar.activate_preset(name, events);
186 };
185 };
187
186
188
187
@@ -215,7 +214,7 define([
215 *
214 *
216 * CellToolbar.activate_preset('foo.foo_preset1');
215 * CellToolbar.activate_preset('foo.foo_preset1');
217 */
216 */
218 CellToolbar.activate_preset = function(preset_name){
217 CellToolbar.activate_preset = function(preset_name, events){
219 var preset = CellToolbar._presets[preset_name];
218 var preset = CellToolbar._presets[preset_name];
220
219
221 if(preset !== undefined){
220 if(preset !== undefined){
@@ -223,8 +222,8 define([
223 CellToolbar.rebuild_all();
222 CellToolbar.rebuild_all();
224 }
223 }
225
224
226 if (this.events) {
225 if (events) {
227 this.events.trigger('preset_activated.CellToolbar', {name: preset_name});
226 events.trigger('preset_activated.CellToolbar', {name: preset_name});
228 }
227 }
229 };
228 };
230
229
@@ -279,7 +278,7 define([
279 }
278 }
280
279
281 // If there are no controls or the cell is a rendered TextCell hide the toolbar.
280 // If there are no controls or the cell is a rendered TextCell hide the toolbar.
282 if (!this.ui_controls_list.length || (this.cell instanceof textcell.TextCell && this.cell.rendered)) {
281 if (!this.ui_controls_list.length || (this.cell_type != 'code' && this.cell.rendered)) {
283 this.hide();
282 this.hide();
284 } else {
283 } else {
285 this.show();
284 this.show();
@@ -1,31 +1,23
1 //----------------------------------------------------------------------------
1 // Copyright (c) IPython Development Team.
2 // Copyright (C) 2012 The IPython Development Team
2 // Distributed under the terms of the Modified BSD License.
3 //
3
4 // Distributed under the terms of the BSD License. The full license is in
4 define([
5 // the file COPYING, distributed as part of this software.
5 'jquery',
6 //----------------------------------------------------------------------------
6 'notebook/js/celltoolbar',
7
7 'base/js/dialog',
8 //============================================================================
8 ], function($, celltoolbar, dialog) {
9 // CellToolbar Default
10 //============================================================================
11
12 /**
13 * Example Use for the CellToolbar library
14 */
15 // IIFE without asignement, we don't modifiy the IPython namespace
16 (function (IPython) {
17 "use strict";
9 "use strict";
18
10
19 var CellToolbar = IPython.CellToolbar;
11 var CellToolbar = celltoolbar.CellToolbar;
20
12
21 var raw_edit = function(cell){
13 var raw_edit = function(cell){
22 IPython.dialog.edit_metadata(cell.metadata, function (md) {
14 dialog.edit_metadata(cell.metadata, function (md) {
23 cell.metadata = md;
15 cell.metadata = md;
24 });
16 });
25 };
17 };
26
18
27 var add_raw_edit_button = function(div, cell) {
19 var add_raw_edit_button = function(div, cell) {
28 var button_container = div;
20 var button_container = $(div);
29 var button = $('<button/>')
21 var button = $('<button/>')
30 .addClass("btn btn-default btn-xs")
22 .addClass("btn btn-default btn-xs")
31 .text("Edit Metadata")
23 .text("Edit Metadata")
@@ -36,11 +28,13
36 button_container.append(button);
28 button_container.append(button);
37 };
29 };
38
30
39 CellToolbar.register_callback('default.rawedit', add_raw_edit_button);
31 var register = function (notebook, events) {
40 var example_preset = [];
32 CellToolbar.register_callback('default.rawedit', add_raw_edit_button);
41 example_preset.push('default.rawedit');
33 var example_preset = [];
34 example_preset.push('default.rawedit');
42
35
43 CellToolbar.register_preset('Edit Metadata', example_preset);
36 CellToolbar.register_preset('Edit Metadata', example_preset, notebook, events);
44 console.log('Default extension for cell metadata editing loaded.');
37 console.log('Default extension for cell metadata editing loaded.');
45
38 };
46 }(IPython));
39 return {'register': register};
40 });
@@ -1,28 +1,20
1 //----------------------------------------------------------------------------
1 // Copyright (c) IPython Development Team.
2 // Copyright (C) 2012 The IPython Development Team
2 // Distributed under the terms of the Modified BSD License.
3 //
3
4 // Distributed under the terms of the BSD License. The full license is in
4 // Example Use for the CellToolbar library
5 // the file COPYING, distributed as part of this software.
5 // add the following to your custom.js to load
6 //----------------------------------------------------------------------------
6 // Celltoolbar UI for slideshow
7
7
8 //============================================================================
8 // ```
9 // CellToolbar Example
9 // $.getScript('/static/js/celltoolbarpresets/example.js');
10 //============================================================================
10 // ```
11
11 define([
12 /**
12 'jquery',
13 * Example Use for the CellToolbar library
13 'notebook/js/celltoolbar',
14 * add the following to your custom.js to load
14 ], function($, celltoolbar) {
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";
15 "use strict";
24
16
25 var CellToolbar = IPython.CellToolbar;
17 var CellToolbar = celltoolbar.CellToolbar;
26
18
27 var example_preset = [];
19 var example_preset = [];
28
20
@@ -32,32 +24,32
32 var fun = function(value){
24 var fun = function(value){
33 try{
25 try{
34 if(value){
26 if(value){
35 cell.code_mirror.setOption('readOnly','nocursor')
27 cell.code_mirror.setOption('readOnly','nocursor');
36 button.button('option','icons',{primary:'ui-icon-locked'})
28 button.button('option','icons',{primary:'ui-icon-locked'});
37 } else {
29 } else {
38 cell.code_mirror.setOption('readOnly',false)
30 cell.code_mirror.setOption('readOnly',false);
39 button.button('option','icons',{primary:'ui-icon-unlocked'})
31 button.button('option','icons',{primary:'ui-icon-unlocked'});
40 }
32 }
41 } catch(e){}
33 } catch(e){}
42
34
43 }
35 };
44 fun(cell.metadata.ro)
36 fun(cell.metadata.ro);
45 button.click(function(){
37 button.click(function(){
46 var v = cell.metadata.ro;
38 var v = cell.metadata.ro;
47 var locked = !v;
39 var locked = !v;
48 cell.metadata.ro = locked;
40 cell.metadata.ro = locked;
49 fun(locked)
41 fun(locked);
50 })
42 })
51 .css('height','16px')
43 .css('height','16px')
52 .css('width','35px');
44 .css('width','35px');
53 button_container.append(button);
45 button_container.append(button);
54 }
46 };
55
47
56 CellToolbar.register_callback('example.lock',simple_button);
48 CellToolbar.register_callback('example.lock',simple_button);
57 example_preset.push('example.lock');
49 example_preset.push('example.lock');
58
50
59 var toggle_test = function(div, cell) {
51 var toggle_test = function(div, cell) {
60 var button_container = $(div)
52 var button_container = $(div);
61 var button = $('<div/>')
53 var button = $('<div/>')
62 .button({label:String(cell.metadata.foo)}).
54 .button({label:String(cell.metadata.foo)}).
63 css('width','65px');
55 css('width','65px');
@@ -65,9 +57,9
65 var v = cell.metadata.foo;
57 var v = cell.metadata.foo;
66 cell.metadata.foo = !v;
58 cell.metadata.foo = !v;
67 button.button("option","label",String(!v));
59 button.button("option","label",String(!v));
68 })
60 });
69 button_container.append(button);
61 button_container.append(button);
70 }
62 };
71
63
72 CellToolbar.register_callback('example.toggle',toggle_test);
64 CellToolbar.register_callback('example.toggle',toggle_test);
73 example_preset.push('example.toggle');
65 example_preset.push('example.toggle');
@@ -76,16 +68,16
76 // setter
68 // setter
77 function(cell, value){
69 function(cell, value){
78 // we check that the slideshow namespace exist and create it if needed
70 // we check that the slideshow namespace exist and create it if needed
79 if (cell.metadata.yn_test == undefined){cell.metadata.yn_test = {}}
71 if (cell.metadata.yn_test === undefined){cell.metadata.yn_test = {};}
80 // set the value
72 // set the value
81 cell.metadata.yn_test.value = value
73 cell.metadata.yn_test.value = value;
82 },
74 },
83 //geter
75 //geter
84 function(cell){ var ns = cell.metadata.yn_test;
76 function(cell){ var ns = cell.metadata.yn_test;
85 // if the slideshow namespace does not exist return `undefined`
77 // if the slideshow namespace does not exist return `undefined`
86 // (will be interpreted as `false` by checkbox) otherwise
78 // (will be interpreted as `false` by checkbox) otherwise
87 // return the value
79 // return the value
88 return (ns == undefined)? undefined: ns.value
80 return (ns === undefined)? undefined: ns.value;
89 }
81 }
90 );
82 );
91
83
@@ -103,16 +95,16
103 // setter
95 // setter
104 function(cell,value){
96 function(cell,value){
105 // we check that the slideshow namespace exist and create it if needed
97 // we check that the slideshow namespace exist and create it if needed
106 if (cell.metadata.test == undefined){cell.metadata.test = {}}
98 if (cell.metadata.test === undefined){cell.metadata.test = {};}
107 // set the value
99 // set the value
108 cell.metadata.test.slide_type = value
100 cell.metadata.test.slide_type = value;
109 },
101 },
110 //geter
102 //geter
111 function(cell){ var ns = cell.metadata.test;
103 function(cell){ var ns = cell.metadata.test;
112 // if the slideshow namespace does not exist return `undefined`
104 // if the slideshow namespace does not exist return `undefined`
113 // (will be interpreted as `false` by checkbox) otherwise
105 // (will be interpreted as `false` by checkbox) otherwise
114 // return the value
106 // return the value
115 return (ns == undefined)? undefined: ns.slide_type
107 return (ns === undefined)? undefined: ns.slide_type;
116 });
108 });
117
109
118 CellToolbar.register_callback('example.select',select_test);
110 CellToolbar.register_callback('example.select',select_test);
@@ -120,7 +112,7
120
112
121 var simple_dialog = function(title,text){
113 var simple_dialog = function(title,text){
122 var dlg = $('<div/>').attr('title',title)
114 var dlg = $('<div/>').attr('title',title)
123 .append($('<p/>').text(text))
115 .append($('<p/>').text(text));
124 $(dlg).dialog({
116 $(dlg).dialog({
125 autoOpen: true,
117 autoOpen: true,
126 height: 300,
118 height: 300,
@@ -131,24 +123,26
131 $(this).remove();
123 $(this).remove();
132 }
124 }
133 });
125 });
134 }
126 };
135
127
136 var add_simple_dialog_button = function(div, cell) {
128 var add_simple_dialog_button = function(div, cell) {
137 var help_text = ["This is the Metadata editting UI.",
129 var help_text = ["This is the Metadata editting UI.",
138 "It heavily rely on plugin to work ",
130 "It heavily rely on plugin to work ",
139 "and is still under developpement. You shouldn't wait too long before",
131 "and is still under developpement. You shouldn't wait too long before",
140 " seeing some customisable buttons in those toolbar."
132 " seeing some customisable buttons in those toolbar."
141 ].join('\n')
133 ].join('\n');
142 var button_container = $(div)
134 var button_container = $(div);
143 var button = $('<div/>').button({label:'?'})
135 var button = $('<div/>').button({label:'?'})
144 .click(function(){simple_dialog('help',help_text); return false;})
136 .click(function(){simple_dialog('help',help_text); return false;});
145 button_container.append(button);
137 button_container.append(button);
146 }
138 };
147
148 CellToolbar.register_callback('example.help',add_simple_dialog_button)
149 example_preset.push('example.help')
150
139
151 CellToolbar.register_preset('Example',example_preset);
140 var register = function (notebook, events) {
152 console.log('Example extension for metadata editing loaded.');
141 CellToolbar.register_callback('example.help',add_simple_dialog_button);
142 example_preset.push('example.help');
153
143
154 }(IPython));
144 CellToolbar.register_preset('Example',example_preset, notebook, events);
145 console.log('Example extension for metadata editing loaded.');
146 };
147 return {'register': register};
148 });
@@ -1,18 +1,15
1 //----------------------------------------------------------------------------
1 // Copyright (c) IPython Development Team.
2 // Copyright (C) 2012 The IPython Development Team
2 // Distributed under the terms of the Modified BSD License.
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
3
8 //============================================================================
4 define([
9 // CellToolbar Example
5 'jquery',
10 //============================================================================
6 'notebook/js/celltoolbar',
7 'base/js/dialog',
8 'base/js/keyboard',
9 ], function($, celltoolbar, dialog, keyboard) {
10 "use strict";
11
11
12 (function(IPython) {
12 var CellToolbar = celltoolbar.CellToolbar;
13 "use strict";
14
15 var CellToolbar = IPython.CellToolbar;
16 var raw_cell_preset = [];
13 var raw_cell_preset = [];
17
14
18 var select_type = CellToolbar.utils.select_ui_generator([
15 var select_type = CellToolbar.utils.select_ui_generator([
@@ -39,7 +36,7
39 $('<input/>').attr('type','text').attr('size','25')
36 $('<input/>').attr('type','text').attr('size','25')
40 .val(cell.metadata.raw_mimetype || "-")
37 .val(cell.metadata.raw_mimetype || "-")
41 );
38 );
42 IPython.dialog.modal({
39 dialog.modal({
43 title: "Raw Cell MIME Type",
40 title: "Raw Cell MIME Type",
44 body: dialog,
41 body: dialog,
45 buttons : {
42 buttons : {
@@ -57,7 +54,7
57 var that = $(this);
54 var that = $(this);
58 // Upon ENTER, click the OK button.
55 // Upon ENTER, click the OK button.
59 that.find('input[type="text"]').keydown(function (event, ui) {
56 that.find('input[type="text"]').keydown(function (event, ui) {
60 if (event.which === IPython.keyboard.keycodes.enter) {
57 if (event.which === keyboard.keycodes.enter) {
61 that.find('.btn-primary').first().click();
58 that.find('.btn-primary').first().click();
62 return false;
59 return false;
63 }
60 }
@@ -77,11 +74,13
77 "Raw NBConvert Format"
74 "Raw NBConvert Format"
78 );
75 );
79
76
80 CellToolbar.register_callback('raw_cell.select', select_type, ['raw']);
77 var register = function (notebook, events) {
81
78 CellToolbar.register_callback('raw_cell.select', select_type, ['raw']);
82 raw_cell_preset.push('raw_cell.select');
79 raw_cell_preset.push('raw_cell.select');
83
80
84 CellToolbar.register_preset('Raw Cell Format', raw_cell_preset);
81 CellToolbar.register_preset('Raw Cell Format', raw_cell_preset, notebook, events);
85 console.log('Raw Cell Format toolbar preset loaded.');
82 console.log('Raw Cell Format toolbar preset loaded.');
83 };
84 return {'register': register};
86
85
87 }(IPython));
86 });
@@ -1,19 +1,14
1 //----------------------------------------------------------------------------
1 // Copyright (c) IPython Development Team.
2 // Copyright (C) 2012 The IPython Development Team
2 // Distributed under the terms of the Modified BSD License.
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
3
8 //============================================================================
4 define([
9 //CellToolbar Example
5 'jquery',
10 //============================================================================
6 'notebook/js/celltoolbar',
11
7 ], function($, celltoolbar) {
12 // IIFE without asignement, we don't modifiy the IPython namespace
13 (function (IPython) {
14 "use strict";
8 "use strict";
15
9
16 var CellToolbar = IPython.CellToolbar;
10
11 var CellToolbar = celltoolbar.CellToolbar;
17 var slideshow_preset = [];
12 var slideshow_preset = [];
18
13
19 var select_type = CellToolbar.utils.select_ui_generator([
14 var select_type = CellToolbar.utils.select_ui_generator([
@@ -27,24 +22,25
27 // setter
22 // setter
28 function(cell, value){
23 function(cell, value){
29 // we check that the slideshow namespace exist and create it if needed
24 // we check that the slideshow namespace exist and create it if needed
30 if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
25 if (cell.metadata.slideshow === undefined){cell.metadata.slideshow = {};}
31 // set the value
26 // set the value
32 cell.metadata.slideshow.slide_type = value
27 cell.metadata.slideshow.slide_type = value;
33 },
28 },
34 //geter
29 //geter
35 function(cell){ var ns = cell.metadata.slideshow;
30 function(cell){ var ns = cell.metadata.slideshow;
36 // if the slideshow namespace does not exist return `undefined`
31 // if the slideshow namespace does not exist return `undefined`
37 // (will be interpreted as `false` by checkbox) otherwise
32 // (will be interpreted as `false` by checkbox) otherwise
38 // return the value
33 // return the value
39 return (ns == undefined)? undefined: ns.slide_type
34 return (ns === undefined)? undefined: ns.slide_type;
40 },
35 },
41 "Slide Type");
36 "Slide Type");
42
37
43 CellToolbar.register_callback('slideshow.select',select_type);
38 var register = function (notebook, events) {
44
39 CellToolbar.register_callback('slideshow.select',select_type);
45 slideshow_preset.push('slideshow.select');
40 slideshow_preset.push('slideshow.select');
46
47 CellToolbar.register_preset('Slideshow',slideshow_preset);
48 console.log('Slideshow extension for metadata editing loaded.');
49
41
50 }(IPython));
42 CellToolbar.register_preset('Slideshow',slideshow_preset, notebook, events);
43 console.log('Slideshow extension for metadata editing loaded.');
44 };
45 return {'register': register};
46 });
@@ -135,7 +135,7 define([
135 notebook: this.notebook});
135 notebook: this.notebook});
136 inner_cell.append(this.celltoolbar.element);
136 inner_cell.append(this.celltoolbar.element);
137 var input_area = $('<div/>').addClass('input_area');
137 var input_area = $('<div/>').addClass('input_area');
138 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
138 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
139 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
139 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
140 inner_cell.append(input_area);
140 inner_cell.append(input_area);
141 input.append(prompt).append(inner_cell);
141 input.append(prompt).append(inner_cell);
@@ -8,7 +8,6
8
8
9 CodeMirror.requireMode('gfm', function(){
9 CodeMirror.requireMode('gfm', function(){
10 CodeMirror.requireMode('stex', function(){
10 CodeMirror.requireMode('stex', function(){
11 console.log('defining custom mode...');
12 CodeMirror.defineMode("ipythongfm", function(config, parserConfig) {
11 CodeMirror.defineMode("ipythongfm", function(config, parserConfig) {
13
12
14 var gfm_mode = CodeMirror.getMode(config, "gfm");
13 var gfm_mode = CodeMirror.getMode(config, "gfm");
@@ -158,6 +158,7 define([
158 .addClass('form-control select-xs')
158 .addClass('form-control select-xs')
159 .append($('<option/>').attr('value', '').text('None'));
159 .append($('<option/>').attr('value', '').text('None'));
160 this.element.append(label).append(select);
160 this.element.append(label).append(select);
161 var that = this;
161 select.change(function() {
162 select.change(function() {
162 var val = $(this).val();
163 var val = $(this).val();
163 if (val ==='') {
164 if (val ==='') {
@@ -165,7 +166,7 define([
165 delete that.notebook.metadata.celltoolbar;
166 delete that.notebook.metadata.celltoolbar;
166 } else {
167 } else {
167 celltoolbar.CellToolbar.global_show();
168 celltoolbar.CellToolbar.global_show();
168 celltoolbar.CellToolbar.activate_preset(val);
169 celltoolbar.CellToolbar.activate_preset(val, that.events);
169 that.notebook.metadata.celltoolbar = val;
170 that.notebook.metadata.celltoolbar = val;
170 }
171 }
171 });
172 });
@@ -14,6 +14,9 define([
14 'notebook/js/mathjaxutils',
14 'notebook/js/mathjaxutils',
15 'base/js/keyboard',
15 'base/js/keyboard',
16 'notebook/js/tooltip',
16 'notebook/js/tooltip',
17 'notebook/js/celltoolbarpresets/default',
18 'notebook/js/celltoolbarpresets/rawcell',
19 'notebook/js/celltoolbarpresets/slideshow',
17 ], function (
20 ], function (
18 IPython,
21 IPython,
19 $,
22 $,
@@ -26,7 +29,10 define([
26 marked,
29 marked,
27 mathjaxutils,
30 mathjaxutils,
28 keyboard,
31 keyboard,
29 tooltip
32 tooltip,
33 default_celltoolbar,
34 rawcell_celltoolbar,
35 slideshow_celltoolbar
30 ) {
36 ) {
31
37
32 var Notebook = function (selector, options) {
38 var Notebook = function (selector, options) {
@@ -115,6 +121,11 define([
115 this.save_notebook = function() { // don't allow save until notebook_loaded
121 this.save_notebook = function() { // don't allow save until notebook_loaded
116 this.save_notebook_error(null, null, "Load failed, save is disabled");
122 this.save_notebook_error(null, null, "Load failed, save is disabled");
117 };
123 };
124
125 // Trigger cell toolbar registration.
126 default_celltoolbar.register(this, options.events);
127 rawcell_celltoolbar.register(this, options.events);
128 slideshow_celltoolbar.register(this, options.events);
118 };
129 };
119
130
120 /**
131 /**
@@ -2201,7 +2212,7 define([
2201 // load toolbar state
2212 // load toolbar state
2202 if (this.metadata.celltoolbar) {
2213 if (this.metadata.celltoolbar) {
2203 celltoolbar.CellToolbar.global_show();
2214 celltoolbar.CellToolbar.global_show();
2204 celltoolbar.CellToolbar.activate_preset(this.metadata.celltoolbar);
2215 celltoolbar.CellToolbar.activate_preset(this.metadata.celltoolbar, this.events);
2205 } else {
2216 } else {
2206 celltoolbar.CellToolbar.global_hide();
2217 celltoolbar.CellToolbar.global_hide();
2207 }
2218 }
General Comments 0
You need to be logged in to leave comments. Login now