From 6a38c4ea4b6853116bf2302cd73d6c51f696888a Mon Sep 17 00:00:00 2001 From: zuber Date: Wed, 30 Sep 2009 16:47:20 +0200 Subject: [PATCH 1/1] Dodanie flash messages. --- project/static/js/messages.js | 34 +++++++++++++++++++++++++ project/static/js/models.js | 3 ++- project/static/js/views/flash.js | 35 ++++++++++++++++++++++++++ project/templates/explorer/editor.html | 16 +++++++++++- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 project/static/js/messages.js create mode 100644 project/static/js/views/flash.js diff --git a/project/static/js/messages.js b/project/static/js/messages.js new file mode 100644 index 00000000..c64aa5bd --- /dev/null +++ b/project/static/js/messages.js @@ -0,0 +1,34 @@ +/*global Editor*/ +Editor.MessageCenter = Editor.Object.extend({ + init: function() { + this.messages = []; + this.flashMessages = []; + this.firstFlashMessage = null; + }, + + addMessage: function(type, text, flash) { + this.messages.push({type: type, text: text}); + if (flash) { + this.flashMessages.push({type: type, text: flash}); + if (this.flashMessages.length == 1) { + this.set('firstFlashMessage', this.flashMessages[0]); + setTimeout(this.changeFlashMessage.bind(this), 1000 * 10); + } + } + }, + + changeFlashMessage: function() { + this.flashMessages.splice(0, 1); + if (this.flashMessages.length > 0) { + this.set('firstFlashMessage', this.flashMessages[0]); + setTimeout(this.changeFlashMessage.bind(this), 1000 * 10); + } else { + this.set('firstFlashMessage', null); + } + } + +}); + + +var messageCenter = new Editor.MessageCenter(); + diff --git a/project/static/js/models.js b/project/static/js/models.js index b5b9c541..9938eb07 100644 --- a/project/static/js/models.js +++ b/project/static/js/models.js @@ -1,4 +1,4 @@ -/*globals Editor fileId SplitView PanelContainerView EditorView*/ +/*globals Editor fileId SplitView PanelContainerView EditorView FlashView messageCenter*/ var documentsUrl = '/api/documents/'; @@ -375,6 +375,7 @@ $(function() { doc = new Editor.DocumentModel(); var editor = new EditorView('#body-wrap', doc); editor.freeze(); + var flashView = new FlashView('#flashview', messageCenter); var splitView = new SplitView('#splitview', doc); leftPanelView = new PanelContainerView('#left-panel-container', doc); rightPanelContainer = new PanelContainerView('#right-panel-container', doc); diff --git a/project/static/js/views/flash.js b/project/static/js/views/flash.js new file mode 100644 index 00000000..3f4f4759 --- /dev/null +++ b/project/static/js/views/flash.js @@ -0,0 +1,35 @@ +/*globals View render_template*/ +var FlashView = View.extend({ + template: 'flash-view-template', + + init: function(element, model, template) { + this.shownMessage = null; + this._super(element, model, template); + this.setModel(model); + }, + + setModel: function(model) { + if (this.model) { + this.model.removeObserver(this); + } + this.model = model; + this.shownMessage = null; + if (this.model) { + this.shownMessage = this.model.get('firstFlashMessage'); + this.model.addObserver(this, 'firstFlashMessage', this.modelFirstFlashMessageChanged.bind(this)); + } + this.render(); + }, + + render: function() { + this.element.html(render_template(this.template, this)); + }, + + modelFirstFlashMessageChanged: function(property, value) { + this.element.fadeOut('slow', function() { + this.shownMessage = value; + this.render(); + this.element.fadeIn('slow'); + }.bind(this)); + } +}); diff --git a/project/templates/explorer/editor.html b/project/templates/explorer/editor.html index c38dfc4f..ff37c24b 100644 --- a/project/templates/explorer/editor.html +++ b/project/templates/explorer/editor.html @@ -16,7 +16,9 @@ {# App and views #} + + @@ -49,7 +51,15 @@ - + +