Add attachments cell toolbar option which opens a metadata-like JSON editor.

This commit is contained in:
Julien Rebetez 2015-10-20 16:52:45 +02:00
parent 3414b83855
commit 1c6f589ff6
3 changed files with 113 additions and 0 deletions

View File

@ -207,11 +207,74 @@ define(function(require) {
modal_obj.on('shown.bs.modal', function(){ editor.refresh(); });
};
var edit_attachments = function (options) {
options.name = options.name || "Cell";
var error_div = $('<div/>').css('color', 'red');
var message =
"Manually edit the JSON below to manipulate attachments for this " + options.name + ".";
var textarea = $('<textarea/>')
.attr('rows', '13')
.attr('cols', '80')
.attr('name', 'attachments')
.text(JSON.stringify(options.attachments || {}, null, 2));
var dialogform = $('<div/>').attr('title', 'Edit attachments')
.append(
$('<form/>').append(
$('<fieldset/>').append(
$('<label/>')
.attr('for','attachments')
.text(message)
)
.append(error_div)
.append($('<br/>'))
.append(textarea)
)
);
var editor = CodeMirror.fromTextArea(textarea[0], {
lineNumbers: true,
matchBrackets: true,
indentUnit: 2,
autoIndent: true,
mode: 'application/json',
});
var modal_obj = modal({
title: "Edit " + options.name + " Attachments",
body: dialogform,
buttons: {
OK: { class : "btn-primary",
click: function() {
/**
* validate json and set it
*/
var new_att;
try {
new_att = JSON.parse(editor.getValue());
} catch(e) {
console.log(e);
error_div.text('WARNING: Could not save invalid JSON.');
return false;
}
options.callback(new_att);
}
},
Cancel: {}
},
notebook: options.notebook,
keyboard_manager: options.keyboard_manager,
});
modal_obj.on('shown.bs.modal', function(){ editor.refresh(); });
};
var dialog = {
modal : modal,
kernel_modal : kernel_modal,
edit_metadata : edit_metadata,
edit_attachments : edit_attachments
};
return dialog;

View File

@ -0,0 +1,48 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'notebook/js/celltoolbar',
'base/js/dialog',
], function($, celltoolbar, dialog) {
"use strict";
var CellToolbar = celltoolbar.CellToolbar;
var edit_attachments_dialog = function(cell) {
dialog.edit_attachments({
attachments: cell.attachments,
callback: function(attachments) {
cell.attachments = attachments;
},
name: 'cell',
notebook: cell.notebook,
keyboard_manager: cell.keyboard_manager
});
};
var add_dialog_button = function(div, cell) {
var button_container = $(div);
var button = $('<button />')
.addClass('btn btn-default btn-xs')
.text('Edit Attachments')
.click( function() {
edit_attachments_dialog(cell);
return false;
});
button_container.append(button);
};
var register = function(notebook) {
CellToolbar.register_callback('attachments.edit', add_dialog_button);
var attachments_preset = [];
attachments_preset.push('attachments.edit');
CellToolbar.register_preset('Attachments', attachments_preset, notebook);
console.log('Attachments editing toolbar loaded.');
};
return {'register' : register}
});

View File

@ -26,6 +26,7 @@ define(function (require) {
var default_celltoolbar = require('notebook/js/celltoolbarpresets/default');
var rawcell_celltoolbar = require('notebook/js/celltoolbarpresets/rawcell');
var slideshow_celltoolbar = require('notebook/js/celltoolbarpresets/slideshow');
var attachments_celltoolbar = require('notebook/js/celltoolbarpresets/attachments');
var scrollmanager = require('notebook/js/scrollmanager');
var commandpalette = require('notebook/js/commandpalette');
@ -155,6 +156,7 @@ define(function (require) {
default_celltoolbar.register(this);
rawcell_celltoolbar.register(this);
slideshow_celltoolbar.register(this);
attachments_celltoolbar.register(this);
// prevent assign to miss-typed properties.
Object.seal(this);