Last active
September 1, 2016 14:47
-
-
Save girliemac/920e46856a9a5decfe0f to your computer and use it in GitHub Desktop.
[Blog] Creating a Chat App with Material Design using Polymer
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 is a gist for my blog/tutorial, "Creating a Chat App with Material Design using Polymer" |
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
<!-- Import element --> | |
<link rel="import" href="paper-fab"> | |
... | |
<!-- Use element --> | |
<paper-fab icon="send"></paper-fab> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script> | |
</head> | |
<body> | |
... |
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
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script> | |
<link rel="import" href="bower_components/core-scaffold/core-scaffold.html"> | |
<link rel="import" href="bower_components/core-item/core-item.html"> | |
<link rel="import" href="bower_components/paper-input/paper-input.html"> | |
<link rel="import" href="bower_components/paper-fab/paper-fab.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
<body fullbleed unresolved> | |
<core-scaffold> | |
<!-- Drawer panel --> | |
<core-header-panel navigation flex> | |
<core-toolbar class="tall"> | |
<!-- an avatar and username will be here --> | |
</core-toolbar> | |
</core-header-panel> | |
<!-- App Title --> | |
<div tool layout horizontal flex> | |
<span flex>Kitteh Anonymous</span> | |
<core-icon icon="account-circle"></core-icon> | |
<span><!-- number of people online --></span> | |
</div> | |
<!-- Main content --> | |
<div flex>...</div> | |
</core-scaffold> | |
</body> |
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
<!-- Main content --> | |
<div class="send-message" layout horizontal> | |
<paper-input flex label="Type message..." id="input" value="{{input}}"></paper-input> | |
<paper-fab icon="send" id="sendButton" on-tap="{{sendMyMessage}}"></paper-fab> | |
</div> |
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
<body fullbleed unresolved> | |
<template is="auto-binding"> | |
<core-scaffold> | |
... | |
</core-scaffold> | |
</template> | |
</body> |
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
var template = document.querySelector('template[is=auto-binding]'); | |
doSomething(template.input); |
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
<template repeat="{{item in items}}"> | |
<core-item icon="{{item.icon}}" label="{{item.title}}"></core-item> | |
</template> |
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
template.items = [ | |
{icon: 'cloud', title: 'PubNub'}, | |
{icon: 'polymer', title: 'Polymer'}, | |
{icon: 'favorite', title: 'Love'} | |
]; |
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
<polymer-element name="x-chat-list" attributes="avatar color username text"> | |
<template> | |
<section class="user-list" layout horizontal> | |
<div class="avatar {{color}}" style="background-image: url({{avatar}})"></div> | |
<div flex> | |
<div class="username">{{username}}</div> | |
<div class="text">{{text}}</div> | |
</div> | |
</section> | |
</template> | |
<script> | |
Polymer('x-chat-list', { // set default values | |
avatar: '', | |
color: '', | |
username: '', | |
text: '' | |
}); | |
</script> | |
</polymer-element> |
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
<link rel="import" href="x-chat-list.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
<div flex class="chat-list"> | |
<template repeat="{{message in messages}}"> | |
<x-chat-list color="{{message.color}}" | |
avatar="{{message.avatar}}" | |
username="{{message.uuid}}" | |
text="{{message.text}}"></x-chat-list> | |
</template> | |
</div> |
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
<link rel="import" href="bower_components/pubnub-polymer/pubnub-element.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
<core-pubnub | |
publish_key="your_pub_key" | |
subscribe_key="your_sub_key" | |
uuid="{{uuid}}"></core-pubnub> |
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
var randomColor = function() { | |
var colors = ['navy', 'slate', 'olive', 'moss', 'chocolate',...]; | |
return colors[(Math.random() * colors.length) >>> 0]; | |
}; | |
var randomCat = function() { ... }; | |
var color = randomColor(); | |
var cat = randomCat(); | |
template.uuid = color + '-' + cat; | |
templtae.color = color; | |
template.avatar = 'images/' + cat + '.jpg'; |
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
<core-pubnub publish_key="demo" subscribe_key="demo"> | |
<core-pubnub-publish id="pub" channel="polymer-chat" message="Hello"> | |
</core-pubnub-publish> | |
</core-pubnub> |
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
<paper-fab icon="send" on-tap="{{sendMyMessage}}"></paper-fab> |
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
template.sendMyMessage = function(e) { | |
if(!template.input) return; // if the input field is empty, do nothing. | |
template.$.pub.message = { | |
uuid: uuid, | |
avatar: avatarUrl, | |
color: color, | |
text: template.input | |
}; | |
template.$.pub.publish(); | |
}; |
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
<core-pubnub-subscribe | |
channel="polymer-chat" | |
id="sub" | |
messages="{{messages}}" | |
on-callback="{{subscribeCallback}}"> |
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
<div flex class="chat-list"> | |
<template repeat="{{message in messages}}"> | |
<x-chat-list | |
color="{{message.color}}" | |
avatar="{{message.avatar}}" | |
username="{{message.uuid}}" | |
text="{{message.text}}"></x-chat-list> | |
</template> | |
</div> |
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
template.subscribeCallback = function(e) { | |
template.async(function(){ | |
var chatDiv = document.querySelector('.chat-list'); | |
chatDiv.scrollTop = chatDiv.scrollHeight; // scroll to bottom | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment