toolbar.js
161 lines
| 5.2 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17196 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Brian Granger
|
r5993 | |||
Jonathan Frederic
|
r17196 | define([ | ||
'base/js/namespace', | ||||
Bussonnier Matthias
|
r19425 | 'jquery' | ||
Jonathan Frederic
|
r17196 | ], function(IPython, $) { | ||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
Brian Granger
|
r5993 | |||
Matthias BUSSONNIER
|
r8769 | /** | ||
* A generic toolbar on which one can add button | ||||
* @class ToolBar | ||||
* @constructor | ||||
Matthias Bussonnier
|
r19423 | * @param {Dom_object} selector | ||
Matthias BUSSONNIER
|
r8769 | */ | ||
Matthias Bussonnier
|
r19423 | var ToolBar = function (selector, options) { | ||
Brian Granger
|
r5993 | this.selector = selector; | ||
Matthias Bussonnier
|
r19423 | this.actions = (options||{}).actions; | ||
Brian Granger
|
r5993 | if (this.selector !== undefined) { | ||
this.element = $(selector); | ||||
this.style(); | ||||
} | ||||
}; | ||||
Bussonnier Matthias
|
r19447 | ToolBar.prototype._pseudo_actions={}; | ||
Matthias Bussonnier
|
r19423 | |||
Bussonnier Matthias
|
r19447 | |||
ToolBar.prototype.construct = function (config) { | ||||
Jason Grout
|
r19555 | for(var k=0; k<config.length; k++) { | ||
this.add_buttons_group(config[k][0],config[k][1]); | ||||
Matthias Bussonnier
|
r19423 | } | ||
}; | ||||
Matthias BUSSONNIER
|
r8769 | /** | ||
Matthias Bussonnier
|
r19423 | * Add a group of button into the current toolbar. | ||
Matthias BUSSONNIER
|
r8769 | * | ||
Matthias Bussonnier
|
r19423 | * Use a [dict of [list of action name]] to trigger | ||
* on click to the button | ||||
Matthias BUSSONNIER
|
r8769 | * | ||
* @example | ||||
* | ||||
Matthias Bussonnier
|
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
|
r19555 | * old methods of adding a button directly bound to callbacks: | ||
Matthias Bussonnier
|
r19423 | * @example | ||
* # deprecate, do not use | ||||
Matthias BUSSONNIER
|
r9055 | * IPython.toolbar.add_buttons_group([ | ||
Matthias BUSSONNIER
|
r8769 | * { | ||
* label:'my button', | ||||
MinRK
|
r10889 | * icon:'icon-hdd', | ||
Matthias BUSSONNIER
|
r9542 | * callback:function(){alert('hoho')}, | ||
Matthias BUSSONNIER
|
r8769 | * id : 'my_button_id', // this is optional | ||
* }, | ||||
* { | ||||
* label:'my second button', | ||||
MinRK
|
r10889 | * icon:'icon-play', | ||
Matthias BUSSONNIER
|
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
|
r10889 | * @param list.icon {string} icon to choose from [Font Awesome](http://fortawesome.github.io/Font-Awesome) | ||
Matthias BUSSONNIER
|
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
|
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
|
r8769 | */ | ||
Matthias Bussonnier
|
r19423 | // TODO JUPYTER: | ||
// get rid of legacy code that handle things that are not actions. | ||||
Matthias BUSSONNIER
|
r8212 | ToolBar.prototype.add_buttons_group = function (list, group_id) { | ||
Matthias Bussonnier
|
r19423 | // handle custom call of pseudoaction binding. | ||
Bussonnier Matthias
|
r19425 | if(typeof(list) === 'string' && list.slice(0,1) === '<' && list.slice(-1) === '>'){ | ||
var _pseudo_action; | ||||
Matthias Bussonnier
|
r19423 | try{ | ||
Bussonnier Matthias
|
r19425 | _pseudo_action = list.slice(1,-1); | ||
Bussonnier Matthias
|
r19481 | this.element.append(this._pseudo_actions[_pseudo_action].call(this)); | ||
Matthias Bussonnier
|
r19423 | } catch (e) { | ||
Matthias Bussonnier
|
r19427 | console.warn('ouch, calling ', _pseudo_action, 'does not seem to work...:', e); | ||
Matthias Bussonnier
|
r19423 | } | ||
return ; | ||||
} | ||||
var that = this; | ||||
MinRK
|
r10889 | var btn_group = $('<div/>').addClass("btn-group"); | ||
MinRK
|
r16226 | if( group_id !== undefined ) { | ||
MinRK
|
r10889 | btn_group.attr('id',group_id); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
MinRK
|
r16226 | for(var i=0; i < list.length; i++) { | ||
Matthias Bussonnier
|
r19423 | |||
Bussonnier Matthias
|
r19425 | // IIFE because javascript don't have loop scope so | ||
Matthias Bussonnier
|
r19423 | // action_name would otherwise be the same on all iteration | ||
// of the loop | ||||
Bussonnier Matthias
|
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
|
r19423 | |||
Bussonnier Matthias
|
r19447 | } | ||
var button = $('<button/>') | ||||
.addClass('btn btn-default') | ||||
.attr("title", el.label||action.help) | ||||
.append( | ||||
Matthias Bussonnier
|
r20913 | $("<i/>").addClass(el.icon||(action||{icon:'fa-exclamation-triangle'}).icon).addClass('fa') | ||
Bussonnier Matthias
|
r19447 | ); | ||
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
|
r19423 | // END IIFE | ||
Matthias BUSSONNIER
|
r7832 | } | ||
MinRK
|
r10889 | $(this.selector).append(btn_group); | ||
Matthias BUSSONNIER
|
r8209 | }; | ||
Brian Granger
|
r5993 | |||
ToolBar.prototype.style = function () { | ||||
Matthias BUSSONNIER
|
r17434 | this.element.addClass('toolbar'); | ||
Brian Granger
|
r5993 | }; | ||
Matthias BUSSONNIER
|
r8769 | /** | ||
* Show and hide toolbar | ||||
* @method toggle | ||||
*/ | ||||
Brian Granger
|
r5994 | ToolBar.prototype.toggle = function () { | ||
this.element.toggle(); | ||||
Brian Granger
|
r5993 | }; | ||
Matthias BUSSONNIER
|
r17435 | // Backwards compatibility. | ||
Brian Granger
|
r5993 | IPython.ToolBar = ToolBar; | ||
Jonathan Frederic
|
r17201 | return {'ToolBar': ToolBar}; | ||
Jonathan Frederic
|
r17196 | }); | ||