Skip to content

Commit

Permalink
Add init.js and js utils
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-pribilinskiy committed Mar 21, 2016
1 parent bdff277 commit 1b92dcb
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 6 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
Expand All @@ -13,6 +13,11 @@
"bower_components",
"test"
],
"dependencies": {
"jquery": "~2",
"lodash": "~4",
"modernizr": "~2"
},
"devDependencies": {
}
}
77 changes: 77 additions & 0 deletions fn/LocalStorageBooleanSetting.js
Original file line number Diff line number Diff line change
@@ -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);
}
};

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('./init');

var requireContext = require.context('.', true, /^\.\/.*\/.*\.js$/);

// Require scripts from subdirectories
Expand Down
84 changes: 84 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
@@ -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);
}
},
});

})();
38 changes: 38 additions & 0 deletions util/browserDetection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Add classes to `<html>` 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');
}
}
})();
8 changes: 8 additions & 0 deletions util/modernizrTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Extend Modernizr
if (typeof Modernizr != 'undefined') {

Modernizr.addTest('firefox', function() {

return !!navigator.userAgent.match(/firefox/i);
});
}

0 comments on commit 1b92dcb

Please sign in to comment.