Skip to content

Commit

Permalink
feature(smalltalk) add progress
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Apr 1, 2019
1 parent 58b2f2b commit 4c10947
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ smalltalk
});
```

## smalltalk.progress(title, message)

![Progress](https://raw.githubusercontent.com/coderaiser/smalltalk/master/screen/progress.png "Progress")

```js
const progress = smalltalk.progress('Cloud Commander', 'Copy /home/coderaiser -> /home/coderaiser/2');

progress.setProgress(41);
.catch(() => {
console.log('abort');
});
```

## Custom label

You can use custom label passing into options param the buttons specification. For example :
Expand Down
5 changes: 5 additions & 0 deletions css/smalltalk.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
background-image: url(../img/IDR_CLOSE_DIALOG_H.png);
}

.smalltalk .progress {
display: block;
width: 100%;
}

.smalltalk .page header {
overflow: hidden;
text-overflow: ellipsis;
Expand Down
3 changes: 2 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
}, function() {
console.log('close');
})
});
});
</script>

14 changes: 14 additions & 0 deletions example/progress.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<meta content="width=device-width,initial-scale=1" name="viewport">

<script src="../dist/smalltalk.min.js"></script>
<script>
window.addEventListener('DOMContentLoaded', () => {
smalltalk.progress('Title').then((value) => {
console.log(value);
}, () => {
console.log('close');
})
});
</script>

41 changes: 40 additions & 1 deletion lib/smalltalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ exports.confirm = (title, msg, options) => {
return showDialog(title, msg, '', buttons, options);
};

exports.progress = (title, message, options) => {
const valueStr = `
<progress value="0" data-name="js-progress" class="progress" max="100"></progress>
<span data-name="js-counter">0%</span>
`;

const buttons = {
cancel: 'Abort',
};

const promise = showDialog(title, message, valueStr, buttons, options);
const {ok, dialog} = promise;
const resolve = ok();

find(dialog, ['cancel']).map((el) => {
el.focus();
});

Object.assign(promise, {
setProgress(count) {
const [elProgress] = find(dialog, ['progress']);
const [elCounter] = find(dialog, ['counter']);

elProgress.value = count;
elCounter.textContent = `${count}%`;

if (count === 100) {
remove();
resolve();
}
},
});

return promise;
};

function getButtons(options = {}) {
const {buttons} = options;

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

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

return promise;
return Object.assign(promise, {
dialog,
ok,
});
}

function keyDown_(dialog, ok, cancel, event) {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
"scripts": {
"watch": "nodemon --watch lib --watch test --exec",
"watch:client": "redrun compile:client -- --watch",
"watch:client:dev": "redrun compile:client:dev -- --watch",
"watch:test": "npm run watch -- npm test",
"watch:lint": "npm run watch -- 'npm run lint'",
"watch:lint:js": "npm run watch -- \"run lint:js\"",
"watch:coverage": "redrun watch -- redrun coverage",
"watch:legacy": "npm run watch -- redrun compile:legacy",
"coverage": "nyc npm test",
"report": "nyc report --reporter=text-lcov | coveralls",
"compile:server": "babel -d legacy lib",
"compile:legacy": "babel -d legacy lib",
"compile:client": "webpack --progress --mode production",
"compile:client:dev": "webpack --progress --mode development",
"build": "redrun clean init build:*",
"build:js": "redrun compile:* legacy:*",
"legacy:index": "echo \"module.exports = require('./smalltalk');\" > legacy/index.js",
Expand All @@ -29,7 +32,7 @@
"lint:js": "eslint lib test webpack.config.js",
"lint": "redrun lint:*",
"test": "tape 'test/**/*.js'",
"test:update": "UPDATE_FIXTURE=1 redrun test",
"test:update": "UPDATE_FIXTURE=1 npm test",
"init": "mkdirp native"
},
"keywords": [
Expand Down
Binary file added screen/progress.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions test/fixture/progress.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="page">
<div data-name="js-close" class="close-button"></div>
<header>title</header>
<div class="content-area">hello<br>world
<progress value="0" data-name="js-progress" class="progress" max="100"></progress>
<span data-name="js-counter">0%</span>
</div>
<div class="action-area">
<div class="button-strip">
<button
tabindex=0
data-name="js-cancel">
Abort
</button>
</div>
</div>
</div>
52 changes: 51 additions & 1 deletion test/smalltalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const fixture = {
promptNoValue: readFixture('prompt-no-value'),
alertCustomLabel: readFixture('alert-custom-label'),
confirmCustomLabel: readFixture('confirm-custom-label'),
promptCustomLabel: readFixture('prompt-custom-label')
promptCustomLabel: readFixture('prompt-custom-label'),
progress: readFixture('progress'),
};

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

test('smalltalk: progress: innerHTML', (t, {document}) => {
const {createElement} = document;
const el = create();
createElement.returns(el);

smalltalk.progress('title', 'hello\nworld');
fixture.progress.update(el.innerHTML);

t.equal(fixture.progress(), el.innerHTML, 'should be equal');
t.end();
});

test('smalltalk: progress: setProgress', (t, {document}) => {
const valueEl = create();

const {createElement} = document;
const el = create();

el.querySelector.returns(valueEl);
createElement.returns(el);

const progress = smalltalk.progress('title', 'hello\nworld');

progress.setProgress(10);

t.equal();
t.end();
});

test('smalltalk: progress: setProgress: 100', (t, {document}) => {
const valueEl = create();
valueEl.parentElement = create();

document.querySelector.returns(valueEl);

const {createElement} = document;
const el = create();

el.querySelector.returns(valueEl);
createElement.returns(el);

const progress = smalltalk.progress('title', 'hello\nworld');

progress.setProgress(100);

t.equal();
t.end();
});

4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const dir = './lib';
const dist = path.resolve(__dirname, 'dist');
const devtool = 'source-map';

const rules = [{
const rules = [/*{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
}, {
},*/ {
test: /\.css$/,
loader: 'style-loader!css-loader!clean-css-loader',
}, {
Expand Down

0 comments on commit 4c10947

Please sign in to comment.