File: /home/imensosw/www/imenso.co/timesheet/js/project.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:"projecttable",
file:'./../../inc/project/table.inc.html'
},
{
name:'projectrow',
file:'./../../inc/project/row.inc.html'
},
{
name:'projectsearch',
file:'./../../inc/project/search.inc.html'
},
{
name:'editor',
file:'./../../inc/project/editor.inc.html'
},
{
name:'pagination',
file:'./../../inc/common/pagination.inc.html'
},
{
name:'navbar',
file:'./../../inc/layouts/navbar.inc.html'
}
];
app.project={id:""};
app.filter={
search_title:"",
page:1,
sorting:"project_name",
sortingby:"asc"
};
$(document).ready(function(){
app.setListeners();
app.init(function(){
app.startApp();
});
});
app.setListeners = function() {
app.setLibListeners();
$(document).on('click touch', '#addprojectbutton', function(e) {
e.preventDefault();
var id = 0;
app.editProject(id);
});
$(document).on('click', '#searchprojectbutton', function(e) {
app.filter.search_title=$('#search_title').val();
app.getProject();
});
$(document).on('click', '#projecteditlink', function(e) {
e.preventDefault();
var id = $(this).attr("data-id");
app.editProject(id);
});
$(document).on('click', '#updateprojectbutton', function(e) {
app.handleProject();
});
$(document).on('click', '#projectdeletelink', function(e) {
e.preventDefault();
app.project.id=$(this).attr("data-id");
app.deleteProject();
});
$(document).on('click touch', '.paginationlink a', function(e) {
app.filter.page=$(this).attr("data-page");
app.getProject();
$('.paginationlink').removeClass('active');
$(this).closest('.paginationlink').addClass('active');
});
}
app.launchProject = 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:"project",
type:app.user.acces_level
}));
app.getProject();
}
app.startApp = function(){
app.log("Starting");
app.user = Cookies.getJSON('usertoken');
if (app.user && app.user.token && app.user.token.length > 20) {
app.launchProject();
} else {
window.location.href=app.location;
}
}
app.getProject = function(){
app.projectForm={
search_title:app.filter.search_title,
page:app.filter.page,
sorting:app.filter.sorting,
sortingby:app.filter.sortingby
};
$.ajax({
method: "POST",
url: app.location+"api/project.php",
dataType: "json",
data:{
action: "getproject",
project: app.projectForm,
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.showProjectList();
}
else{
alert("There was an error connecting to the server SR[02]")
}
});
}
app.showProjectList = function(){
var rows = [];
// console.log(app.resp.project);
_.each(app.resp.project, function(project){
rows.push(app.templates.projectrow(project));
});
if($('#projectsearch').length==0)
{
$("#maincontainer").html(app.templates.projectsearch());
}
$("#result").html(app.templates.projecttable({
rows: rows.join("")
}));
$('#pagination').html(app.templates.pagination({
pagination: app.resp.paginationresult
}));
}
app.editProject = function(editId) {
app.log("Got click: ", editId);
var edit = {};
var heading="Add project";
if (_.isNumber(parseInt(editId)) && parseInt(editId) > 0) {
edit = _.findWhere(app.resp.project, {
id: editId
});
heading="Edit project";
} else {
edit = {
id: "",
project_name: "",
start_date: "",
project_desc: "",
};
}
edit.heading=heading;
app.log("Got click: ", edit);
$('#modalContainer').html(app.templates.modal({
'title': edit.heading,
'content': app.templates.editor(edit)
}));
$('#modal').modal('show');
} //- ef
app.handleProject = function(){
app.addProject();
var errors = app.validateProject();
if(errors.length>0)
{
app.alertMessage(errors.join("<br \>"),"warning");
return;
}
app.saveProject();
app.showProjectList(app.resp.projects);
}
app.addProject = function() {
var editId = $('#id').val().trim();
app.projectForm={
id:editId,
projectname:$('#projectname').val().trim(),
projectstartdate:$('#projectstartdate').val().trim(),
projectdesciption:$('#projectdesciption').val().trim(),
}
// app.log(app.projectForm);
}
app.validateProject=function(){
var error=[];
if(app.projectForm.projectname=="")
{
error.push("Please enter project name");
}
if(app.projectForm.projectstartdate=="")
{
error.push("Please enter start date");
}
if(app.projectForm.projectdesciption=="")
{
error.push("Please enter description");
}
return error;
}
app.saveProject=function(){
$('#updateprojectbutton').prop("disabled",true);
$('#updateprojectbutton').html("Saving....");
$.ajax({
method: "POST",
url: app.location+"api/project.php",
dataType: "json",
data: {
action: "saveproject",
project:app.projectForm,
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");
$('#updateprojectbutton').prop("disabled",false);
$('#updateprojectbutton').html("save");
} else {
$("#modal").modal('hide');
app.getProject();
}
}
else {
alert("There was an error connecting to the server SR[02]");
}
}); //- ajax
}//-ef getUser
app.deleteProject=function(){
if (!confirm("Please confirm if you want to delete this record?")) {
return;
}
$.ajax({
method: "POST",
url: app.location+"api/project.php",
dataType: "json",
data: {
action: "deleteproject",
project:app.project,
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.getProject();
}
}
else {
alert("There was an error connecting to the server SR[02]")
}
}); //- ajax
}//-ef deleteProject
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>");
}
;