This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>x-state</title> | ||
|
||
<script src="components/platform/platform.js"></script> | ||
<link rel="import" href="components/polymer/polymer.html"> | ||
<link rel="import" href="x-state.html"> | ||
</head> | ||
<body> | ||
|
||
<template is="auto-binding"> | ||
<button on-tap="{{push}}">Push</button> | ||
<button on-tap="{{back}}" disabled?="{{!$.state.canBack}}">Back</button> | ||
<button on-tap="{{forward}}" disabled?="{{!$.state.canForward}}">Forward</button> | ||
<x-state id="state" on-last-state="{{last}}" on-first-state="{{first}}" on-state-change="{{change}}"></x-state> | ||
<div>{{state}}</div> | ||
</template> | ||
<script> | ||
var scope = document.querySelector('template'); | ||
scope.count = 0; | ||
scope.push = function() { | ||
this.count++; | ||
this.updateState(); | ||
this.$.state.push(scope.state); | ||
} | ||
|
||
scope.back = function() { | ||
this.$.state.back(); | ||
} | ||
|
||
scope.forward = function() { | ||
this.$.state.forward(); | ||
} | ||
|
||
scope.change = function(e, detail) { | ||
this.state = detail.state; | ||
} | ||
|
||
scope.updateState = function() { | ||
return this.state = (this.count + Math.random()).toFixed(3); | ||
} | ||
|
||
addEventListener('template-bound', function(e) { | ||
scope.$.state.push(scope.updateState()); | ||
}) | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
<polymer-element name="x-state" attributes="canBack canForward"> | ||
<script> | ||
|
||
Polymer('x-state', { | ||
_cursor: -1, | ||
cursor: -1, | ||
canBack: false, | ||
canForward: false, | ||
created: function() { | ||
this.stack = []; | ||
}, | ||
|
||
push: function(state) { | ||
if (!state && state !== 0) { | ||
return; | ||
} | ||
if (!this.isAtLast()) { | ||
var f = this.cursor + 1; | ||
this.stack.splice(f, this.stack.length - f); | ||
} | ||
this.stack.push(state); | ||
this.cursor = this.stack.length - 1; | ||
this._statePushed = true; | ||
Platform.flush(); | ||
}, | ||
|
||
back: function() { | ||
this._cursor--; | ||
}, | ||
|
||
forward: function() { | ||
this._cursor++; | ||
}, | ||
|
||
_cursorChanged: function() { | ||
this.cursor = Math.max(0, Math.min(this._cursor, | ||
this.stack.length-1)); | ||
}, | ||
|
||
cursorChanged: function(old) { | ||
//console.log('cursorChanged', this.cursor, this._statePushed); | ||
this._cursor = this.cursor; | ||
if (!this._statePushed) { | ||
var state = this.stack[this.cursor]; | ||
this.fire('state-change', {state: state}); | ||
} else { | ||
this._statePushed = false; | ||
} | ||
var first = this.isAtFirst(), last = this.isAtLast(); | ||
this.canBack = !first; | ||
this.canForward = !last; | ||
if (first) { | ||
this.fire('first-state'); | ||
} else if (last) { | ||
this.fire('last-state'); | ||
} | ||
}, | ||
|
||
isAtLast: function() { | ||
return (this.cursor === this.stack.length -1) | ||
}, | ||
|
||
isAtFirst: function() { | ||
return (this.cursor === 0) | ||
} | ||
|
||
}); | ||
|
||
</script> | ||
|
||
</polymer-element> |