Last active
June 5, 2019 13:28
-
-
Save AndersDJohnson/017f99778c06b989d3c4 to your computer and use it in GitHub Desktop.
Bootstrap version detect
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
/** | |
* Hacks to detect Bootstrap version (2 vs. 3) from JavaScript. | |
* Does some detection of stringified functions - should be safe against most minification, but please test! | |
* | |
* Example: | |
* ```js | |
* bootstrapVersionDetect.isVersion('2', $); | |
* ``` | |
* | |
* May not be necessary soon for Bootstrap 3+, @see https://github.com/twbs/bootstrap/pull/13578 | |
*/ | |
define([] , function () { | |
var bootstrapVersionDetect = {}; | |
/** | |
* | |
* @param $ A jQuery instance loaded with Bootstrap plugins. | |
* @param {string} [pluginName=modal] Optional Bootstrap plugin name used to detect version, | |
* if the default 'modal' plugin used isn't included in your JS. | |
* @return {string} Bootstrap version string, or empty string on failure to lookup. | |
*/ | |
bootstrapVersionDetect.getVersion = function ($, pluginName) { | |
if (! $) throw new Error('bootstrap-version-detect needs a jQuery instance'); | |
pluginName = pluginName || 'modal'; | |
var pluginFn = $.fn[pluginName]; | |
if (pluginFn) { | |
if (pluginFn.VERSION) { | |
return pluginFn.VERSION; | |
} | |
if (pluginName === 'modal') { | |
// Bootstrap 2 doesn't use namespace on modal data (at least for now...) | |
return pluginFn.toString().indexOf('bs.modal') === -1 ? '2.x' : '3.x'; | |
} | |
} | |
return ''; | |
}; | |
/** | |
* Check for Bootstrap version. If we can't detect the version, returns false. | |
* | |
* @param {string} version A version string to check for, e.g. '2' or '3.2'. | |
* @param $ | |
* @param {string} [pluginName] | |
* @return {boolean} | |
*/ | |
bootstrapVersionDetect.isVersion = function (version, $, pluginName) { | |
version = typeof version === 'string' ? version : version + ''; // coerce to string | |
var detectedVersion = bootstrapVersionDetect.getVersion($, pluginName); | |
return !! ( detectedVersion && ( detectedVersion.indexOf(version) === 0 ) ); | |
}; | |
return bootstrapVersionDetect; | |
}); |
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
define(['bootstrap-version-detect'], function (bootstrapVersionDetect) { | |
describe('detect bootstrap version', function () { | |
it('should detect 2 correctly', function (done) { | |
require(['jquery', 'bootstrap2'], function ($) { | |
assert.ok(bootstrapVersionDetect.isVersion('2', $)); | |
assert.notOk(bootstrapVersionDetect.isVersion('3', $)); | |
done(); | |
}); | |
}); | |
it('should detect 3 correctly', function (done) { | |
require(['jquery', 'bootstrap3'], function ($) { | |
assert.ok(bootstrapVersionDetect.isVersion('3', $)); | |
assert.notOk(bootstrapVersionDetect.isVersion('2', $)); | |
done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment