-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdff277
commit 1b92dcb
Showing
7 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>" | ||
|
@@ -13,6 +13,11 @@ | |
"bower_components", | ||
"test" | ||
], | ||
"dependencies": { | ||
"jquery": "~2", | ||
"lodash": "~4", | ||
"modernizr": "~2" | ||
}, | ||
"devDependencies": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
}); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |