|
| 1 | +$( document ).ready(function() { |
| 2 | + getDBList(); |
| 3 | + $("#query").keypress(function(e){ |
| 4 | + if(e.which == 13) { |
| 5 | + queryFunction(); |
| 6 | + } |
| 7 | + }); |
| 8 | + //update currently selected database |
| 9 | + $( document ).on( "click", "#db-list .list-group-item", function() { |
| 10 | + $("#db-list .list-group-item").each(function() { |
| 11 | + $(this).removeClass('selected'); |
| 12 | + }); |
| 13 | + $(this).addClass('selected'); |
| 14 | + }); |
| 15 | + |
| 16 | + //update currently table database |
| 17 | + $( document ).on( "click", "#table-list .list-group-item", function() { |
| 18 | + $("#table-list .list-group-item").each(function() { |
| 19 | + $(this).removeClass('selected'); |
| 20 | + }); |
| 21 | + $(this).addClass('selected'); |
| 22 | + }); |
| 23 | + |
| 24 | + |
| 25 | +}); |
| 26 | + |
| 27 | +var isDatabaseSelected = true; |
| 28 | + |
| 29 | +function getData(tableName) { |
| 30 | + |
| 31 | + $.ajax({url: "getAllDataFromTheTable?tableName="+tableName, success: function(result){ |
| 32 | + |
| 33 | + result = JSON.parse(result); |
| 34 | + inflateData(result); |
| 35 | + |
| 36 | + }}); |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +function queryFunction() { |
| 41 | + |
| 42 | + var query = $('#query').val(); |
| 43 | + |
| 44 | + $.ajax({url: "query?query="+escape(query), success: function(result){ |
| 45 | + |
| 46 | + result = JSON.parse(result); |
| 47 | + inflateData(result); |
| 48 | + |
| 49 | + }}); |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +function downloadDb() { |
| 54 | + if (isDatabaseSelected) { |
| 55 | + $.ajax({url: "downloadDb", success: function(){ |
| 56 | + window.location = 'downloadDb'; |
| 57 | + }}); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +function getDBList() { |
| 63 | + |
| 64 | + $.ajax({url: "getDbList", success: function(result){ |
| 65 | + |
| 66 | + result = JSON.parse(result); |
| 67 | + var dbList = result.rows; |
| 68 | + $('#db-list').empty(); |
| 69 | + var isSelectionDone = false; |
| 70 | + for(var count = 0; count < dbList.length; count++){ |
| 71 | + if(dbList[count].indexOf("journal") == -1){ |
| 72 | + $("#db-list").append("<a href='#' id=" +dbList[count] + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbList[count] + "\");'>" +dbList[count] + "</a>"); |
| 73 | + if(!isSelectionDone){ |
| 74 | + isSelectionDone = true; |
| 75 | + $('#db-list').find('a').trigger('click'); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + }}); |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +function openDatabaseAndGetTableList(db) { |
| 85 | + |
| 86 | + if("APP_SHARED_PREFERENCES" == db) { |
| 87 | + $('#run-query').removeClass('active'); |
| 88 | + $('#run-query').addClass('disabled'); |
| 89 | + $('#selected-db-info').removeClass('active'); |
| 90 | + $('#selected-db-info').addClass('disabled'); |
| 91 | + isDatabaseSelected = false; |
| 92 | + $("#selected-db-info").text("SharedPreferences"); |
| 93 | + } else { |
| 94 | + $('#run-query').removeClass('disabled'); |
| 95 | + $('#run-query').addClass('active'); |
| 96 | + $('#selected-db-info').removeClass('disabled'); |
| 97 | + $('#selected-db-info').addClass('active'); |
| 98 | + isDatabaseSelected = true; |
| 99 | + $("#selected-db-info").text("Export Selected Database : "+db); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + $.ajax({url: "getTableList?database="+db, success: function(result){ |
| 104 | + |
| 105 | + result = JSON.parse(result); |
| 106 | + var tableList = result.rows; |
| 107 | + var dbVersion = result.dbVersion; |
| 108 | + if("APP_SHARED_PREFERENCES" != db) { |
| 109 | + $("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion); |
| 110 | + } |
| 111 | + $('#table-list').empty() |
| 112 | + for(var count = 0; count < tableList.length; count++){ |
| 113 | + var tableName = tableList[count]; |
| 114 | + $("#table-list").append("<a href='#' data-db-name='"+db+"' data-table-name='"+tableName+"' class='list-group-item' onClick='getData(\""+ tableName + "\");'>" +tableName + "</a>"); |
| 115 | + } |
| 116 | + |
| 117 | + }}); |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +function inflateData(result){ |
| 122 | + |
| 123 | + if(result.isSuccessful){ |
| 124 | + |
| 125 | + if(!result.isSelectQuery){ |
| 126 | + showSuccessInfo("Query Executed Successfully"); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + var columnHeader = result.tableInfos; |
| 131 | + |
| 132 | + // set function to return cell data for different usages like set, display, filter, search etc.. |
| 133 | + for(var i = 0; i < columnHeader.length; i++) { |
| 134 | + columnHeader[i]['targets'] = i; |
| 135 | + columnHeader[i]['data'] = function(row, type, val, meta) { |
| 136 | + var dataType = row[meta.col].dataType; |
| 137 | + if (type == "sort" && dataType == "boolean") { |
| 138 | + return row[meta.col].value ? 1 : 0; |
| 139 | + } |
| 140 | + return row[meta.col].value; |
| 141 | + } |
| 142 | + } |
| 143 | + var columnData = result.rows; |
| 144 | + var tableId = "#db-data"; |
| 145 | + if ($.fn.DataTable.isDataTable(tableId) ) { |
| 146 | + $(tableId).DataTable().destroy(); |
| 147 | + } |
| 148 | + |
| 149 | + $("#db-data-div").remove(); |
| 150 | + $("#parent-data-div").append('<div id="db-data-div"><table class="display nowrap" cellpadding="0" border="0" cellspacing="0" width="100%" class="table table-striped table-bordered display" id="db-data"></table></div>'); |
| 151 | + |
| 152 | + $(tableId).dataTable({ |
| 153 | + "data": columnData, |
| 154 | + "columnDefs": columnHeader, |
| 155 | + 'bPaginate': true, |
| 156 | + 'searching': true, |
| 157 | + 'bFilter': true, |
| 158 | + 'bInfo': true, |
| 159 | + "bSort" : true, |
| 160 | + "scrollX": true, |
| 161 | + "iDisplayLength": 10, |
| 162 | + "dom": "Bfrtip", |
| 163 | + select: 'single', |
| 164 | + altEditor: true, // Enable altEditor |
| 165 | + buttons: [ |
| 166 | + { |
| 167 | + extend: 'selected', // Bind to Selected row |
| 168 | + text: 'Edit', |
| 169 | + name: 'edit' // do not change name |
| 170 | + }, |
| 171 | + { |
| 172 | + extend: 'selected', |
| 173 | + text: 'Delete', |
| 174 | + name: 'delete' |
| 175 | + } |
| 176 | + ] |
| 177 | + }) |
| 178 | + |
| 179 | + //attach row-updated listener |
| 180 | + $(tableId).on('update-row.dt', function (e, updatedRowData, callback) { |
| 181 | + var updatedRowDataArray = JSON.parse(updatedRowData); |
| 182 | + //add value for each column |
| 183 | + var data = columnHeader; |
| 184 | + for(var i = 0; i < data.length; i++) { |
| 185 | + data[i].value = updatedRowDataArray[i].value; |
| 186 | + data[i].dataType = updatedRowDataArray[i].dataType; |
| 187 | + } |
| 188 | + //send update table data request to server |
| 189 | + updateTableData(data, callback); |
| 190 | + }); |
| 191 | + |
| 192 | + |
| 193 | + //attach delete-updated listener |
| 194 | + $(tableId).on('delete-row.dt', function (e, updatedRowData, callback) { |
| 195 | + var deleteRowDataArray = JSON.parse(updatedRowData); |
| 196 | + |
| 197 | + console.log(deleteRowDataArray); |
| 198 | + |
| 199 | + //add value for each column |
| 200 | + var data = columnHeader; |
| 201 | + for(var i = 0; i < data.length; i++) { |
| 202 | + data[i].value = deleteRowDataArray[i].value; |
| 203 | + data[i].dataType = deleteRowDataArray[i].dataType; |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + //send delete table data request to server |
| 208 | + deleteTableData(data, callback); |
| 209 | + }); |
| 210 | + |
| 211 | + // hack to fix alignment issue when scrollX is enabled |
| 212 | + $(".dataTables_scrollHeadInner").css({"width":"100%"}); |
| 213 | + $(".table ").css({"width":"100%"}); |
| 214 | + }else{ |
| 215 | + if(!result.isSelectQuery){ |
| 216 | + showErrorInfo("Query Execution Failed"); |
| 217 | + }else { |
| 218 | + showErrorInfo("Some Error Occurred"); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | +} |
| 223 | + |
| 224 | +//send update database request to server |
| 225 | +function updateTableData(updatedData, callback) { |
| 226 | + //get currently selected element |
| 227 | + var selectedTableElement = $("#table-list .list-group-item.selected"); |
| 228 | + |
| 229 | + var filteredUpdatedData = updatedData.map(function(columnData){ |
| 230 | + return { |
| 231 | + title: columnData.title, |
| 232 | + isPrimary: columnData.isPrimary, |
| 233 | + value: columnData.value, |
| 234 | + dataType: columnData.dataType |
| 235 | + } |
| 236 | + }); |
| 237 | + //build request parameters |
| 238 | + var requestParameters = {}; |
| 239 | + requestParameters.dbName = selectedTableElement.attr('data-db-name'); |
| 240 | + requestParameters.tableName = selectedTableElement.attr('data-table-name');; |
| 241 | + requestParameters.updatedData = encodeURIComponent(JSON.stringify(filteredUpdatedData)); |
| 242 | + |
| 243 | + //execute request |
| 244 | + $.ajax({ |
| 245 | + url: "updateTableData", |
| 246 | + type: 'GET', |
| 247 | + data: requestParameters, |
| 248 | + success: function(response) { |
| 249 | + response = JSON.parse(response); |
| 250 | + if(response.isSuccessful){ |
| 251 | + console.log("Data updated successfully"); |
| 252 | + callback(true); |
| 253 | + showSuccessInfo("Data Updated Successfully"); |
| 254 | + } else { |
| 255 | + console.log("Data updated failed"); |
| 256 | + callback(false); |
| 257 | + } |
| 258 | + } |
| 259 | + }) |
| 260 | +} |
| 261 | + |
| 262 | + |
| 263 | +function deleteTableData(deleteData, callback) { |
| 264 | + |
| 265 | + var selectedTableElement = $("#table-list .list-group-item.selected"); |
| 266 | + var filteredUpdatedData = deleteData.map(function(columnData){ |
| 267 | + return { |
| 268 | + title: columnData.title, |
| 269 | + isPrimary: columnData.isPrimary, |
| 270 | + value: columnData.value, |
| 271 | + dataType: columnData.dataType |
| 272 | + } |
| 273 | + }); |
| 274 | + |
| 275 | + console.log(filteredUpdatedData); |
| 276 | + |
| 277 | + //build request parameters |
| 278 | + var requestParameters = {}; |
| 279 | + requestParameters.dbName = selectedTableElement.attr('data-db-name'); |
| 280 | + requestParameters.tableName = selectedTableElement.attr('data-table-name');; |
| 281 | + requestParameters.deleteData = encodeURIComponent(JSON.stringify(filteredUpdatedData)); |
| 282 | + |
| 283 | + //execute request |
| 284 | + $.ajax({ |
| 285 | + url: "deleteTableData", |
| 286 | + type: 'GET', |
| 287 | + data: requestParameters, |
| 288 | + success: function(response) { |
| 289 | + response = JSON.parse(response); |
| 290 | + if(response.isSuccessful){ |
| 291 | + console.log("Data deleted successfully"); |
| 292 | + callback(true); |
| 293 | + showSuccessInfo("Data Deleted Successfully"); |
| 294 | + } else { |
| 295 | + console.log("Data delete failed"); |
| 296 | + callback(false); |
| 297 | + } |
| 298 | + } |
| 299 | + }) |
| 300 | +} |
| 301 | + |
| 302 | +function showSuccessInfo(message){ |
| 303 | + var snackbarId = "snackbar"; |
| 304 | + var snackbarElement = $("#"+snackbarId); |
| 305 | + snackbarElement.addClass("show"); |
| 306 | + snackbarElement.css({"backgroundColor": "#5cb85c"}); |
| 307 | + snackbarElement.html(message) |
| 308 | + setTimeout(function(){ |
| 309 | + snackbarElement.removeClass("show"); |
| 310 | + }, 3000); |
| 311 | +} |
| 312 | + |
| 313 | +function showErrorInfo(message){ |
| 314 | + var snackbarId = "snackbar"; |
| 315 | + var snackbarElement = $("#"+snackbarId); |
| 316 | + snackbarElement.addClass("show"); |
| 317 | + snackbarElement.css({"backgroundColor": "#d9534f"}); |
| 318 | + snackbarElement.html(message) |
| 319 | + setTimeout(function(){ |
| 320 | + snackbarElement.removeClass("show"); |
| 321 | + }, 3000); |
| 322 | +} |
0 commit comments