Skip to content

Instantly share code, notes, and snippets.

@tomodutch
Last active September 12, 2015 14:10
Show Gist options
  • Save tomodutch/b415a6a3f1b7ab683583 to your computer and use it in GitHub Desktop.
Save tomodutch/b415a6a3f1b7ab683583 to your computer and use it in GitHub Desktop.
polymer pomodoro
<html>
<head>
...
<link rel="import" href="pomodoro-clock.html">
<link rel="import" href="pomodoro-timer.html">
</head>
<body>
<pomodoro-clock work-seconds="25" break-seconds="5"></pomodoro-clock>
</body>
</html>
<dom-module id="pomodoro-clock">
<style></style>
<template>
<pomodoro-timer seconds="[[current]]"></pomodoro-timer>
</template>
<script>
(function () {
Polymer({
is: 'pomodoro-clock',
properties: {
isWork: {
type: Boolean,
value: true
},
workSeconds: Number,
breakSeconds: Number,
current: {
type: Number,
computed: 'getCurrent(workSeconds, breakSeconds, isWork)'
}
},
listeners: {
'done': 'switchTimer'
},
getCurrent: function (workSeconds, breakSeconds, isWork) {
return isWork ? workSeconds : breakSeconds;
},
switchTimer: function (e) {
this.isWork = !this.isWork;
}
});
})();
</script>
</dom-module>
<dom-module id="pomodoro-timer">
<style></style>
<template>
<span>{{seconds}}</span>
<paper-button id="start">
start
</paper-button>
</template>
<script>
(function () {
Polymer({
is: 'pomodoro-timer',
properties: {
seconds: Number
},
factoryImpl: function () {
this.isCounting = false;
this.interval = {};
},
listeners: {
'tap': 'toggle'
},
toggle: function () {
if (this.isCounting)
this.stop();
else
this.start();
},
start: function () {
var element = this;
this.isCounting = true;
this.interval = window.setInterval(function () {
element.seconds--;
if (element.seconds === 0) {
element.fire('done');
element.stop();
}
}, 1000);
},
stop: function () {
this.isCounting = false;
window.clearInterval(this.interval);
}
});
})();
</script>
</dom-module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment