jquery-strength.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * jQuery strength v0.2.5
  3. * https://github.com/amazingSurge/jquery-strength
  4. *
  5. * Copyright (c) amazingSurge
  6. * Released under the LGPL-3.0 license
  7. */
  8. (function(global, factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(['jquery', 'password_strength'], factory);
  11. } else if (typeof exports !== 'undefined') {
  12. factory(require('jquery'), require('password_strength'));
  13. } else {
  14. var mod = {
  15. exports: {}
  16. };
  17. factory(global.jQuery, global.PasswordStrength);
  18. global.jqueryStrengthEs = mod.exports;
  19. }
  20. })(this, function(_jquery, _password_strength) {
  21. 'use strict';
  22. var _jquery2 = _interopRequireDefault(_jquery);
  23. var _password_strength2 = _interopRequireDefault(_password_strength);
  24. function _interopRequireDefault(obj) {
  25. return obj && obj.__esModule
  26. ? obj
  27. : {
  28. default: obj
  29. };
  30. }
  31. function _classCallCheck(instance, Constructor) {
  32. if (!(instance instanceof Constructor)) {
  33. throw new TypeError('Cannot call a class as a function');
  34. }
  35. }
  36. var _createClass = (function() {
  37. function defineProperties(target, props) {
  38. for (var i = 0; i < props.length; i++) {
  39. var descriptor = props[i];
  40. descriptor.enumerable = descriptor.enumerable || false;
  41. descriptor.configurable = true;
  42. if ('value' in descriptor) descriptor.writable = true;
  43. Object.defineProperty(target, descriptor.key, descriptor);
  44. }
  45. }
  46. return function(Constructor, protoProps, staticProps) {
  47. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  48. if (staticProps) defineProperties(Constructor, staticProps);
  49. return Constructor;
  50. };
  51. })();
  52. var DEFAULTS = {
  53. namespace: 'strength',
  54. skin: null,
  55. showMeter: true,
  56. showToggle: true,
  57. usernameField: '',
  58. templates: {
  59. toggle:
  60. '<span class="input-group-addon"><input type="checkbox" class="{toggleClass}" title="Show/Hide Password" /></span>',
  61. meter: '<div class="{meterClass}">{score}</div>',
  62. score: '<span class="label {scoreClass}"></span>',
  63. main:
  64. '<div class="{containerClass}"><div class="input-group">{input}{toggle}</div>{meter}</div>'
  65. },
  66. classes: {
  67. container: 'strength-container',
  68. status: 'strength-{status}',
  69. input: 'strength-input',
  70. toggle: 'strength-toggle',
  71. meter: 'strength-meter',
  72. score: 'strength-score',
  73. shown: 'strength-shown'
  74. },
  75. scoreLables: {
  76. empty: 'Empty',
  77. invalid: 'Invalid',
  78. weak: 'Weak',
  79. good: 'Good',
  80. strong: 'Strong'
  81. },
  82. scoreClasses: {
  83. empty: '',
  84. invalid: 'label-danger',
  85. weak: 'label-warning',
  86. good: 'label-info',
  87. strong: 'label-success'
  88. },
  89. emptyStatus: true,
  90. scoreCallback: null,
  91. statusCallback: null
  92. };
  93. var NAMESPACE$1 = 'strength';
  94. /**
  95. * Plugin constructor
  96. **/
  97. var Strength = (function() {
  98. function Strength(element) {
  99. var options =
  100. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  101. _classCallCheck(this, Strength);
  102. this.element = element;
  103. this.$element = (0, _jquery2.default)(element);
  104. this.options = _jquery2.default.extend(
  105. true,
  106. {},
  107. DEFAULTS,
  108. options,
  109. this.$element.data()
  110. );
  111. this.classes = this.options.classes;
  112. this.$username = (0, _jquery2.default)(this.options.usernameField);
  113. this.score = 0;
  114. this.status = null;
  115. this.shown = false;
  116. this.trigger('init');
  117. this.init();
  118. }
  119. _createClass(
  120. Strength,
  121. [
  122. {
  123. key: 'init',
  124. value: function init() {
  125. this.createHtml();
  126. this.$element.addClass(this.classes.input);
  127. this.$toggle = this.$container.find('.' + this.classes.toggle);
  128. this.$meter = this.$container.find('.' + this.classes.meter);
  129. this.$score = this.$container.find('.' + this.classes.score);
  130. this.$input = this.$container.find('.' + this.classes.input);
  131. this.bindEvents();
  132. this.initialized = true;
  133. this.trigger('ready');
  134. }
  135. },
  136. {
  137. key: 'bindEvents',
  138. value: function bindEvents() {
  139. var _this = this;
  140. if (this.$toggle.is(':checkbox')) {
  141. this.$toggle.on('change', function() {
  142. _this.toggle();
  143. });
  144. } else {
  145. this.$toggle.on('click', function() {
  146. _this.toggle();
  147. });
  148. }
  149. this.$input.bind('keyup.strength keydown.strength', function() {
  150. _this.check();
  151. });
  152. this.$element.on(NAMESPACE$1 + '::check', function(
  153. e,
  154. api,
  155. score,
  156. status
  157. ) {
  158. _this.$score.html(_this.options.scoreLables[status]);
  159. if (status !== _this.status) {
  160. var newClass = _this.options.scoreClasses[status];
  161. var oldClass = _this.options.scoreClasses[_this.status];
  162. _this.$score.removeClass(oldClass).addClass(newClass);
  163. _this.trigger('statusChange', status, _this.status);
  164. }
  165. _this.status = status;
  166. _this.score = score;
  167. });
  168. this.$element.on(NAMESPACE$1 + '::statusChange', function(
  169. e,
  170. api,
  171. current,
  172. old
  173. ) {
  174. _this.$container
  175. .removeClass(_this.getStatusClass(old))
  176. .addClass(_this.getStatusClass(current));
  177. });
  178. }
  179. },
  180. {
  181. key: 'getStatusClass',
  182. value: function getStatusClass(status) {
  183. return this.options.classes.status.replace('{status}', status);
  184. }
  185. },
  186. {
  187. key: 'createHtml',
  188. value: function createHtml() {
  189. var output = this.options.templates.main;
  190. output = output.replace('{containerClass}', this.classes.container);
  191. output = output.replace('{toggle}', this.generateToggle());
  192. output = output.replace('{meter}', this.generateMeter());
  193. output = output.replace('{score}', this.generateScore());
  194. output = output.replace(
  195. '{input}',
  196. '<div class="' + this.classes.input + '"></div>'
  197. );
  198. this.$container = (0, _jquery2.default)(output);
  199. if (this.options.skin) {
  200. this.$container.addClass(this.options.skin);
  201. }
  202. this.$element.before(this.$container);
  203. var $holder = this.$container.find('.' + this.classes.input);
  204. var el = this.$element.detach();
  205. $holder.before(el);
  206. $holder.remove();
  207. }
  208. },
  209. {
  210. key: 'generateToggle',
  211. value: function generateToggle() {
  212. if (this.options.showToggle) {
  213. var output = this.options.templates.toggle;
  214. output = output.replace('{toggleClass}', this.classes.toggle);
  215. return output;
  216. }
  217. return '';
  218. }
  219. },
  220. {
  221. key: 'generateMeter',
  222. value: function generateMeter() {
  223. if (this.options.showMeter) {
  224. var output = this.options.templates.meter;
  225. output = output.replace('{meterClass}', this.classes.meter);
  226. return output;
  227. }
  228. return '';
  229. }
  230. },
  231. {
  232. key: 'generateScore',
  233. value: function generateScore() {
  234. var output = this.options.templates.score;
  235. output = output.replace('{scoreClass}', this.classes.score);
  236. return output;
  237. }
  238. },
  239. {
  240. key: 'check',
  241. value: function check() {
  242. var score = 0;
  243. var status = null;
  244. if (_jquery2.default.isFunction(this.options.scoreCallback)) {
  245. score = this.options.scoreCallback.call(this);
  246. if (_jquery2.default.isFunction(this.options.statusCallback)) {
  247. status = this.options.statusCallback.call(this, score);
  248. }
  249. } else {
  250. var check = new _password_strength2.default();
  251. check.username = this.$username.val() || null;
  252. check.password = this.$input.val();
  253. score = check.test();
  254. status = check.status;
  255. }
  256. if (
  257. this.options.emptyStatus &&
  258. status !== 'invalid' &&
  259. this.$input.val() === ''
  260. ) {
  261. status = 'empty';
  262. }
  263. this.trigger('check', score, status);
  264. }
  265. },
  266. {
  267. key: 'getScore',
  268. value: function getScore() {
  269. if (!this.score) {
  270. this.check();
  271. }
  272. return this.score;
  273. }
  274. },
  275. {
  276. key: 'getStatus',
  277. value: function getStatus() {
  278. if (!this.status) {
  279. this.check();
  280. }
  281. return this.status;
  282. }
  283. },
  284. {
  285. key: 'toggle',
  286. value: function toggle() {
  287. var type = void 0;
  288. if (this.$toggle.is(':checkbox')) {
  289. type = this.$toggle.is(':checked') ? 'text' : 'password';
  290. } else {
  291. type = this.shown === false ? 'text' : 'password';
  292. }
  293. this.shown = type === 'text';
  294. if (this.shown) {
  295. this.$container.addClass(this.classes.shown);
  296. } else {
  297. this.$container.removeClass(this.classes.shown);
  298. }
  299. this.$input.attr('type', type);
  300. this.trigger('toggle', type);
  301. }
  302. },
  303. {
  304. key: 'trigger',
  305. value: function trigger(eventType) {
  306. for (
  307. var _len = arguments.length,
  308. params = Array(_len > 1 ? _len - 1 : 0),
  309. _key = 1;
  310. _key < _len;
  311. _key++
  312. ) {
  313. params[_key - 1] = arguments[_key];
  314. }
  315. var data = [this].concat(params);
  316. // event
  317. this.$element.trigger(NAMESPACE$1 + '::' + eventType, data);
  318. // callback
  319. eventType = eventType.replace(/\b\w+\b/g, function(word) {
  320. return word.substring(0, 1).toUpperCase() + word.substring(1);
  321. });
  322. var onFunction = 'on' + eventType;
  323. if (typeof this.options[onFunction] === 'function') {
  324. this.options[onFunction].apply(this, params);
  325. }
  326. }
  327. },
  328. {
  329. key: 'destroy',
  330. value: function destroy() {
  331. this.$element.data(NAMESPACE$1, null);
  332. this.trigger('destroy');
  333. }
  334. }
  335. ],
  336. [
  337. {
  338. key: 'setDefaults',
  339. value: function setDefaults(options) {
  340. _jquery2.default.extend(
  341. true,
  342. DEFAULTS,
  343. _jquery2.default.isPlainObject(options) && options
  344. );
  345. }
  346. }
  347. ]
  348. );
  349. return Strength;
  350. })();
  351. var info = {
  352. version: '0.2.5'
  353. };
  354. var NAMESPACE = 'strength';
  355. var OtherStrength = _jquery2.default.fn.strength;
  356. var jQueryStrength = function jQueryStrength(options) {
  357. for (
  358. var _len2 = arguments.length,
  359. args = Array(_len2 > 1 ? _len2 - 1 : 0),
  360. _key2 = 1;
  361. _key2 < _len2;
  362. _key2++
  363. ) {
  364. args[_key2 - 1] = arguments[_key2];
  365. }
  366. if (typeof options === 'string') {
  367. var method = options;
  368. if (/^_/.test(method)) {
  369. return false;
  370. } else if (/^(get)/.test(method)) {
  371. var instance = this.first().data(NAMESPACE);
  372. if (instance && typeof instance[method] === 'function') {
  373. return instance[method].apply(instance, args);
  374. }
  375. } else {
  376. return this.each(function() {
  377. var instance = _jquery2.default.data(this, NAMESPACE);
  378. if (instance && typeof instance[method] === 'function') {
  379. instance[method].apply(instance, args);
  380. }
  381. });
  382. }
  383. }
  384. return this.each(function() {
  385. if (!(0, _jquery2.default)(this).data(NAMESPACE)) {
  386. (0, _jquery2.default)(this).data(
  387. NAMESPACE,
  388. new Strength(this, options)
  389. );
  390. }
  391. });
  392. };
  393. _jquery2.default.fn.strength = jQueryStrength;
  394. _jquery2.default.strength = _jquery2.default.extend(
  395. {
  396. setDefaults: Strength.setDefaults,
  397. noConflict: function noConflict() {
  398. _jquery2.default.fn.strength = OtherStrength;
  399. return jQueryStrength;
  400. }
  401. },
  402. info
  403. );
  404. });