File: /home/imensosw/public_html/imenso.co/timesheet/js/timesheet.js
// THIS FILE REQURES THAT THE LIB FILE IS LOADED FIRST
if (!_.isObject(app)) {
alert("System Library failed to load [SYSLD01]. Pleasr report this error to support@nthriveeducation.com");
//the app object is defined in lib.js
}
if(app.dev){
app.log("DEV MODE ON");
}
app.files = [{
name:"calendar",
file:'./../../inc/timesheet/calendar.inc.html'
},
{
name:'editor',
file:'./../../inc/timesheet/editor.inc.html'
},
{
name:'timesheettable',
file:'./../../inc/timesheet/table.inc.html'
},
{
name:'timesheetrow',
file:'./../../inc/timesheet/row.inc.html'
},
{
name:'select',
file:'./../../inc/timesheet/select.inc.html'
},
{
name:'navbar',
file:'./../../inc/layouts/navbar.inc.html'
}
];
app.request={id:""};
app.filter={
search_title:"",
page:1,
sorting:"",
sortingby:""
};
$(document).ready(function(){
app.setListeners();
app.init(function(){
app.startApp();
});
});
app.setListeners = function() {
app.setLibListeners();
$(document).on('click', '#userlogoutlink', function(e) {
e.preventDefault();
app.userLogout();
});
$(document).on('click', '#updatetimehistorybutton', function(e){
e.preventDefault();
});
$(document).on('click','#updatetimehistorybutton', function(e){
e.preventDefault();
app.handleForm();
})
$(document).on('click','.slick-prev,.slick-next',function(){
app.getTimesheet();
});
$(document).on('click','.slick-slide',function(){
$('.slick-slide').removeClass('slick-current');
$(this).addClass('slick-current');
app.getTimesheet();
});
$(document).on('click', '#timesheetdeletelink',function(e) {
e.preventDefault();
app.request.id = $(this).attr('data-id');
app.deleteTimesheet();
});
}
app.launchUser = function() {
app.log("Launching with user ",app.user);
$.ajax({
method: "POST",
url: app.location+"api/imensoapi.php",
dataType: "json",
data: {
action: "launchload",
user: app.user
}
})
.fail(function() {
alert("There was an error connecting to the server SR[01]")
})
.done(function(resp) {
if (resp.resp == 'OK') {
if (resp.error) {
alert("Transaction error. " + resp.error);
} else {
app.log("User launched");
app.user = Cookies.getJSON('usertoken');
app.launcheScreen();
}
} else {
alert("There was an error connecting to the server SR[02]")
}
}); //- ajax
} //- launchUser
app.launcheScreen=function(){
$("body").prepend(app.templates.navbar({
active:"timesheet",
type:app.user.acces_level
}));
app.getAdd();
}
app.startApp = function(){
app.log("Starting");
app.user = Cookies.getJSON('usertoken');
if (app.user && app.user.token && app.user.token.length > 20) {
app.launchUser();
} else {
window.location.href=app.location;
}
}
app.getTimesheet=function(){
app.timesheetForm={
search_title:app.filter.search_title,
page:app.filter.page,
sorting:app.filter.sorting,
sortingby:app.filter.sortingby,
user_id:app.user.id,
date:$('.slick-current').find('.date').attr('data-date')
};
$.ajax({
method: "POST",
url: app.location+"api/timesheet.php",
dataType: "json",
data:{
action: "gettimesheet",
timesheet: app.timesheetForm,
token:app.user.token
}
}).fail(function(){
alert("There was an error connecting to the server SR[01]");
}).done(function(resp){
if(resp.resp == "OK")
{
app.resp = resp;
app.showTimesheetList();
}
else{
alert("There was an error connecting to the server SR[02]")
}
});
}
app.getAdd=function(){
$.ajax({
method: "POST",
url: app.location+"api/timesheet.php",
dataType: "json",
data:{
action: "getadd",
token:app.user.token
}
}).fail(function(){
alert("There was an error connecting to the server SR[01]");
}).done(function(resp){
if(resp.resp == "OK")
{
app.respprojecttask = resp;
if($('.slider').length==0)
{
$('#maincontainer').html(app.templates.calendar());
setTimeout(function(){
$('.chosen-select').chosen('update');
},100);
}
app.getTimesheet();
}
else{
alert("There was an error connecting to the server SR[02]")
}
});
}
app.showTimesheetList=function(){
var rows = [];
var projects = [];
var tasks = [];
var hours=0;
var minute=0;
_.each(app.resp.timesheets, function(time){
tm=time.time.split(":");
hours+=Number(tm[0]);
minute+=Number(tm[1]);
rows.push(app.templates.timesheetrow(time));
});
if(minute>59)
{
remaningHours=Math.floor(minute/60);
minute=minute%60;
hours+=Number(remaningHours);
}
if($('.editor').length==0)
{
_.each(app.respprojecttask.getproject, function(getp){
projects.push(app.templates.select(getp));
});
_.each(app.respprojecttask.gettask, function(gett){
tasks.push(app.templates.select(gett));
});
$("#addform").html(app.templates.editor({
projects: projects.join(""),
tasks: tasks.join("")
}));
}
$("#result").html(app.templates.timesheettable({
rows: rows.join("")
}));
$('.tital_time').html(hours+":"+minute);
}
app.handleForm = function(){
app.makeForm();
var errors = app.validateForm();
if(errors.length>0)
{
app.alertMessage(errors.join("<br \>"),"warning");
return;
}
app.saveAddForm();
// app.showUserList(app.resp.users);
}
app.makeForm = function(){
app.timeAddForm= {
project_name:$("#selectproject").val().trim(),
task_name:$("#selecttask").val().trim(),
time: $("#time").val(),
comment: $("#comment").val().trim(),
date:$('.slick-current').find('.date').attr('data-date'),
user_id:app.user.id
}
}
app.validateForm=function(){
var regex=/^\d{2}:\d{2}$/;
regex=/((1[0-2]|0?[1-9]):(1[0-7]|0?[0-9]))/;
var error=[];
if(app.timeAddForm.project_name=="")
{
error.push("Please select project");
}
if(app.timeAddForm.task_name=="")
{
error.push("Please select task");
}
if(app.timeAddForm.time=="")
{
error.push("Please enter time");
}
else if (!regex.test(app.timeAddForm.time) )
{
error.push("Please enter valid time HH:MM");
}
if(app.timeAddForm.comment=="")
{
error.push("Please enter comment");
}
return error;
}
app.saveAddForm = function(){
$('#updatetimehistorybutton').prop("disabled",true);
$('#updatetimehistorybutton').html("Saving....");
$.ajax({
method: "POST",
url: app.location+"api/timesheet.php",
dataType: "json",
data: {
action: "saveaddform",
timesheet:app.timeAddForm,
token:app.user.token
}
})
.fail(function() {
alert("There was an error connecting to the server SR[01]")
})
.done(function(resp) {
if(resp.msg.length>0 && resp.msg=="timeout")
{
app.userLogout();
}
else if (resp.resp == 'OK') {
if(resp.msg.length) {
app.alertMessage("Failed User Save. " + resp.msg,"warning");
$('#updatetimehistorybutton').prop("disabled",false);
$('#updatetimehistorybutton').html("save");
} else {
$('#updatetimehistorybutton').prop("disabled",false);
$('#updatetimehistorybutton').html("save");
$(".editor").trigger("reset");
app.getTimesheet();
/* $("#modal").modal('hide');
app.getUser();*/
}
}
else {
alert("There was an error connecting to the server SR[02]");
}
}); //- ajax
}
app.deleteTimesheet=function(){
if (!confirm("Please confirm if you want to delete this record?")) {
return;
}
var timesheet={
id:app.request.id
};
$.ajax({
method: "POST",
url: app.location+"api/timesheet.php",
dataType: "json",
data: {
action: "deletetimesheet",
timesheet:timesheet,
token:app.user.token
}
})
.fail(function() {
alert("There was an error connecting to the server SR[01]")
})
.done(function(resp) {
if(resp.msg.length>0 && resp.msg=="timeout")
{
app.userLogout();
}
else if (resp.resp == 'OK') {
if(resp.msg.length){
app.alertMessage("<strong>Failed:</strong> " + resp.msg,"danger");
} else {
app.getTimesheet();
}
}
else {
alert("There was an error connecting to the server SR[02]")
}
}); //- ajax
}//-ef deleteUser
app.loadTemplates = function() {
app.templates.modal = _.template("<div class='modal fade' tabindex='-1' role='dialog' id='modal'><div class='modal-dialog' role='document'><div class='modal-content'><div class='modal-header'><h5 class='modal-title'><%=title%></h5><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>×</span></button></div><div class='modal-body'><%=content%></div><div style='display:none' class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>Close</button></div></div></div></div>");
}
;