271 lines
9.0 KiB
JavaScript
271 lines
9.0 KiB
JavaScript
|
|
import './Products.html';
|
|
|
|
let QUERY_LIMIT = 20;
|
|
let PREFIX = "Products.";
|
|
|
|
Tracker.autorun(function() {
|
|
Meteor.subscribe("products");
|
|
Meteor.subscribe("productTags");
|
|
Meteor.subscribe("measures");
|
|
});
|
|
|
|
Template.Products.onCreated(function() {
|
|
Session.set(PREFIX + "displayNewProduct", false);
|
|
Session.set(PREFIX + "showHidden", false);
|
|
});
|
|
Template.Products.helpers({
|
|
displayNewProduct: function() {
|
|
return Session.get(PREFIX + "displayNewProduct");
|
|
},
|
|
products: function() {
|
|
let skipCount = Session.get(PREFIX + 'skipCount') || 0;
|
|
let query = Session.get(PREFIX + 'searchQuery');
|
|
let dbQuery = [];
|
|
|
|
if(query) {
|
|
_.each(_.keys(query), function(key) {
|
|
if(_.isFunction(query[key])) dbQuery.push({[key]: query[key]}); //dbQuery[key] = query[key]();
|
|
else if(_.isObject(query[key])) dbQuery.push({[key]: query[key]}); //dbQuery[key] = query[key]; //Will look something like: {$in: [xxx,xxx,xxx]}
|
|
else if(_.isNumber(query[key])) dbQuery.push({[key]: query[key]}); //dbQuery[key] = query[key];
|
|
else {
|
|
//dbQuery[key] = {$regex: query[key], $options: 'i'};
|
|
let searchValue = query[key];
|
|
let searches = searchValue && searchValue.length > 0 ? searchValue.split(/\s+/) : undefined;
|
|
|
|
for(let search of searches) {
|
|
dbQuery.push({[key]: {$regex: '\\b' + search, $options: 'i'}});
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
if(!Session.get(PREFIX + "showHidden")) {
|
|
//Ignore any hidden elements by showing those not hidden, or those without the hidden field.
|
|
dbQuery.push({$or: [{hidden: false}, {hidden: {$exists:false}}]});
|
|
}
|
|
|
|
dbQuery = dbQuery.length > 0 ? {$and: dbQuery} : {};
|
|
Session.set(PREFIX + 'productCount', Meteor.collections.Products.find(dbQuery).count()); //Always get a full count.
|
|
return Meteor.collections.Products.find(dbQuery, {limit: QUERY_LIMIT, skip: skipCount, sort: {name: 1}});
|
|
},
|
|
disablePrev: function() {
|
|
return (Session.get(PREFIX + 'skipCount') || 0) == 0;
|
|
},
|
|
disableNext: function() {
|
|
return Session.get(PREFIX + 'productCount') - (Session.get(PREFIX + 'skipCount') || 0) - QUERY_LIMIT <= 0;
|
|
}
|
|
});
|
|
Template.Products.events({
|
|
'click .prevProducts': function(event, template) {
|
|
if(!$(event.target).hasClass('disabled'))
|
|
Session.set(PREFIX + 'skipCount', Math.max(0, (Session.get(PREFIX + 'skipCount') || 0) - QUERY_LIMIT));
|
|
},
|
|
'click .nextProducts': function(event, template) {
|
|
if(!$(event.target).hasClass('disabled'))
|
|
Session.set(PREFIX + 'skipCount', (Session.get(PREFIX + 'skipCount') || 0) + QUERY_LIMIT);
|
|
},
|
|
'click .newProductButton': function(event, template) {
|
|
if(template.$('.newProductButton').hasClass('active')) {
|
|
Session.set(PREFIX + 'displayNewProduct', false);
|
|
}
|
|
else {
|
|
Session.set(PREFIX + 'displayNewProduct', true);
|
|
Session.set(PREFIX + "editedProduct", undefined); //Clear the edited product so that only one editor is open at a time.
|
|
}
|
|
template.$('.newProductButton').toggleClass('active');
|
|
},
|
|
'change input[name="showHidden"]': function(event, template) {
|
|
//console.log("changed " + $(event.target).prop('checked'));
|
|
Session.set(PREFIX + "showHidden", $(event.target).prop('checked'));
|
|
}
|
|
});
|
|
|
|
Template.ProductSearch.events({
|
|
"keyup .searchInput": _.throttle(function(event, template) {
|
|
let searchQuery = Session.get(PREFIX + 'searchQuery') || {};
|
|
let searchFields = Session.get(PREFIX + 'searchFields') || {};
|
|
let searchValue = template.$('.searchInput').val();
|
|
|
|
if(searchValue) {
|
|
if(this.number) searchValue = parseFloat(searchValue);
|
|
|
|
if(this.collection) {
|
|
let ids = Meteor.collections[this.collection].find({[this.collectionQueryColumnName]: {$regex: searchValue, $options: 'i'}}, {fields: {[this.collectionResultColumnName]: 1}}).fetch();
|
|
|
|
//Convert the ids to an array of ids instead of an array of objects containing an id.
|
|
for(let i = 0; i < ids.length; i++) {ids[i] = ids[i]._id;}
|
|
searchQuery[this.columnName] = {$in: ids};
|
|
searchFields[this.columnName] = searchValue;
|
|
}
|
|
else {
|
|
searchFields[this.columnName] = searchQuery[this.columnName] = searchValue;
|
|
}
|
|
}
|
|
else {
|
|
//Remove columns from the search query whose values are empty so we don't bother the database with them.
|
|
delete searchQuery[this.columnName];
|
|
delete searchFields[this.columnName];
|
|
}
|
|
|
|
Session.set(PREFIX + 'searchQuery', searchQuery);
|
|
Session.set(PREFIX + 'searchFields', searchFields);
|
|
Session.set(PREFIX + 'skipCount', 0); //Reset the paging of the results.
|
|
}, 500)
|
|
});
|
|
Template.ProductSearch.helpers({
|
|
searchValue: function() {
|
|
let searchFields = Session.get(PREFIX + 'searchFields');
|
|
|
|
return (searchFields && searchFields[this.columnName]) ? searchFields[this.columnName] : '';
|
|
}
|
|
});
|
|
|
|
Template.Product.helpers({
|
|
measures: function() {
|
|
let result = "";
|
|
|
|
if(this.measures && this.measures.length > 0) {
|
|
let measureNames = [];
|
|
|
|
for(let i = 0; i < this.measures.length; i++) {
|
|
let measureObject = Meteor.collections.Measures.findOne(this.measures[i]);
|
|
|
|
if(measureObject && measureObject.name)
|
|
measureNames.push(measureObject.name);
|
|
}
|
|
|
|
result = measureNames.join(", ");
|
|
}
|
|
|
|
return result;
|
|
},
|
|
aliases: function() {
|
|
return this.aliases.join(', ');
|
|
},
|
|
tags: function() {
|
|
let result = "";
|
|
|
|
if(this.tags && this.tags.length > 0) {
|
|
let tagNames = [];
|
|
|
|
for(let i = 0; i < this.tags.length; i++) {
|
|
let obj = Meteor.collections.ProductTags.findOne(this.tags[i]);
|
|
|
|
if(obj && obj.name)
|
|
tagNames.push(obj.name);
|
|
}
|
|
|
|
result = tagNames.join(", ");
|
|
}
|
|
|
|
return result;
|
|
},
|
|
editing: function() {
|
|
let editedProduct = Session.get(PREFIX + "editedProduct");
|
|
|
|
return editedProduct == this._id;
|
|
},
|
|
getRowClass: function() {
|
|
return this.hidden ? "hidden" : this.deactivated ? "deactivated" : "";
|
|
}
|
|
});
|
|
Template.Product.events({
|
|
"click .actionEdit": function(event, template) {
|
|
Session.set(PREFIX + "editedProduct", this._id);
|
|
Session.set(PREFIX + 'displayNewProduct', false); //Ensure the new product editor is closed.
|
|
template.$('.newProductButton').removeClass('active');
|
|
},
|
|
"click .actionRemove": function(event, template) {
|
|
Meteor.call('deactivateProduct', this._id, function(error, result) {
|
|
if(error) sAlert.error(error);
|
|
else sAlert.success("Product Deactivated");
|
|
});
|
|
},
|
|
'click .actionActivate': function(event, template) {
|
|
Meteor.call('reactivateProduct', this._id, function(error, result) {
|
|
if(error) sAlert.error(error);
|
|
else sAlert.success("Product Reactivated");
|
|
});
|
|
},
|
|
"click .actionShow": function(event, template) {
|
|
Meteor.call('showProduct', this._id, function(error, result) {
|
|
if(error) sAlert.error(error);
|
|
else sAlert.success("Product Visibility Enabled");
|
|
});
|
|
},
|
|
'click .actionHide': function(event, template) {
|
|
Meteor.call('hideProduct', this._id, function(error, result) {
|
|
if(error) sAlert.error(error);
|
|
else sAlert.success("Product Visibility Disabled");
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
Template.ProductEditor.onRendered(function() {
|
|
this.$(".productTagsEditor").select2();
|
|
this.$(".productAliasesEditor").select2({tags: true, tokenSeparators: [';', '.']});
|
|
this.$(".productMeasuresEditor").select2();
|
|
});
|
|
Template.ProductEditor.helpers({
|
|
measures: function() {
|
|
return Meteor.collections.Measures.find({});
|
|
},
|
|
measureSelected: function() {
|
|
let measure = this;
|
|
let product = Template.parentData();
|
|
|
|
return product.measures && product.measures.includes(measure._id) ? "selected" : "";
|
|
},
|
|
aliases: function() {
|
|
return this.aliases;
|
|
},
|
|
tags: function() {
|
|
return Meteor.collections.ProductTags.find({});
|
|
},
|
|
tagSelected: function() {
|
|
let tag = this;
|
|
let product = Template.parentData();
|
|
|
|
return product.tags && product.tags.includes(tag._id) ? "selected" : "";
|
|
}
|
|
});
|
|
Template.ProductEditor.events({
|
|
"click .editorCancel": function(event, template) {
|
|
Session.set(PREFIX + "editedProduct", undefined);
|
|
Session.set(PREFIX + 'displayNewProduct', false);
|
|
template.parentTemplate().$('.newProductButton').removeClass('active');
|
|
},
|
|
"click .editorApply": function(event, template) {
|
|
let name = template.$("input[name='name']").val().trim();
|
|
let tags = template.$(".productTagsEditor").select2('data');
|
|
let aliases = template.$(".productAliasesEditor").select2('data');
|
|
let measures = template.$(".productMeasuresEditor").select2('data');
|
|
|
|
tags = tags.map((n)=>n.id);
|
|
aliases = aliases.map((n)=>n.id);
|
|
measures = measures.map((n)=>n.id);
|
|
|
|
if(Session.get(PREFIX + 'displayNewProduct')) {
|
|
Meteor.call("createProduct", name, tags, aliases, measures, function(error, result) {
|
|
if(error) sAlert.error(error);
|
|
else {
|
|
sAlert.success("Product created.");
|
|
Session.set(PREFIX + 'displayNewProduct', false);
|
|
template.parentTemplate().$('.newProductButton').removeClass('active');
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
Meteor.call("updateProduct", this._id, name, tags, aliases, measures, function(error, result) {
|
|
if(error) sAlert.error(error);
|
|
else {
|
|
sAlert.success("Product updated.");
|
|
Session.set(PREFIX + "editedProduct", undefined);
|
|
template.parentTemplate().$('.newProductButton').removeClass('active');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}); |