2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-02-13 11:40:20 -05:00
|
|
|
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
2011-02-28 12:22:12 -05:00
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Params = imports.misc.params;
|
2011-02-13 11:40:20 -05:00
|
|
|
|
|
|
|
const DEFAULT_LIMIT = 512;
|
|
|
|
|
2011-02-28 12:22:12 -05:00
|
|
|
function HistoryManager(params) {
|
|
|
|
this._init(params);
|
2011-02-13 11:40:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
HistoryManager.prototype = {
|
2011-02-28 12:22:12 -05:00
|
|
|
_init: function(params) {
|
|
|
|
params = Params.parse(params, { gsettingsKey: null,
|
|
|
|
limit: DEFAULT_LIMIT,
|
|
|
|
entry: null });
|
|
|
|
|
|
|
|
this._key = params.gsettingsKey;
|
|
|
|
this._limit = params.limit;
|
|
|
|
|
|
|
|
this._historyIndex = 0;
|
|
|
|
if (this._key) {
|
|
|
|
this._history = global.settings.get_strv(this._key);
|
|
|
|
global.settings.connect('changed::' + this._key,
|
|
|
|
Lang.bind(this, this._historyChanged));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this._history = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._entry = params.entry;
|
|
|
|
|
|
|
|
if (this._entry) {
|
|
|
|
this._entry.connect('key-press-event',
|
|
|
|
Lang.bind(this, this._onEntryKeyPress));
|
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_historyChanged: function() {
|
|
|
|
this._history = global.settings.get_strv(this._key);
|
|
|
|
this._historyIndex = this._history.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
prevItem: function(text) {
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._historyIndex <= 0)
|
|
|
|
return text;
|
|
|
|
|
|
|
|
if (text)
|
|
|
|
this._history[this._historyIndex] = text;
|
|
|
|
this._historyIndex--;
|
2011-02-13 11:40:20 -05:00
|
|
|
return this._indexChanged();
|
|
|
|
},
|
|
|
|
|
|
|
|
nextItem: function(text) {
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._historyIndex >= this._history.length)
|
|
|
|
return text;
|
|
|
|
|
|
|
|
if (text)
|
|
|
|
this._history[this._historyIndex] = text;
|
|
|
|
this._historyIndex++;
|
2011-02-13 11:40:20 -05:00
|
|
|
return this._indexChanged();
|
|
|
|
},
|
|
|
|
|
|
|
|
lastItem: function() {
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._historyIndex != this._history.length) {
|
|
|
|
this._historyIndex = this._history.length;
|
|
|
|
this._indexChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._historyIndex[this._history.length];
|
2011-02-13 11:40:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
addItem: function(input) {
|
|
|
|
if (this._history.length == 0 ||
|
|
|
|
this._history[this._history.length - 1] != input) {
|
|
|
|
|
|
|
|
this._history.push(input);
|
|
|
|
this._save();
|
2011-04-27 09:51:53 -04:00
|
|
|
}
|
|
|
|
this._historyIndex = this._history.length;
|
2011-02-13 11:40:20 -05:00
|
|
|
},
|
|
|
|
|
2011-02-28 12:22:12 -05:00
|
|
|
_onEntryKeyPress: function(entry, event) {
|
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
if (symbol == Clutter.KEY_Up) {
|
|
|
|
this.prevItem(entry.get_text());
|
|
|
|
return true;
|
|
|
|
} else if (symbol == Clutter.KEY_Down) {
|
|
|
|
this.nextItem(entry.get_text());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2011-02-13 11:40:20 -05:00
|
|
|
_indexChanged: function() {
|
|
|
|
let current = this._history[this._historyIndex] || '';
|
|
|
|
this.emit('changed', current);
|
|
|
|
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._entry)
|
|
|
|
this._entry.set_text(current);
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2011-02-28 12:22:12 -05:00
|
|
|
return current;
|
2011-02-13 11:40:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_save: function() {
|
|
|
|
if (this._history.length > this._limit)
|
2011-02-28 12:22:12 -05:00
|
|
|
this._history.splice(0, this._history.length - this._limit);
|
|
|
|
|
|
|
|
if (this._key)
|
|
|
|
global.settings.set_strv(this._key, this._history);
|
2011-02-13 11:40:20 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Signals.addSignalMethods(HistoryManager.prototype);
|