2017-01-15 11:33:37 -08:00
|
|
|
|
|
|
|
|
import './Measures.html';
|
|
|
|
|
|
2017-10-20 14:54:58 -07:00
|
|
|
let QUERY_LIMIT = 100;
|
|
|
|
|
let QUERY_LIMIT_INCREMENT = 100;
|
2017-02-03 09:20:29 -08:00
|
|
|
let PREFIX = "Measures.";
|
|
|
|
|
|
|
|
|
|
Tracker.autorun(function() {
|
|
|
|
|
Meteor.subscribe("measures");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.Measures.onCreated(function() {
|
|
|
|
|
Session.set(PREFIX + "displayNewMeasure", false);
|
|
|
|
|
Session.set(PREFIX + "showHidden", false);
|
2017-10-20 14:54:58 -07:00
|
|
|
Session.set(PREFIX + "queryLimit", QUERY_LIMIT);
|
|
|
|
|
});
|
|
|
|
|
Template.Measures.onRendered(function() {
|
|
|
|
|
$(".tableContainer").mCustomScrollbar({
|
|
|
|
|
scrollButtons: {enable:true},
|
|
|
|
|
theme: "light-thick",
|
|
|
|
|
scrollbarPosition: "outside",
|
|
|
|
|
scrollEasing: "linear"
|
|
|
|
|
});
|
2017-02-03 09:20:29 -08:00
|
|
|
});
|
2017-01-15 11:33:37 -08:00
|
|
|
Template.Measures.helpers({
|
2017-02-03 09:20:29 -08:00
|
|
|
displayNewMeasure: function() {
|
|
|
|
|
return Session.get(PREFIX + "displayNewMeasure");
|
|
|
|
|
},
|
|
|
|
|
measures: 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 + 'measureCount', Meteor.collections.Measures.find(dbQuery).count()); //Always get a full count.
|
2017-10-20 14:54:58 -07:00
|
|
|
return Meteor.collections.Measures.find(dbQuery, {limit: Session.get(PREFIX + "queryLimit"), skip: skipCount, sort: {order: 1}});
|
2017-02-03 09:20:29 -08:00
|
|
|
},
|
2017-10-20 14:54:58 -07:00
|
|
|
disableLoadMore: function() {
|
|
|
|
|
return Session.get(PREFIX + 'measureCount') - (Session.get(PREFIX + 'skipCount') || 0) - Session.get(PREFIX + "queryLimit") <= 0;
|
2017-01-15 11:33:37 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Template.Measures.events({
|
2017-10-20 14:54:58 -07:00
|
|
|
'click .loadMoreLink': function(event, template) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
Session.set(PREFIX + 'queryLimit', Session.get(PREFIX + "queryLimit") + QUERY_LIMIT_INCREMENT);
|
2017-02-03 09:20:29 -08:00
|
|
|
},
|
|
|
|
|
'click .newMeasureButton': function(event, template) {
|
|
|
|
|
if(template.$('.newMeasureButton').hasClass('active')) {
|
|
|
|
|
Session.set(PREFIX + 'displayNewMeasure', false);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Session.set(PREFIX + 'displayNewMeasure', true);
|
|
|
|
|
Session.set(PREFIX + "editedMeasure", undefined); //Clear the edited measure so that only one editor is open at a time.
|
|
|
|
|
}
|
|
|
|
|
template.$('.newMeasureButton').toggleClass('active');
|
|
|
|
|
},
|
|
|
|
|
'change input[name="showHidden"]': function(event, template) {
|
|
|
|
|
Session.set(PREFIX + "showHidden", $(event.target).prop('checked'));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.MeasureSearch.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.MeasureSearch.helpers({
|
|
|
|
|
searchValue: function() {
|
|
|
|
|
let searchFields = Session.get(PREFIX + 'searchFields');
|
|
|
|
|
|
|
|
|
|
return (searchFields && searchFields[this.columnName]) ? searchFields[this.columnName] : '';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.Measure.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;
|
|
|
|
|
},
|
|
|
|
|
editing: function() {
|
|
|
|
|
let editedMeasure = Session.get(PREFIX + "editedMeasure");
|
|
|
|
|
|
|
|
|
|
return editedMeasure == this._id;
|
|
|
|
|
},
|
|
|
|
|
getRowClass: function() {
|
|
|
|
|
return this.hidden ? "hidden" : this.deactivated ? "deactivated" : "";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Template.Measure.events({
|
|
|
|
|
"click .actionEdit": function(event, template) {
|
|
|
|
|
Session.set(PREFIX + "editedMeasure", this._id);
|
|
|
|
|
Session.set(PREFIX + 'displayNewMeasure', false); //Ensure the new measure editor is closed.
|
2017-10-20 14:54:58 -07:00
|
|
|
template.parentTemplate().$('.newMeasureButton').removeClass('active');
|
2017-02-03 09:20:29 -08:00
|
|
|
},
|
|
|
|
|
"click .actionRemove": function(event, template) {
|
|
|
|
|
Meteor.call('deactivateMeasure', this._id, function(error, result) {
|
|
|
|
|
if(error) sAlert.error(error);
|
|
|
|
|
else sAlert.success("Measure Deactivated");
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
'click .actionActivate': function(event, template) {
|
|
|
|
|
Meteor.call('reactivateMeasure', this._id, function(error, result) {
|
|
|
|
|
if(error) sAlert.error(error);
|
|
|
|
|
else sAlert.success("Measure Reactivated");
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
"click .actionShow": function(event, template) {
|
|
|
|
|
Meteor.call('showMeasure', this._id, function(error, result) {
|
|
|
|
|
if(error) sAlert.error(error);
|
|
|
|
|
else sAlert.success("Measure Visibility Enabled");
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
'click .actionHide': function(event, template) {
|
|
|
|
|
Meteor.call('hideMeasure', this._id, function(error, result) {
|
|
|
|
|
if(error) sAlert.error(error);
|
|
|
|
|
else sAlert.success("Measure Visibility Disabled");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Template.MeasureEditor.helpers({
|
|
|
|
|
});
|
|
|
|
|
Template.MeasureEditor.events({
|
|
|
|
|
"click .editorCancel": function(event, template) {
|
|
|
|
|
Session.set(PREFIX + "editedMeasure", undefined);
|
|
|
|
|
Session.set(PREFIX + 'displayNewMeasure', false);
|
|
|
|
|
template.parentTemplate().$('.newMeasureButton').removeClass('active');
|
|
|
|
|
},
|
|
|
|
|
"click .editorApply": function(event, template) {
|
|
|
|
|
let name = template.$("input[name='name']").val().trim();
|
|
|
|
|
let postfix = template.$("input[name='postfix']").val().trim();
|
|
|
|
|
let order = 0; //TODO:
|
|
|
|
|
|
|
|
|
|
if(Session.get(PREFIX + 'displayNewMeasure')) {
|
|
|
|
|
Meteor.call("createMeasure", name, postfix, order, function(error, result) {
|
|
|
|
|
if(error) sAlert.error(error);
|
|
|
|
|
else {
|
|
|
|
|
sAlert.success("Measure created.");
|
|
|
|
|
Session.set(PREFIX + 'displayNewMeasure', false);
|
|
|
|
|
template.parentTemplate().$('.newMeasureButton').removeClass('active');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Meteor.call("updateMeasure", this._id, name, postfix, order, function(error, result) {
|
|
|
|
|
if(error) sAlert.error(error);
|
|
|
|
|
else {
|
|
|
|
|
sAlert.success("Measure updated.");
|
|
|
|
|
Session.set(PREFIX + "editedMeasure", undefined);
|
|
|
|
|
template.parentTemplate().$('.newMeasureButton').removeClass('active');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 11:33:37 -08:00
|
|
|
}
|
|
|
|
|
});
|