Skip to content

Instantly share code, notes, and snippets.

@kriskornel
Created November 27, 2017 09:58
Show Gist options
  • Save kriskornel/b6abceab24bc792ca9ebb31d5e329244 to your computer and use it in GitHub Desktop.
Save kriskornel/b6abceab24bc792ca9ebb31d5e329244 to your computer and use it in GitHub Desktop.
SCHEDULE LOGIC USING VUE.JS
window.App = new Vue({
el: '#app',
data: {
low: 0,
median: 0,
high: 0,
duration: 0,
lecture_duration: [],
weekdays: {
mon: [],
tue: [],
wed: [],
thu: [],
fri: [],
sat: [],
sun: []
}
},
created: function() {
EventBus.$on('hour-check', function(day, hour, checked) {
App.changeWeekdays(day, hour, checked);
});
},
computed: {
durationClass: function() {
var duration = this.duration;
if(duration == 1) {
return 4;
}
if(duration == 2) {
return 12;
}
return 0;
},
partitionTime: function() {
var morning = false;
var afternoon = false;
var evening = false;
var groupWeekdays = this.groupWeekdays;
_.mapObject(groupWeekdays, function(val, key) {
if(_.isEqual(morning, false)) {
if(_.has(groupWeekdays[key], 'morning')) {
morning = true;
}
}
if(_.isEqual(afternoon, false)) {
if(_.has(groupWeekdays[key], 'afternoon')) {
afternoon = true;
}
}
if(_.isEqual(evening, false)) {
if(_.has(groupWeekdays[key], 'evening')) {
evening = true;
}
}
}.bind(this));
return morning + afternoon + evening;
},
groupWeekdays: function() {
var groupWeekdays = {
mon: {},
tue: {},
wed: {},
thu: {},
fri: {},
sat: {},
sun: {}
};
_.mapObject(this.weekdays, function(val, key) {
if(this.morningHour(val) > 0) {
groupWeekdays[key]['morning'] = this.morningHour(val);
}
if(this.afternoonHour(val) > 0) {
groupWeekdays[key]['afternoon'] = this.afternoonHour(val);
}
if(this.eveningHour(val) > 0) {
groupWeekdays[key]['evening'] = this.eveningHour(val);
}
}.bind(this));
return groupWeekdays;
},
getAll: function() {
var weekHours = {};
var duration = this.durationClass;
// Duration Start With 4 weeks.
if(duration > 3) {
var countMorning = this.totalMorningInWeek() * duration;
var countAfternoon = this.totalAfternoonInWeek() * duration;
var countEvening = this.totalEveningInWeek() * duration;
var countAll = countMorning + countAfternoon + countEvening;
// Just set object property when count is not zero
if(countMorning > 0 ) {
weekHours['morning'] = countMorning;
}
if(countAfternoon > 0 ) {
weekHours['afternoon'] = countAfternoon;
}
if(countEvening > 0 ) {
weekHours['evening'] = countEvening;
}
var lowest = _.min(weekHours);
var highest = _.max(weekHours);
var median = countAll / _.size(weekHours);
return { 'lowest': lowest, 'median': median, 'highest': highest };
}
return { 'lowest': null, 'median': null, 'highest': null };
},
lowestHourCount: function() {
var count = [];
_.each(this.groupWeekdays, function(value, key, list) {
// Get lowest total hour each day in groupWeekdays
var getValue = _.values(value);
var min = _.min(getValue);
if(_.isNumber(min)) {
count.push(min);
}
});
var result = _.min(count);
if(result == 'Infinity') {
return null;
}
return result;
},
highestHourCount: function() {
var count = [];
_.each(this.groupWeekdays, function(value, key, list) {
// Get lowest total hour each day in groupWeekdays
var getValue = _.values(value);
var max = _.max(getValue);
if(_.isNumber(max)) {
count.push(max);
}
});
var result = _.max(count);
if(result == '-Infinity') {
return null;
}
return result;
}
},
filters: {
inMinutes: function(hour) {
var roundHour = Math.round(hour);
return roundHour * 60;
}
},
methods: {
addToCount: function (key, hours) {
this.weekdays[key].push(hours);
},
morningHour: function(items) {
var hour = _.filter(items, function(num) {
return num >= 7 && num <= 12;
});
return _.size(hour);
},
afternoonHour: function(items) {
var hour = _.filter(items, function(num) {
return num >= 13 && num <= 17 ;
});
return _.size(hour);
},
eveningHour: function(items) {
var hour = _.filter(items, function(num) {
return num >= 18 && num <= 24;
});
return _.size(hour);
},
sortBy: function(items) {
return _.sortBy(items, function(num) {
return num;
});
},
changeWeekdays: function(day, hour, checked) {
var weekdays = this.weekdays[day];
var getChecked = !checked;
var exists = _.contains(weekdays, hour);
if(getChecked) {
if(!exists) {
weekdays.push(hour);
}
} else {
if(exists) {
var remaining = _.without(weekdays, hour);
this.$set(this.weekdays, day, remaining);
}
}
},
totalMorningInWeek: function() {
var groupWeekdays = this.groupWeekdays;
var total = 0;
_.mapObject(groupWeekdays, function(val, key) {
if( _.has(groupWeekdays[key], 'morning') ) {
total+=groupWeekdays[key].morning;
}
});
return total;
},
totalAfternoonInWeek: function() {
var groupWeekdays = this.groupWeekdays;
var total = 0;
_.mapObject(groupWeekdays, function(val, key) {
if( _.has(groupWeekdays[key], 'afternoon') ) {
total+=groupWeekdays[key].afternoon;
}
});
return total;
},
totalEveningInWeek: function() {
var groupWeekdays = this.groupWeekdays;
var total = 0;
_.mapObject(groupWeekdays, function(val, key) {
if( _.has(groupWeekdays[key], 'evening') ) {
total+=groupWeekdays[key].evening;
}
});
return total;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment