Light12306/RwTicketAssistantV2/app/air/js/flight/FlightMonitorBox.js
iFish addcafcdf8 + 集成扩展的源码
+ 增加相关TypedScript的导入
2014-08-08 14:33:43 +08:00

109 lines
2.3 KiB
JavaScript

var FlightMonitorBox = function() {
var instance = this;
this.history = [];
this.storage = Storage;
this.init();
FlightMonitorBox = function() {
return instance;
};
};
FlightMonitorBox.prototype.init = function() {
var flightIdsString = this.storage.get('monitorBox'),
flightIds = [],
data;
this.flights = {};
this.count = 0;
if (flightIdsString !== null) {
flightIds = JSON.parse(flightIdsString);
}
for (var i = flightIds.length - 1; i >= 0; i--) {
if (this.storage.get(flightIds[i]) === null) {
continue;
}
data = JSON.parse(this.storage.get(flightIds[i]));
this.add(
FlightFactory.getFromId(flightIds[i]).setData(data.flight),
data.monitor,
data.lastShowedPrice,
data.error
);
};
return this;
};
FlightMonitorBox.prototype.add = function(flight, monitor, lastShowedPrice, error) {
var flightId = flight.getId();
if (this.flights[flightId] !== undefined) {
throw {
name: 'MonitorDulipcateId',
message: '重复航班'
};
}
if (this.count >= config.monitor.limit) {
throw {
name: 'MonitorLimit',
message: '最多允许同时关注' + config.monitor.limit + '条航线'
};
}
this.flights[flightId] = {
flight: flight,
monitor: monitor,
lastShowedPrice: lastShowedPrice,
error: error
};
this.count++;
this.history.push(flightId);
return this;
};
FlightMonitorBox.prototype.remove = function(id) {
delete this.flights[id];
this.storage.remove(id);
this.count--;
return this;
};
FlightMonitorBox.prototype.getAll = function() {
return this.flights;
};
FlightMonitorBox.prototype.getCount = function() {
return this.count;
};
FlightMonitorBox.prototype.get = function(id) {
return this.flights[id];
};
FlightMonitorBox.prototype.persistenceOne = function(id) {
this.storage.set(id, JSON.stringify({
flight: this.flights[id].flight.data,
monitor: this.flights[id].monitor,
lastShowedPrice: this.flights[id].lastShowedPrice,
error: this.flights[id].error
}));
};
FlightMonitorBox.prototype.persistence = function() {
var flightIds = [],
flightId;
for (flightId in this.flights) {
flightIds.push(flightId);
this.persistenceOne(flightId);
}
this.storage.set('monitorBox', JSON.stringify(flightIds));
this.storage.set('monitorHistory', JSON.stringify(this.history.slice(-20)));
};