Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various changes to enable g-overlay #6

Merged
merged 6 commits into from
Oct 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 92 additions & 18 deletions src/g-component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,35 @@
});
</script>
<script>
// polyfill for DOMTokenList features: list of classes in add/remove;
// enable method.
(function() {
'use strict';
var add = DOMTokenList.prototype.add;
var remove = DOMTokenList.prototype.remove;
DOMTokenList.prototype.add = function() {
for (var i = 0; i < arguments.length; i++) {
add.call(this, arguments[i]);
}
};
DOMTokenList.prototype.remove = function() {
for (var i = 0; i < arguments.length; i++) {
remove.call(this, arguments[i]);
}
};
DOMTokenList.prototype.enable = function(name, value) {
value ? this.add(name) : this.remove(name);
};
})();

(function(){
var bindAttrs = function(inAttrs) {
inAttrs.forEach(function(a) {
a = a.trim();
Object.defineProperty(this, a, {
get: function() {
return this.getAttribute(a);
return this.hasAttribute(a) ? this.getAttribute(a) :
this.__proto__[a];
},
set: function(v) {
return this.setAttribute(a, v);
Expand All @@ -26,6 +48,18 @@
}, this);
};

var bindEvents = function(inEvents) {
inEvents.forEach(function(e) {
// event name: handler name pairs
var pair = e.split(":");
var event = pair[0].trim();
var handler = pair[1].trim();
if (this[handler]) {
this.addEventListener(event, this[handler].bind(this));
}
}, this);
};

var deref = function(inNode) {
return inNode.baby || inNode;
};
Expand Down Expand Up @@ -56,8 +90,64 @@
return observer;
};

var $ = document.querySelector.bind(document);
var shadowRootCreated = function(inRoot, inUber, inAttributes) {
if (inAttributes.attributes) {
var attributes = inAttributes.attributes.value.split(",");
bindAttrs.call(this, attributes);
}
if (inAttributes.handlers) {
var events = inAttributes.handlers.value.split(",");
bindEvents.call(this, events);
}
establishNodeReferences.call(this, inRoot);
if (inUber.shadowRootCreated) {
inUber.shadowRootCreated(inRoot);
}
this.attrObserver = new AttrObserver(this);
};

// utility methods

var job = function(inJobName, inJob, inWait) {
job.stop(inJobName);
job._jobs[inJobName] = setTimeout(function() {
job.stop(inJobName);
inJob();
}, inWait);
};
job.stop = function(inJobName) {
if (job._jobs[inJobName]) {
clearTimeout(job._jobs[inJobName]);
delete job._jobs[inJobName];
}
};
job._jobs = {};

var utils = {
job: job
};

// decorate HTMLElementElement with toolkit API

HTMLElementElement.prototype.component = function(inUber) {
var attributes = this.element.attributes;
this.lifecycle({
shadowRootCreated: function(inRoot) {
shadowRootCreated.call(this, inRoot, inUber, attributes);
},
created: inUber.created
});
var p = this.generatedConstructor.prototype = inUber.prototype;
// attach utility library
// TODO(sjmiles): this is probably not the best way to do this
p.utils = utils;
};

// code below provides a shim for declarative event handlers
// (aka 'x', as in onclick="x('click')")
// it's only really for evaluating syntax, and not
// a real solution

var nodeIterator = function(inNodes, inFn) {
if (inNodes) {
for (var i=0, n; (n=inNodes[i]); i++) {
Expand Down Expand Up @@ -106,22 +196,6 @@
}
};

HTMLElementElement.prototype.component = function(inUber) {
var attributes = this.element.getAttribute("attributes").split(",");
//var events = this.getAttribute("events");
this.lifecycle({
shadowRootCreated: function(inRoot) {
bindAttrs.call(this, attributes);
establishNodeReferences.call(this, inRoot);
if (inUber.shadowRootCreated) {
inUber.shadowRootCreated(inRoot);
}
this.attrObserver = new AttrObserver(this);
},
created: inUber.created
});
this.generatedConstructor.prototype = inUber.prototype;
};
})();
</script>
</element>
Expand Down
94 changes: 94 additions & 0 deletions src/g-overlay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!--
/*
* Copyright 2012 The Toolkitchen Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
-->
<element name="g-overlay" attributes="showClass, hideClass, showing, timeout">
<link rel="components" href="g-component.html">
<template>
<style scoped>
@host {
position: absolute;
z-index: 10;
}
</style>
<content></content>
</template>
<script>
this.component({
created: function(inSuper) {
this.jobId = "animate-" + Math.random();
this._squelchShowingChange = true;
this.showingAttributeChanged();
},
prototype: {
timeout: 1000,
// TODO(sorvell): why is this not an attr-prop?
allowAnimation: true,
toggleShowing: function() {
this.showing = this.showing == "true" ? "false" : "true";
},
showingAttributeChanged: function() {
if (this.canAnimate()) {
this.animateShowing();
} else {
this[this.showing == "true" ? "removeAttribute" : "setAttribute"]("hidden", "true");
}
if (this._squelchShowingChange) {
this._squelchShowingChange = false;
} else {
webkitRequestAnimationFrame(this.fireShowingChange.bind(this));
}
},
setShowingDirect: function(inValue) {
this._squelchShowingChange = true;
this.showing = inValue;
},
fireShowingChange: function() {
var detail = {showing: this.showing == "true", rect: this.getBoundingClientRect()};
var event = new CustomEvent("showingChange", {bubbles: true, detail: detail})
this.dispatchEvent(event);
},
canAnimate: function() {
return this.allowAnimation && Boolean(this.hideClass || this.showClass);
},
animateShowing: function() {
this.removeAttribute("hidden");
webkitRequestAnimationFrame(this._animateShowing.bind(this));
},
_animateShowing: function() {
if (this.showClass) {
this.classList.enable(this.showClass, this.showing == "true");
}
if (this.hideClass) {
this.classList.enable(this.hideClass, this.showing == "false");
}
this.unlisten();
this.listen();
},
listen: function() {
this.animationListener = this.finishAnimate.bind(this);
this.addEventListener("webkitAnimationEnd", this.animationListener, false);
this.addEventListener("webkitTransitionEnd", this.animationListener, false);
// always finish animation within timeout
this.utils.job(this.jobId, this.animationListener, this.timeout);
},
finishAnimate: function() {
//console.log("end animate");
if (this.showing == "false") {
this.setAttribute("hidden", "true");
this.classList.remove(this.hideClass);
}
this.unlisten();
},
unlisten: function() {
this.removeEventListener("webkitAnimationEnd", this.animationListener, false);
this.removeEventListener("webkitTransitionEnd", this.animationListener, false);
this.utils.job.stop(this.jobId);
}
}
});
</script>
</element>
142 changes: 142 additions & 0 deletions workbench/overlay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!DOCTYPE html>
<html>
<head>
<title>Overlay</title>
<script src="../../polyfills/Components/components-polyfill.js" shimShadow></script>
<link rel="components" href="../../toolkit/src/g-overlay.html">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
margin: 20px;
-webkit-user-select: none;
font-size: 13px;
}

.dialog {
box-shadow: 0 4px 16px rgba(0,0,0,0.2);
background: white;
left: 50%;
outline: 1px solid rgba(0,0,0,0.2);
padding:30px 42px;
position: fixed;
right: auto;
width: 512px;
height: auto;
overflow: hidden;
z-index: 100;
top: 72px;
margin-left: -256px;

/*display: none;*/
opacity: 0.0;
-webkit-transform: scale(1.05);
-webkit-transition: all 0.218s;
}

.dialogShown {
/*display: block;*/
opacity: 1.0;
-webkit-transform: scale(1.0);
}

.dialogHidden {
/*display: block;*/
opacity: 0;
-webkit-transform: translateY(-100%);
-webkit-transition: all 1s;
}

@-webkit-keyframes shakeFadeIn {
0% {
display: block;
opacity: 0;
-webkit-transform: translateX(0);
}
10% {
display: block;
-webkit-transform: translateX(-50px);
}
30% {
display: block;
-webkit-transform: translateX(50px);
}
50% {
display: block;
-webkit-transform: translateX(-25px);
}
70% {
display: block;
-webkit-transform: translateX(25px);
}
90% {
display: block;
-webkit-transform: translateX(-13px);
}
100% {
display: block;
-webkit-transform: translateX(0);
opacity: 1;
}
}

@-webkit-keyframes shakeFadeOut {
0% {
opacity: 1;
-webkit-transform: translateX(0);
}
10% {
-webkit-transform: translateX(-50px);
}
30% {
-webkit-transform: translateX(50px);
}
100% {
-webkit-transform: translateX(-100%);
opacity: 0;
}
}

.dialog2Shown {
-webkit-animation-duration: 0.5s;
-webkit-animation-fill-mode: both;
-webkit-animation-name: shakeFadeIn;
}

.dialog2Hidden {
-webkit-animation-duration: 0.3s;
-webkit-animation-fill-mode: both;
-webkit-animation-name: shakeFadeOut;
}

</style>
</head>
<body>
<button onclick="toggleDialog()">Toggle Dialog</button>
<g-overlay id="dialog" class="dialog" showClass="dialogShown" hideClass="dialogHidden">
<h2>Dialog</h2>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla sapien sed enim sollicitudin laoreet. Suspendisse suscipit, metus ac volutpat sodales, libero magna semper lacus, molestie fringilla massa orci ut arcu. Nullam sodales urna sit amet odio vehicula mattis.</div><br><br>
<div>Ut aliquam vulputate congue. Vestibulum pretium pretium nulla quis sollicitudin. Praesent lacinia congue erat nec mattis. Fusce commodo lacus est. Duis turpis eros, ultrices sed aliquet non, blandit egestas velit. Integer a augue nec lorem tristique hendrerit. Curabitur imperdiet risus id enim bibendum vestibulum. Integer id magna at arcu faucibus fermentum vel a augue. Sed fringilla venenatis dolor, in blandit magna molestie luctus. Vestibulum dignissim posuere ultrices. Aenean urna nisl, tincidunt vitae iaculis ut, pharetra nec eros.</div><br><br>
<div>
<g-checkbox></g-checkbox>
I agree with this wholeheartedly.
</div><br><br>
<button onclick="toggleDialog()">OK</button>
</g-overlay>
<br><br>
<button onclick="toggleDialog2()">Toggle Dialog 2</button>
<g-overlay id="dialog2" class="dialog" showClass="dialog2Shown" hideClass="dialog2Hidden">
<h2>Dialog 2</h2>
I'm dizzy.
</div><br><br>
<button onclick="toggleDialog2()">OK</button>
</g-overlay>
<script>
toggleDialog = function() {
document.querySelector("#dialog").toggleShowing();
}
toggleDialog2 = function() {
document.querySelector("#dialog2").toggleShowing();
}
</script>
</body>
</html>