From 191ad1e4ee9bd45d8d28ea3b9ea3e324c3f632ac Mon Sep 17 00:00:00 2001 From: Willem Date: Sun, 24 Jan 2016 18:58:52 +0100 Subject: [PATCH] Fixed websql open and options --- README.md | 16 ++++++++++---- es5-ff-spa-loader.js | 50 +++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 899bbb5..7af113e 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ A javascript library providing server defined loading of assets for a single pag * question.title = The question ui title. (default: 'Server') * question.submit = The start button text. (default: 'Start') * question.text = The question ui text. (default: 'Please provide the server name;') + * question.size = The question ui input size. (default: 32) * question.style = The question ui css style. (default: blue input) * question.validate.min.value = The minimal hostname length, false is disabled (default: 3) * question.validate.min.message = The error message (default: 'Server name is to short.') @@ -163,11 +164,13 @@ A javascript library providing server defined loading of assets for a single pag * Opera 12 Presto * IE 11 Edge * Android 5.1.1 in Cordova + Site + * Android 4.4.4 in Cordova + Site * Android 4.2.2 in Cordova + Site ## Todo * test in production + * Add Loader progress bar * Server header check support * Add table+instance websql options so it can also be used in application code. * Split assets per type so do js first then boot then css + cssData. @@ -175,7 +178,8 @@ A javascript library providing server defined loading of assets for a single pag * Add more tests * css: set tag.media = 'only you'; * css: add media in resouces - * Add in browser tests + * Add in browser tests + * example: rm from dist and add cordova and use gulp ## Contributing @@ -187,10 +191,14 @@ Add unit tests for any new or changed functionality. Lint and test your code. ### 0.1.1 * Moved websql delete timeout to cleanServerlUrl for faster boot. * Fixed websql db-size and db-name for older androids. -* Added more jsdoc +* Added more jsdoc. +* Added question.size option. +* Changed websql option openDatabase to returning open function. +* Fixed sqlitePlugin open function was typo on openDatabase. +* Added websql table option. (defaults to 'cache_store') ### 0.1.0 -* Moved options.server.question to options.question +* Moved options.server.question to options.question. * Added question.validate.[min|max|regex].value|message options. * Strip question value to hostname+port before use and validating. * Allow user upgrade to https in question input from default of transport option. @@ -199,7 +207,7 @@ Add unit tests for any new or changed functionality. Lint and test your code. * Fixed cordova booting. ### 0.0.4 -* Added auto cache clean code +* Added auto cache clean code. * Disable cordova timeout per default. * Remove unused mobileAgent detect. * Fixed cached resources injection order. diff --git a/es5-ff-spa-loader.js b/es5-ff-spa-loader.js index a142a6a..7f6ae08 100644 --- a/es5-ff-spa-loader.js +++ b/es5-ff-spa-loader.js @@ -83,6 +83,7 @@ transport: 'http://', title: 'Question', submit: 'Start', + size: 32, text: 'Please provide the server name', style: 'body {color: #EFF0F1;background: #484948;} .ffQuestion { margin: 3em;border-left: 0.3em solid #3F68AD;border-radius: 1em;padding: 0em 1em 0.3em 1em;} .ffQuestion > div > input {margin: 0.4em;padding: 0.4em; line-height: 2em;background-color: #454442;color: #EFF0F1;border: none;border-radius: 0.4em;outline: none;min-width: 5em;} .ffQuestion > div > input:focus {border: none;} .ffQuestionError{color: #B55858;} .ffQuestionLoad {transition: all 0.5s ease;color: #484948;} .ffQuestionLoad > div > input {background-color: #484948;color: #484948;}', validate: { @@ -173,11 +174,17 @@ }; }, websql: function(opt) { - if (opt === undefined) { opt = {}; } - if (opt.name === undefined) { opt.name = 'FFSpaLoader'; } - if (opt.size === undefined) { opt.size = 4 * 1024 * 1024; } // reg 4MB let user do higher - if (opt.version === undefined) { opt.version = '1.0'; } - if (opt.openDatabase === undefined) { opt.openDatabase = rootWindow.openDatabase; } + if (opt === undefined) { opt = {}; } + if (opt.name === undefined) { opt.name = 'FFSpaLoader'; } + if (opt.size === undefined) { opt.size = 4 * 1024 * 1024; } // reg 4MB let user do higher + if (opt.version === undefined) { opt.version = '1.0'; } + if (opt.table === undefined) { opt.table = 'cache_store'; } + if (opt.open === undefined) { + opt.open = function(dbOpt) { + return rootWindow.openDatabase(dbOpt.name, dbOpt.version, dbOpt.name, dbOpt.size); + }; + } + var nullDataHandler = function(cb) { return function () { cb(null); @@ -191,9 +198,9 @@ }; var cacheDBInit = function(cb) { cacheDB.transaction(function(tx) { - var query = 'CREATE TABLE cache_store(id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL, value TEXT NOT NULL)'; + var query = 'CREATE TABLE '+opt.table+'(id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL, value TEXT NOT NULL)'; executeSql(tx, query, [], function(tx) { - executeSql(tx, 'CREATE UNIQUE INDEX cache_store__key__udx ON cache_store (key)', [], nullDataHandler(cb), cb); + executeSql(tx, 'CREATE UNIQUE INDEX '+opt.table+'__key__udx ON '+opt.table+' (key)', [], nullDataHandler(cb), cb); }, cb); }); }; @@ -202,13 +209,14 @@ if (cacheDB !== null) { return cb(null); // open once. } + utilDebug('websql.cacheOpen '+JSON.stringify(opt)); try { - cacheDB = rootWindow.openDatabase(opt.name, opt.version, opt.name, opt.size); + cacheDB = opt.open(opt); } catch(e) { return cb(e); } cacheDB.transaction(function(tx) { - executeSql(tx,'SELECT value FROM cache_store WHERE key = \"test-for-table\"', [], function() { + executeSql(tx,'SELECT value FROM '+opt.table+' WHERE key = \"if-err-init\"', [], function() { cb(null); }, function() { cacheDBInit(cb); @@ -217,7 +225,7 @@ }, cacheGetValue: function(key, cb) { cacheDB.transaction(function(tx) { - executeSql(tx, 'SELECT value FROM cache_store WHERE key = ?',[key], function(tx, res) { + executeSql(tx, 'SELECT value FROM '+opt.table+' WHERE key = ?',[key], function(tx, res) { if (res.rows.length === 0) { cb(null, null); } else { @@ -229,12 +237,12 @@ }, cacheSetValue: function(key, value, cb) { cacheDB.transaction(function(tx) { - executeSql(tx, 'SELECT value FROM cache_store WHERE key = ?',[key], function(tx, res) { + executeSql(tx, 'SELECT value FROM '+opt.table+' WHERE key = ?',[key], function(tx, res) { if (res.rows.length === 0) { - var queryInsert = 'INSERT INTO cache_store (key,value) VALUES (?,?)'; + var queryInsert = 'INSERT INTO '+opt.table+' (key,value) VALUES (?,?)'; executeSql(tx, queryInsert, [key,JSON.stringify(value)], nullDataHandler(cb), cb); } else { - var queryUpdate = 'UPDATE cache_store SET value = ? WHERE key = ?'; + var queryUpdate = 'UPDATE '+opt.table+' SET value = ? WHERE key = ?'; executeSql(tx, queryUpdate, [JSON.stringify(value),key], nullDataHandler(cb), cb); } }, cb); @@ -242,7 +250,7 @@ }, cacheDeleteValue: function(key, cb) { cacheDB.transaction(function(tx) { - executeSql(tx, 'DELETE FROM cache_store WHERE key = ?', [key], nullDataHandler(cb), cb); + executeSql(tx, 'DELETE FROM '+opt.table+' WHERE key = ?', [key], nullDataHandler(cb), cb); }); } }; @@ -346,7 +354,7 @@ if (stack.length === 0) { return cb(null); } - utilDebug(runType); + utilDebug(runType+' start'); var startTime = new Date().getTime(); var runStack = stack; var runStackStep = function(err) { @@ -736,16 +744,12 @@ var formTag = document.createElement('div'); rootTag.appendChild(formTag); -// var transportTag = document.createElement('label'); -// transportTag.setAttribute('for','serverInput'); -// transportTag.appendChild(document.createTextNode(options.question.transport)); -// formTag.appendChild(transportTag); - var inputTag = document.createElement('input'); inputTag.type = 'text'; inputTag.id = 'serverInput'; inputTag.setAttribute('autofocus',''); inputTag.setAttribute('onkeydown','if (event.keyCode == 13) {document.getElementById(\'serverSubmit\').click()}'); + inputTag.setAttribute('size',options.question.size); formTag.appendChild(inputTag); var submitTag = document.createElement('input'); @@ -779,7 +783,7 @@ if (options.cache[type] === null) { if (factory.detect.cordovaDevice() && factory.detect.sqlitePlugin()) { utilDebug('startCacheType auto sqlitePlugin for '+type); - options.cache[type] = factory.cache.websql({openDatabase: rootWindow.sqlitePlugin}); + options.cache[type] = factory.cache.websql({open: function(dbOpt) { return rootWindow.sqlitePlugin(dbOpt.name, dbOpt.version, dbOpt.name, dbOpt.size);}}); } else if (factory.detect.openDatabase()) { utilDebug('startCacheType auto openDatabase for '+type); options.cache[type] = factory.cache.websql(); @@ -804,6 +808,7 @@ * @private */ var startCache = function (cb) { + // FIXME: use dynamic loop for user defined types. startCacheType('meta', function(err) { if (err !== null) { return cb(err); } startCacheType('js', function(err) { @@ -827,7 +832,7 @@ if (err !== null) { options.error.handler(err); } else { - utilDebug('start done in '+(new Date().getTime()-startTime)+' ms.'); + utilDebug('start done in '+(new Date().getTime()-startTime)+' ms.'); // last debug line TODO: move bootAngular to onjsloaded bootAngular(function(err) { if (err !== null) { return options.error.handler(err); } if (typeof cbArgu === 'function') { @@ -836,6 +841,7 @@ }); } }; + utilDebug('start spa loader'); // first debug line TODO: Add version if (options.debug.enable === true) { var optionsKeys = Object.keys(options); for (var keyId in optionsKeys) {