igiya
Version:
Suggestion engine
184 lines (147 loc) • 7.44 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _store = require('store');
var axios = require('axios');
var _filter = require('lodash/filter');
var _matches = require('lodash/matches');
var Igiya = function () {
function Igiya() {
_classCallCheck(this, Igiya);
}
_createClass(Igiya, [{
key: 'initialize',
value: function initialize(url) {
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var store_name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'igiya';
var refetch_limit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
var refetch_keyword = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'q';
var data_merge_element = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 'id';
var callback = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : function () {};
var forced = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : true;
var nestedAttribute = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : false;
var removeOld = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : false;
this.url = url;
this.param = param;
this.store_name = store_name;
this.nestedAttribute = nestedAttribute;
//refetch
this.refetch_limit = refetch_limit;
this.refetch_keyword = refetch_keyword;
this.data_merge_element = data_merge_element;
this.list_refetch_keyword = [];
this.removeOld = removeOld;
//remove existing data
if (this.removeOld) {
_store.set(this.store_name, []);
this.data = [];
} else this.data = _store.get(this.store_name);
if (this.data && this.data !== undefined && this.data.length > 0 && !forced) {
callback(false, true, []);
return true;
}
this.fetch(function (error, body) {
callback(error, self.data);
});
}
}, {
key: 'fetch',
value: function fetch() {
var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
var keyword = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var self = this;
var data = this.param;
if (this.busy) {
callback(null, self.data);
return self.data;
}
if (self.list_refetch_keyword.includes(keyword)) {
callback(null, self.data);
return self.data;
}
if (keyword) {
self.list_refetch_keyword.push(keyword);
data[self.refetch_keyword] = keyword;
}
self.busy = true;
var request = axios.get(self.url, this.param);
return request.then(function (response) {
self.busy = false;
try {
var responseData = self.nestedAttribute ? responseData[self.nestedAttribute] : response.data;
if (self.data !== undefined) self.data = Igiya.merge(self.data, responseData, self.data_merge_element);else self.data = responseData;
_store.set(self.store_name, self.data);
callback(false, self.data);
} catch (e) {
callback(e.message, self.data);
}
}, function (error) {
callback(true, error.response);
});
}
}, {
key: 'search',
value: function search(callback, attribute, keyword) {
var matches = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var refetch = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
var self = this;
var filter_list = _store.get(self.store_name);
if (matches) filter_list = _filter(filter_list, _matches(matches));
if (keyword && !Array.isArray(keyword)) {
keyword = keyword.split(' ');
filter_list = _filter(filter_list, function (data_array) {
if (Array.isArray(attribute)) {
//if column multiple
var found = false;
attribute.forEach(function (attr) {
if (data_array[attr] !== undefined) {
keyword.forEach(function (keywordItem) {
if (data_array[attr].toUpperCase().includes(keywordItem.toUpperCase())) found = true;
});
}
});
return found;
} else {
var _found = false;
if (data_array[attribute] !== undefined) {
keyword.forEach(function (keywardItem) {
if (data_array[attribute].toUpperCase().includes(keywardItem.toUpperCase())) _found = true;
});
return _found;
}
}
});
}
if (filter_list && filter_list.length < this.refetch_limit && refetch) {
//if there are less suggestion refetch the server
self.fetch(function (error, body) {
self.search(function (result) {
callback(result);
}, attribute, keyword, matches, false);
}, keyword);
} else {
callback(filter_list);
}
}
}], [{
key: 'merge',
value: function merge(main_data, data_merge, data_merge_element) {
var data = main_data;
for (var e = 0; e < data_merge.length; e++) {
var is_found = false;
for (var i = 0; i < main_data.length; i++) {
if (main_data[i][data_merge_element] === data_merge[e][data_merge_element]) {
data[i] = data_merge[e];
is_found = true;
}
}
if (!is_found) {
data.push(data_merge[e]);
}
}
return data;
}
}]);
return Igiya;
}();
module.exports = Igiya;