// Copyright (c) IPython Development Team.1// Distributed under the terms of the Modified BSD License.23define([4'base/js/namespace',5'jquery'6], function(IPython, $) {7"use strict";89/**10* A generic toolbar on which one can add button11* @class ToolBar12* @constructor13* @param {Dom_object} selector14*/15var ToolBar = function (selector, options) {16this.selector = selector;17this.actions = (options||{}).actions;18if (this.selector !== undefined) {19this.element = $(selector);20this.style();21}22};2324ToolBar.prototype._pseudo_actions={};252627ToolBar.prototype.construct = function (config) {28for(var k=0; k<config.length; k++) {29this.add_buttons_group(config[k][0],config[k][1]);30}31};3233/**34* Add a group of button into the current toolbar.35*36* Use a [dict of [list of action name]] to trigger37* on click to the button38*39* @example40*41* ... todo, maybe use a list of list to keep ordering.42*43* [44* [45* [46* action_name_1,47* action_name_2,48* action_name_3,49* ],50* optional_group_name51* ],52* ...53* ]54*55* For backward compatibility this also support the56* old methods of adding a button directly bound to callbacks:57* @example58* # deprecate, do not use59* IPython.toolbar.add_buttons_group([60* {61* label:'my button',62* icon:'icon-hdd',63* callback:function(){alert('hoho')},64* id : 'my_button_id', // this is optional65* },66* {67* label:'my second button',68* icon:'icon-play',69* callback:function(){alert('be carefull I cut')}70* }71* ],72* "my_button_group_id"73* )74*75* @method add_buttons_group76* @param list {List}77* List of button of the group, with the following paramter for each :78* @param list.label {string} text to show on button hover79* @param list.icon {string} icon to choose from [Font Awesome](http://fortawesome.github.io/Font-Awesome)80* @param list.callback {function} function to be called on button click81* @param [list.id] {String} id to give to the button82* @param [group_id] {String} optionnal id to give to the group83*84*85* for private usage, the key can also be strings starting with '<' and ending with '>' to inject custom element that cannot86* be bound to an action.87*88*/89// TODO JUPYTER:90// get rid of legacy code that handle things that are not actions.91ToolBar.prototype.add_buttons_group = function (list, group_id) {92// handle custom call of pseudoaction binding.93if(typeof(list) === 'string' && list.slice(0,1) === '<' && list.slice(-1) === '>'){94var _pseudo_action;95try{96_pseudo_action = list.slice(1,-1);97this.element.append(this._pseudo_actions[_pseudo_action].call(this));98} catch (e) {99console.warn('ouch, calling ', _pseudo_action, 'does not seem to work...:', e);100}101return ;102}103var that = this;104var btn_group = $('<div/>').addClass("btn-group");105if( group_id !== undefined ) {106btn_group.attr('id',group_id);107}108for(var i=0; i < list.length; i++) {109110// IIFE because javascript don't have loop scope so111// action_name would otherwise be the same on all iteration112// of the loop113(function(i,list){114var el = list[i];115var action_name;116var action;117if(typeof(el) === 'string'){118action = that.actions.get(el);119action_name = el;120121}122var button = $('<button/>')123.addClass('btn btn-default')124.attr("title", el.label||action.help)125.append(126$("<i/>").addClass(el.icon||(action||{icon:'fa-exclamation-triangle'}).icon).addClass('fa')127);128var id = el.id;129if( id !== undefined ){130button.attr('id',id);131}132button.attr('data-jupyter-action', action_name);133var fun = el.callback|| function(){134that.actions.call(action_name);135};136button.click(fun);137btn_group.append(button);138})(i,list);139// END IIFE140}141$(this.selector).append(btn_group);142};143144ToolBar.prototype.style = function () {145this.element.addClass('toolbar');146};147148/**149* Show and hide toolbar150* @method toggle151*/152ToolBar.prototype.toggle = function () {153this.element.toggle();154};155156// Backwards compatibility.157IPython.ToolBar = ToolBar;158159return {'ToolBar': ToolBar};160});161162163