##// END OF EJS Templates
Merge pull request #7439 from minrk/toolbar-events...
Thomas Kluyver -
r19897:4a46872f merge
parent child Browse files
Show More
@@ -1,161 +1,162 b''
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 this.notebook.keyboard_manager.register_events(this.element);
28 Object.seal(this);
29 Object.seal(this);
29 };
30 };
30
31
31 MainToolBar.prototype = Object.create(toolbar.ToolBar.prototype);
32 MainToolBar.prototype = Object.create(toolbar.ToolBar.prototype);
32
33
33 MainToolBar.prototype._make = function () {
34 MainToolBar.prototype._make = function () {
34 var grps = [
35 var grps = [
35 [
36 [
36 ['ipython.save-notebook'],
37 ['ipython.save-notebook'],
37 'save-notbook'
38 'save-notbook'
38 ],
39 ],
39 [
40 [
40 ['ipython.insert-cell-after'],
41 ['ipython.insert-cell-after'],
41 'insert_above_below'],
42 'insert_above_below'],
42 [
43 [
43 ['ipython.cut-selected-cell',
44 ['ipython.cut-selected-cell',
44 'ipython.copy-selected-cell',
45 'ipython.copy-selected-cell',
45 'ipython.paste-cell-after'
46 'ipython.paste-cell-after'
46 ] ,
47 ] ,
47 'cut_copy_paste'],
48 'cut_copy_paste'],
48 [
49 [
49 ['ipython.move-selected-cell-up',
50 ['ipython.move-selected-cell-up',
50 'ipython.move-selected-cell-down'
51 'ipython.move-selected-cell-down'
51 ],
52 ],
52 'move_up_down'],
53 'move_up_down'],
53 [ ['ipython.run-select-next',
54 [ ['ipython.run-select-next',
54 'ipython.interrupt-kernel',
55 'ipython.interrupt-kernel',
55 'ipython.restart-kernel'
56 'ipython.restart-kernel'
56 ],
57 ],
57 'run_int'],
58 'run_int'],
58 ['<add_celltype_list>'],
59 ['<add_celltype_list>'],
59 ['<add_celltoolbar_list>']
60 ['<add_celltoolbar_list>']
60 ];
61 ];
61 this.construct(grps);
62 this.construct(grps);
62 };
63 };
63
64
64 // add a cell type drop down to the maintoolbar.
65 // add a cell type drop down to the maintoolbar.
65 // triggered when the pseudo action `<add_celltype_list>` is
66 // triggered when the pseudo action `<add_celltype_list>` is
66 // encountered when building a toolbar.
67 // encountered when building a toolbar.
67 MainToolBar.prototype._pseudo_actions.add_celltype_list = function () {
68 MainToolBar.prototype._pseudo_actions.add_celltype_list = function () {
68 var that = this;
69 var that = this;
69 var sel = $('<select/>')
70 var sel = $('<select/>')
70 .attr('id','cell_type')
71 .attr('id','cell_type')
71 .addClass('form-control select-xs')
72 .addClass('form-control select-xs')
72 .append($('<option/>').attr('value','code').text('Code'))
73 .append($('<option/>').attr('value','code').text('Code'))
73 .append($('<option/>').attr('value','markdown').text('Markdown'))
74 .append($('<option/>').attr('value','markdown').text('Markdown'))
74 .append($('<option/>').attr('value','raw').text('Raw NBConvert'))
75 .append($('<option/>').attr('value','raw').text('Raw NBConvert'))
75 .append($('<option/>').attr('value','heading').text('Heading'));
76 .append($('<option/>').attr('value','heading').text('Heading'));
76 this.events.on('selected_cell_type_changed.Notebook', function (event, data) {
77 this.events.on('selected_cell_type_changed.Notebook', function (event, data) {
77 if (data.cell_type === 'heading') {
78 if (data.cell_type === 'heading') {
78 sel.val('Markdown');
79 sel.val('Markdown');
79 } else {
80 } else {
80 sel.val(data.cell_type);
81 sel.val(data.cell_type);
81 }
82 }
82 });
83 });
83 sel.change(function () {
84 sel.change(function () {
84 var cell_type = $(this).val();
85 var cell_type = $(this).val();
85 switch (cell_type) {
86 switch (cell_type) {
86 case 'code':
87 case 'code':
87 that.notebook.to_code();
88 that.notebook.to_code();
88 break;
89 break;
89 case 'markdown':
90 case 'markdown':
90 that.notebook.to_markdown();
91 that.notebook.to_markdown();
91 break;
92 break;
92 case 'raw':
93 case 'raw':
93 that.notebook.to_raw();
94 that.notebook.to_raw();
94 break;
95 break;
95 case 'heading':
96 case 'heading':
96 that.notebook._warn_heading();
97 that.notebook._warn_heading();
97 that.notebook.to_heading();
98 that.notebook.to_heading();
98 sel.val('markdown');
99 sel.val('markdown');
99 break;
100 break;
100 default:
101 default:
101 console.log("unrecognized cell type:", cell_type);
102 console.log("unrecognized cell type:", cell_type);
102 }
103 }
103 });
104 });
104 return sel;
105 return sel;
105
106
106 };
107 };
107
108
108 MainToolBar.prototype._pseudo_actions.add_celltoolbar_list = function () {
109 MainToolBar.prototype._pseudo_actions.add_celltoolbar_list = function () {
109 var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:');
110 var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:');
110 var select = $('<select/>')
111 var select = $('<select/>')
111 .attr('id', 'ctb_select')
112 .attr('id', 'ctb_select')
112 .addClass('form-control select-xs')
113 .addClass('form-control select-xs')
113 .append($('<option/>').attr('value', '').text('None'));
114 .append($('<option/>').attr('value', '').text('None'));
114 var that = this;
115 var that = this;
115 select.change(function() {
116 select.change(function() {
116 var val = $(this).val();
117 var val = $(this).val();
117 if (val ==='') {
118 if (val ==='') {
118 celltoolbar.CellToolbar.global_hide();
119 celltoolbar.CellToolbar.global_hide();
119 delete that.notebook.metadata.celltoolbar;
120 delete that.notebook.metadata.celltoolbar;
120 } else {
121 } else {
121 celltoolbar.CellToolbar.global_show();
122 celltoolbar.CellToolbar.global_show();
122 celltoolbar.CellToolbar.activate_preset(val, that.events);
123 celltoolbar.CellToolbar.activate_preset(val, that.events);
123 that.notebook.metadata.celltoolbar = val;
124 that.notebook.metadata.celltoolbar = val;
124 }
125 }
125 });
126 });
126 // Setup the currently registered presets.
127 // Setup the currently registered presets.
127 var presets = celltoolbar.CellToolbar.list_presets();
128 var presets = celltoolbar.CellToolbar.list_presets();
128 for (var i=0; i<presets.length; i++) {
129 for (var i=0; i<presets.length; i++) {
129 var name = presets[i];
130 var name = presets[i];
130 select.append($('<option/>').attr('value', name).text(name));
131 select.append($('<option/>').attr('value', name).text(name));
131 }
132 }
132 // Setup future preset registrations.
133 // Setup future preset registrations.
133 this.events.on('preset_added.CellToolbar', function (event, data) {
134 this.events.on('preset_added.CellToolbar', function (event, data) {
134 var name = data.name;
135 var name = data.name;
135 select.append($('<option/>').attr('value', name).text(name));
136 select.append($('<option/>').attr('value', name).text(name));
136 });
137 });
137 this.events.on('unregistered_preset.CellToolbar', function (event, data) {
138 this.events.on('unregistered_preset.CellToolbar', function (event, data) {
138 if (select.val() === data.name){
139 if (select.val() === data.name){
139 select.val('');
140 select.val('');
140 celltoolbar.CellToolbar.global_hide();
141 celltoolbar.CellToolbar.global_hide();
141 delete that.notebook.metadata.celltoolbar;
142 delete that.notebook.metadata.celltoolbar;
142 }
143 }
143 select.find("option[value='"+name+"']" ).remove();
144 select.find("option[value='"+name+"']" ).remove();
144 });
145 });
145 // Update select value when a preset is activated.
146 // Update select value when a preset is activated.
146 this.events.on('preset_activated.CellToolbar', function (event, data) {
147 this.events.on('preset_activated.CellToolbar', function (event, data) {
147 if (select.val() !== data.name){
148 if (select.val() !== data.name){
148 select.val(data.name);
149 select.val(data.name);
149 }
150 }
150 });
151 });
151
152
152 var wrapper = $('<div/>').addClass('btn-group');
153 var wrapper = $('<div/>').addClass('btn-group');
153 wrapper.append(label).append(select);
154 wrapper.append(label).append(select);
154 return wrapper;
155 return wrapper;
155 };
156 };
156
157
157 // Backwards compatibility.
158 // Backwards compatibility.
158 IPython.MainToolBar = MainToolBar;
159 IPython.MainToolBar = MainToolBar;
159
160
160 return {'MainToolBar': MainToolBar};
161 return {'MainToolBar': MainToolBar};
161 });
162 });
General Comments 0
You need to be logged in to leave comments. Login now