##// END OF EJS Templates
Update Widget Events.ipynb
Update Widget Events.ipynb

File last commit:

r19555:f622d016
r20620:313bf26c
Show More
toolbar.js
161 lines | 5.1 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Progress...
r17196 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
Brian Granger
First draft of toolbar....
r5993
Jonathan Frederic
Progress...
r17196 define([
'base/js/namespace',
Bussonnier Matthias
Fix comments...
r19425 'jquery'
Jonathan Frederic
Progress...
r17196 ], function(IPython, $) {
Matthias BUSSONNIER
"use strict" in most (if not all) our javascript...
r12103 "use strict";
Brian Granger
First draft of toolbar....
r5993
Matthias BUSSONNIER
document base of toolbar
r8769 /**
* A generic toolbar on which one can add button
* @class ToolBar
* @constructor
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 * @param {Dom_object} selector
Matthias BUSSONNIER
document base of toolbar
r8769 */
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 var ToolBar = function (selector, options) {
Brian Granger
First draft of toolbar....
r5993 this.selector = selector;
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 this.actions = (options||{}).actions;
Brian Granger
First draft of toolbar....
r5993 if (this.selector !== undefined) {
this.element = $(selector);
this.style();
}
};
Bussonnier Matthias
make pseudo action in their own dcit....
r19447 ToolBar.prototype._pseudo_actions={};
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423
Bussonnier Matthias
make pseudo action in their own dcit....
r19447
ToolBar.prototype.construct = function (config) {
Jason Grout
Fix javascript iteration through array...
r19555 for(var k=0; k<config.length; k++) {
this.add_buttons_group(config[k][0],config[k][1]);
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 }
};
Matthias BUSSONNIER
document base of toolbar
r8769 /**
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 * Add a group of button into the current toolbar.
Matthias BUSSONNIER
document base of toolbar
r8769 *
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 * Use a [dict of [list of action name]] to trigger
* on click to the button
Matthias BUSSONNIER
document base of toolbar
r8769 *
* @example
*
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 * ... todo, maybe use a list of list to keep ordering.
*
* [
* [
* [
* action_name_1,
* action_name_2,
* action_name_3,
* ],
* optional_group_name
* ],
* ...
* ]
*
* For backward compatibility this also support the
Jason Grout
Fix javascript iteration through array...
r19555 * old methods of adding a button directly bound to callbacks:
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 * @example
* # deprecate, do not use
Matthias BUSSONNIER
Add a per cell toolbar....
r9055 * IPython.toolbar.add_buttons_group([
Matthias BUSSONNIER
document base of toolbar
r8769 * {
* label:'my button',
MinRK
bootstrap toolbar
r10889 * icon:'icon-hdd',
Matthias BUSSONNIER
Method to show hide linenumber of cell...
r9542 * callback:function(){alert('hoho')},
Matthias BUSSONNIER
document base of toolbar
r8769 * id : 'my_button_id', // this is optional
* },
* {
* label:'my second button',
MinRK
bootstrap toolbar
r10889 * icon:'icon-play',
Matthias BUSSONNIER
document base of toolbar
r8769 * callback:function(){alert('be carefull I cut')}
* }
* ],
* "my_button_group_id"
* )
*
* @method add_buttons_group
* @param list {List}
* List of button of the group, with the following paramter for each :
* @param list.label {string} text to show on button hover
MinRK
bootstrap toolbar
r10889 * @param list.icon {string} icon to choose from [Font Awesome](http://fortawesome.github.io/Font-Awesome)
Matthias BUSSONNIER
document base of toolbar
r8769 * @param list.callback {function} function to be called on button click
* @param [list.id] {String} id to give to the button
* @param [group_id] {String} optionnal id to give to the group
*
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 *
* for private usage, the key can also be strings starting with '<' and ending with '>' to inject custom element that cannot
* be bound to an action.
*
Matthias BUSSONNIER
document base of toolbar
r8769 */
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 // TODO JUPYTER:
// get rid of legacy code that handle things that are not actions.
Matthias BUSSONNIER
reorder methods and fix typo
r8212 ToolBar.prototype.add_buttons_group = function (list, group_id) {
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 // handle custom call of pseudoaction binding.
Bussonnier Matthias
Fix comments...
r19425 if(typeof(list) === 'string' && list.slice(0,1) === '<' && list.slice(-1) === '>'){
var _pseudo_action;
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 try{
Bussonnier Matthias
Fix comments...
r19425 _pseudo_action = list.slice(1,-1);
Bussonnier Matthias
return elements instead of append them directly...
r19481 this.element.append(this._pseudo_actions[_pseudo_action].call(this));
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 } catch (e) {
Matthias Bussonnier
adresses Min comments x2
r19427 console.warn('ouch, calling ', _pseudo_action, 'does not seem to work...:', e);
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 }
return ;
}
var that = this;
MinRK
bootstrap toolbar
r10889 var btn_group = $('<div/>').addClass("btn-group");
MinRK
never use `for (var i in array)`...
r16226 if( group_id !== undefined ) {
MinRK
bootstrap toolbar
r10889 btn_group.attr('id',group_id);
Matthias BUSSONNIER
jslint 1
r8209 }
MinRK
never use `for (var i in array)`...
r16226 for(var i=0; i < list.length; i++) {
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423
Bussonnier Matthias
Fix comments...
r19425 // IIFE because javascript don't have loop scope so
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 // action_name would otherwise be the same on all iteration
// of the loop
Bussonnier Matthias
make pseudo action in their own dcit....
r19447 (function(i,list){
var el = list[i];
var action_name;
var action;
if(typeof(el) === 'string'){
action = that.actions.get(el);
action_name = el;
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423
Bussonnier Matthias
make pseudo action in their own dcit....
r19447 }
var button = $('<button/>')
.addClass('btn btn-default')
.attr("title", el.label||action.help)
.append(
$("<i/>").addClass(el.icon||action.icon).addClass('fa')
);
var id = el.id;
if( id !== undefined ){
button.attr('id',id);
}
button.attr('data-jupyter-action', action_name);
var fun = el.callback|| function(){
that.actions.call(action_name);
};
button.click(fun);
btn_group.append(button);
})(i,list);
Matthias Bussonnier
Add notion of actions to celltoolbar...
r19423 // END IIFE
Matthias BUSSONNIER
Allow toolbar construction in js...
r7832 }
MinRK
bootstrap toolbar
r10889 $(this.selector).append(btn_group);
Matthias BUSSONNIER
jslint 1
r8209 };
Brian Granger
First draft of toolbar....
r5993
ToolBar.prototype.style = function () {
Matthias BUSSONNIER
move tolbar styling to css
r17434 this.element.addClass('toolbar');
Brian Granger
First draft of toolbar....
r5993 };
Matthias BUSSONNIER
document base of toolbar
r8769 /**
* Show and hide toolbar
* @method toggle
*/
Brian Granger
Further work on the toolbar UI....
r5994 ToolBar.prototype.toggle = function () {
this.element.toggle();
Brian Granger
First draft of toolbar....
r5993 };
Matthias BUSSONNIER
comment typo
r17435 // Backwards compatibility.
Brian Granger
First draft of toolbar....
r5993 IPython.ToolBar = ToolBar;
Jonathan Frederic
Return dicts instead of classes,...
r17201 return {'ToolBar': ToolBar};
Jonathan Frederic
Progress...
r17196 });