Skip to content

Commit 9f83e45

Browse files
committed
Fix some code smells
1 parent 37e532f commit 9f83e45

File tree

4 files changed

+127
-108
lines changed

4 files changed

+127
-108
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Replace JSLint with ESLint.
77
- Add new rules.specialCharClass option to personalize special chars
88
detection.
9+
- Separate progress bar and popover widgets to their own files.
910

1011
## 3.0.4
1112

Gruntfile.js

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* eslint-env node */
22

3-
module.exports = function(grunt) {
4-
'use strict';
5-
6-
var license =
3+
var license =
74
'/*!\n' +
85
'* jQuery Password Strength plugin for Twitter Bootstrap\n' +
96
'* Version: <%= pkg.version %>\n' +
@@ -12,79 +9,87 @@ module.exports = function(grunt) {
129
'* Copyright (c) 2013 Alejandro Blanco\n' +
1310
'* Dual licensed under the MIT and GPL licenses.\n' +
1411
'*/\n\n' +
15-
'(function (jQuery) {\n';
16-
17-
grunt.initConfig({
18-
pkg: grunt.file.readJSON('package.json'),
19-
eslint: {
20-
target: ['src/*js', 'spec/*js', 'Gruntfile.js']
21-
},
22-
// eslint-disable-next-line camelcase
23-
jasmine_node: {
24-
options: {
25-
forceExit: true,
26-
jUnit: {
27-
report: false
28-
}
29-
},
30-
all: ['spec/']
31-
},
32-
concat: {
33-
options: {
34-
banner: license,
35-
footer: '}(jQuery));',
36-
process: function(src, filepath) {
37-
// Remove ALL block comments, the stripBanners only removes
38-
// the first one
39-
src = src.replace(/\/\*[\s\S]*?\*\//g, '');
40-
return '// Source: ' + filepath + src;
41-
}
42-
},
43-
dist: {
44-
src: [
45-
'src/i18n.js',
46-
'src/rules.js',
47-
'src/options.js',
48-
'src/ui.js',
49-
'src/ui.progressbar.js',
50-
'src/ui.popover.js',
51-
'src/methods.js'
52-
],
53-
dest: '<%= pkg.name %>.js'
12+
'(function (jQuery) {\n',
13+
eslintConfig = {
14+
target: ['src/*js', 'spec/*js', 'Gruntfile.js']
15+
},
16+
jasmineConfig = {
17+
options: {
18+
forceExit: true,
19+
jUnit: {
20+
report: false
5421
}
5522
},
56-
uglify: {
57-
options: {
58-
banner:
59-
'/* <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> - GPLv3 & MIT License */\n',
60-
sourceMap: true,
61-
sourceMapName: '<%= pkg.name %>.min.map'
62-
},
63-
dist: {
64-
files: {
65-
'<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
66-
}
23+
all: ['spec/']
24+
},
25+
concatConfig = {
26+
options: {
27+
banner: license,
28+
footer: '}(jQuery));',
29+
process: function(src, filepath) {
30+
// Remove ALL block comments, the stripBanners only removes
31+
// the first one
32+
src = src.replace(/\/\*[\s\S]*?\*\//g, '');
33+
return '// Source: ' + filepath + src;
6734
}
6835
},
69-
shell: {
70-
copyFile: {
71-
command: 'cp <%= concat.dist.dest %> examples/pwstrength.js'
72-
},
73-
copyZxcvbn: {
74-
command:
75-
'cp bower_components/zxcvbn/dist/zxcvbn.js examples/zxcvbn.js'
76-
},
77-
copyI18next: {
78-
command:
79-
'cp bower_components/i18next/i18next.min.js examples/i18next.js'
80-
},
81-
makeDir: {
82-
command: 'mkdir -p dist'
83-
},
84-
moveFiles: {
85-
command: 'mv <%= pkg.name %>* dist/'
36+
dist: {
37+
src: [
38+
'src/i18n.js',
39+
'src/rules.js',
40+
'src/options.js',
41+
'src/ui.js',
42+
'src/ui.progressbar.js',
43+
'src/ui.popover.js',
44+
'src/methods.js'
45+
],
46+
dest: '<%= pkg.name %>.js'
47+
}
48+
},
49+
uglifyConfig = {
50+
options: {
51+
banner:
52+
'/* <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> - GPLv3 & MIT License */\n',
53+
sourceMap: true,
54+
sourceMapName: '<%= pkg.name %>.min.map'
55+
},
56+
dist: {
57+
files: {
58+
'<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
8659
}
8760
}
61+
},
62+
shellConfig = {
63+
copyFile: {
64+
command: 'cp <%= concat.dist.dest %> examples/pwstrength.js'
65+
},
66+
copyZxcvbn: {
67+
command:
68+
'cp bower_components/zxcvbn/dist/zxcvbn.js examples/zxcvbn.js'
69+
},
70+
copyI18next: {
71+
command:
72+
'cp bower_components/i18next/i18next.min.js examples/i18next.js'
73+
},
74+
makeDir: {
75+
command: 'mkdir -p dist'
76+
},
77+
moveFiles: {
78+
command: 'mv <%= pkg.name %>* dist/'
79+
}
80+
};
81+
82+
module.exports = function(grunt) {
83+
'use strict';
84+
85+
grunt.initConfig({
86+
pkg: grunt.file.readJSON('package.json'),
87+
eslint: eslintConfig,
88+
// eslint-disable-next-line camelcase
89+
jasmine_node: jasmineConfig,
90+
concat: concatConfig,
91+
uglify: uglifyConfig,
92+
shell: shellConfig
8893
});
8994

9095
// Load the plugins

src/ui.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,7 @@ var ui = {};
184184
}
185185

186186
$.each(statusClasses, function(idx, css) {
187-
if (options.ui.bootstrap3) {
188-
css = 'has-' + css;
189-
} else if (!options.ui.bootstrap2) {
190-
// BS4
191-
if (css === 'error') {
192-
css = 'danger';
193-
}
194-
css = 'border-' + css;
195-
}
187+
css = ui.cssClassesForBS(options, css);
196188
$target.removeClass(css);
197189
});
198190

@@ -201,16 +193,21 @@ var ui = {};
201193
}
202194

203195
cssClass = statusClasses[Math.floor(cssClass / 2)];
196+
cssClass = ui.cssClassesForBS(options, cssClass);
197+
$target.addClass(cssClass);
198+
};
199+
200+
ui.cssClassesForBS = function(options, css) {
204201
if (options.ui.bootstrap3) {
205-
cssClass = 'has-' + cssClass;
202+
css = 'has-' + css;
206203
} else if (!options.ui.bootstrap2) {
207204
// BS4
208-
if (cssClass === 'error') {
209-
cssClass = 'danger';
205+
if (css === 'error') {
206+
css = 'danger';
210207
}
211-
cssClass = 'border-' + cssClass;
208+
css = 'border-' + css;
212209
}
213-
$target.addClass(cssClass);
210+
return css;
214211
};
215212

216213
ui.getVerdictAndCssClass = function(options, score) {
@@ -240,27 +237,22 @@ var ui = {};
240237
};
241238

242239
ui.updateUI = function(options, $el, score) {
243-
var cssClass, barPercentage, verdictText, verdictCssClass;
240+
var cssClass, verdictText, verdictCssClass;
244241

245242
cssClass = ui.getVerdictAndCssClass(options, score);
246243
verdictText = score === 0 ? '' : cssClass[0];
247244
cssClass = cssClass[1];
248245
verdictCssClass = options.ui.useVerdictCssClass ? cssClass : -1;
249246

250247
if (options.ui.showProgressBar) {
251-
if (score === undefined) {
252-
barPercentage = options.ui.progressBarEmptyPercentage;
253-
} else {
254-
barPercentage = ui.percentage(
255-
options,
256-
score,
257-
options.ui.scores[4]
258-
);
259-
}
260-
ui.updateProgressBar(options, $el, cssClass, barPercentage);
261-
if (options.ui.showVerdictsInsideProgressBar) {
262-
ui.updateVerdict(options, $el, verdictCssClass, verdictText);
263-
}
248+
ui.showProgressBar(
249+
options,
250+
$el,
251+
score,
252+
cssClass,
253+
verdictCssClass,
254+
verdictText
255+
);
264256
}
265257

266258
if (options.ui.showStatus) {

src/ui.progressbar.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
(function($) {
1212
'use strict';
1313

14+
ui.percentage = function(options, score, maximun) {
15+
var result = Math.floor((100 * score) / maximun),
16+
min = options.ui.progressBarMinPercentage;
17+
18+
result = result <= min ? min : result;
19+
result = result > 100 ? 100 : result;
20+
return result;
21+
};
22+
1423
ui.initProgressBar = function(options, $el) {
1524
var $container = ui.getContainer(options, $el),
1625
progressbar = '<div class="progress ';
@@ -42,6 +51,27 @@
4251
}
4352
};
4453

54+
ui.showProgressBar = function(
55+
options,
56+
$el,
57+
score,
58+
cssClass,
59+
verdictCssClass,
60+
verdictText
61+
) {
62+
var barPercentage;
63+
64+
if (score === undefined) {
65+
barPercentage = options.ui.progressBarEmptyPercentage;
66+
} else {
67+
barPercentage = ui.percentage(options, score, options.ui.scores[4]);
68+
}
69+
ui.updateProgressBar(options, $el, cssClass, barPercentage);
70+
if (options.ui.showVerdictsInsideProgressBar) {
71+
ui.updateVerdict(options, $el, verdictCssClass, verdictText);
72+
}
73+
};
74+
4575
ui.updateProgressBar = function(options, $el, cssClass, percentage) {
4676
var $progressbar = ui.getUIElements(options, $el).$progressbar,
4777
$bar = $progressbar.find('.progress-bar'),
@@ -73,13 +103,4 @@
73103
}
74104
$bar.css('width', percentage + '%');
75105
};
76-
77-
ui.percentage = function(options, score, maximun) {
78-
var result = Math.floor((100 * score) / maximun),
79-
min = options.ui.progressBarMinPercentage;
80-
81-
result = result <= min ? min : result;
82-
result = result > 100 ? 100 : result;
83-
return result;
84-
};
85106
})(jQuery);

0 commit comments

Comments
 (0)