##// END OF EJS Templates
note to self
Bussonnier Matthias -
Show More
@@ -1,152 +1,149
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'require',
6 6 'base/js/namespace',
7 7 'jquery',
8 8 './toolbar',
9 9 './celltoolbar',
10 10 ], function(require, IPython, $, toolbar, celltoolbar) {
11 11 "use strict";
12 12
13 13 var MainToolBar = function (selector, options) {
14 14 /**
15 15 * Constructor
16 16 *
17 17 * Parameters:
18 18 * selector: string
19 19 * options: dictionary
20 20 * Dictionary of keyword arguments.
21 21 * events: $(Events) instance
22 22 * notebook: Notebook instance
23 23 **/
24 24 toolbar.ToolBar.apply(this, [selector, options] );
25 25 this.events = options.events;
26 26 this.notebook = options.notebook;
27 27 this._make();
28 28 Object.seal(this);
29 29 };
30 30
31 31 MainToolBar.prototype = Object.create(toolbar.ToolBar.prototype);
32 32
33 // thought, this might not be the best way as dict might not keep the right order.
34 // Might want to put the group name as second to make it optional
35 //
36 33 MainToolBar.prototype._make = function () {
37 34 var grps = [
38 35 [
39 36 ['ipython.save-notebook'],
40 37 'save-notbook'
41 38 ],
42 39 [
43 40 ['ipython.insert-cell-after'],
44 41 'insert_above_below'],
45 42 [
46 43 ['ipython.cut-selected-cell',
47 44 'ipython.copy-selected-cell',
48 45 'ipython.paste-cell-after'
49 46 ] ,
50 47 'cut_copy_paste'],
51 48 [
52 49 ['ipython.move-selected-cell-up',
53 50 'ipython.move-selected-cell-down'
54 51 ],
55 52 'move_up_down'],
56 53 [ ['ipython.run-select-next',
57 54 'ipython.interrupt-kernel',
58 55 'ipython.restart-kernel',
59 56 ],
60 57 'run_int'],
61 58 ['<add_celltype_list>'],
62 59 ['<add_celltoolbar_list>'],
63 60 ];
64 61 this.construct(grps);
65 62 };
66 63
67 64 // add a cell type drop down to the maintoolbar.
68 65 // triggered when the pseudo action `<add_celltype_list>` is
69 66 // encountered when building a toolbar.
70 67 MainToolBar.prototype.add_celltype_list = function () {
71 68 var that = this;
72 69 var sel = $('<select/>')
73 70 .attr('id','cell_type')
74 71 .addClass('form-control select-xs')
75 72 .append($('<option/>').attr('value','code').text('Code'))
76 73 .append($('<option/>').attr('value','markdown').text('Markdown'))
77 74 .append($('<option/>').attr('value','raw').text('Raw NBConvert'))
78 75 .append($('<option/>').attr('value','heading').text('Heading'));
79 76 this.events.on('selected_cell_type_changed.Notebook', function (event, data) {
80 77 if (data.cell_type === 'heading') {
81 78 sel.val('Markdown');
82 79 } else {
83 80 sel.val(data.cell_type);
84 81 }
85 82 });
86 83 sel.change(function () {
87 84 var cell_type = $(this).val();
88 85 switch (cell_type) {
89 86 case 'code':
90 87 that.notebook.to_code();
91 88 break;
92 89 case 'markdown':
93 90 that.notebook.to_markdown();
94 91 break;
95 92 case 'raw':
96 93 that.notebook.to_raw();
97 94 break;
98 95 case 'heading':
99 96 that.notebook._warn_heading();
100 97 that.notebook.to_heading();
101 98 sel.val('markdown')
102 99 break;
103 100 default:
104 101 console.log("unrecognized cell type:", cell_type);
105 102 }
106 103 });
107 104 this.element.append(sel);
108 105
109 106 };
110 107
111 108 MainToolBar.prototype.add_celltoolbar_list = function () {
112 109 var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:');
113 110 var select = $('<select/>')
114 111 .attr('id', 'ctb_select')
115 112 .addClass('form-control select-xs')
116 113 .append($('<option/>').attr('value', '').text('None'));
117 114 this.element.append(label).append(select);
118 115 var that = this;
119 116 select.change(function() {
120 117 var val = $(this).val();
121 118 if (val ==='') {
122 119 celltoolbar.CellToolbar.global_hide();
123 120 delete that.notebook.metadata.celltoolbar;
124 121 } else {
125 122 celltoolbar.CellToolbar.global_show();
126 123 celltoolbar.CellToolbar.activate_preset(val, that.events);
127 124 that.notebook.metadata.celltoolbar = val;
128 125 }
129 126 });
130 127 // Setup the currently registered presets.
131 128 var presets = celltoolbar.CellToolbar.list_presets();
132 129 for (var i=0; i<presets.length; i++) {
133 130 var name = presets[i];
134 131 select.append($('<option/>').attr('value', name).text(name));
135 132 }
136 133 // Setup future preset registrations.
137 134 this.events.on('preset_added.CellToolbar', function (event, data) {
138 135 var name = data.name;
139 136 select.append($('<option/>').attr('value', name).text(name));
140 137 });
141 138 // Update select value when a preset is activated.
142 139 this.events.on('preset_activated.CellToolbar', function (event, data) {
143 140 if (select.val() !== data.name)
144 141 select.val(data.name);
145 142 });
146 143 };
147 144
148 145 // Backwards compatibility.
149 146 IPython.MainToolBar = MainToolBar;
150 147
151 148 return {'MainToolBar': MainToolBar};
152 149 });
General Comments 0
You need to be logged in to leave comments. Login now