Bug 10240: Offline circulation using HTML5 and IndexedDB
[koha-equinox.git] / koha-tmpl / intranet-tmpl / prog / en / js / offlinecirc.js
1 /* Copyright 2013 C & P Bibliography Services
2  *
3  * This file is part of Koha.
4  *
5  * Koha is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 3 of the License, or (at your option) any later
8  * version.
9  *
10  * Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with Koha; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 (function( kohadb, $, undefined ) {
20     kohadb.settings = kohadb.settings || {};
21     kohadb.initialize = function (callback) {
22         $.indexedDB("koha", {
23             "version": 1,
24             "schema": {
25                 "1": function(versionTransaction){
26                     var patrons = versionTransaction.createObjectStore("patrons", {
27                         "keyPath": "cardnumber"
28                     });
29                     var items = versionTransaction.createObjectStore("items", {
30                         "keyPath": "barcode"
31                     });
32                     var issues = versionTransaction.createObjectStore("issues", {
33                         "keyPath": "barcode"
34                     });
35                     issues.createIndex("cardnumber", { "multiEntry": true });
36                     var transactions = versionTransaction.createObjectStore("transactions", {
37                         "keyPath": "timestamp"
38                     });
39                     var settings = versionTransaction.createObjectStore("offline_settings", {
40                         "keyPath": "key"
41                     });
42                 },
43             }
44         }).done(function(){
45             if (typeof callback === 'function') {
46                 callback();
47                 kohadb.loadSetting('userid');
48                 kohadb.loadSetting('branchcode');
49             }
50         });
51     };
52     kohadb.loadSetting = function (key, callback) {
53         $.indexedDB("koha").transaction(["offline_settings"]).then(function(){
54         }, function(err, e){
55         }, function(transaction){
56             var settings = transaction.objectStore("offline_settings");
57             settings.get(key).done(function (item, error) {
58                 if (typeof item !== 'undefined') {
59                     kohadb.settings[key] = item.value;
60                 }
61                 if (typeof callback === 'function') {
62                     callback(key, kohadb.settings[key]);
63                 }
64             });
65         });
66     };
67     kohadb.saveSetting = function (key, value) {
68         $.indexedDB("koha").transaction(["offline_settings"]).then(function(){
69         }, function(err, e){
70         }, function(transaction){
71             var settings = transaction.objectStore("offline_settings");
72             settings.put({ "key" : key, "value" : value });
73             kohadb.settings[key] = value;
74         });
75     };
76     kohadb.recordTransaction = function (newtrans, callback) {
77         $.indexedDB("koha").transaction(["transactions"]).then(function(){
78             callback(newtrans);
79         }, function(err, e){
80         }, function(dbtransaction) {
81             var transactions = dbtransaction.objectStore("transactions");
82             transactions.put(newtrans);
83         });
84     };
85 }( window.kohadb = window.bndb || {}, jQuery ));
86
87 if ( !Date.prototype.toMySQLString ) {
88   ( function() {
89
90     function pad(number) {
91       var r = String(number);
92       if ( r.length === 1 ) {
93         r = '0' + r;
94       }
95       return r;
96     }
97
98     Date.prototype.toMySQLString = function() {
99       return this.getFullYear()
100         + '-' + pad( this.getMonth() + 1 )
101         + '-' + pad( this.getDate() )
102         + ' ' + pad( this.getHours() )
103         + ':' + pad( this.getMinutes() )
104         + ':' + pad( this.getSeconds() )
105         + '.' + String( (this.getMilliseconds()/1000).toFixed(3) ).slice( 2, 5 );
106     };
107
108   }() );
109 }