From e5e2dfc56fba24254bfa403b01c5495bfb3a81da Mon Sep 17 00:00:00 2001 From: Willem Date: Sat, 16 Jan 2016 18:24:37 +0100 Subject: [PATCH] Fixed websql init async,cleanup options,add tested browsers --- README.md | 34 +- es5-ff-spa-loader.js | 381 +++++++++++-------- example/www_static/js/controller/page-foo.js | 13 +- example/www_static/js/example-app.js | 4 +- example/www_views/index.ejs | 11 +- 5 files changed, 261 insertions(+), 182 deletions(-) diff --git a/README.md b/README.md index 2684b91..b3e39cd 100644 --- a/README.md +++ b/README.md @@ -13,29 +13,23 @@ A javascript library providing server defined loading of resources for a single Single index.html - FFSpaLoader.options.debug = true; + FFSpaLoader.options.debug.enable = true; + FFSpaLoader.options.boot.angular.modules.push('exampleUI'); FFSpaLoader.options.server.url = 'http://myserver'; - FFSpaLoader.options.server.urlPath = '/api/path/to/spa/client/resources'; - FFSpaLoader.bootDevice(function() { - FFSpaLoader.options.cache.factory = FFSpaLoader.autoSelectCache();// need deviceready via bootDevice for sqllite. - FFSpaLoader.start(function() { - console.log('FFExample.bootstrap angular'); - angular.bootstrap( $('body'), ['exampleUI']); - }); - }); + FFSpaLoader.options.server.assets = '/api/path/to/spa/client/resources'; + FFSpaLoader.start(); ## Usage Web Single index.html - FFSpaLoader.options.debug = true; - FFSpaLoader.options.server.url = ''; - FFSpaLoader.options.server.urlPath = '/api/path/to/spa/client/resources'; - FFSpaLoader.options.cache.factory = FFSpaLoader.autoSelectCache(); + FFSpaLoader.options.debug.enable = true; + FFSpaLoader.options.boot.angular.modules.push('exampleUI'); + FFSpaLoader.options.server.url = 'http://myserver'; + FFSpaLoader.options.server.assets = '/api/path/to/spa/client/resources'; FFSpaLoader.start(function() { - console.log('FFExample.bootstrap angular'); - angular.bootstrap( $('body'), ['exampleUI']); + console.log('FFExample.boot done'); }); ## Usage Multi Server @@ -45,12 +39,20 @@ A javascript library providing server defined loading of resources for a single ## Options - * options.debug = Enable console debug output. + * options.debug.enable = Enable console debug output. + * TODO ## Example Application There is a fully working express example application in the example folder. +## Tested Browsers + + * Chromium 46 + * Iceweasel 43 + * Opera 12 Presto + * Android 5 in Cordova + ## Contributing In lieu of a formal styleguide, take care to maintain the existing coding style. diff --git a/es5-ff-spa-loader.js b/es5-ff-spa-loader.js index a6b5e62..196017a 100644 --- a/es5-ff-spa-loader.js +++ b/es5-ff-spa-loader.js @@ -26,7 +26,6 @@ // TODO: // set tag.media = 'only you'; // add media in resouces -// check supported browsers /** * FFSpaLoader is an assets loader for single page applications. @@ -38,40 +37,57 @@ * - Assets hashing for fast syncing. * - Assets types: js,css,cssData * - Caching backends: null,localStorage,webSqlDB,sqllite - * - Optional ask and cache server assets url. - * - Cordova deviceready event booting. + * - Server url question ui. + * - Loader error ui. + * - Build-in Cordova booting. + * - Build-in AngularJS booting. * * @module FFSpaLoader */ var FFSpaLoader = (/** @lends module:FFSpaLoader */function () { - + var options = { - debug: false, - errorHandler: null, - bootDevice: { - timeout: 4096, - windowFlag: 'FFCordovaDevice' + debug: { + enable: false, + handler: null, // auto filled + prefix: 'FFSpaLoader.' }, - askUrl: { - urlTransport: 'http://', - questionText: 'Please provide the server name;', - cssText: '.ffAskUrl { font-size: 1em;margin: auto;width: 50%;border: 3px solid #73AD21;padding: 1em;} .ffAskUrl > div {font-size: 0.8em;color: #ccc;} .ffAskUrl > div > * {} .ffAskUrl > div > input {} .ffAskUrlError{ color: red}', + error: { + handler: null, // auto filled + title: 'Loader Error', + style: '.ffError { margin: auto;width: 90%;border: 3px solid red;padding-left: 1em;padding-bottom: 1em;}' + }, + boot: { + cordova: { + enable: true, + timeout: 4096, // FIXME: Test really slow devices -> increase ? or add support -1 for disable or ? + flag: 'FFCordovaDevice' + }, + angular: { + enable: true, + modules: [] + }, }, server: { - url: null, - urlPath: null, - timeout: 4096, - windowFlag: 'FFServerUrl', + url: null, + assets: null, + timeout: 4096, + flag: 'FFServerUrl', header: { - request: { + request: { // TODO: add header support 'X-FFSpaLoader': '42' }, response: { } + }, + question: { + transport: 'http://', + title: 'Server', + text: 'Please provide the server name;', // TODO: rename .ffAskUrl + style: '.ffAskUrl { font-size: 1em;margin: auto;width: 90%;border: 3px solid #73AD21;padding-left: 1em;padding-bottom: 1em;} .ffAskUrl > div {font-size: 0.8em;color: #ccc;} .ffAskUrl > div > * {} .ffAskUrl > div > input {} .ffAskUrlError{ color: red}', } }, cache: { - factory: null, meta: null, js: null, css: null, @@ -79,18 +95,8 @@ var FFSpaLoader = (/** @lends module:FFSpaLoader */function () { } }; - /** - * Prints debug message to the console if options.debug is true. - * @param {String} message The message to log. - * @private - */ - var utilDebug = function (message) { - if (options.debug !== true) { - return; - } - console.log('FFSpaLoader.'+message); - }; - + var cacheDB = null; // single instance for websql + var factory = { detect: { localStorage: function() { @@ -113,7 +119,7 @@ var FFSpaLoader = (/** @lends module:FFSpaLoader */function () { return 'cordova' in window; }, cordovaDevice: function() { - return options.bootDevice.windowFlag in window; + return options.boot.cordova.flag in window; }, mobileAgent: function() { return navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/); @@ -165,25 +171,38 @@ var FFSpaLoader = (/** @lends module:FFSpaLoader */function () { cb(err.message+' code: '+err.code); }; }; - var cacheDB = window.openDatabase(opt.name, opt.version, opt.name, opt.size); - cacheDB.transaction(function(tx) { - var query = 'SELECT value FROM cache_store WHERE key = \"test-for-table\"'; - utilDebug('websql.init query: '+query); - tx.executeSql(query, [], function(tx,res) {}, function(tx,errorNoTable) { + 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)'; - utilDebug('websql.init query: '+query); + utilDebug('websql.init query '+query); tx.executeSql(query, [], function(tx,res) { var query = 'CREATE UNIQUE INDEX cache_store__key__udx ON cache_store (key)'; - utilDebug('websql.init query: '+query); - tx.executeSql(query, [], function(tx,res) {}, sqlErrorHandler(options.errorHandler)); // FIX <%- inline %>