Skip to content

Commit

Permalink
🌋 Polymer 1.0 functional changes
Browse files Browse the repository at this point in the history
* Update for spec complience
* Polymer 0.5 -> 1 changes
  • Loading branch information
samccone committed Sep 28, 2015
1 parent d597070 commit 11ba30e
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 138 deletions.
14 changes: 11 additions & 3 deletions examples/polymer/elements/td-input.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<link rel="import" href="../bower_components/polymer/polymer.html">

<polymer-element name="td-input" extends="input" on-keyup="{{keyupAction}}" on-keypress="{{keypressAction}}">
<dom-module id="td-input">
<script>
(function() {
'use strict';

var ENTER_KEY = 13;
var ESC_KEY = 27;
Polymer('td-input', {
Polymer({
is: 'td-input',
extends: 'input',
listeners: {
'keyup': 'keyupAction',
'keypress': 'keypressAction'
},
keypressAction: function(e, detail, sender) {
// Listen for enter on keypress but esc on keyup, because
// IE doesn't fire keyup for enter.
Expand All @@ -21,4 +29,4 @@
});
})();
</script>
</polymer-element>
</dom-module>
63 changes: 48 additions & 15 deletions examples/polymer/elements/td-item.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="td-input.html">

<polymer-element name="td-item" extends="li" attributes="item editing" on-blur="{{commitAction}}">
<dom-module id="td-item">
<template>
<div class="view {{ {completed: item.completed, editing: editing} | tokenList }}" hidden?="{{editing}}" on-dblclick="{{editAction}}">
<input type="checkbox" class="toggle" checked="{{item.completed}}" on-click="{{itemChangeAction}}">
<label>{{item.title}}</label>
<button class="destroy" on-click="{{destroyAction}}"></button>
</div>
<input is="td-input" id="edit" class="edit" value="{{item.title}}" hidden?="{{!editing}}" on-td-input-commit="{{commitAction}}" on-td-input-cancel="{{cancelAction}}">
<template is="dom-if" if="{{!editing}}">
<div on-dblclick="editAction">
<input type="checkbox" class="toggle" checked="{{item.completed::change}}" on-click="itemChangeAction">
<label>{{item.title}}</label>
<button class="destroy" on-click="destroyAction"></button>
</div>
</template>
<template is="dom-if" if="{{editing}}">
<input is="td-input" id="edit" class="edit" value$="{{item.title}}" on-td-input-commit="commitAction" on-td-input-cancel="cancelAction" on-blur="onBlur">
</template>
</template>
<script>
(function() {
Polymer('td-item', {
editing: false,
'use strict';

Polymer({
is: 'td-item',
extends: 'li',
properties: {
editing: {
type: Boolean,
value: false
},
item: {
type: Object,
value: function() {
return {};
}
},
},
observers: ['setRootClass(item.completed, editing)'],
setRootClass: function(completed, editing) {
this.classList[completed ? 'add' : 'remove']('completed');
this.classList[editing ? 'add' : 'remove']('editing');
},
onBlur: function() {
this.commitAction();
this.editing = false;
},
editAction: function() {
this.editing = true;
// schedule focus for the end of microtask, when the input will be visible
this.asyncMethod(function() {
this.$.edit.focus();
this.async(function() {
var elm = this.querySelector('#edit');
// It looks like polymer is trying to be smart here and not updating the
// title attribute on the input when it is changed. To work around this, we manually have
// to set the value again when we go into edit mode.
elm.value = this.item.title;
elm.focus();
});
},
commitAction: function() {
if (this.editing) {
this.editing = false;
this.item.title = this.item.title.trim();
this.set('item.title', this.querySelector('#edit').value.trim());
if (this.item.title === '') {
this.destroyAction();
}
Expand All @@ -34,7 +66,8 @@
cancelAction: function() {
this.editing = false;
},
itemChangeAction: function() {
itemChangeAction: function(e, details) {
this.set('item.completed', e.target.checked);
this.fire('td-item-changed');
},
destroyAction: function() {
Expand All @@ -43,4 +76,4 @@
});
})();
</script>
</polymer-element>
</dom-module>
146 changes: 77 additions & 69 deletions examples/polymer/elements/td-model.html
Original file line number Diff line number Diff line change
@@ -1,77 +1,85 @@
<link rel="import" href="../bower_components/polymer/polymer.html">

<polymer-element name="td-model" attributes="filter items storageId">
<dom-module id="td-model">
<template>
<iron-localstorage name="todos-polymer" value="{{items}}" on-iron-localstorage-load-empty="initializeDefaultTodos"></iron-localstorage>
</template>
<script>
Polymer('td-model', {
filtered: null,
completedCount: 0,
activeCount: 0,
allCompleted: false,
ready: function() {
this.asyncMethod(function() {
this.items = this.storage.value || [];
});
},
filterChanged: function() {
this.asyncMethod(function() {
this.filterItems();
});
},
itemsChanged: function() {
this.completedCount = this.items.filter(this.filters.completed).length;
this.activeCount = this.items.length - this.completedCount;
this.allCompleted = this.completedCount && !this.activeCount;
this.filterItems();
if (this.storage) {
this.storage.value = this.items;
this.storage.save();
}
},
storageIdChanged: function() {
this.storage = document.querySelector('#' + this.storageId);
if (this.storage) {
this.items = this.storage.value;
}
},
filterItems: function() {
var fn = this.filters[this.filter];
this.filtered = fn ? this.items.filter(fn) : this.items;
},
newItem: function(title) {
title = String(title).trim();
if (title) {
var item = {
(function() {
'use strict';

Polymer({
is: 'td-model',
properties: {
items: {
type: Array,
notify: true
},
filter: String
},
initializeDefaultTodos: function() {
this.items = [];
},
newItem: function(title) {
title = String(title).trim();

if (!title) {
return;
}

this.push('items', {
title: title,
completed: false
};
this.items.push(item);
this.itemsChanged();
}
},
destroyItem: function(item) {
var i = this.items.indexOf(item);
if (i >= 0) {
this.items.splice(i, 1);
}
this.itemsChanged();
},
clearItems: function (){
this.items = this.items.filter(this.filters.active);
},
setItemsCompleted: function(completed) {
this.items.forEach(function(item) {
item.completed = completed;
});
this.itemsChanged();
},
filters: {
active: function(item) {
return !item.completed;
});
},
getCompletedCount: function(items) {
return items === null ? 0 : items.filter(this.filters.completed).length
},
getActiveCount: function(items) {
return items.length - this.getCompletedCount(items);
},
areAllCompleted: function(items) {
return this.getCompletedCount(items) && !this.getActiveCount(items) ? true : false;
},
matchesFilter: function(item, filter) {
var fn = this.filters[filter];
return this.filtered = fn ? fn(item) : true;
},
destroyItem: function(item) {
var i = this.items.indexOf(item);

i !== -1 && this.splice('items', i, 1);
},
clearCompletedItems: function (){
this.items = this.items.filter(this.filters.active);
},
setItemsCompleted: function(completed) {
// Since we are mutating elements in an array here
// and we want everyone to know about it we must go through
// the polymer internal `splice` api. The fix that comes to my mind here
// would be to use a hash or set to represent this structure so that the mutations
// would happening on an object with real key value pairs instead of an object
// nested inside of an array
// Polymer array mutation docs: https://www.polymer-project.org/1.0/docs/devguide/properties.html#array-mutation
this.items.forEach(function(item, i) {
if (this.filter) {
if (this.filters[this.filter](item)) {
this.splice('items', i, 1, {title: item.title, completed: completed});
}
} else {
this.splice('items', i, 1, {title: item.title, completed: completed});
}
}, this);
},
completed: function(item) {
return item.completed;
filters: {
active: function(item) {
return !item.completed;
},
completed: function(item) {
return item.completed;
}
}
}
});
});
})();
</script>
</polymer-element>
</dom-module>
Loading

0 comments on commit 11ba30e

Please sign in to comment.