Commit fb25a111 authored by Sven Franck's avatar Sven Franck

added JSON application configuration (config module)

parent 5056db75
......@@ -4,23 +4,16 @@ define([], function () {
var priv = {};
var that = {};
// ========================================================================
// ATTRIBUTES
// ========================================================================
/* ========================================================================
ATTRIBUTES
======================================================================== */
// document
priv.doc = $.mobile.document;
// settings > retrieve from JIO later
priv.settings = {};
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
// ========================================================================
/* ========================================================================
FLAGS
======================================================================== */
// first page loaded
priv.pageWrapLoaded;
......@@ -32,13 +25,22 @@ define([], function () {
// add page header and footer (meta/scripts) to first page loaded (only!)
// @method wrapFirstPage
//
priv.wrapFirstPage = function () {
priv.wrapFirstPage = function (spec) {
// TODO: clean this up
priv.doc.on('pagecreate.pageWrap', 'div.ui-page', function () {
if (priv.pageWrapLoaded === undefined) {
priv.pageWrapLoaded = true;
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 = [
// "text!../gadgets/pageheader.html",
// "text!../gadgets/pagefooter.html"
......@@ -61,6 +63,9 @@ 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
// @method setupGlobalParameters
......@@ -186,9 +173,6 @@ define([], function () {
// TODO: this would be the place to load JSON configuration settings
priv.setupGlobalParameters = function () {
// screen dimensions
priv.setScreenDimensions();
// initialize renderJs
priv.initializeRenderJs();
};
......@@ -197,13 +181,20 @@ define([], function () {
// initialize application
// @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
priv.setupGlobalParameters()
// > add <head>er and footer to first page only
priv.wrapFirstPage();
priv.wrapFirstPage(spec);
// initialize JQM
// TODO: is there a better way with requireJS/renderJs?
......@@ -294,6 +285,7 @@ define([], function () {
// PUBLISH APP
// ========================================================================
// return public methods to main.js
// return public methods
return that;
});
\ No newline at end of file
......@@ -16,6 +16,7 @@
, css: '../js/plugins/require-css/require-css'
, normalize: '../js/plugins/require-css/normalize'
, json: '../js/plugins/requirejs-plugins/json'
, async: '../js/plugins/requirejs-plugins/async'
// plugins/libs
, jquery: '../js/libs/jquery/jquery'
......@@ -35,6 +36,7 @@
, address: '../modules/address/address'
, basket: '../modules/basket/basket'
, browser: '../modules/browser/browser'
, config: '../modules/config/config'
, confirm: '../modules/confirm/confirm'
, description: '../modules/description/description'
, details: '../modules/details/details'
......@@ -93,7 +95,6 @@ define(
, 'renderjs'
, 'i18next'
, 'localstorage'
//, 'indexstorage'
, 'complex_queries'
, 'md5'
, 'css!../css/jquery-mobile/jquery-mobile.latest'
......@@ -101,15 +102,31 @@ define(
, 'css!../css/normalize/normalize'
],
function (require) {
require(['app', 'storage'], function(App, Storage) {
require(['app', 'config'], function(App, Config) {
App.initialize();
var setup = new $.Deferred();
var spec = {};
// expose App
window.App = App;
spec.app = App;
spec.config = Config;
spec.setup = setup;
// attach storage
window.App.storage = Storage;
// expose App once everything is loaded
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 @@
}'>
</div>
<!-- top brands -->
<!-- top brands
<div id="topbrands"
data-gadget="modules/items/items.html"
data-gadget-module="items"
......@@ -114,7 +114,7 @@
}
]
}'
></div>
></div>-->
<!-- seo story -->
<div id="seo" data-gadget="modules/seo/seo.html" data-gadget-module="seo"></div>
......
define([
'jquery'
'jquery'
, 'complex_queries'
, 'jio'
, 'md5'
......@@ -25,22 +25,7 @@ define([
// @method setupJIO
//
/*
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 () {
priv.setupJIO = function (callback) {
// items
priv.items = jIO.newJio({
......@@ -56,12 +41,14 @@ define([
"application_name": "basket"
});
// settings
// priv.settings = jIO.newJio({
// "type": "local",
// "username": "ecs",
// "application_name": "settings"
// });
// settings - should these be in localstorage or global object?
priv.settings = jIO.newJio({
"type": "local",
"username": "ecs",
"application_name": "settings"
});
// add items
callback();
};
//
......@@ -616,11 +603,10 @@ define([
priv.initialize = function () {
// > set up JIOs
priv.setupJIO();
// > populate JIOs
priv.populate_item_storage();
priv.setupJIO(function() {
// > populate JIOs
priv.populate_item_storage();
});
};
// ========================================================================
......@@ -630,23 +616,14 @@ define([
//
// query one of the available storages
// @method queryStorage
// @param {storage} string > jIO storage to be queried (we use more than 1!)
// @param {object} options > query options
// @param {object} doc > document object
// @param {object} params > query parameters
// @param {method} callback > callback to run
// @param {object} param > jIO parameters (storage, method, doc, options...)
// @return {object} response > response/error object
//
that.queryStorage = function (storage, method, doc, options, callback) {
// TODO: error handling!
if (doc) {
priv[storage][method](doc, options, function (error, response) {
callback(response);
});
that.queryStorage = function (param) {
if (param.doc !== undefined) {
priv[param.storage][param.method](param.doc, param.options, param.callback);
} else {
priv[storage][method](options, function (error, response) {
callback(response);
});
priv[param.storage][param.method](param.options, param.callback);
}
};
......@@ -657,8 +634,9 @@ define([
// @param {string} method > jIO method to call
// @param {object} options > options for jIO object
// @param {method} callback > callback to run with results
//
that.switchboard = function (pointer, method, options, callback) {
// pointer, method, options, callback
that.switchboard = function (param) {
var query = {};
var spec = {};
// default object
......@@ -673,35 +651,31 @@ define([
"dimensions"
];
spec.query = {
"limit":[],
"sort_on":[],
"select_list": spec.default_items,
"wildcard_character":'%'
};
query.limit = [];
query.sort_on = [];
query.select_list = [];
query.wildcard_character = '%'
// if we have a pointer, we can disregard method & options
if (pointer === undefined) {
spec.query.limit = options.limit;
spec.query.sort_on = options.sort_on;
spec.query.select_list = options.select_list || spec.default_items;
if (param.pointer === undefined) {
query.limit = param.options.limit;
query.sort_on = param.options.sort_on;
query.select_list = param.options.select_list || spec.default_items;
}
switch (pointer) {
switch (param.pointer) {
case "topsellers":
spec.query.limit.push(0, 4);
spec.query.sort_on.push(
["total_sales", "descending"]
);
that.queryStorage(
"items",
"allDocs",
null,
spec.query,
function(response){
callback(response);
}
);
query.limit.push(0, 4);
query.sort_on.push(["total_sales", "descending"]);
query.select_list = spec.default_items;
spec.storage = "items";
spec.method = "allDocs";
spec.options = query;
spec.callback = param.callback;
// query
that.queryStorage(spec);
break;
case "topbrands":
......@@ -709,22 +683,13 @@ define([
// default search with parameters passed!
default:
that.queryStorage(
"items",
"allDocs",
null,
spec.query,
function(response){
callback(response);
}
);
break;
}
};
// ========================================================================
// INITIALIZE
// ========================================================================
priv.initialize();
// ========================================================================
......
......@@ -67,7 +67,7 @@ define([
// @param {string} language > default language to set
//
priv.setTranslations = function (language) {
if (window.App.settings.language_set === false) {
if (window.App.settings.language_set === "false") {
$.i18n.init({
"lng": language,
"load": 'current',
......@@ -78,7 +78,7 @@ define([
}, function () {
priv.translate(language);
});
window.App.settings.language_set = true;
window.App.settings.language_set = "true";
} else {
priv.translate(language);
}
......@@ -136,11 +136,6 @@ define([
// PUBLISH METHODS
// ====================================================================
// 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;
// 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