diff --git a/src/collections.js b/src/collections.js index 8d6014a8..4044115c 100644 --- a/src/collections.js +++ b/src/collections.js @@ -10,6 +10,11 @@ var BranchCollection = Backbone.Collection.extend({ model: Branch }); +var CommandEntryCollection = Backbone.Collection.extend({ + model: CommandEntry, + localStorage: new Backbone.LocalStorage('CommandEntries') +}); + var CommandBuffer = Backbone.Model.extend({ defaults: { collection: null, diff --git a/src/commandModel.js b/src/commandModel.js index 805da904..7024a29e 100644 --- a/src/commandModel.js +++ b/src/commandModel.js @@ -305,3 +305,11 @@ OptionParser.prototype.explodeAndSet = function() { // done! }; +// command entry is for the commandview +var CommandEntry = Backbone.Model.extend({ + defaults: { + text: '' + }, + localStorage: new Backbone.LocalStorage('CommandEntries') +}); + diff --git a/src/commandViews.js b/src/commandViews.js index 86393d7f..bb0fc158 100644 --- a/src/commandViews.js +++ b/src/commandViews.js @@ -1,7 +1,26 @@ var CommandPromptView = Backbone.View.extend({ initialize: function(options) { this.collection = options.collection; - this.commands = []; + + // uses local storage + this.commands = new CommandEntryCollection(); + this.commands.fetch({ + success: _.bind(function() { + // reverse the commands. this is ugly but needs to be done... + var commands = []; + this.commands.each(function(c) { + commands.push(c); + }); + + commands.reverse(); + this.commands.reset(); + + _.each(commands, function(c) { + this.commands.add(c); + }, this); + }, this) + }); + this.index = -1; this.commandSpan = this.$('#prompt span.command')[0]; @@ -134,7 +153,7 @@ var CommandPromptView = Backbone.View.extend({ this.index += delta; // if we are over / under, display blank line. yes this eliminates your - // partially written command, but i doubt that is much in this demo + // partially edited command, but i doubt that is much in this demo if (this.index >= this.commands.length || this.index < 0) { this.clear(); this.index = -1; @@ -142,7 +161,15 @@ var CommandPromptView = Backbone.View.extend({ } // yay! we actually can display something - this.setTextField(this.commands[this.index]); + var commandEntry = this.commands.toArray()[this.index].get('text'); + this.setTextField(commandEntry); + }, + + clearLocalStorage: function() { + this.commands.each(function(c) { + Backbone.sync('delete', c, function() { }); + }, this); + localStorage.setItem('CommandEntries', ''); }, setTextField: function(value) { @@ -160,9 +187,23 @@ var CommandPromptView = Backbone.View.extend({ }, submitValue: function(value) { - // if we are entering a real command, add it to our history - if (value.length) { - this.commands.unshift(value); + // we should add if it's not a blank line and this is a new command... + // or if we edited the command + var shouldAdd = (value.length && this.index == -1) || + ((value.length && this.index !== -1 && + this.commands.toArray()[this.index].get('text') !== value)); + + if (shouldAdd) { + var commandEntry = new CommandEntry({text: value}); + this.commands.unshift(commandEntry); + + // store to local storage + Backbone.sync('create', commandEntry, function() { }); + + // if our length is too egregious, reset + if (this.commands.length > 100) { + this.clearLocalStorage(); + } } this.index = -1; diff --git a/src/main.js b/src/main.js index 072ab8c9..bc8bbc85 100644 --- a/src/main.js +++ b/src/main.js @@ -24,11 +24,12 @@ $(document).ready(function(){ collection: commandCollection }); - new CommandPromptView({ + // TODO make not global + commandPromptView = new CommandPromptView({ el: $('#commandLineBar'), collection: commandCollection }); - new CommandLineHistoryView({ + commandLineHistoryView = new CommandLineHistoryView({ el: $('#commandLineHistory'), collection: commandCollection });