Merge pull request #6119 from minrk/global-events

return global events from require(['base/js/events']).
This commit is contained in:
Paul Ivanov 2014-07-24 12:23:14 -07:00
commit b67aac5aef
6 changed files with 56 additions and 44 deletions

View File

@ -3,17 +3,22 @@
// 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.
// To trigger an event handler:
// $([IPython.events]).trigger('event.Namespace');
// To handle it:
// $([IPython.events]).on('event.Namespace',function () {});
define(['base/js/namespace'], function(IPython) {
// To register an event handler:
//
// require(['base/js/events'], function (events) {
// events.on("event.Namespace", function () { do_stuff(); });
// });
define(['base/js/namespace', 'jquery'], function(IPython, $) {
"use strict";
var Events = function () {};
var events = new Events();
// Backwards compatability.
IPython.Events = Events;
IPython.events = events;
return {'Events': Events};
return $([events]);
});

View File

@ -18,6 +18,8 @@ require([
'notebook/js/savewidget',
'notebook/js/keyboardmanager',
'notebook/js/config',
// only loaded, not used:
'custom/custom',
], function(
IPython,
$,
@ -51,7 +53,6 @@ require([
var user_config = $.extend({}, config.default_config);
var page = new page.Page();
var layout_manager = new layoutmanager.LayoutManager();
var events = $([new events.Events()]);
var pager = new pager.Pager('div#pager', 'div#pager_splitter', {
layout_manager: layout_manager,
events: events});
@ -115,10 +116,7 @@ require([
// only do this once
events.off('notebook_loaded.Notebook', first_load);
};
events.on('notebook_loaded.Notebook', first_load);
events.trigger('app_initialized.NotebookApp');
notebook.load_notebook(common_options.notebook_name, common_options.notebook_path);
IPython.page = page;
IPython.layout_manager = layout_manager;
@ -129,9 +127,12 @@ require([
IPython.menubar = menubar;
IPython.toolbar = toolbar;
IPython.notification_area = notification_area;
IPython.events = events;
IPython.keyboard_manager = keyboard_manager;
IPython.save_widget = save_widget;
IPython.config = user_config;
IPython.tooltip = notebook.tooltip;
events.trigger('app_initialized.NotebookApp');
notebook.load_notebook(common_options.notebook_name, common_options.notebook_path);
});

View File

@ -12,8 +12,10 @@ require([
'tree/js/sessionlist',
'tree/js/kernellist',
'auth/js/loginwidget',
// only loaded, not used:
'jqueryui',
'bootstrap',
'custom/custom',
], function(
IPython,
$,
@ -32,7 +34,6 @@ require([
base_url: utils.get_body_data("baseUrl"),
notebook_path: utils.get_body_data("notebookPath"),
};
events = $([new events.Events()]);
session_list = new sesssionlist.SesssionList($.extend({
events: events},
common_options));
@ -91,6 +92,15 @@ require([
enable_autorefresh();
page.show();
// 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;
events.trigger('app_initialized.DashboardApp');
// bound the upload method to the on change of the file select list
@ -108,12 +118,4 @@ require([
$("#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;
IPython.events = events;
});

View File

@ -102,8 +102,6 @@
{% block script %}
{% endblock %}
<script src="{{static_url("custom/custom.js") }}" type="text/javascript" charset="utf-8"></script>
</body>
</html>

View File

@ -9,16 +9,18 @@ casper.notebook_test(function () {
var nbname = "has#hash and space and unicø∂e.ipynb";
this.evaluate(function (nbname) {
require(['base/js/events'], function (events) {
IPython.notebook.notebook_name = nbname;
IPython._save_success = IPython._save_failed = false;
IPython.events.on('notebook_saved.Notebook', function () {
events.on('notebook_saved.Notebook', function () {
IPython._save_success = true;
});
IPython.events.on('notebook_save_failed.Notebook',
events.on('notebook_save_failed.Notebook',
function (event, xhr, status, error) {
IPython._save_failed = "save failed with " + xhr.status + xhr.responseText;
});
IPython.notebook.save_notebook();
});
}, {nbname:nbname});
this.waitFor(function () {
@ -41,10 +43,12 @@ casper.notebook_test(function () {
});
this.thenEvaluate(function(){
IPython.events.on('checkpoint_created.Notebook', function (evt, data) {
IPython._checkpoint_created = false;
require(['base/js/events'], function (events) {
events.on('checkpoint_created.Notebook', function (evt, data) {
IPython._checkpoint_created = true;
});
IPython._checkpoint_created = false;
});
IPython.notebook.save_checkpoint();
});
@ -71,7 +75,7 @@ casper.notebook_test(function () {
this.then(function(){
var notebook_url = this.evaluate(function(nbname){
var escaped_name = encodeURIComponent(nbname);
var return_this_thing;
var return_this_thing = null;
$("a.item_link").map(function (i,a) {
if (a.href.indexOf(escaped_name) >= 0) {
return_this_thing = a.href;

View File

@ -26,13 +26,16 @@ casper.open_new_notebook = function () {
this.waitFor(this.kernel_running);
// track the IPython busy/idle state
this.thenEvaluate(function () {
IPython.events.on('status_idle.Kernel',function () {
require(['base/js/namespace', 'base/js/events'], function (IPython, events) {
events.on('status_idle.Kernel',function () {
IPython._status = 'idle';
});
IPython.events.on('status_busy.Kernel',function () {
events.on('status_busy.Kernel',function () {
IPython._status = 'busy';
});
});
});
// Because of the asynchronous nature of SlimerJS (Gecko), we need to make
// sure the notebook has actually been loaded into the IPython namespace
@ -48,8 +51,7 @@ casper.page_loaded = function() {
// Return whether or not the kernel is running.
return this.evaluate(function() {
return IPython !== undefined &&
IPython.page !== undefined &&
IPython.events !== undefined;
IPython.page !== undefined;
});
};