/* This file is part of Ice Framework. Ice Framework is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Ice Framework is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Ice Framework. If not, see . ------------------------------------------------------------------ Original work Copyright (c) 2012 [Gamonoid Media Pvt. Ltd] Developer: Thilina Hasantha (thilina.hasantha[at]gmail.com / facebook.com/thilinah) */ function AdapterBase(endPoint) { } this.moduleRelativeURL = null; this.tableData = new Array(); this.sourceData = new Array(); this.filter = null; this.origFilter = null; this.orderBy = null; this.currentElement = null; AdapterBase.inherits(IceHRMBase); AdapterBase.method('initAdapter' , function(endPoint,tab,filter,orderBy) { this.moduleRelativeURL = baseUrl; this.table = endPoint; if(tab == undefined || tab == null){ this.tab = endPoint; }else{ this.tab = tab; } if(filter == undefined || filter == null){ this.filter = null; }else{ this.filter = filter; } this.origFilter = this.filter; if(orderBy == undefined || orderBy == null){ this.orderBy = null; }else{ this.orderBy = orderBy; } this.trackEvent("initAdapter",tab); this.requestCache = new RequestCache(); }); AdapterBase.method('setFilter', function(filter) { this.filter = filter; }); AdapterBase.method('getFilter', function() { return this.filter; }); AdapterBase.method('setOrderBy', function(orderBy) { this.orderBy = orderBy; }); AdapterBase.method('getOrderBy', function() { return this.orderBy; }); /** * @method add * @param object {Array} object data to be added to database * @param getFunctionCallBackData {Array} once a success is returned call get() function for this module with these parameters * @param callGetFunction {Boolean} if false the get function of the module will not be called (default: true) * @param successCallback {Function} this will get called after success response */ AdapterBase.method('add', function(object,getFunctionCallBackData,callGetFunction,successCallback) { var that = this; if(callGetFunction == undefined || callGetFunction == null){ callGetFunction = true; } $(object).attr('a','add'); $(object).attr('t',this.table); that.showLoader(); $.post(this.moduleRelativeURL, object, function(data) { if(data.status == "SUCCESS"){ that.addSuccessCallBack(getFunctionCallBackData,data.object, callGetFunction, successCallback, that); }else{ that.addFailCallBack(getFunctionCallBackData,data.object); } },"json").always(function() {that.hideLoader()}); this.trackEvent("add",this.tab,this.table); }); AdapterBase.method('addSuccessCallBack', function(callBackData,serverData, callGetFunction, successCallback, thisObject) { if(callGetFunction){ this.get(callBackData); } this.initFieldMasterData(); if(successCallback != undefined && successCallback != null){ successCallback.apply(thisObject,[serverData]); } this.trackEvent("addSuccess",this.tab,this.table); }); AdapterBase.method('addFailCallBack', function(callBackData,serverData) { try{ this.closePlainMessage(); }catch(e){} this.showMessage("Error saving",serverData); this.trackEvent("addFailed",this.tab,this.table); }); AdapterBase.method('deleteObj', function(id,callBackData) { var that = this; that.showLoader(); $.post(this.moduleRelativeURL, {'t':this.table,'a':'delete','id':id}, function(data) { if(data.status == "SUCCESS"){ that.deleteSuccessCallBack(callBackData,data.object); }else{ that.deleteFailCallBack(callBackData,data.object); } },"json").always(function() {that.hideLoader()}); this.trackEvent("delete",this.tab,this.table); }); AdapterBase.method('deleteSuccessCallBack', function(callBackData,serverData) { this.get(callBackData); this.clearDeleteParams(); }); AdapterBase.method('deleteFailCallBack', function(callBackData,serverData) { this.clearDeleteParams(); this.showMessage("Error Occurred while Deleting Item",serverData); }); AdapterBase.method('get', function(callBackData) { var that = this; if(this.getRemoteTable()){ this.createTableServer(this.getTableName()); $("#"+this.getTableName()+'Form').hide(); $("#"+this.getTableName()).show(); return; } var sourceMappingJson = JSON.stringify(this.getSourceMapping()); var filterJson = ""; if(this.getFilter() != null){ filterJson = JSON.stringify(this.getFilter()); } var orderBy = ""; if(this.getOrderBy() != null){ orderBy = this.getOrderBy(); } sourceMappingJson = this.fixJSON(sourceMappingJson); filterJson = this.fixJSON(filterJson); that.showLoader(); $.post(this.moduleRelativeURL, {'t':this.table,'a':'get','sm':sourceMappingJson,'ft':filterJson,'ob':orderBy}, function(data) { if(data.status == "SUCCESS"){ that.getSuccessCallBack(callBackData,data.object); }else{ that.getFailCallBack(callBackData,data.object); } },"json").always(function() {that.hideLoader()}); that.initFieldMasterData(); this.trackEvent("get",this.tab,this.table); //var url = this.getDataUrl(); //console.log(url); }); AdapterBase.method('getDataUrl', function(columns) { var that = this; var sourceMappingJson = JSON.stringify(this.getSourceMapping()); var columns = JSON.stringify(columns); var filterJson = ""; if(this.getFilter() != null){ filterJson = JSON.stringify(this.getFilter()); } var orderBy = ""; if(this.getOrderBy() != null){ orderBy = this.getOrderBy(); } var url = this.moduleRelativeURL.replace("service.php","data.php"); url = url+"?"+"t="+this.table; url = url+"&"+"sm="+this.fixJSON(sourceMappingJson); url = url+"&"+"cl="+this.fixJSON(columns); url = url+"&"+"ft="+this.fixJSON(filterJson); url = url+"&"+"ob="+orderBy; if(this.isSubProfileTable()){ url = url+"&"+"type=sub"; } if(this.remoteTableSkipProfileRestriction()){ url = url+"&"+"skip=1"; } return url; }); AdapterBase.method('isSubProfileTable', function() { return false; }); AdapterBase.method('remoteTableSkipProfileRestriction', function() { return false; }); AdapterBase.method('preProcessTableData', function(row) { return row; }); AdapterBase.method('getSuccessCallBack', function(callBackData,serverData) { var data = []; var mapping = this.getDataMapping(); for(var i=0;i
  • '; var editButton = ''; var table = $('
    '); //add Header var header = this.getSubHeader(); table.append(header); if(data.length == 0){ table.append(''+this.getNoDataMessage()+''); }else{ for(var i=0;i

    '+this.getSubHeaderTitle()+'

    '); return header; }); /** * IdNameAdapter */ function IdNameAdapter(endPoint) { this.initAdapter(endPoint); } IdNameAdapter.inherits(AdapterBase); IdNameAdapter.method('getDataMapping', function() { return [ "id", "name" ]; }); IdNameAdapter.method('getHeaders', function() { return [ { "sTitle": "ID" ,"bVisible":false}, { "sTitle": "Name"} ]; }); IdNameAdapter.method('getFormFields', function() { return [ [ "id", {"label":"ID","type":"hidden"}], [ "name", {"label":"Name","type":"text","validation":""}] ]; }); /** * ApproveAdminAdapter */ function ApproveAdminAdapter(endPoint,tab,filter,orderBy) { this.initAdapter(endPoint,tab,filter,orderBy); } ApproveAdminAdapter.inherits(AdapterBase); ApproveAdminAdapter.method('openStatus', function(id,status) { $('#'+this.itemNameLower+'StatusModel').modal('show'); $('#'+this.itemNameLower+'_status').val(status); this.statusChangeId = id; }); ApproveAdminAdapter.method('closeDialog', function() { $('#'+this.itemNameLower+'StatusModel').modal('hide'); }); ApproveAdminAdapter.method('changeStatus', function() { var status = $('#'+this.itemNameLower+'_status').val(); var reason = $('#'+this.itemNameLower+'_reason').val(); if(status == undefined || status == null || status == ""){ this.showMessage("Error", "Please select "+this.itemNameLower+" status"); return; } var object = {"id":this.statusChangeId,"status":status,"reason":reason}; var reqJson = JSON.stringify(object); var callBackData = []; callBackData['callBackData'] = []; callBackData['callBackSuccess'] = 'changeStatusSuccessCallBack'; callBackData['callBackFail'] = 'changeStatusFailCallBack'; this.customAction('changeStatus','admin='+this.modulePathName,reqJson,callBackData); this.closeDialog(); this.statusChangeId = null; }); ApproveAdminAdapter.method('changeStatusSuccessCallBack', function(callBackData) { this.showMessage("Successful", this.itemName + " Request status changed successfully"); this.get([]); }); ApproveAdminAdapter.method('changeStatusFailCallBack', function(callBackData) { this.showMessage("Error", "Error occurred while changing "+this.itemName+" request status"); }); ApproveAdminAdapter.method('getActionButtonsHtml', function(id,data) { var editButton = ''; var deleteButton = ''; var statusChangeButton = ''; var html = '
    _edit__delete__status_
    '; html = html.replace('_status_',statusChangeButton); if(this.showDelete){ html = html.replace('_delete_',deleteButton); }else{ html = html.replace('_delete_',''); } if(this.showEdit){ html = html.replace('_edit_',editButton); }else{ html = html.replace('_edit_',''); } html = html.replace(/_id_/g,id); html = html.replace(/_BASE_/g,this.baseUrl); return html; }); ApproveAdminAdapter.method('isSubProfileTable', function() { if(this.user.user_level == "Admin"){ return false; }else{ return true; } }); /** * ApproveModuleAdapter */ function ApproveModuleAdapter(endPoint,tab,filter,orderBy) { this.initAdapter(endPoint,tab,filter,orderBy); } ApproveModuleAdapter.inherits(AdapterBase); ApproveModuleAdapter.method('cancelRequest', function(id) { var that = this; var object = {}; object['id'] = id; var reqJson = JSON.stringify(object); var callBackData = []; callBackData['callBackData'] = []; callBackData['callBackSuccess'] = 'cancelSuccessCallBack'; callBackData['callBackFail'] = 'cancelFailCallBack'; this.customAction('cancel','modules='+this.modulePathName,reqJson,callBackData); }); ApproveModuleAdapter.method('cancelSuccessCallBack', function(callBackData) { this.showMessage("Successful", this.itemName + " cancellation request sent"); this.get([]); }); ApproveModuleAdapter.method('cancelFailCallBack', function(callBackData) { this.showMessage("Error Occurred while cancelling "+this.itemName, callBackData); }); ApproveModuleAdapter.method('getActionButtonsHtml', function(id,data) { var editButton = ''; var deleteButton = ''; var requestCancellationButton = ''; var html = '
    _edit__delete_
    '; if(this.showDelete){ if(data[7] == "Approved"){ html = html.replace('_delete_',requestCancellationButton); }else{ html = html.replace('_delete_',deleteButton); } }else{ html = html.replace('_delete_',''); } if(this.showEdit){ html = html.replace('_edit_',editButton); }else{ html = html.replace('_edit_',''); } html = html.replace(/_id_/g,id); html = html.replace(/_BASE_/g,this.baseUrl); return html; }); /** * TableEditAdapter */ function TableEditAdapter(endPoint) { this.initAdapter(endPoint); this.cellDataUpdates = {}; this.modulePath = ''; this.rowFieldName = ''; this.columnFieldName = ''; this.rowTable = ''; this.columnTable = ''; this.valueTable = ''; this.csvData = []; } TableEditAdapter.inherits(AdapterBase); TableEditAdapter.method('setModulePath', function(path) { this.modulePath = path; }); TableEditAdapter.method('setRowFieldName', function(name) { this.rowFieldName = name; }); TableEditAdapter.method('setTables', function(rowTable, columnTable, valueTable) { this.rowTable = rowTable; this.columnTable = columnTable; this.valueTable = valueTable; }); TableEditAdapter.method('setColumnFieldName', function(name) { this.columnFieldName = name; }); TableEditAdapter.method('getDataMapping', function() { return [ ]; }); TableEditAdapter.method('getFormFields', function() { return [ ]; }); TableEditAdapter.method('get', function() { this.getAllData(); }); TableEditAdapter.method('getAllData', function(save) { var req = {}; req.rowTable = this.rowTable; req.columnTable = this.columnTable; req.valueTable = this.valueTable; req = this.addAdditionalRequestData('getAllData', req); req.save = (save == undefined || save == null || save == false)?0:1; var reqJson = JSON.stringify(req); var callBackData = []; callBackData['callBackData'] = []; callBackData['callBackSuccess'] = 'getAllDataSuccessCallBack'; callBackData['callBackFail'] = 'getAllDataFailCallBack'; this.customAction('getAllData',this.modulePath,reqJson,callBackData); }); TableEditAdapter.method('getDataItem', function(row,column,allData) { var columnData = allData[1]; var rowData = allData[0]; var serverData = allData[2]; if(column == -1){ return rowData[row].name; }else{ return this.getDataItemByKeyValues(this.rowFieldName, rowData[row].id, this.columnFieldName, columnData[column].id, serverData); } }); TableEditAdapter.method('getDataItemByKeyValues', function(rowKeyName, rowKeyVal, colKeyName, colKeyVal, data) { for(var i=0;i
    '; //Find current page var activePage = $('#'+elementId +" .dataTables_paginate .active a").html(); var start = 0; if(activePage != undefined && activePage != null){ start = parseInt(activePage, 10)*15 - 15; } $('#'+elementId).html(html); var dataTableParams = { "oLanguage": { "sLengthMenu": "_MENU_ records per page" }, "aaData": data, "aoColumns": headers, "bSort": false, "iDisplayLength": 15, "iDisplayStart": start }; var customTableParams = this.getCustomTableParams(); $.extend(dataTableParams, customTableParams); $('#'+elementId+' #grid').dataTable( dataTableParams ); $(".dataTables_paginate ul").addClass("pagination"); $(".dataTables_length").hide(); $(".dataTables_filter input").addClass("form-control"); $(".dataTables_filter input").attr("placeholder","Search"); $(".dataTables_filter label").contents().filter(function(){ return (this.nodeType == 3); }).remove(); //$('.tableActionButton').tooltip(); $('#'+elementId+' #grid').editableTableWidget(); $('#'+elementId+' #grid .editcell').on('validate', function(evt, newValue) { return modJs.validateCellValue($(this), evt, newValue); }); }); TableEditAdapter.method('addCellDataUpdate' , function(colId, rowId, data) { this.cellDataUpdates[colId+"="+rowId] = [colId, rowId, data]; }); TableEditAdapter.method('addAdditionalRequestData' , function(type, req) { return req; }); TableEditAdapter.method('sendCellDataUpdates' , function() { var req = this.cellDataUpdates; req.rowTable = this.rowTable; req.columnTable = this.columnTable; req.valueTable = this.valueTable; req = this.addAdditionalRequestData('updateData', req); var reqJson = JSON.stringify(req); var callBackData = []; callBackData['callBackData'] = []; callBackData['callBackSuccess'] = 'updateDataSuccessCallBack'; callBackData['callBackFail'] = 'updateDataFailCallBack'; this.showLoader(); this.customAction('updateData',this.modulePath,reqJson,callBackData); }); TableEditAdapter.method('updateDataSuccessCallBack', function(callBackData,serverData) { this.hideLoader(); modJs.cellDataUpdates = {}; modJs.get(); }); TableEditAdapter.method('updateDataFailCallBack', function(callBackData,serverData) { this.hideLoader(); }); TableEditAdapter.method('sendAllCellDataUpdates' , function() { var req = this.cellDataUpdates; req.rowTable = this.rowTable; req.columnTable = this.columnTable; req.valueTable = this.valueTable; req = this.addAdditionalRequestData('updateAllData', req); var reqJson = JSON.stringify(req); var callBackData = []; callBackData['callBackData'] = []; callBackData['callBackSuccess'] = 'updateDataAllSuccessCallBack'; callBackData['callBackFail'] = 'updateDataAllFailCallBack'; this.showLoader(); this.customAction('updateAllData',this.modulePath,reqJson,callBackData); }); TableEditAdapter.method('updateDataAllSuccessCallBack', function(callBackData,serverData) { this.hideLoader(); modJs.cellDataUpdates = {}; modJs.getAllData(true); }); TableEditAdapter.method('updateDataAllFailCallBack', function(callBackData,serverData) { this.hideLoader(); }); TableEditAdapter.method('showActionButtons' , function() { return false; }); /** * RequestCache */ function RequestCache() { } RequestCache.method('getKey', function(url,params) { var key = url+"|"; for(index in params){ key += index+"="+params[index]+"|"; } return key; }); RequestCache.method('getData', function(key) { var data; if (typeof(Storage) == "undefined") { return null; } var strData = localStorage.getItem(key); if(strData != undefined && strData != null && strData != ""){ data = JSON.parse(strData); if(data == undefined || data == null){ return null; } if(data.status != undefined && data.status != null && data.status != "SUCCESS"){ return null; } return data; } return null; }); RequestCache.method('setData', function(key, data) { if (typeof(Storage) == "undefined") { return null; } if(data.status != undefined && data.status != null && data.status != "SUCCESS"){ return null; } var strData = JSON.stringify(data); var strData = localStorage.setItem(key,strData); return strData; });