Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
[Issue 243] Move to new metadata spec
Browse files Browse the repository at this point in the history
(c) Copyright IBM Corp. 2016
  • Loading branch information
dalogsdon committed Jun 1, 2016
1 parent 63a02d6 commit 2f04872
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 174 deletions.
Expand Up @@ -181,7 +181,7 @@ define([
var $el = $(this);
var state = $el.attr('data-dashboard-state');
if ($el.parents('#jupyter-dashboard-layout-menu').length) {
Metadata.dashboardLayout = state;
Metadata.activeView = state;
}
setDashboardState(state);
})
Expand All @@ -206,7 +206,7 @@ define([
updateAuthoringOptions(currentState);
// also set the initial layout button default to whatever layout the
// notebook currently has
updateAuthoringButtonState(Metadata.dashboardLayout);
updateAuthoringButtonState(Metadata.activeView);
};

DashboardActions.prototype.switchToNotebook = function() {
Expand Down
@@ -0,0 +1,100 @@
/**
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
/**
* This module provides an API to manage compatibility between
* Jupyter Dashboards extension metadata.
*
* Jupyter Dashboard metadata structure:
* https://github.com/jupyter-incubator/dashboards/wiki/Dashboard-Metadata-and-Rendering
*/
define([
'jquery',
'base/js/namespace',
'./object-util'
], function(
$,
IPython,
ObjectUtil
) {
'use strict';

var GRID_DEFAULT = 'grid_default';
var REPORT_DEFAULT = 'report_default';

// Mapping of view type to view id (from dashboard-metadata.js)
// This will change when multiple views of the same type are supported.
var VIEW_TO_ID = {
grid: GRID_DEFAULT,
report: REPORT_DEFAULT
};

return {
/**
* Converts version 0.x metadata structure to version 1.
* Versions 0.x and 1 currently do not overlap.
* This must be run AFTER the version 1 metadata is initialized in
* `dashboard-metadata.js`.
*/
convert: function() {
var metadata = IPython.notebook.metadata;
var jupyter_dashboards = metadata.extensions.jupyter_dashboards;

// 0.x metadata resides under "urth"
var oldNotebookMetadata = IPython.notebook.metadata.urth;
if (!oldNotebookMetadata) { return; }

// notebook-level metadata
if (oldNotebookMetadata.dashboard) {
// set active view
if (oldNotebookMetadata.dashboard.layout) {
jupyter_dashboards.activeView =
VIEW_TO_ID[oldNotebookMetadata.dashboard.layout];
} else {
jupyter_dashboards.activeView = GRID_DEFAULT;
}

// copy dashboard properties
Object.keys(oldNotebookMetadata.dashboard).filter(function(key) {
return key !== 'layout';
}).forEach(function(key) {
jupyter_dashboards.views[jupyter_dashboards.activeView][key] =
oldNotebookMetadata.dashboard[key];
});
}

// cell-level metadata
IPython.notebook.get_cells().forEach(function(cell) {
if (ObjectUtil.has(cell, 'metadata.urth.dashboard')) {
var cellMetadata = cell.metadata;
var cellDashboard = cellMetadata.extensions.jupyter_dashboards;

// copy hidden state
Object.keys(cellDashboard.views).forEach(function(view) {
cellDashboard.views[view].hidden = false;
});
cellDashboard.views[jupyter_dashboards.activeView].hidden =
!!cellMetadata.urth.dashboard.hidden;

// copy layout properties
if (cellMetadata.urth.dashboard.layout) {
var cellLayout = cellMetadata.urth.dashboard.layout;

// only grid layout has properties
// so copy all layout properties into default grid view
Object.keys(cellLayout).forEach(function(key) {
cellDashboard.views[GRID_DEFAULT][key] = cellLayout[key];
});
}

// clear old metadata
delete cellMetadata.urth;
}
});

// clear old metadata
delete IPython.notebook.metadata.urth;
}
};
});

0 comments on commit 2f04872

Please sign in to comment.