Started work to make tree requirejs friendly.

pull/37/head
Jonathan Frederic 12 years ago
parent 855815d207
commit 9f4a03ce79

@ -1,20 +1,13 @@
//----------------------------------------------------------------------------
// Copyright (C) 2013 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
//============================================================================
// Utility for modal dialogs with bootstrap
//============================================================================
IPython.namespace('IPython.dialog');
IPython.dialog = (function (IPython) {
define([
'base/js/namespace',
'components/jquery/jquery.min',
], function(IPython, $) {
"use strict";
var modal = function (options) {
var modal = function (options, keyboard_manager, notebook) {
var modal = $("<div/>")
.addClass("modal")
.addClass("fade")
@ -79,16 +72,16 @@ IPython.dialog = (function (IPython) {
});
}
modal.on("hidden.bs.modal", function () {
if (IPython.notebook) {
var cell = IPython.notebook.get_selected_cell();
if (notebook) {
var cell = notebook.get_selected_cell();
if (cell) cell.select();
IPython.keyboard_manager.enable();
IPython.keyboard_manager.command_mode();
keyboard_manager.enable();
keyboard_manager.command_mode();
}
});
if (IPython.keyboard_manager) {
IPython.keyboard_manager.disable();
if (keyboard_manager) {
keyboard_manager.disable();
}
return modal.modal(options);
@ -128,7 +121,7 @@ IPython.dialog = (function (IPython) {
autoIndent: true,
mode: 'application/json',
});
var modal = IPython.dialog.modal({
var modal = modal({
title: "Edit " + name + " Metadata",
body: dialogform,
buttons: {
@ -153,9 +146,13 @@ IPython.dialog = (function (IPython) {
modal.on('shown.bs.modal', function(){ editor.refresh(); });
};
return {
Dialog = {
modal : modal,
edit_metadata : edit_metadata,
};
}(IPython));
// Backwards compatability.
IPython.Dialog = Dialog;
return Dialog;
});

@ -1,13 +1,5 @@
//----------------------------------------------------------------------------
// Copyright (C) 2008-2011 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
// Events
//============================================================================
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
// Give us an object to bind all events to. This object should be created
// before all other objects so it exists when others register event handlers.
@ -15,18 +7,15 @@
// $([IPython.events]).trigger('event.Namespace');
// To handle it:
// $([IPython.events]).on('event.Namespace',function () {});
var IPython = (function (IPython) {
define(['base/js/namespace'], function(IPython) {
"use strict";
var utils = IPython.utils;
var Events = function () {};
events = new Events();
// Backwards compatability.
IPython.Events = Events;
IPython.events = new Events();
return IPython;
}(IPython));
IPython.events = events;
return events;
});

@ -1,33 +1,46 @@
//----------------------------------------------------------------------------
// Copyright (C) 2008-2011 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
'components/jquery/jquery.min',
'base/js/page',
'base/js/utils',
'tree/js/notebooklist',
'tree/js/clusterlist',
'tree/js/sessionlist',
'tree/js/kernellist',
'auth/js/loginwidget',
'components/jquery-ui/ui/minified/jquery-ui.min',
'components/bootstrap/js/bootstrap.min',
'base/js/dialog',
], function(
IPython,
$,
Page,
Utils,
NotebookList,
ClusterList,
SesssionList,
KernelList,
LoginWidget){
page = new Page();
//============================================================================
// On document ready
//============================================================================
$(document).ready(function () {
IPython.page = new IPython.Page();
var opts = {
base_url : Utils.get_body_data("baseUrl"),
notebook_path : Utils.get_body_data("notebookPath"),
};
session_list = new SesssionList(opts);
notebook_list = new NotebookList('#notebook_list', opts, undefined, session_list);
cluster_list = new ClusterList('#cluster_list', opts);
kernel_list = new KernelList('#running_list', opts);
login_widget = new LoginWidget('#login_widget', opts);
$('#new_notebook').button().click(function (e) {
IPython.notebook_list.new_notebook()
notebook_list.new_notebook();
});
var opts = {
base_url : IPython.utils.get_body_data("baseUrl"),
notebook_path : IPython.utils.get_body_data("notebookPath"),
};
IPython.session_list = new IPython.SesssionList(opts);
IPython.notebook_list = new IPython.NotebookList('#notebook_list', opts);
IPython.cluster_list = new IPython.ClusterList('#cluster_list', opts);
IPython.kernel_list = new IPython.KernelList('#running_list', opts);
IPython.login_widget = new IPython.LoginWidget('#login_widget', opts);
var interval_id=0;
// auto refresh every xx secondes, no need to be fast,
// update is done at least when page get focus
@ -35,31 +48,31 @@ $(document).ready(function () {
var enable_autorefresh = function(){
//refresh immediately , then start interval
if($('.upload_button').length == 0)
if($('.upload_button').length === 0)
{
IPython.session_list.load_sessions();
IPython.cluster_list.load_list();
session_list.load_sessions();
cluster_list.load_list();
}
if (!interval_id){
interval_id = setInterval(function(){
if($('.upload_button').length == 0)
if($('.upload_button').length === 0)
{
IPython.session_list.load_sessions();
IPython.cluster_list.load_list();
session_list.load_sessions();
cluster_list.load_list();
}
}, time_refresh*1000);
}
}
};
var disable_autorefresh = function(){
clearInterval(interval_id);
interval_id = 0;
}
};
// stop autorefresh when page lose focus
$(window).blur(function() {
disable_autorefresh();
})
});
//re-enable when page get focus back
$(window).focus(function() {
@ -69,24 +82,31 @@ $(document).ready(function () {
// finally start it, it will refresh immediately
enable_autorefresh();
IPython.page.show();
$([IPython.events]).trigger('app_initialized.DashboardApp');
page.show();
events.trigger('app_initialized.DashboardApp');
// bound the upload method to the on change of the file select list
$("#alternate_upload").change(function (event){
IPython.notebook_list.handleFilesUpload(event,'form');
notebook_list.handleFilesUpload(event,'form');
});
// set hash on tab click
$("#tabs").find("a").click(function() {
window.location.hash = $(this).attr("href");
})
});
// load tab if url hash
if (window.location.hash) {
$("#tabs").find("a[href=" + window.location.hash + "]").click();
}
// For backwards compatability.
IPython.page = page;
IPython.notebook_list = notebook_list;
IPython.cluster_list = cluster_list;
IPython.session_list = session_list;
IPython.kernel_list = kernel_list;
IPython.login_widget = login_widget;
return page;
});

@ -1,21 +1,18 @@
//----------------------------------------------------------------------------
// Copyright (C) 2011 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
// NotebookList
//============================================================================
var IPython = (function (IPython) {
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
'components/jquery/jquery.min',
'base/js/utils',
'base/js/events',
'base/js/dialog',
], function(IPython, $, Utils, Events, Dialog) {
"use strict";
var utils = IPython.utils;
var NotebookList = function (selector, options, element_name) {
var that = this
var NotebookList = function (selector, options, element_name, session_list) {
var that = this;
this.session_list = session_list;
// allow code re-use by just changing element_name in kernellist.js
this.element_name = element_name || 'notebook';
this.selector = selector;
@ -26,14 +23,14 @@ var IPython = (function (IPython) {
}
this.notebooks_list = [];
this.sessions = {};
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
$([IPython.events]).on('sessions_loaded.Dashboard',
this.base_url = options.base_url || Utils.get_body_data("baseUrl");
this.notebook_path = options.notebook_path || Utils.get_body_data("notebookPath");
$([Events]).on('sessions_loaded.Dashboard',
function(e, d) { that.sessions_loaded(d); });
};
NotebookList.prototype.style = function () {
var prefix = '#' + this.element_name
var prefix = '#' + this.element_name;
$(prefix + '_toolbar').addClass('list_toolbar');
$(prefix + '_list_info').addClass('toolbar_info');
$(prefix + '_buttons').addClass('toolbar_buttons');
@ -69,7 +66,7 @@ var IPython = (function (IPython) {
var f = files[i];
var reader = new FileReader();
reader.readAsText(f);
var name_and_ext = utils.splitext(f.name);
var name_and_ext = Utils.splitext(f.name);
var file_ext = name_and_ext[1];
if (file_ext === '.ipynb') {
var item = that.new_notebook_item(0);
@ -85,7 +82,7 @@ var IPython = (function (IPython) {
};
} else {
var dialog = 'Uploaded notebooks must be .ipynb files';
IPython.dialog.modal({
Dialog.modal({
title : 'Invalid file type',
body : dialog,
buttons : {'OK' : {'class' : 'btn-primary'}}
@ -114,7 +111,7 @@ var IPython = (function (IPython) {
};
NotebookList.prototype.load_sessions = function(){
IPython.session_list.load_sessions();
this.session_list.load_sessions();
};
@ -132,12 +129,12 @@ var IPython = (function (IPython) {
dataType : "json",
success : $.proxy(this.list_loaded, this),
error : $.proxy( function(xhr, status, error){
utils.log_ajax_error(xhr, status, error);
Utils.log_ajax_error(xhr, status, error);
that.list_loaded([], null, null, {msg:"Error connecting to server."});
},this)
};
var url = utils.url_join_encode(
var url = Utils.url_join_encode(
this.base_url,
'api',
'notebooks',
@ -177,7 +174,7 @@ var IPython = (function (IPython) {
var name = data[i].name;
item = this.new_notebook_item(i+offset);
this.add_link(path, name, item);
name = utils.url_path_join(path, name);
name = Utils.url_path_join(path, name);
if(this.sessions[name] === undefined){
this.add_delete_button(item);
} else {
@ -218,7 +215,7 @@ var IPython = (function (IPython) {
item.find(".item_icon").addClass('folder_icon').addClass('icon-fixed-width');
item.find("a.item_link")
.attr('href',
utils.url_join_encode(
Utils.url_join_encode(
this.base_url,
"tree",
path,
@ -235,7 +232,7 @@ var IPython = (function (IPython) {
item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
item.find("a.item_link")
.attr('href',
utils.url_join_encode(
Utils.url_join_encode(
this.base_url,
"notebooks",
path,
@ -251,7 +248,7 @@ var IPython = (function (IPython) {
item.find(".item_name").empty().append(
$('<input/>')
.addClass("nbname_input")
.attr('value', utils.splitext(nbname)[0])
.attr('value', Utils.splitext(nbname)[0])
.attr('size', '30')
.attr('type', 'text')
);
@ -275,9 +272,9 @@ var IPython = (function (IPython) {
success : function () {
that.load_sessions();
},
error : utils.log_ajax_error,
error : Utils.log_ajax_error,
};
var url = utils.url_join_encode(
var url = Utils.url_join_encode(
that.base_url,
'api/sessions',
session
@ -301,7 +298,7 @@ var IPython = (function (IPython) {
var parent_item = that.parents('div.list_item');
var nbname = parent_item.data('nbname');
var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
IPython.dialog.modal({
Dialog.modal({
title : "Delete notebook",
body : message,
buttons : {
@ -316,9 +313,9 @@ var IPython = (function (IPython) {
success : function (data, status, xhr) {
parent_item.remove();
},
error : utils.log_ajax_error,
error : Utils.log_ajax_error,
};
var url = utils.url_join_encode(
var url = Utils.url_join_encode(
notebooklist.base_url,
'api/notebooks',
notebooklist.notebook_path,
@ -362,10 +359,10 @@ var IPython = (function (IPython) {
that.add_link(path, nbname, item);
that.add_delete_button(item);
},
error : utils.log_ajax_error,
error : Utils.log_ajax_error,
};
var url = utils.url_join_encode(
var url = Utils.url_join_encode(
that.base_url,
'api/notebooks',
that.notebook_path,
@ -399,7 +396,7 @@ var IPython = (function (IPython) {
success : function (data, status, xhr) {
var notebook_name = data.name;
window.open(
utils.url_join_encode(
Utils.url_join_encode(
base_url,
'notebooks',
path,
@ -409,7 +406,7 @@ var IPython = (function (IPython) {
},
error : $.proxy(this.new_notebook_failed, this),
};
var url = utils.url_join_encode(
var url = Utils.url_join_encode(
base_url,
'api/notebooks',
path
@ -419,23 +416,22 @@ var IPython = (function (IPython) {
NotebookList.prototype.new_notebook_failed = function (xhr, status, error) {
utils.log_ajax_error(xhr, status, error);
Utils.log_ajax_error(xhr, status, error);
var msg;
if (xhr.responseJSON && xhr.responseJSON.message) {
msg = xhr.responseJSON.message;
} else {
msg = xhr.statusText;
}
IPython.dialog.modal({
Dialog.modal({
title : 'Creating Notebook Failed',
body : "The error was: " + msg,
buttons : {'OK' : {'class' : 'btn-primary'}}
});
}
};
// Backwards compatability.
IPython.NotebookList = NotebookList;
return IPython;
}(IPython));
return NotebookList;
});

@ -110,12 +110,5 @@ data-notebook-path="{{notebook_path}}"
{% endblock %}
{% block script %}
{{super()}}
<script src="{{ static_url("base/js/utils.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("base/js/dialog.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("tree/js/sessionlist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("tree/js/notebooklist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("tree/js/kernellist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("tree/js/clusterlist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("tree/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("tree/js/tree_app.js") }}" type="text/javascript" charset="utf-8"></script>
{% endblock %}

Loading…
Cancel
Save