From 089160025f70dd9d54a65a56d66a6b8f4c32d40a 2014-11-20 19:52:35 From: Thomas Kluyver Date: 2014-11-20 19:52:35 Subject: [PATCH] Saving files works --- diff --git a/IPython/html/static/texteditor/js/main.js b/IPython/html/static/texteditor/js/main.js index 2ed7713..9866646 100644 --- a/IPython/html/static/texteditor/js/main.js +++ b/IPython/html/static/texteditor/js/main.js @@ -5,16 +5,20 @@ require([ 'jquery', 'base/js/utils', 'base/js/page', + 'base/js/events', 'contents', 'codemirror/lib/codemirror', + 'texteditor/js/menubar', 'codemirror/mode/meta', 'custom/custom', ], function( - $, + $, utils, page, + events, contents, - CodeMirror + CodeMirror, + menubar ){ page = new page.Page(); @@ -29,7 +33,7 @@ require([ basename = file_path; } else { dir_path = file_path.substring(0, ix); - basename = file_path.substring(ix); + basename = file_path.substring(ix+1); } contents.load(dir_path, basename, { success: function(model) { @@ -40,6 +44,14 @@ require([ value: model.content, }); + var menus = new menubar.MenuBar('#menubar', { + base_url: base_url, + codemirror: cm, + contents: contents, + events: events, + file_path: file_path + }); + // Find and load the highlighting mode var modeinfo = CodeMirror.findModeByMIME(model.mimetype); if (modeinfo) { diff --git a/IPython/html/static/texteditor/js/menubar.js b/IPython/html/static/texteditor/js/menubar.js new file mode 100644 index 0000000..552f9c8 --- /dev/null +++ b/IPython/html/static/texteditor/js/menubar.js @@ -0,0 +1,70 @@ +// Copyright (c) IPython Development Team. +// Distributed under the terms of the Modified BSD License. + +define([ + 'base/js/namespace', + 'jquery', + 'base/js/utils', + 'bootstrap', +], function(IPython, $, utils, bootstrap) { + "use strict"; + + var MenuBar = function (selector, options) { + // Constructor + // + // A MenuBar Class to generate the menubar of IPython notebook + // + // Parameters: + // selector: string + // options: dictionary + // Dictionary of keyword arguments. + // codemirror: CodeMirror instance + // contents: ContentManager instance + // events: $(Events) instance + // base_url : string + // file_path : string + options = options || {}; + this.base_url = options.base_url || utils.get_body_data("baseUrl"); + this.selector = selector; + this.codemirror = options.codemirror; + this.contents = options.contents; + this.events = options.events; + this.file_path = options.file_path; + + if (this.selector !== undefined) { + this.element = $(selector); + this.bind_events(); + } + }; + + MenuBar.prototype.bind_events = function () { + // File + var that = this; + this.element.find('#save_file').click(function () { + var ix = that.file_path.lastIndexOf("/"); + var dir_path, basename; + if (ix === -1) { + dir_path = ''; + basename = that.file_path; + } else { + dir_path = that.file_path.substring(0, ix); + basename = that.file_path.substring(ix+1); + } + var model = { + path: dir_path, + name: basename, + type: 'file', + format: 'text', + content: that.codemirror.getValue(), + }; + console.log(model); + that.contents.save(dir_path, basename, model, { + success: function() { + that.events.trigger("save_succeeded.TextEditor"); + } + }); + }); + }; + + return {'MenuBar': MenuBar}; +}); diff --git a/IPython/html/templates/texteditor.html b/IPython/html/templates/texteditor.html index e1ffb6f..bae58dd 100644 --- a/IPython/html/templates/texteditor.html +++ b/IPython/html/templates/texteditor.html @@ -17,6 +17,37 @@ data-file-path="{{file_path}}" {% block site %} + +
{% endblock %}