YES converted over local storage

This commit is contained in:
Peter Cottle 2015-04-04 11:06:23 -07:00
parent ca938ec79e
commit 3a710c8ab4
3 changed files with 59 additions and 48 deletions

View file

@ -17,6 +17,36 @@ describe('this store', function() {
expect(CommandLineStore.getCommandHistory()[0]) expect(CommandLineStore.getCommandHistory()[0])
.toEqual(command); .toEqual(command);
var newCommand = 'echo "yo dude";';
CommandLineActions.submitCommand(newCommand);
expect(CommandLineStore.getCommandHistoryLength())
.toEqual(2);
expect(CommandLineStore.getCommandHistory()[0])
.toEqual(newCommand);
expect(CommandLineStore.getCommandHistory()[1])
.toEqual(command);
});
it('slices after max length', function() {
var maxLength = CommandLineStore.getMaxHistoryLength();
var numOver = 10;
for (var i = 0; i < maxLength + numOver; i++) {
CommandLineActions.submitCommand('commandNum' + i);
}
var numNow = 11 + numOver;
expect(
CommandLineStore.getCommandHistoryLength()
).toEqual(numNow);
expect(
CommandLineStore.getCommandHistory()[0]
).toEqual('commandNum109');
expect(
CommandLineStore.getCommandHistory()[numNow - 1]
).toEqual('commandNum89');
}); });
}); });

View file

@ -8,6 +8,8 @@ var assign = require('object-assign');
var ActionTypes = AppConstants.ActionTypes; var ActionTypes = AppConstants.ActionTypes;
var COMMAND_HISTORY_KEY = 'lgb_CommandHistory'; var COMMAND_HISTORY_KEY = 'lgb_CommandHistory';
var COMMAND_HISTORY_MAX_LENGTH = 100;
var COMMAND_HISTORY_TO_KEEP = 10;
var _commandHistory = []; var _commandHistory = [];
try { try {
@ -17,7 +19,16 @@ try {
} catch (e) { } catch (e) {
} }
var _saveToLocalStorage = function() { function _checkForSize() {
// if our command line history is too big...
if (_commandHistory.length > COMMAND_HISTORY_MAX_LENGTH) {
// grab the last 10
_commandHistory =
_commandHistory.slice(0, COMMAND_HISTORY_TO_KEEP);
}
}
function _saveToLocalStorage() {
try { try {
localStorage.setItem( localStorage.setItem(
COMMAND_HISTORY_KEY, COMMAND_HISTORY_KEY,
@ -25,7 +36,7 @@ var _saveToLocalStorage = function() {
); );
} catch (e) { } catch (e) {
} }
}; }
var CommandLineStore = assign( var CommandLineStore = assign(
{}, {},
@ -33,6 +44,10 @@ EventEmitter.prototype,
AppConstants.StoreSubscribePrototype, AppConstants.StoreSubscribePrototype,
{ {
getMaxHistoryLength: function() {
return COMMAND_HISTORY_MAX_LENGTH;
},
getCommandHistoryLength: function() { getCommandHistoryLength: function() {
return _commandHistory.length; return _commandHistory.length;
}, },
@ -47,7 +62,8 @@ AppConstants.StoreSubscribePrototype,
switch (action.type) { switch (action.type) {
case ActionTypes.SUBMIT_COMMAND: case ActionTypes.SUBMIT_COMMAND:
_commandHistory.push(action.text); _commandHistory.unshift(String(action.text));
_checkForSize();
_saveToLocalStorage(); _saveToLocalStorage();
shouldInform = true; shouldInform = true;
break; break;

View file

@ -2,10 +2,11 @@ var _ = require('underscore');
// horrible hack to get localStorage Backbone plugin // horrible hack to get localStorage Backbone plugin
var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone; var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone;
var CommandEntryCollection = require('../models/collections').CommandEntryCollection;
var Main = require('../app'); var Main = require('../app');
var Command = require('../models/commandModel').Command; var Command = require('../models/commandModel').Command;
var CommandEntry = require('../models/commandModel').CommandEntry; var CommandEntry = require('../models/commandModel').CommandEntry;
var CommandLineStore = require('../stores/CommandLineStore');
var CommandLineActions = require('../actions/CommandLineActions');
var Errors = require('../util/errors'); var Errors = require('../util/errors');
var Warning = Errors.Warning; var Warning = Errors.Warning;
@ -18,25 +19,6 @@ var CommandPromptView = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
Main.getEvents().on('commandSubmittedPassive', this.addToCommandHistory, this); Main.getEvents().on('commandSubmittedPassive', this.addToCommandHistory, this);
// 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.index = -1;
this.commandParagraph = this.$('#prompt p.command')[0]; this.commandParagraph = this.$('#prompt p.command')[0];
this.commandCursor = this.$('#prompt span.cursor')[0]; this.commandCursor = this.$('#prompt span.cursor')[0];
@ -164,23 +146,17 @@ var CommandPromptView = Backbone.View.extend({
// if we are over / under, display blank line. yes this eliminates your // if we are over / under, display blank line. yes this eliminates your
// partially edited 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) { if (this.index >= CommandLineStore.getCommandHistoryLength() || this.index < 0) {
this.clear(); this.clear();
this.index = -1; this.index = -1;
return; return;
} }
// yay! we actually can display something // yay! we actually can display something
var commandEntry = this.commands.toArray()[this.index].get('text'); var commandEntry = CommandLineStore.getCommandHistory()[this.index];
this.setTextField(commandEntry); this.setTextField(commandEntry);
}, },
clearLocalStorage: function() {
this.commands.each(function(c) {
Backbone.sync('delete', c, function() { });
}, this);
},
setTextField: function(value) { setTextField: function(value) {
this.$('#commandTextField').val(value); this.$('#commandTextField').val(value);
}, },
@ -198,17 +174,15 @@ var CommandPromptView = Backbone.View.extend({
}, },
rollupCommands: function(numBack) { rollupCommands: function(numBack) {
var which = this.commands.toArray().slice(1, Number(numBack) + 1); var which = CommandLineStore.getCommandHistory().slice(1, Number(numBack) + 1);
which.reverse(); which.reverse();
var str = ''; var str = '';
_.each(which, function(commandEntry) { _.each(which, function(text) {
str += commandEntry.get('text') + ';'; str += text + ';';
}, this); }, this);
var rolled = new CommandEntry({text: str}); CommandLineActions.submitCommand(str);
this.commands.unshift(rolled);
Backbone.sync('create', rolled, function() { });
}, },
addToCommandHistory: function(value) { addToCommandHistory: function(value) {
@ -217,22 +191,13 @@ var CommandPromptView = Backbone.View.extend({
// or if we edited the command in place in history // or if we edited the command in place in history
var shouldAdd = (value.length && this.index === -1) || var shouldAdd = (value.length && this.index === -1) ||
((value.length && this.index !== -1 && ((value.length && this.index !== -1 &&
this.commands.toArray()[this.index].get('text') !== value)); CommandLineStore.getCommandHistory()[this.index] !== value));
if (!shouldAdd) { if (!shouldAdd) {
return; return;
} }
var commandEntry = new CommandEntry({text: value}); CommandLineActions.submitCommand(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();
}
log.commandEntered(value); log.commandEntered(value);
}, },