-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory_manager.js
143 lines (114 loc) · 4.02 KB
/
history_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2015 Ivan [email protected]
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const Lang = imports.lang;
const Signals = imports.signals;
const Params = imports.misc.params;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const DEFAULT_LIMIT = 512;
const CONNECTION_IDS = {
SETTINGS: 0
};
var HistoryManager = new Lang.Class({
Name: 'GoogleCalculator.HistoryManager',
_init: function(params) {
this._params = Params.parse(params, {
key: null,
settings: null,
limit: DEFAULT_LIMIT,
reverse_order: false
});
this._current_index = 0;
this._history = this._params.settings.get_strv(this._params.key);
CONNECTION_IDS.SETTINGS = this._params.settings.connect(
'changed::' + this._params.key,
Lang.bind(this, this._on_history_changed)
);
this.reset();
},
_on_history_changed: function() {
this._history = this._params.settings.get_strv(this._params.key);
this.reset();
},
next: function() {
let next_index = this.current_index + 1;
if(next_index > this._history.length) return false;
this.current_index = next_index;
return this.current;
},
prev: function() {
let prev_index = this.current_index - 1;
if(prev_index < 0) return false;
this.current_index = prev_index;
return this.current;
},
add: function(text) {
if(Utils.is_blank(text)) return false;
if(this.last === text) return false;
if(this._params.reverse_order) this._history.unshift(text);
else this._history.push(text);
if(this._history.length > this._params.limit) {
this._history = this._history.slice(
0, -(this._history.length - this._params.limit)
);
}
this._params.settings.set_strv(this._params.key, this._history);
return true;
},
remove: function(item) {
if(item instanceof Array) {
let items_list = item;
let new_history = this._history.filter(
Lang.bind(this, function(value, index, array) {
return (items_list.indexOf(value) === -1);
})
);
this._history = new_history;
}
else {
let index = this._history.indexOf(item);
this._history.splice(index, 1);
}
this._params.settings.set_strv(this._params.key, this._history);
},
move_to_top: function(item) {
let index = this._history.indexOf(item);
this._history.unshift(this._history.splice(index, 1)[0]);
this._params.settings.set_strv(this._params.key, this._history);
},
reset: function() {
this.current_index = this._history.length;
},
destroy: function() {
this._params.settings.disconnect(CONNECTION_IDS.SETTINGS);
this._params = null;
},
get current_index() {
return this._current_index;
},
set current_index(index) {
this._current_index = index;
},
get current() {
return this._history[this.current_index] || '';
},
get last() {
return this._history[this._history.length - 1];
},
get all() {
return this._history.slice(0);
}
});
Signals.addSignalMethods(HistoryManager.prototype);