Commit fb25a111 authored by Sven Franck's avatar Sven Franck

added JSON application configuration (config module)

parent 5056db75
...@@ -4,23 +4,16 @@ define([], function () { ...@@ -4,23 +4,16 @@ define([], function () {
var priv = {}; var priv = {};
var that = {}; var that = {};
// ======================================================================== /* ========================================================================
// ATTRIBUTES ATTRIBUTES
// ======================================================================== ======================================================================== */
// document // document
priv.doc = $.mobile.document; priv.doc = $.mobile.document;
// settings > retrieve from JIO later /* ========================================================================
priv.settings = {}; FLAGS
priv.settings.language_default = "en-EN"; ======================================================================== */
priv.settings.language_current = priv.settings.language_default;
priv.settings.language_selector = "translate";
priv.settings.language_set = false;
// ========================================================================
// FLAGS
// ========================================================================
// first page loaded // first page loaded
priv.pageWrapLoaded; priv.pageWrapLoaded;
...@@ -32,13 +25,22 @@ define([], function () { ...@@ -32,13 +25,22 @@ define([], function () {
// add page header and footer (meta/scripts) to first page loaded (only!) // add page header and footer (meta/scripts) to first page loaded (only!)
// @method wrapFirstPage // @method wrapFirstPage
// //
priv.wrapFirstPage = function () { priv.wrapFirstPage = function (spec) {
// TODO: clean this up // TODO: clean this up
priv.doc.on('pagecreate.pageWrap', 'div.ui-page', function () { priv.doc.on('pagecreate.pageWrap', 'div.ui-page', function () {
if (priv.pageWrapLoaded === undefined) { if (priv.pageWrapLoaded === undefined) {
priv.pageWrapLoaded = true; priv.pageWrapLoaded = true;
priv.doc.off('.pageWrap'); priv.doc.off('.pageWrap');
// add a callback to setup to allow hooking into settings to
// setup page header and footer configuration
priv.allset.resolve(spec);
// .then(function(App, Config) {
// console.log("this should be the 2nd callback run AFTER we have set configuration");
// console.log(App);
// console.log(Config);
// var widgetsForThisRequest = [ // var widgetsForThisRequest = [
// "text!../gadgets/pageheader.html", // "text!../gadgets/pageheader.html",
// "text!../gadgets/pagefooter.html" // "text!../gadgets/pagefooter.html"
...@@ -61,6 +63,9 @@ define([], function () { ...@@ -61,6 +63,9 @@ define([], function () {
// } // }
// } // }
// ); // );
// });
} }
}); });
}; };
...@@ -161,24 +166,6 @@ define([], function () { ...@@ -161,24 +166,6 @@ define([], function () {
}) })
}; };
priv.setScreenDimensions = function () {
var spec = {};
// breakpoints should be optionable?
spec.lowerTresh = 320;
spec.upperThresh = 720;
spec.framed = "medium";
if ($.mobile.media("screen and (max-width:20em)") ||
($.mobile.browser.oldIE && $(window).width() < o.lowerThresh)) {
spec.framed = "small";
} else if ($.mobile.media("screen and (min-width:45em)") ||
($.mobile.browser.oldIE && $(window).width() >= o.upperThresh )) {
spec.framed = "large";
}
priv.settings.screen_format = spec.framed;
};
// //
// setup global application parameters // setup global application parameters
// @method setupGlobalParameters // @method setupGlobalParameters
...@@ -186,9 +173,6 @@ define([], function () { ...@@ -186,9 +173,6 @@ define([], function () {
// TODO: this would be the place to load JSON configuration settings // TODO: this would be the place to load JSON configuration settings
priv.setupGlobalParameters = function () { priv.setupGlobalParameters = function () {
// screen dimensions
priv.setScreenDimensions();
// initialize renderJs // initialize renderJs
priv.initializeRenderJs(); priv.initializeRenderJs();
}; };
...@@ -197,13 +181,20 @@ define([], function () { ...@@ -197,13 +181,20 @@ define([], function () {
// initialize application // initialize application
// @method initialize // @method initialize
// //
priv.initialize = function () { priv.initialize = function (spec) {
// create new deferred for waiting until all initialization is done
priv.allset = new $.Deferred();
priv.allset.done(function(spec) {
spec.configuration_setter(spec);
});
// > set up globals // > set up globals
priv.setupGlobalParameters() priv.setupGlobalParameters()
// > add <head>er and footer to first page only // > add <head>er and footer to first page only
priv.wrapFirstPage(); priv.wrapFirstPage(spec);
// initialize JQM // initialize JQM
// TODO: is there a better way with requireJS/renderJs? // TODO: is there a better way with requireJS/renderJs?
...@@ -294,6 +285,7 @@ define([], function () { ...@@ -294,6 +285,7 @@ define([], function () {
// PUBLISH APP // PUBLISH APP
// ======================================================================== // ========================================================================
// return public methods to main.js // return public methods
return that; return that;
}); });
\ No newline at end of file
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
, css: '../js/plugins/require-css/require-css' , css: '../js/plugins/require-css/require-css'
, normalize: '../js/plugins/require-css/normalize' , normalize: '../js/plugins/require-css/normalize'
, json: '../js/plugins/requirejs-plugins/json' , json: '../js/plugins/requirejs-plugins/json'
, async: '../js/plugins/requirejs-plugins/async'
// plugins/libs // plugins/libs
, jquery: '../js/libs/jquery/jquery' , jquery: '../js/libs/jquery/jquery'
...@@ -35,6 +36,7 @@ ...@@ -35,6 +36,7 @@
, address: '../modules/address/address' , address: '../modules/address/address'
, basket: '../modules/basket/basket' , basket: '../modules/basket/basket'
, browser: '../modules/browser/browser' , browser: '../modules/browser/browser'
, config: '../modules/config/config'
, confirm: '../modules/confirm/confirm' , confirm: '../modules/confirm/confirm'
, description: '../modules/description/description' , description: '../modules/description/description'
, details: '../modules/details/details' , details: '../modules/details/details'
...@@ -93,7 +95,6 @@ define( ...@@ -93,7 +95,6 @@ define(
, 'renderjs' , 'renderjs'
, 'i18next' , 'i18next'
, 'localstorage' , 'localstorage'
//, 'indexstorage'
, 'complex_queries' , 'complex_queries'
, 'md5' , 'md5'
, 'css!../css/jquery-mobile/jquery-mobile.latest' , 'css!../css/jquery-mobile/jquery-mobile.latest'
...@@ -101,15 +102,31 @@ define( ...@@ -101,15 +102,31 @@ define(
, 'css!../css/normalize/normalize' , 'css!../css/normalize/normalize'
], ],
function (require) { function (require) {
require(['app', 'storage'], function(App, Storage) { require(['app', 'config'], function(App, Config) {
App.initialize(); var setup = new $.Deferred();
var spec = {};
// expose App spec.app = App;
window.App = App; spec.config = Config;
spec.setup = setup;
// attach storage // expose App once everything is loaded
window.App.storage = Storage; spec.setup.done(function(App, Config) {
window.App = App;
// expose configuration - settings
window.App.settings = Config.settings;
// expose configuration - storages
window.App.storage = Config.storage;
});
spec.configuration_setter = function (spec) {
spec.config.set(spec);
};
// initialize
App.initialize(spec);
}); });
} }
); );
......
define([
"storage"
, "async"
],
function (storage) {
"use strict";
var priv = {};
var that = {};
// set return object
that.settings = {};
that.storage = storage;
priv.setScreenDimensions = function () {
var spec = {};
// breakpoints should be optionable?
spec.lowerTresh = 320;
spec.upperThresh = 720;
spec.framed = "medium";
if ($.mobile.media("screen and (max-width:20em)") ||
($.mobile.browser.oldIE && $(window).width() < o.lowerThresh)) {
spec.framed = "small";
} else if ($.mobile.media("screen and (min-width:45em)") ||
($.mobile.browser.oldIE && $(window).width() >= o.upperThresh )) {
spec.framed = "large";
}
return spec.framed;
};
priv.setApplicationSetters = function (spec) {
if (spec.response === undefined) {
// TODO: this defaults could be set somewhere else as well
spec.config.settings.language_default = "en-EN";
spec.config.settings.language_current = spec.config.settings.language_default;
spec.config.settings.language_selector = "translate";
spec.config.settings.language_set = false;
} else {
// create application settings
spec.config.settings.language_default = spec.response.default_language || "en-EN";
spec.config.settings.language_current = spec.response.language_current || spec.config.settings.language_default;
spec.config.settings.language_selector = spec.response.selector || "translate";
spec.config.settings.language_set = spec.response.language_set || false;
}
spec.config.settings.screen_format = priv.setScreenDimensions();
// finally...
spec.setup.resolve(spec.app, spec.config);
};
priv.JSONPfallback = function (spec) {
// bah...
window.config_call = priv.config_call = function (data) {
// store settings
if (data !== undefined) {
// add to localstorage
data["_id"] = "config";
storage.queryStorage({
"storage": "settings",
"method": "put",
"doc": data
});
}
spec.response = data || {};
// finish him...
priv.setApplicationSetters(spec);
};
// fallback: collect configuration from file
$.getJSON(
"http://www.franckreich.de/members/export/x/configuration.json?callback=?",
function(data) {
priv.config_call(data);
}
)
// require(
// ["async!http://www.franckreich.de/members/export/x/configuration.json"],
// config_call
// );
};
that.set = function (param) {
var spec = {};
spec.storage = "settings";
spec.method = "get";
spec.doc = {"_id":"config"};
spec.callback = function(err, response) {
if (err) {
priv.JSONPfallback(param);
}
param.response = response;
priv.setApplicationSetters(param);
}
storage.queryStorage(spec);
}
// return config
return that;
}
);
configuration({"default_language":"de-DE","language_current":"de-DE","language_selector":"translate","language_set":"false"});
config_call({"default_language":"de-DE","language_current":"de-DE","language_selector":"translate","language_set":"false"});
...@@ -89,7 +89,7 @@ ...@@ -89,7 +89,7 @@
}'> }'>
</div> </div>
<!-- top brands --> <!-- top brands
<div id="topbrands" <div id="topbrands"
data-gadget="modules/items/items.html" data-gadget="modules/items/items.html"
data-gadget-module="items" data-gadget-module="items"
...@@ -114,7 +114,7 @@ ...@@ -114,7 +114,7 @@
} }
] ]
}' }'
></div> ></div>-->
<!-- seo story --> <!-- seo story -->
<div id="seo" data-gadget="modules/seo/seo.html" data-gadget-module="seo"></div> <div id="seo" data-gadget="modules/seo/seo.html" data-gadget-module="seo"></div>
......
define([ define([
'jquery' 'jquery'
, 'complex_queries' , 'complex_queries'
, 'jio' , 'jio'
, 'md5' , 'md5'
...@@ -25,22 +25,7 @@ define([ ...@@ -25,22 +25,7 @@ define([
// @method setupJIO // @method setupJIO
// //
/* priv.setupJIO = function (callback) {
var jio_instance = jIO.newJio({
"type": "indexed",
"sub_storage": {
"type": "local" // for instance
"username": "me"
},
"indices": [{
"id": "index_database.json",
"index": ["title", "author", "subject", "posted_date"]
}, {
...
}]
});
*/
priv.setupJIO = function () {
// items // items
priv.items = jIO.newJio({ priv.items = jIO.newJio({
...@@ -56,12 +41,14 @@ define([ ...@@ -56,12 +41,14 @@ define([
"application_name": "basket" "application_name": "basket"
}); });
// settings // settings - should these be in localstorage or global object?
// priv.settings = jIO.newJio({ priv.settings = jIO.newJio({
// "type": "local", "type": "local",
// "username": "ecs", "username": "ecs",
// "application_name": "settings" "application_name": "settings"
// }); });
// add items
callback();
}; };
// //
...@@ -616,11 +603,10 @@ define([ ...@@ -616,11 +603,10 @@ define([
priv.initialize = function () { priv.initialize = function () {
// > set up JIOs // > set up JIOs
priv.setupJIO(); priv.setupJIO(function() {
// > populate JIOs
// > populate JIOs priv.populate_item_storage();
priv.populate_item_storage(); });
}; };
// ======================================================================== // ========================================================================
...@@ -630,23 +616,14 @@ define([ ...@@ -630,23 +616,14 @@ define([
// //
// query one of the available storages // query one of the available storages
// @method queryStorage // @method queryStorage
// @param {storage} string > jIO storage to be queried (we use more than 1!) // @param {object} param > jIO parameters (storage, method, doc, options...)
// @param {object} options > query options
// @param {object} doc > document object
// @param {object} params > query parameters
// @param {method} callback > callback to run
// @return {object} response > response/error object // @return {object} response > response/error object
// //
that.queryStorage = function (storage, method, doc, options, callback) { that.queryStorage = function (param) {
// TODO: error handling! if (param.doc !== undefined) {
if (doc) { priv[param.storage][param.method](param.doc, param.options, param.callback);
priv[storage][method](doc, options, function (error, response) {
callback(response);
});
} else { } else {
priv[storage][method](options, function (error, response) { priv[param.storage][param.method](param.options, param.callback);
callback(response);
});
} }
}; };
...@@ -657,8 +634,9 @@ define([ ...@@ -657,8 +634,9 @@ define([
// @param {string} method > jIO method to call // @param {string} method > jIO method to call
// @param {object} options > options for jIO object // @param {object} options > options for jIO object
// @param {method} callback > callback to run with results // @param {method} callback > callback to run with results
// // pointer, method, options, callback
that.switchboard = function (pointer, method, options, callback) { that.switchboard = function (param) {
var query = {};
var spec = {}; var spec = {};
// default object // default object
...@@ -673,35 +651,31 @@ define([ ...@@ -673,35 +651,31 @@ define([
"dimensions" "dimensions"
]; ];
spec.query = { query.limit = [];
"limit":[], query.sort_on = [];
"sort_on":[], query.select_list = [];
"select_list": spec.default_items, query.wildcard_character = '%'
"wildcard_character":'%'
};
// if we have a pointer, we can disregard method & options // if we have a pointer, we can disregard method & options
if (pointer === undefined) { if (param.pointer === undefined) {
spec.query.limit = options.limit; query.limit = param.options.limit;
spec.query.sort_on = options.sort_on; query.sort_on = param.options.sort_on;
spec.query.select_list = options.select_list || spec.default_items; query.select_list = param.options.select_list || spec.default_items;
} }
switch (pointer) { switch (param.pointer) {
case "topsellers": case "topsellers":
spec.query.limit.push(0, 4); query.limit.push(0, 4);
spec.query.sort_on.push( query.sort_on.push(["total_sales", "descending"]);
["total_sales", "descending"] query.select_list = spec.default_items;
);
that.queryStorage( spec.storage = "items";
"items", spec.method = "allDocs";
"allDocs", spec.options = query;
null, spec.callback = param.callback;
spec.query,
function(response){ // query
callback(response); that.queryStorage(spec);
}
);
break; break;
case "topbrands": case "topbrands":
...@@ -709,22 +683,13 @@ define([ ...@@ -709,22 +683,13 @@ define([
// default search with parameters passed! // default search with parameters passed!
default: default:
that.queryStorage(
"items",
"allDocs",
null,
spec.query,
function(response){
callback(response);
}
);
break; break;
} }
}; };
// ======================================================================== // ========================================================================
// INITIALIZE // INITIALIZE
// ======================================================================== // ========================================================================
priv.initialize(); priv.initialize();
// ======================================================================== // ========================================================================
......
...@@ -67,7 +67,7 @@ define([ ...@@ -67,7 +67,7 @@ define([
// @param {string} language > default language to set // @param {string} language > default language to set
// //
priv.setTranslations = function (language) { priv.setTranslations = function (language) {
if (window.App.settings.language_set === false) { if (window.App.settings.language_set === "false") {
$.i18n.init({ $.i18n.init({
"lng": language, "lng": language,
"load": 'current', "load": 'current',
...@@ -78,7 +78,7 @@ define([ ...@@ -78,7 +78,7 @@ define([
}, function () { }, function () {
priv.translate(language); priv.translate(language);
}); });
window.App.settings.language_set = true; window.App.settings.language_set = "true";
} else { } else {
priv.translate(language); priv.translate(language);
} }
...@@ -136,11 +136,6 @@ define([ ...@@ -136,11 +136,6 @@ define([
// PUBLISH METHODS // PUBLISH METHODS
// ==================================================================== // ====================================================================
// TODO: remove once callable via link // TODO: remove once callable via link
// methods should either be available INTERNALLY
// > gadget.translate("en-EN")
// > same for iFrame/Sandbox = public methods must be "exported"
// or methods are available EXTERNALLY
// > get a credit-score ?param=username
window.App.translate = priv.translate; window.App.translate = priv.translate;
// prefix instance-ids, pass JSON, trigger("create") // prefix instance-ids, pass JSON, trigger("create")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment