From 457d86b8b2650669575621cd1382c24888d0c085 Mon Sep 17 00:00:00 2001 From: Vidar Tonaas Fauske Date: Thu, 19 Jan 2017 18:28:49 +0000 Subject: [PATCH 1/5] Add initial version of tags celltoolbar --- .../notebook/js/celltoolbarpresets/tags.js | 141 ++++++++++++++++++ notebook/static/notebook/js/notebook.js | 3 + notebook/static/notebook/less/style.less | 1 + notebook/static/notebook/less/tagbar.less | 25 ++++ 4 files changed, 170 insertions(+) create mode 100644 notebook/static/notebook/js/celltoolbarpresets/tags.js create mode 100644 notebook/static/notebook/less/tagbar.less diff --git a/notebook/static/notebook/js/celltoolbarpresets/tags.js b/notebook/static/notebook/js/celltoolbarpresets/tags.js new file mode 100644 index 0000000000..ae7ec53d3f --- /dev/null +++ b/notebook/static/notebook/js/celltoolbarpresets/tags.js @@ -0,0 +1,141 @@ +// Copyright (c) Jupyter Development Team. +// Distributed under the terms of the Modified BSD License. + +define([ + 'notebook/js/celltoolbar' +], function(celltoolbar) { + "use strict"; + + var CellToolbar = celltoolbar.CellToolbar; + + var remove_tag = function(cell) { + return function(tag_name) { + if (!cell.metadata || !cell.metadata.tags) { + // No tags to remove + return; + } + // Remove tag from tags list + var index = cell.metadata.tags.indexOf(tag_name); + if (index !== -1) { + cell.metadata.tags.splice(index, 1); + } + // If tags list is empty, remove it + if (cell.metadata.tags.length === 0) { + delete cell.metadata.tags; + } + }; + }; + + var make_tag_container = function(cell, on_remove) { + + var tag_container = $(''). + addClass('tag-container'); + var tag_list = cell.metadata.tags || []; + if (!Array.isArray(tag_list)) { + // We cannot make tags UI for this cell! + // Maybe someone else used "tags" for something? + return null; // Fail gracefully + } + for (var i=0; i < tag_list.length; ++i) { + var tag_name = tag_list[i]; + if (typeof tag_name !== 'string') { + // Unexpected type, disable toolbar for safety + return null; + } + var tag = make_tag(tag_name, on_remove); + tag_container.append(tag); + } + return tag_container; + }; + + var make_tag = function(name, on_remove) { + var tag_container = $('') + .addClass('cell-tag') + .text(name); + + var remove_button = $('') + .addClass('remove-tag-btn') + .text('X') + .click( function(ev) { + if (ev.button === 0) { + // Remove tag from container + tag_container.remove(); + // Remove tag from cell + if (on_remove) { + on_remove(name); + } + return false; + } + }); + tag_container.append(remove_button); + return tag_container; + }; + + // Single edit with button to add tags + var add_tag_edit = function(div, cell, on_add) { + var button_container = $(div); + + var text = $('').attr('type', 'text'); + var button = $('