Commited current refactoring state of working_plan.js (not finished yet).

This commit is contained in:
Alex Tselegidis 2016-04-24 19:28:33 +02:00
parent edc97e771e
commit 3f8fd91714

View file

@ -9,13 +9,17 @@
* @since v1.0.0 * @since v1.0.0
* ---------------------------------------------------------------------------- */ * ---------------------------------------------------------------------------- */
/** (function() {
'use strict';
/**
* Contains the working plan functionality. The working plan DOM elements must be same * Contains the working plan functionality. The working plan DOM elements must be same
* in every page this class is used. * in every page this class is used.
* *
* @class WorkingPlan * @class WorkingPlan
*/ */
var WorkingPlan = function() { var WorkingPlan = function() {
/** /**
* This flag is used when trying to cancel row editing. It is * This flag is used when trying to cancel row editing. It is
* true only whenever the user presses the cancel button. * true only whenever the user presses the cancel button.
@ -31,14 +35,14 @@ var WorkingPlan = function() {
* @type {bool} * @type {bool}
*/ */
this.enableSubmit = false; this.enableSubmit = false;
}; };
/** /**
* Setup the dom elements of a given working plan. * Setup the dom elements of a given working plan.
* *
* @param {object} workingPlan Contains the working hours and breaks for each day of the week. * @param {object} workingPlan Contains the working hours and breaks for each day of the week.
*/ */
WorkingPlan.prototype.setup = function(workingPlan) { WorkingPlan.prototype.setup = function(workingPlan) {
$.each(workingPlan, function(index, workingDay) { $.each(workingPlan, function(index, workingDay) {
if (workingDay != null) { if (workingDay != null) {
$('#' + index).prop('checked', true); $('#' + index).prop('checked', true);
@ -47,7 +51,7 @@ WorkingPlan.prototype.setup = function(workingPlan) {
// Add the day's breaks on the breaks table. // Add the day's breaks on the breaks table.
$.each(workingDay.breaks, function(i, brk) { $.each(workingDay.breaks, function(i, brk) {
var day = WorkingPlan.prototype.convertValueToDay(index); var day = this.convertValueToDay(index);
var tr = var tr =
'<tr>' + '<tr>' +
@ -70,25 +74,25 @@ WorkingPlan.prototype.setup = function(workingPlan) {
'</td>' + '</td>' +
'</tr>'; '</tr>';
$('.breaks').append(tr); $('.breaks').append(tr);
}); }.bind(this));
} else { } else {
$('#' + index).prop('checked', false); $('#' + index).prop('checked', false);
$('#' + index + '-start').prop('disabled', true); $('#' + index + '-start').prop('disabled', true);
$('#' + index + '-end').prop('disabled', true); $('#' + index + '-end').prop('disabled', true);
} }
}); }.bind(this));
// Make break cells editable. // Make break cells editable.
WorkingPlan.prototype.editableBreakDay($('.breaks .break-day')); this.editableBreakDay($('.breaks .break-day'));
WorkingPlan.prototype.editableBreakTime($('.breaks').find('.break-start, .break-end')); this.editableBreakTime($('.breaks').find('.break-start, .break-end'));
}; };
/** /**
* This method makes editable the break day cells. * This method makes editable the break day cells.
* *
* @param {object} $selector The jquery selector ready for use. * @param {object} $selector The jquery selector ready for use.
*/ */
WorkingPlan.prototype.editableBreakDay = function($selector) { WorkingPlan.prototype.editableBreakDay = function($selector) {
var weekDays = {}; var weekDays = {};
weekDays[EALang['monday']] = EALang['monday']; //'Monday'; weekDays[EALang['monday']] = EALang['monday']; //'Monday';
weekDays[EALang['tuesday']] = EALang['tuesday']; //'Tuesday'; weekDays[EALang['tuesday']] = EALang['tuesday']; //'Tuesday';
@ -101,50 +105,58 @@ WorkingPlan.prototype.editableBreakDay = function($selector) {
$selector.editable(function(value, settings) { $selector.editable(function(value, settings) {
return value; return value;
}, { }, {
'type': 'select', type: 'select',
'data': weekDays, data: weekDays,
'event': 'edit', event: 'edit',
'height': '30px', height: '30px',
'submit': '<button type="button" class="hidden submit-editable">Submit</button>', submit: '<button type="button" class="hidden submit-editable">Submit</button>',
'cancel': '<button type="button" class="hidden cancel-editable">Cancel</button>', cancel: '<button type="button" class="hidden cancel-editable">Cancel</button>',
'onblur': 'ignore', onblur: 'ignore',
'onreset': function(settings, td) { onreset: function(settings, td) {
if (!WorkingPlan.prototype.enableCancel) return false; // disable ESC button if (!this.enableCancel) {
}, return false; // disable ESC button
'onsubmit': function(settings, td) {
if (!WorkingPlan.prototype.enableSubmit) return false; // disable Enter button
} }
}.bind(this),
onsubmit: function(settings, td) {
if (!this.enableSubmit) {
return false; // disable Enter button
}
}.bind(this)
}); });
}; };
/** /**
* This method makes editable the break time cells. * This method makes editable the break time cells.
* *
* @param {object} $selector The jquery selector ready for use. * @param {object} $selector The jquery selector ready for use.
*/ */
WorkingPlan.prototype.editableBreakTime = function($selector) { WorkingPlan.prototype.editableBreakTime = function($selector) {
$selector.editable(function(value, settings) { $selector.editable(function(value, settings) {
// Do not return the value because the user needs to press the "Save" button. // Do not return the value because the user needs to press the "Save" button.
return value; return value;
}, { }, {
'event': 'edit', event: 'edit',
'height': '25px', height: '25px',
'submit': '<button type="button" class="hidden submit-editable">Submit</button>', submit: '<button type="button" class="hidden submit-editable">Submit</button>',
'cancel': '<button type="button" class="hidden cancel-editable">Cancel</button>', cancel: '<button type="button" class="hidden cancel-editable">Cancel</button>',
'onblur': 'ignore', onblur: 'ignore',
'onreset': function(settings, td) { onreset: function(settings, td) {
if (!WorkingPlan.prototype.enableCancel) return false; // disable ESC button if (!this.enableCancel) {
}, return false; // disable ESC button
'onsubmit': function(settings, td) {
if (!WorkingPlan.prototype.enableSubmit) return false; // disable Enter button
} }
}.bind(this),
onsubmit: function(settings, td) {
if (!this.enableSubmit) {
return false; // disable Enter button
}
}.bind(this)
}); });
}; };
/** /**
* Binds the event handlers for the working plan dom elements. * Binds the event handlers for the working plan dom elements.
*/ */
WorkingPlan.prototype.bindEventHandlers = function() { WorkingPlan.prototype.bindEventHandlers = function() {
/** /**
* Event: Day Checkbox "Click" * Event: Day Checkbox "Click"
* *
@ -192,12 +204,12 @@ WorkingPlan.prototype.bindEventHandlers = function() {
$('.breaks').prepend(tr); $('.breaks').prepend(tr);
// Bind editable and event handlers. // Bind editable and event handlers.
tr = $('.breaks tr').get()[1]; tr = $('.breaks tr')[1];
WorkingPlan.prototype.editableBreakDay($(tr).find('.break-day')); this.editableBreakDay($(tr).find('.break-day'));
WorkingPlan.prototype.editableBreakTime($(tr).find('.break-start, .break-end')); this.editableBreakTime($(tr).find('.break-start, .break-end'));
$(tr).find('.edit-break').trigger('click'); $(tr).find('.edit-break').trigger('click');
$('.add-break').prop('disabled', true); $('.add-break').prop('disabled', true);
}); }.bind(this));
/** /**
* Event: Edit Break Button "Click" * Event: Edit Break Button "Click"
@ -208,7 +220,9 @@ WorkingPlan.prototype.bindEventHandlers = function() {
// Reset previous editable tds // Reset previous editable tds
var $previousEdt = $(this).closest('table').find('.editable').get(); var $previousEdt = $(this).closest('table').find('.editable').get();
$.each($previousEdt, function(index, edt) { $.each($previousEdt, function(index, edt) {
if (edt.reset !== undefined) edt.reset(); if (edt.reset !== undefined) {
edt.reset();
}
}); });
// Make all cells in current row editable. // Make all cells in current row editable.
@ -244,90 +258,90 @@ WorkingPlan.prototype.bindEventHandlers = function() {
* *
* Bring the ".breaks" table back to its initial state. * Bring the ".breaks" table back to its initial state.
*/ */
$(document).on('click', '.cancel-break', function() { $(document).on('click', '.cancel-break', function(e) {
WorkingPlan.prototype.enableCancel = true; var element = e.target;
$(this).parent().parent().find('.cancel-editable').trigger('click'); this.enableCancel = true;
WorkingPlan.prototype.enableCancel = false; $(element).parent().parent().find('.cancel-editable').trigger('click');
this.enableCancel = false;
$(this).closest('table').find('.edit-break, .delete-break').removeClass('hidden'); $(element).closest('table').find('.edit-break, .delete-break').removeClass('hidden');
$(this).parent().find('.save-break, .cancel-break').addClass('hidden'); $(element).parent().find('.save-break, .cancel-break').addClass('hidden');
$('.add-break').prop('disabled', false); $('.add-break').prop('disabled', false);
}); }.bind(this));
/** /**
* Event: Save Break Button "Click" * Event: Save Break Button "Click"
* *
* Save the editable values and restore the table to its initial state. * Save the editable values and restore the table to its initial state.
*/ */
$(document).on('click', '.save-break', function() { $(document).on('click', '.save-break', function(e) {
// Break's start time must always be prior to break's end. // Break's start time must always be prior to break's end.
var start = Date.parse($(this).parent().parent().find('.break-start input').val()); var element = e.target,
var end = Date.parse($(this).parent().parent().find('.break-end input').val()); start = Date.parse($(element).parent().parent().find('.break-start input').val()),
end = Date.parse($(element).parent().parent().find('.break-end input').val());
if (start > end) { if (start > end) {
$(this).parent().parent().find('.break-end input').val(start.addHours(1).toString('HH:mm')); $(element).parent().parent().find('.break-end input').val(start.addHours(1).toString('HH:mm'));
} }
WorkingPlan.prototype.enableSubmit = true; this.enableSubmit = true;
$(this).parent().parent().find('.editable .submit-editable').trigger('click'); $(element).parent().parent().find('.editable .submit-editable').trigger('click');
WorkingPlan.prototype.enableSubmit = false; this.enableSubmit = false;
$(this).parent().find('.save-break, .cancel-break').addClass('hidden'); $(element).parent().find('.save-break, .cancel-break').addClass('hidden');
$(this).closest('table').find('.edit-break, .delete-break').removeClass('hidden'); $(element).closest('table').find('.edit-break, .delete-break').removeClass('hidden');
$('.add-break').prop('disabled', false); $('.add-break').prop('disabled', false);
}); }.bind(this));
}; };
/** /**
* Get the working plan settings. * Get the working plan settings.
* *
* @returns {object} Returns the working plan settings object. * @returns {object} Returns the working plan settings object.
*/ */
WorkingPlan.prototype.get = function() { WorkingPlan.prototype.get = function() {
var workingPlan = {}; var workingPlan = {};
$('.working-plan input[type="checkbox"]').each(function() { $('.working-plan input[type="checkbox"]').each(function(index, checkbox) {
var id = $(this).attr('id'); var id = $(checkbox).attr('id');
if ($(this).prop('checked') == true) { if ($(checkbox).prop('checked') == true) {
workingPlan[id] = {}; workingPlan[id] = {};
workingPlan[id].start = $('#' + id + '-start').val(); workingPlan[id].start = $('#' + id + '-start').val();
workingPlan[id].end = $('#' + id + '-end').val(); workingPlan[id].end = $('#' + id + '-end').val();
workingPlan[id].breaks = []; workingPlan[id].breaks = [];
$('.breaks tr').each(function(index, tr) { $('.breaks tr').each(function(index, tr) {
var day = WorkingPlan.prototype.convertDayToValue( var day = this.convertDayToValue($(tr).find('.break-day').text());
$(tr).find('.break-day').text());
if (day == id) { if (day == id) {
var start = $(tr).find('.break-start').text(); var start = $(tr).find('.break-start').text(),
var end = $(tr).find('.break-end').text(); end = $(tr).find('.break-end').text();
workingPlan[id].breaks.push({ workingPlan[id].breaks.push({
'start': start, 'start': start,
'end': end 'end': end
}); });
} }
}); }.bind(this));
} else { } else {
workingPlan[id] = null; workingPlan[id] = null;
} }
}); });
return workingPlan; return workingPlan;
}; };
/** /**
* Enables or disabled the timepicker functionality from the working plan input * Enables or disabled the timepicker functionality from the working plan input
* text fields. * text fields.
* *
* @param {bool} disabled (OPTIONAL = false) If true then the timepickers will be * @param {bool} disabled (OPTIONAL = false) If true then the timepickers will be
* disabled. * disabled.
*/ */
WorkingPlan.prototype.timepickers = function(disabled) { WorkingPlan.prototype.timepickers = function(disabled) {
if (disabled == undefined) disabled == false; disabled = disabled || false;
if (disabled == false) { if (disabled == false) {
// Set timepickers where needed. // Set timepickers where needed.
$('.working-plan input[type="text"]').timepicker({ $('.working-plan input[type="text"]').timepicker({
'timeFormat': 'HH:mm', timeFormat: 'HH:mm',
currentText: EALang['now'], currentText: EALang['now'],
closeText: EALang['close'], closeText: EALang['close'],
@ -336,10 +350,10 @@ WorkingPlan.prototype.timepickers = function(disabled) {
hourText: EALang['hour'], hourText: EALang['hour'],
minuteText: EALang['minutes'], minuteText: EALang['minutes'],
'onSelect': function(datetime, inst) { onSelect: function(datetime, inst) {
// Start time must be earlier than end time. // Start time must be earlier than end time.
var start = Date.parse($(this).parent().parent().find('.work-start').val()); var start = Date.parse($(this).parent().parent().find('.work-start').val()),
var end = Date.parse($(this).parent().parent().find('.work-end').val()); end = Date.parse($(this).parent().parent().find('.work-end').val());
if (start > end) { if (start > end) {
$(this).parent().parent().find('.work-end').val(start.addHours(1).toString('HH:mm')); $(this).parent().parent().find('.work-end').val(start.addHours(1).toString('HH:mm'));
@ -349,21 +363,21 @@ WorkingPlan.prototype.timepickers = function(disabled) {
} else { } else {
$('.working-plan input').timepicker('destroy'); $('.working-plan input').timepicker('destroy');
} }
}; };
/** /**
* Reset the current plan back to the company's default working plan. * Reset the current plan back to the company's default working plan.
*/ */
WorkingPlan.prototype.reset = function() { WorkingPlan.prototype.reset = function() {
}; };
/** /**
* This is necessary for translated days. * This is necessary for translated days.
* *
* @param {string} value Day value could be like "monday", "tuesday" etc. * @param {string} value Day value could be like "monday", "tuesday" etc.
*/ */
WorkingPlan.prototype.convertValueToDay = function(value) { WorkingPlan.prototype.convertValueToDay = function(value) {
switch (value) { switch (value) {
case 'monday': case 'monday':
return EALang['monday']; return EALang['monday'];
@ -387,14 +401,14 @@ WorkingPlan.prototype.convertValueToDay = function(value) {
return EALang['sunday']; return EALang['sunday'];
break; break;
} }
}; };
/** /**
* This is necessary for translated days. * This is necessary for translated days.
* *
* @param {string} value Day value could be like "Monday", "Tuesday" etc. * @param {string} value Day value could be like "Monday", "Tuesday" etc.
*/ */
WorkingPlan.prototype.convertDayToValue = function(day) { WorkingPlan.prototype.convertDayToValue = function(day) {
switch (day) { switch (day) {
case EALang['monday']: case EALang['monday']:
return 'monday'; return 'monday';
@ -418,4 +432,8 @@ WorkingPlan.prototype.convertDayToValue = function(day) {
return 'sunday'; return 'sunday';
break; break;
} }
}; };
window.WorkingPlan = WorkingPlan;
})();