/
EventsViewModel.js
52 lines (43 loc) · 1.96 KB
/
EventsViewModel.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
define(
['knockout', 'jquery', 'log', 'postal', 'app/utils/PostalConfig'],
function (ko, $, log, postal, config) {
"use strict";
function EventsViewModel() {
this.events = ko.observableArray(JSON.parse(localStorage.getItem('EventsHistory')) || []);
this.eventsEnabled = ko.observable(true);
this.updatesEnabled = ko.observable(true);
this.historyVisible = ko.observable(true);
this.winHeight = ko.observable($('#topRightSplitter').height() - 100);
this.winWidth = ko.observable($('#topRightSplitter').width());
this.widget = ko.observable(null);
this.setupSubscriptions();
}
EventsViewModel.prototype.save = function (msgDict) {
var events = JSON.parse(localStorage.getItem('EventsHistory')) || [];-9
events.push(msgDict);
localStorage.setItem('EventsHistory', JSON.stringify(events));
}
EventsViewModel.prototype.setupSubscriptions = function () {
var channel = postal.channel(config.TRADE_BLOTTER_CHANNEL),
_this = this;
channel.subscribe('Selection', function (msg) {
log.debug("Got Selection ev. " + msg);
var msgObj = JSON.parse(msg);
var msgDict = {id: msgObj.tradeId, name: msgObj.counterParty};
_this.events.push(msgDict);
_this.save(msgDict);
});
$(window).bind('storage', function (e) {
log.info('Storage event');
if (e.originalEvent.key == 'EventsHistory') {
_this.events(JSON.parse(e.originalEvent.newValue));
}
});
};
EventsViewModel.prototype.clearHistory = function () {
localStorage.setItem('EventsHistory', JSON.stringify([]));
this.events([]);
};
return EventsViewModel;
}
);