Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //1. initialization
  2.  
  3. var localDB = null;
  4.  
  5. function onInit(){
  6.     try {
  7.         if (!window.openDatabase) {
  8.             updateStatus("Error: DB not supported");
  9.         }
  10.         else {
  11.             initDB();
  12.             createTables();
  13.             queryAndUpdateOverview();
  14.         }
  15.     }
  16.     catch (e) {
  17.         if (e == 2) {
  18.             updateStatus("Error: Invalid database version.");
  19.         }
  20.         else {
  21.             updateStatus("Error: Unknown error " + e + ".");
  22.         }
  23.         return;
  24.     }
  25. }
  26.  
  27. function initDB(){
  28.     var shortName = 'tripDb';
  29.     var version = '1.0';
  30.     var displayName = 'MyGCTripManager';
  31.     var maxSize = 65536; // in bytes
  32.     localDB = window.openDatabase(shortName, version, displayName, maxSize);
  33. }
  34.  
  35. function createTables(){
  36.     var query = 'CREATE TABLE IF NOT EXISTS gcTrip(gcId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, gcType VARCHAR NOT NULL, gcDate VARCHAR NOT NULL, gcContact VARCHAR, gcOdoStart INTEGER NOT NULL, gcOdoEnd INTEGER NOT NULL, gcComments VARCHAR, gcDistance INTEGER NOT NULL, gcRate FLOAT NOT NULL, gcAmount FLOAT NOT NULL);';
  37.     try {
  38.         localDB.transaction(function(transaction){
  39.             transaction.executeSql(query, [], nullDataHandler, errorHandler);
  40.             updateStatus("Table 'gcTrip' is present");
  41.         });
  42.     }
  43.     catch (e) {
  44.         updateStatus("Error: Unable to create table 'gcTrip' " + e + ".");
  45.         return;
  46.     }
  47. }
  48.  
  49. function dropTable(){  
  50.     var query = 'DROP TABLE gcTrip;';  
  51.     try {  
  52.         localDB.transaction(function(transaction){  
  53.             transaction.executeSql(query, [], nullDataHandler, errorHandler);  
  54.             updateStatus("Table 'tsTrip' is dropped");  
  55.         });  
  56.     }  
  57.     catch (e) {  
  58.         updateStatus("Error: Unable to drop table 'tsTrip' " + e + ".");  
  59.         return;
  60.     }  
  61. }
  62.  
  63. //2. query db and view update
  64.  
  65. // event handler start with on*
  66.  
  67.  
  68. function onUpdate(){
  69.     var gcId = document.tripForm.txtId.value;
  70.     var gcType = document.tripForm.txtType.value;
  71.     var gcContact = document.tripForm.txtContact.value;
  72.     // adds for nulls here
  73.     if (gcType == "") {
  74.         updateStatus("'gcType' and put in date are required fields!");
  75.     }
  76.     else {
  77.         var query = "update gcTrip set gcType=? where gcId=?;";
  78.         try {
  79.             localDB.transaction(function(transaction){
  80.                 transaction.executeSql(query, [gcType, gcContact, gcId], function(transaction, results){
  81.                     if (!results.rowsAffected) {
  82.                         updateStatus("Error: No rows affected");
  83.                     }
  84.                     else {
  85.                         updateForm("", "", "");
  86.                         updateStatus("Updated rows:" + results.rowsAffected);
  87.                         queryAndUpdateOverview();
  88.                     }
  89.                 }, errorHandler);
  90.             });
  91.         }
  92.         catch (e) {
  93.             updateStatus("Error: Unable to perform an UPDATE " + e + ".");
  94.         }
  95.     }
  96. }
  97.  
  98. function onDelete(){
  99.     var gcId = document.tripForm.gcId.value;
  100.    
  101.     var query = "delete from gcTrip where gcId=?;";
  102.     try {
  103.         localDB.transaction(function(transaction){
  104.        
  105.             transaction.executeSql(query, [gcId], function(transaction, results){
  106.                 if (!results.rowsAffected) {
  107.                     updateStatus("Error: No rows affected.");
  108.                 }
  109.                 else {
  110.                     updateForm("", "", "");
  111.                     updateStatus("Deleted rows:" + results.rowsAffected);
  112.                     queryAndUpdateOverview();
  113.                 }
  114.             }, errorHandler);
  115.         });
  116.     }
  117.     catch (e) {
  118.         updateStatus("Error: Unable to perform an DELETE " + e + ".");
  119.     }
  120.    
  121. }
  122.  
  123. function onCreate(){
  124.  
  125.     var type = document.tripForm.txtType.value;
  126.     var date = document.tripForm.txtDate.value;
  127.     var contact = document.tripForm.txtContact.value;
  128.     var start = document.tripForm.txtStart.value;
  129.     var end = document.tripForm.txtEnd.value;
  130.     var comment = document.tripForm.txtComment.value;
  131.     var distance = document.tripForm.txtDistance.value;
  132.     var rate = document.tripForm.txtRate.value;
  133.     var amount = document.tripForm.txtAmount.value;
  134.     // more error handling needed
  135.     if (gcType == "" ) {
  136.         updateStatus("Error: 'gcType' and 'Name' are required fields!");
  137.     }
  138.     else {
  139.         var query = "INSERT INTO gcTrip(gcType, gcDate, gcContact, gcOdoStart, gcOdoEnd, gcComments, gcDistance, gcRate, gcAmount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
  140.         try {
  141.             localDB.transaction(function(transaction){
  142.                 transaction.executeSql(query, [type, date, contact, start, end, comment, distance, rate, amount], function(transaction, results){
  143.                     if (!results.rowsAffected) {
  144.                         updateStatus("Error: No rows affected.");
  145.                     }
  146.                     else {
  147.                         updateForm("", "", "");
  148.                         updateStatus("Inserted row with id " + results.insertId);
  149.                         queryAndUpdateOverview();
  150.                     }
  151.                 }, errorHandler);
  152.             });
  153.         }
  154.         catch (e) {
  155.             updateStatus("Error: Unable to perform an INSERT " + e + ".");
  156.         }
  157.     }
  158. }
  159.  
  160. function onSelect(htmlLIElement){
  161.     var id = htmlLIElement.getAttribute("gcId");
  162.    
  163.     query = "SELECT * FROM gcTrip where gcId=?;";
  164.     try {
  165.         localDB.transaction(function(transaction){
  166.        
  167.             transaction.executeSql(query, [id], function(transaction, results){
  168.            
  169.                 var row = results.rows.item(0);
  170.                
  171.                 updateForm(row['gcId'], row['gcType'], row['gcDate'], row['gcContact'], row['gcOdoStart'], row['gcOdoEnd'], row['gcComments'], row['gcDistance'], row['gcRate'], row['gcAmount']);
  172.                
  173.             }, function(transaction, error){
  174.                 updateStatus("Error: " + error.code + "<br>Message: " + error.message);
  175.             });
  176.         });
  177.     }
  178.     catch (e) {
  179.         updateStatus("Error: Unable to select data from the db " + e + ".");
  180.     }
  181.    
  182. }
  183.  
  184. function queryAndUpdateOverview(){
  185.  
  186.     //remove old table rows
  187.     var dataRows = document.getElementById("itemData").getElementsByClassName("data");
  188.    
  189.     while (dataRows.length > 0) {
  190.         row = dataRows[0];
  191.         document.getElementById("itemData").removeChild(row);
  192.     };
  193.    
  194.     //read db data and create new table rows
  195.     var query = "SELECT * FROM gcTrip;";
  196.     try {
  197.         localDB.transaction(function(transaction){
  198.        
  199.             transaction.executeSql(query, [], function(transaction, results){
  200.                 for (var i = 0; i < results.rows.length; i++) {
  201.                
  202.                     var row = results.rows.item(i);
  203.                     var li = document.createElement("li");
  204.                     li.setAttribute("id", row['gcId']);
  205.                     li.setAttribute("class", "data");
  206.                     li.setAttribute("onclick", "onSelect(this)");
  207.                    
  208.                     var liText = document.createTextNode(row['gcId'] + " x "+ row['gcType']);
  209.                     li.appendChild(liText);
  210.                    
  211.                     document.getElementById("itemData").appendChild(li);
  212.                 }
  213.             }, function(transaction, error){
  214.                 updateStatus("Error: " + error.code + "<br>Message: " + error.message);
  215.             });
  216.         });
  217.     }
  218.     catch (e) {
  219.         updateStatus("Error: Unable to select data from the db " + e + ".");
  220.     }
  221. }
  222.  
  223. // 3. misc utility functions
  224.  
  225. // db data handler
  226.  
  227. errorHandler = function(transaction, error){
  228.     updateStatus("Error: " + error.message);
  229.     return true;
  230. }
  231.  
  232. nullDataHandler = function(transaction, results){
  233. }
  234.  
  235. // update view functions
  236.  
  237. function updateForm(id, type, date, contact, start, end, comment, distance, rate, amount){
  238.     document.tripForm.txtId.value = id;
  239.     document.tripForm.txtType.value = type;
  240.     document.tripForm.txtDate.value = date;
  241.     document.tripForm.txtContact.value = contact;
  242.     document.tripForm.txtStart.value = start;
  243.     document.tripForm.txtEnd.value = end;
  244.     document.tripForm.txtComment.value = comment;
  245.     document.tripForm.txtDistance.value = distance;
  246.     document.tripForm.txtRate.value = rate;
  247.     document.tripForm.txtAmount.value = amount;
  248. }
  249.  
  250. function updateStatus(status){
  251.     document.getElementById('status').innerHTML = status;
  252. }
  253.  
  254. // Total cost calculate function
  255. function onCalc(){
  256.     var qty = document.tripForm.gcType.value;
  257.     var cost = document.tripForm.unitcost.value;
  258.                 var totalcost = qty * cost;
  259.                 updateCalcFields(totalcost);
  260. }
  261.  
  262. function updateCalcFields(totalcost){
  263.     document.tripForm.totalcost.value = totalcost;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement