Skip to content

Commit 4c10947

Browse files
committed
feature(smalltalk) add progress
1 parent 58b2f2b commit 4c10947

10 files changed

Lines changed: 149 additions & 7 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ smalltalk
8888
});
8989
```
9090

91+
## smalltalk.progress(title, message)
92+
93+
![Progress](https://raw.githubusercontent.com/coderaiser/smalltalk/master/screen/progress.png "Progress")
94+
95+
```js
96+
const progress = smalltalk.progress('Cloud Commander', 'Copy /home/coderaiser -> /home/coderaiser/2');
97+
98+
progress.setProgress(41);
99+
.catch(() => {
100+
console.log('abort');
101+
});
102+
```
103+
91104
## Custom label
92105

93106
You can use custom label passing into options param the buttons specification. For example :

css/smalltalk.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
background-image: url(../img/IDR_CLOSE_DIALOG_H.png);
5757
}
5858

59+
.smalltalk .progress {
60+
display: block;
61+
width: 100%;
62+
}
63+
5964
.smalltalk .page header {
6065
overflow: hidden;
6166
text-overflow: ellipsis;

example/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
}, function() {
1010
console.log('close');
1111
})
12-
});
12+
});
1313
</script>
14+

example/progress.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<meta content="width=device-width,initial-scale=1" name="viewport">
3+
4+
<script src="../dist/smalltalk.min.js"></script>
5+
<script>
6+
window.addEventListener('DOMContentLoaded', () => {
7+
smalltalk.progress('Title').then((value) => {
8+
console.log(value);
9+
}, () => {
10+
console.log('close');
11+
})
12+
});
13+
</script>
14+

lib/smalltalk.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ exports.confirm = (title, msg, options) => {
4040
return showDialog(title, msg, '', buttons, options);
4141
};
4242

43+
exports.progress = (title, message, options) => {
44+
const valueStr = `
45+
<progress value="0" data-name="js-progress" class="progress" max="100"></progress>
46+
<span data-name="js-counter">0%</span>
47+
`;
48+
49+
const buttons = {
50+
cancel: 'Abort',
51+
};
52+
53+
const promise = showDialog(title, message, valueStr, buttons, options);
54+
const {ok, dialog} = promise;
55+
const resolve = ok();
56+
57+
find(dialog, ['cancel']).map((el) => {
58+
el.focus();
59+
});
60+
61+
Object.assign(promise, {
62+
setProgress(count) {
63+
const [elProgress] = find(dialog, ['progress']);
64+
const [elCounter] = find(dialog, ['counter']);
65+
66+
elProgress.value = count;
67+
elCounter.textContent = `${count}%`;
68+
69+
if (count === 100) {
70+
remove();
71+
resolve();
72+
}
73+
},
74+
});
75+
76+
return promise;
77+
};
78+
4379
function getButtons(options = {}) {
4480
const {buttons} = options;
4581

@@ -135,7 +171,10 @@ function showDialog(title, msg, value, buttons, options) {
135171

136172
dialog.addEventListener('keydown', keyDown(dialog, ok(), cancel()));
137173

138-
return promise;
174+
return Object.assign(promise, {
175+
dialog,
176+
ok,
177+
});
139178
}
140179

141180
function keyDown_(dialog, ok, cancel, event) {

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
"scripts": {
1212
"watch": "nodemon --watch lib --watch test --exec",
1313
"watch:client": "redrun compile:client -- --watch",
14+
"watch:client:dev": "redrun compile:client:dev -- --watch",
1415
"watch:test": "npm run watch -- npm test",
1516
"watch:lint": "npm run watch -- 'npm run lint'",
1617
"watch:lint:js": "npm run watch -- \"run lint:js\"",
1718
"watch:coverage": "redrun watch -- redrun coverage",
19+
"watch:legacy": "npm run watch -- redrun compile:legacy",
1820
"coverage": "nyc npm test",
1921
"report": "nyc report --reporter=text-lcov | coveralls",
20-
"compile:server": "babel -d legacy lib",
22+
"compile:legacy": "babel -d legacy lib",
2123
"compile:client": "webpack --progress --mode production",
24+
"compile:client:dev": "webpack --progress --mode development",
2225
"build": "redrun clean init build:*",
2326
"build:js": "redrun compile:* legacy:*",
2427
"legacy:index": "echo \"module.exports = require('./smalltalk');\" > legacy/index.js",
@@ -29,7 +32,7 @@
2932
"lint:js": "eslint lib test webpack.config.js",
3033
"lint": "redrun lint:*",
3134
"test": "tape 'test/**/*.js'",
32-
"test:update": "UPDATE_FIXTURE=1 redrun test",
35+
"test:update": "UPDATE_FIXTURE=1 npm test",
3336
"init": "mkdirp native"
3437
},
3538
"keywords": [

screen/progress.png

4.41 KB
Loading

test/fixture/progress.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="page">
2+
<div data-name="js-close" class="close-button"></div>
3+
<header>title</header>
4+
<div class="content-area">hello<br>world
5+
<progress value="0" data-name="js-progress" class="progress" max="100"></progress>
6+
<span data-name="js-counter">0%</span>
7+
</div>
8+
<div class="action-area">
9+
<div class="button-strip">
10+
<button
11+
tabindex=0
12+
data-name="js-cancel">
13+
Abort
14+
</button>
15+
</div>
16+
</div>
17+
</div>

test/smalltalk.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const fixture = {
4141
promptNoValue: readFixture('prompt-no-value'),
4242
alertCustomLabel: readFixture('alert-custom-label'),
4343
confirmCustomLabel: readFixture('confirm-custom-label'),
44-
promptCustomLabel: readFixture('prompt-custom-label')
44+
promptCustomLabel: readFixture('prompt-custom-label'),
45+
progress: readFixture('progress'),
4546
};
4647

4748
test('smalltalk: alert: innerHTML', (t, {document}) => {
@@ -1038,3 +1039,52 @@ test('smalltalk: prompt: custom label', (t, {document}) => {
10381039
t.end();
10391040
});
10401041

1042+
test('smalltalk: progress: innerHTML', (t, {document}) => {
1043+
const {createElement} = document;
1044+
const el = create();
1045+
createElement.returns(el);
1046+
1047+
smalltalk.progress('title', 'hello\nworld');
1048+
fixture.progress.update(el.innerHTML);
1049+
1050+
t.equal(fixture.progress(), el.innerHTML, 'should be equal');
1051+
t.end();
1052+
});
1053+
1054+
test('smalltalk: progress: setProgress', (t, {document}) => {
1055+
const valueEl = create();
1056+
1057+
const {createElement} = document;
1058+
const el = create();
1059+
1060+
el.querySelector.returns(valueEl);
1061+
createElement.returns(el);
1062+
1063+
const progress = smalltalk.progress('title', 'hello\nworld');
1064+
1065+
progress.setProgress(10);
1066+
1067+
t.equal();
1068+
t.end();
1069+
});
1070+
1071+
test('smalltalk: progress: setProgress: 100', (t, {document}) => {
1072+
const valueEl = create();
1073+
valueEl.parentElement = create();
1074+
1075+
document.querySelector.returns(valueEl);
1076+
1077+
const {createElement} = document;
1078+
const el = create();
1079+
1080+
el.querySelector.returns(valueEl);
1081+
createElement.returns(el);
1082+
1083+
const progress = smalltalk.progress('title', 'hello\nworld');
1084+
1085+
progress.setProgress(100);
1086+
1087+
t.equal();
1088+
t.end();
1089+
});
1090+

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const dir = './lib';
66
const dist = path.resolve(__dirname, 'dist');
77
const devtool = 'source-map';
88

9-
const rules = [{
9+
const rules = [/*{
1010
test: /\.js$/,
1111
exclude: /node_modules/,
1212
loader: 'babel-loader',
13-
}, {
13+
},*/ {
1414
test: /\.css$/,
1515
loader: 'style-loader!css-loader!clean-css-loader',
1616
}, {

0 commit comments

Comments
 (0)