File: /home/imensosw/public_html/imenso.co/timesheet/js/lib.js
/* global $,_ *,Rollbar */
// THIS FILE MUST BE LOADED FIRST
var app = {
//dev: false,
templates: {}
}
if(!window.console){
window.console = {
log:function(){}
};
}
if (app.dev || window.location.href.match(/imenso\.co/i)) { // local url of mahesh-pc
app.location = 'https://imenso.co/timesheet/';
}
else if (app.dev || window.location.href.match(/timesheet/i)) { // imenso.co url
app.location = 'http://192.168.1.13/timesheet/';
}
else {
app.location = 'hs';
}
app.setLibListeners = function() {
$(document).on('click','#submitloginbutton',function(e){
e.preventDefault();
app.checkLogin();
});
$(document).on('click touch', '#userlogoutlink', function(e) {
e.preventDefault();
app.userLogout();
});
/*$(document).on('click','#timesheet_link',function(e){
e.preventDefault();
window.location.href = app.location+"timesheet";
});
$(document).on('click','#project_link',function(e){
e.preventDefault();
window.location.href = app.location+"project";
});
$(document).on('click','#team_link',function(e){
e.preventDefault();
window.location.href = app.location+"team";
});*/
}
app.loadAssets = function(cb){
var count = 0;
_.each(app.files, function(f){
app.log("Loading: ", f.file);
$.get("inc/"+f.file, function(html){
app.templates[f.name] = _.template(html);
count++;
if(count == app.files.length){
cb();
}
}).fail(function(){
//app.warn("Got error from ajax call load", resp);
alert("There was an error");
});
});
}
app.init = function(cb){
app.log("loading config");
app.loadAssets(function(){
app.log("template loaded");
app.loadTemplates();
app.log("Init complete");
if (_.isFunction(cb)) {
cb();
}
});
}
app.checkLogin = function() {
var username = $("#inputEmail").val().trim();
var password = $("#inputPassword").val().trim();
if (username.length == 0 || password.length == 0) {
app.log("Please enter username and password","warning");
return;
}
$.ajax({
method: "POST",
url: "api/imensoapi.php",
datatype: "json",
data:{
action: "checklogin",
username: username,
password: password
}
}).fail(function(){
//alert("There was an error connecting to the server SR[01]");
}).done(function(resp) {
if (resp.resp == 'OK') {
if (resp.msg.length>0 || resp.token.length < 10) {
app.alertMessage("Failed login. " + resp.msg,"warning");
} else {
app.user = {
id:resp.id,
name: username,
token: resp.token,
acces_level:resp.access_level,
};
Cookies.set('usertoken', app.user , {
expires: 90,
path:''
});
//app.log("COOKIES: ", Cookies.get('usertoken'));
if(resp.access_level== "admin")
{
window.location.href="timesheet";
/*if (window.location.href.indexOf('timesheet')) {
//Hide the element.
$("#project_link").show();
$("#team_link").show();
}*/
}
else if(resp.access_level== "user")
{
window.location.href="timesheet";
/*if (window.location.href.indexOf('timesheet')) {
//Hide the element.
$("#project_link").hide();
$("#team_link").hide();
}*/
}
//app.launchUser();
//app.init();
}
} else {
alert("There was an error connecting to the server SR[02]")
}
}); //- ajax
} //-ef checkLogin
app.userLogout = function() {
//app.log("Logout successfully asas");
/* $('#modalContainer').html(app.templates.modal({
'title': 'User Logout',
'content': 'You have successfully logged out'
}));*/
//$('#useraccess').hide();
//$('#userlogout').hide();
//$('#userlogin').show();
$('#maincontainer').html('');
Cookies.remove('usertoken', {
path:''
});
// app.log(Cookies.get('usertoken'));
window.location.href=app.location+"";
//app.init();
} //-showLogin
app.alertMessage=function(msg,type){
var randId=Math.random().toString(36).substring(7);
msg='<a href="#" id="'+randId+'" class="close" aria-label="close">×</a>'+msg;
msg="<div class='alert alert-"+type+"'>"+msg+"</div>";
$(".error_section").prepend(msg);
$('#'+randId).closest(".alert-"+type).show();
setTimeout(function(){
$('#'+randId).closest(".alert-"+type).remove();
},5000);
}
//loader start
app.loader= function(){
$.ajaxSetup({
beforeSend: function(){
var progressBar = $('#loader');
if (progressBar.length == 0) {
$('body').append('<div id="loader" class="loader"><dt></dt><dd></dd></div>');
}
$('#loader').animate({
width:'30%'
},200);
},
complete: function() {
$('#loader').animate({
width: '101%'
}, 50);
$('#loader').delay(200);
$('#loader').fadeOut({
complete: function() {
$('#loader').remove();
}
}, 200);
},
success: function(resp) {
if (resp.error == true && resp.msg == 'timeout out') {
window.location.href=app.location+"logout.html";
}
},
error: function() {
$('#ytLoad').remove();
}
});
}
app.loader();
app.validateEmail = function(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
app.log = function() {
if (app) {
switch (arguments.length) { // yes this is janky, blame IE
case 1:
console.log(arguments[0]);
break;
case 2:
console.log(arguments[0], arguments[1]);
break;
case 3:
console.log(arguments[0], arguments[1], arguments[2]);
break;
case 4:
console.log(arguments[0], arguments[1], arguments[2], arguments[3]);
break;
}
}
}; //- app.log
app.warn = function() {
switch (arguments.length) {
case 1:
console.log(arguments[0]);
break;
case 2:
console.log(arguments[0], arguments[1]);
break;
case 3:
console.log(arguments[0], arguments[1], arguments[2]);
break;
}
if (typeof Rollbar != "undefined") {
if (app.location == 'imenso-timesheet') {
switch (arguments.length) {
case 1:
Rollbar.warn(arguments[0]);
break;
case 2:
Rollbar.warn(arguments[0], arguments[1]);
break;
case 3:
Rollbar.warn(arguments[0], arguments[1], arguments[2]);
break;
case 4:
Rollbar.warn(arguments[0], arguments[1], arguments[2], arguments[3]);
break;
}
}
}
}; //- app.warn;