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