From 1b92dcb37edb2d3f4af0e238c4a787aa39fd7665 Mon Sep 17 00:00:00 2001 From: Steven Pribilinskiy Date: Mon, 14 Mar 2016 18:04:43 +0200 Subject: [PATCH] Add `init.js` and js utils --- README.md | 7 ++- bower.json | 7 ++- fn/LocalStorageBooleanSetting.js | 77 +++++++++++++++++++++++++++++ index.js | 2 + init.js | 84 ++++++++++++++++++++++++++++++++ util/browserDetection.js | 38 +++++++++++++++ util/modernizrTests.js | 8 +++ 7 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 fn/LocalStorageBooleanSetting.js create mode 100644 init.js create mode 100644 util/browserDetection.js create mode 100644 util/modernizrTests.js diff --git a/README.md b/README.md index d68eacb..9be34e6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ ## OBB JavaScript shared code -Collection of general-purpose in-house JS functions and classes for OpenBizBox apps. +Collection of general-purpose in-house JS functions and classes for OpenBizBox apps + +### Requirements + +- `jQuery` and `lodash` must be available before including these scripts +- `init.js` must be included before all other files ### Usage diff --git a/bower.json b/bower.json index e6af99c..1ffde5c 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "js-obb", "main": "index.js", - "version": "0.0.1", + "version": "0.1.0", "homepage": "https://github.com/goldenplanetdk/js-obb", "authors": [ "Steven Pribilinskiy " @@ -13,6 +13,11 @@ "bower_components", "test" ], + "dependencies": { + "jquery": "~2", + "lodash": "~4", + "modernizr": "~2" + }, "devDependencies": { } } diff --git a/fn/LocalStorageBooleanSetting.js b/fn/LocalStorageBooleanSetting.js new file mode 100644 index 0000000..5a0977f --- /dev/null +++ b/fn/LocalStorageBooleanSetting.js @@ -0,0 +1,77 @@ +/** + * Manager for boolean values that are stored in browser's local storage as strings + * + * @param {string} key A key in localstorage that will be managed + * @param {*} defaultValue A default value to be set if it is not defined + * @constructor + */ +obb.fn.LocalStorageBooleanSetting = function(key, defaultValue) { + + /** + * Retrieve value from local storage and convert to boolean type + * + * @returns {boolean} `true` for `'true'` and `1`, in all other cases `false` + */ + this.get = function() { + return localStorage[key] == 'true' || localStorage[key] == 1; + }; + + /** + * Store boolean value as `true`/`false` string + * @param {*} value + */ + this.set = function(value) { + localStorage[key] = !!value; + }; + + /** + * Toggle boolean value + * @param {*} value + */ + this.toggle = function(value) { + this.set(!this.get()); + }; + + /** + * Set value only if it is not `undefined` + * @param value + */ + this.setDefault = function(value) { + !this.defined ? this.set(value) : null; + }; + + // Define getters for the local storage key + Object.defineProperties(this, { + + /** + * Getter for the `get` method + */ + enabled: { + get: this.get + }, + + /** + * Getter for the `get` method, returns the opposite value + */ + disabled: { + get: function() { + return !this.enabled; + } + }, + + /** + * Test whether a value is defined + */ + defined: { + get: function() { + return localStorage[key] !== undefined; + } + } + }); + + // A default value to be set if it is not defined + if (defaultValue !== undefined) { + this.setDefault(defaultValue); + } +}; + diff --git a/index.js b/index.js index 8552ada..578a0ae 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +require('./init'); + var requireContext = require.context('.', true, /^\.\/.*\/.*\.js$/); // Require scripts from subdirectories diff --git a/init.js b/init.js new file mode 100644 index 0000000..d63ebeb --- /dev/null +++ b/init.js @@ -0,0 +1,84 @@ +/* Initialize collections + ----------------------------------------------------------------------*/ +window.obb = window.obb || {}; + +_.defaults(obb, { + components: {}, + constants: {}, + data: {}, + fn: {}, +}); + +(function() { + + _.merge(obb.fn, { + + /** + * OBB JavaScript modules initializer + * + * @param modules + */ + init: function(modules) { + + if (!_.isArray(modules)) { + modules = [modules] + } + + modules.forEach(function(modulePath) { + + var module = _.get(window, modulePath); + + if (module) { + module.initialized = false; + + if (_(obb.disabledModules).includes(modulePath)) { + module.disabled = true; + return; + } + + $(document).trigger('initialize.' + modulePath); + + var initialized = false; + + if (typeof module.init == 'function') { + module.init(); + initialized = true; + } else if (typeof module == 'function') { + module(); + initialized = true; + } + + if (initialized) { + $(document).trigger('obbInitialized.' + modulePath); + module.initialized = true; + } + } + else { + var message = 'module "' + modulePath + '" is not available'; + + obb.DEVMODE + ? console.error(message) + : (obb.jsServerLoggerEnabled ? log(message) : null); + } + }); + }, + + /** + * Invoke a function only after specific module is already initialized + * + * Useful in modules where some code depends on other module availability + * + * @param {string} modulePath + * @param {Function} callback Code that will run only after the specified module is initialized + */ + initAfter: function(modulePath, callback) { + + if (_.get(obb, modulePath + '.initialized')) { + callback(); + } else { + $(document).on('obbInitialized.' + modulePath, callback); + } + }, + }); + +})(); diff --git a/util/browserDetection.js b/util/browserDetection.js new file mode 100644 index 0000000..1a43ed9 --- /dev/null +++ b/util/browserDetection.js @@ -0,0 +1,38 @@ +/** + * Add classes to `` for detected browser and `js` execution state + */ +(function() { + + var $html = $('html'); + + // Add JavaScript execution state class + $html + .removeClass('no-js') + .addClass('js') + ; + + // IE + var userAgent = navigator.userAgent.toString().toLowerCase(); + + var match = /(trident)(?:.*rv:([\w.]+))/.exec(userAgent) + || /(msie) ([\w.]+)/.exec(userAgent) + || ['', null, -1]; + + var ieVersion = match[2]; + + if (ieVersion != -1) { + $('html').addClass('ie ie' + parseInt(ieVersion)); + } + + // Android + if (userAgent.indexOf('mozilla/5.0') > -1 + && userAgent.indexOf('android') > -1 + && userAgent.indexOf('applewebkit') > -1 + ) { + $html.addClass('android'); + + if (userAgent.indexOf('chrome') == -1) { + $html.addClass('android-stock'); + } + } +})(); diff --git a/util/modernizrTests.js b/util/modernizrTests.js new file mode 100644 index 0000000..97c0014 --- /dev/null +++ b/util/modernizrTests.js @@ -0,0 +1,8 @@ +// Extend Modernizr +if (typeof Modernizr != 'undefined') { + + Modernizr.addTest('firefox', function() { + + return !!navigator.userAgent.match(/firefox/i); + }); +}