Replaced prototype by class paradigm

This commit is contained in:
Jesús Ángel González Novez 2017-10-27 13:20:57 +02:00
parent 946b7a5ebd
commit 0ab26883be

View File

@ -1,7 +1,8 @@
var app = require('ui/modules').get('app/wazuh', []);
app.factory('DataHandler', function($q, apiReq) {
var DataHandler = function() {
app.factory('DataHandler', ($q, apiReq) => {
class DataHandler {
constructor() {
this.items = [];
this.filters = [];
this.path = '';
@ -10,15 +11,23 @@ app.factory('DataHandler', function($q, apiReq) {
this.initial = true;
this.initialBatch = 40;
this.regularBatch = 15;
};
}
DataHandler.prototype.nextPage = function() {
nextPage () {
if (this.busy) return;
this.busy = true;
var requestData;
if (this.initial) { requestData = { offset: this.offset, limit: this.initialBatch }; this.initial = false; }
else requestData = { offset: this.offset, limit: this.regularBatch };
if (this.initial) {
requestData = {
offset: this.offset,
limit: this.initialBatch
};
this.initial = false;
} else requestData = {
offset: this.offset,
limit: this.regularBatch
};
angular.forEach(this.filters, function (filter) {
if (filter.value != '') requestData[filter.name] = filter.value;
@ -32,7 +41,8 @@ app.factory('DataHandler', function($q, apiReq) {
var totalItems = data.data.data.totalItems;
var items = data.data.data.items;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i]); this.items[i].selected = false;
this.items.push(items[i]);
this.items[i].selected = false;
}
this.offset += items.length;
(this.offset >= totalItems) ? this.end = true: this.busy = false
@ -42,39 +52,50 @@ app.factory('DataHandler', function($q, apiReq) {
}.bind(this));
return deferred.promise;
};
}
DataHandler.prototype.hasFilter = function(filterName) {
hasFilter (filterName) {
var filterOrNot = false;
angular.forEach(this.filters, function(filter) { if (filter.name == filterName) filterOrNot = true; });
angular.forEach(this.filters, function (filter) {
if (filter.name == filterName) filterOrNot = true;
});
return filterOrNot;
};
}
DataHandler.prototype.addFilter = function(filterName, value) {
addFilter (filterName, value) {
this.removeFilter(filterName, false);
this.filters.push({ "name": filterName, "value": value });
this.filters.push({
"name": filterName,
"value": value
});
this.search();
};
}
DataHandler.prototype.removeFilter = function(filterName, search) {
removeFilter (filterName, search) {
angular.forEach(this.filters, function (filter, key) {
if (filterName == filter.name) { this.filters.splice(key, 1); }
if (filterName == filter.name) {
this.filters.splice(key, 1);
}
}, this);
if (search) this.search();
};
}
DataHandler.prototype.delete = function(name, index) {
delete (name, index) {
apiReq.request('DELETE', this.path, {}).then(function (data) {
this.items.splice(index, 1);
}.bind(this));
};
}
DataHandler.prototype.search = function() {
search () {
var requestData;
this.end = false; this.busy = false;
this.end = false;
this.busy = false;
this.sortValue = '';
requestData = { offset: 0, limit: this.initialBatch }
requestData = {
offset: 0,
limit: this.initialBatch
}
angular.forEach(this.filters, function (filter) {
if (filter.value != '') requestData[filter.name] = filter.value;
@ -84,18 +105,19 @@ app.factory('DataHandler', function($q, apiReq) {
this.items = [];
var items = data.data.data.items;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i]); this.items[i].selected = false;
this.items.push(items[i]);
this.items[i].selected = false;
}
this.offset = items.length;
}.bind(this));
};
}
DataHandler.prototype.sort = function(by) {
sort(by) {
this.sortValue = by;
this.sortDir = !this.sortDir;
};
}
DataHandler.prototype.reset = function() {
reset() {
this.items = [];
this.filters = [];
this.offset = 0;
@ -103,6 +125,7 @@ app.factory('DataHandler', function($q, apiReq) {
this.initial = true;
this.end = false;
this.busy = false;
};
}
}
return DataHandler;
});