Skip to content

Commit

Permalink
Merge pull request #75 from jcmoore/mixins
Browse files Browse the repository at this point in the history
Add mixin pattern for auto-generated components
  • Loading branch information
anthonykoerber committed Apr 14, 2016
2 parents 13f7b9a + ae56bbc commit 20a3d4a
Show file tree
Hide file tree
Showing 10 changed files with 495 additions and 97 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"build": "gulp",
"start": "gulp live",
"dist": "gulp build:dist",
"release": "gulp release:patch"
"release": "gulp release:patch",
"bower": "bower"
},
"devDependencies": {
"autoprefixer": "^6.1.2",
Expand Down
91 changes: 66 additions & 25 deletions src/mm-grid/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,55 @@
</style>
</head>
<body>
<dom-module id="arbitrary-controller">
<template>
<mm-grid selectable expandable style="height: 100px; min-height: 20px;" data="{{arbitraryData}}" mixin-findable="{{ref}}" arbitrary-field="{{ref}}">
<mm-grid-column width="20%" field="name" sort resize>Name</mm-grid-column>
<mm-grid-column width="10%" field="id" sort resize>Id</mm-grid-column>
<template preserve-content>
<mm-grid-item model="{{model}}" scope="{{scope}}">
<div class="expansion">
<div class="expansion-row">
<div class="expansion-col" on-tap="_echo">
<h4>Expanded Content</h4>
<span>{{model.name}}</span>
</div>
<div class="expansion-col">
<h4>Expanded Advertiser</h4>
<span>{{model.advertiser}}</span>
</div>
<div class="expansion-col">
<h4>Expanded Concept</h4>
<span>{{model.concept}}</span>
</div>
</div>
</div>
</mm-grid-item>
</template>
</mm-grid>
</template>
<style>
#unused {
display: none;
}
</style>
<script>
HTMLImports.whenReady(function() {
Polymer(StrandTraits.MixinFindable.prototypeAsMixin({
is:"arbitrary-controller",
behaviors: [
StrandTraits.Refable,
],

_echo: function (ev) {
var templateInstance = this;
var arbitraryController = this.scope.arbitraryField;
console.log("_echo", arbitraryController.is, ev);
},
}));
});
</script>
</dom-module>
<mm-grid id="mmTestGrid" gpu="4d" selectable expandable="false" unresolved>
<mm-grid-column width="20%" field="name" sort resize>Name</mm-grid-column>
<mm-grid-column width="10%" field="id" sort resize>Id</mm-grid-column>
Expand All @@ -47,7 +96,8 @@
<mm-grid-column width="10%" field="dimensions">Dimensions</mm-grid-column>
<mm-grid-column width="30%" field="actions"></mm-grid-column>

<template preserve-content>
<template preserve-content id="arbitraryIdentifier">
<mm-icon type="info" on-click="_customClick" on-track="_customDrag"></mm-icon>
<mm-grid-item model="{{model}}" scope="{{scope}}">
<div field="actions">
<!--
Expand Down Expand Up @@ -84,30 +134,7 @@
value="{{model.value}}">
</mm-dropdown>
-->
<mm-grid selectable expandable style="height: 100px; min-height: 20px;" data="{{model.subdata}}">
<mm-grid-column width="20%" field="name" sort resize>Name</mm-grid-column>
<mm-grid-column width="10%" field="id" sort resize>Id</mm-grid-column>
<template preserve-content>
<mm-grid-item model="{{model}}" scope="{{scope}}">
<div class="expansion">
<div class="expansion-row">
<div class="expansion-col">
<h4>Expanded Content</h4>
<span>{{model.name}}</span>
</div>
<div class="expansion-col">
<h4>Expanded Advertiser</h4>
<span>{{model.advertiser}}</span>
</div>
<div class="expansion-col">
<h4>Expanded Concept</h4>
<span>{{model.concept}}</span>
</div>
</div>
</div>
</mm-grid-item>
</template>
</mm-grid>
<arbitrary-controller id="mixin" arbitrary-data="{{model.subdata}}"></arbitrary-controller>
</div>
<div field="name">
Creative - {{model.name}}
Expand Down Expand Up @@ -306,6 +333,20 @@ <h4>Expanded Concept</h4>

window.addEventListener("WebComponentsReady", function() {
app = document.querySelector("#mmTestGrid");
app.mixinsForValue = function (value) {
if (value.id === "arbitraryIdentifier" &&
value instanceof HTMLTemplateElement) {
return [{
_customClick: function (ev) {
console.log("_customClick", ev);
},
}, {
_customDrag: function (ev) {
console.log("_customDrag", ev);
},
}];
}
};
app.data = data;

app.addEventListener('grid-item-clicked', function(e) {
Expand Down
129 changes: 129 additions & 0 deletions src/mm-grid/mixin1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<!DOCTYPE html>
<html>
<head>
<script language="javascript" src="../../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="mm-grid.html">
<style type="text/css">
html, body {
padding: 0;
margin: 0;
background: #eee;
width: 100%;
height: 100%;
min-height: 100%;
}

*[unresolved] {
display: none;
}

arbitrary-controller {
height: 100%;
}

mm-grid {
height: 100%;
}

div[field="scroll"] {
width: 100px;
height: 120px;
overflow: auto;
}

.oversized {
width: 100%;
height: 500%;
background: linear-gradient(0deg, #0fb8ad 0%, #1fc8db 51%, #2cb5e8 75%);
}
</style>
</head>
<body>
<dom-module id="arbitrary-controller">
<template>
<mm-grid selectable expandable data="{{arbitraryData}}" mixin-findable="{{ref}}" arbitrary-field="{{ref}}">

<mm-grid-column width="10%" field="scroll" sort resize>V-SCROLL</mm-grid-column>
<mm-grid-column width="10%" field="id" sort resize>Id</mm-grid-column>
<mm-grid-column width="10%" field="name" sort resize>Name</mm-grid-column>
<template preserve-content>
<mm-grid-item model="{{model}}" scope="{{scope}}">
<div field="scroll" on-scroll="_scrollback">
<div class="oversized"></div>
</div>
<div field="id" on-click="_clickback">CLICK the {{model.id}}</div>
</mm-grid-item>
</template>
</mm-grid>
</template>
<style>
#unused {
display: none;
}
</style>
<script>
HTMLImports.whenReady(function() {
// NOTE the StrandTraits.MixinFindable.prototypeAsMixin() wrapping/invocation
Polymer(StrandTraits.MixinFindable.prototypeAsMixin({
is:"arbitrary-controller",
behaviors: [
StrandTraits.Refable,
],

_scrollback: function (ev) {
var templateInstance = this;
var arbitraryController = this.scope.arbitraryField;
console.log("_scrollback", arbitraryController.is, ev.target.is || ev.target.localName, ev);
},

_clickback: function (ev) {
var templateInstance = this;
var arbitraryController = this.scope.arbitraryField;
console.log("_clickback", arbitraryController.is, ev.target.is || ev.target.localName, ev);
},
}));
});
</script>
</dom-module>



<arbitrary-controller id="mixin"></arbitrary-controller>



<script type="text/javascript">
var data = [],
columns = [
{field: "id", label:"id", width:"10%"},
{field: "name", label: "Name Here", width: "25%", resize: true, info: true},
],
app,
NUM_ITEMS = 300;

for(var i=0; i<NUM_ITEMS; i++) {
generateItem();
}

if (data.length) {
data[0].content = new Array(100).join(" "+data[0].name);
data[0|(data.length / 2)].content = new Array(100).join(" "+data[0|(data.length / 2)].name);
data[data.length - 1].content = new Array(100).join(" "+data[data.length - 1].name);
}

function generateItem() {
data.push({
id: i,
name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi odio justo, vulputate eu consequat quis, ullamcorper sed ipsum. Nullam molestie, urna sed facilisis suscipit, leo urna molestie lorem, nec tincidunt ipsum tellus sit amet velit. Nulla pulvinar mi et commodo blandit.',
selected: false,
expanded: false,
});
}

window.addEventListener("WebComponentsReady", function() {
app = document.querySelector("arbitrary-controller");
app.set("arbitraryData", data);
});
</script>
</body>
</html>
114 changes: 114 additions & 0 deletions src/mm-grid/mixin2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html>
<head>
<script language="javascript" src="../../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="mm-grid.html">
<style type="text/css">
html, body {
padding: 0;
margin: 0;
background: #eee;
width: 100%;
height: 100%;
min-height: 100%;
}

*[unresolved] {
display: none;
}

mm-grid {
height: 100%;
}

div[field="drag"] {
width: 100px;
height:20px;
background: #555555;
}

</style>
</head>
<body>
<mm-grid id="mmTestGrid" gpu="4d" selectable unresolved>
<mm-grid-column width="10%" field="drag">DRAG</mm-grid-column>

<mm-grid-column width="10%" field="id" sort resize>Id</mm-grid-column>
<mm-grid-column width="20%" field="name" sort resize>Name</mm-grid-column>
<mm-grid-column width="10%" field="advertiser" sort resize>Advertiser</mm-grid-column>
<mm-grid-column width="10%" field="concept" sort resize>Concept</mm-grid-column>
<mm-grid-column width="10%" field="type" resize>Type</mm-grid-column>
<mm-grid-column width="10%" field="dimensions">Dimensions</mm-grid-column>

<template preserve-content id="potentiallyUsedForDifferentiatingInvocationsTo-mixinsForValue">
<mm-grid-item model="{{model}}" scope="{{scope}}">
<div field="drag" on-track="_customDrag"></div>
<div field="id" on-mouseover="_customHover">HOVER {{model.id}}</div>
<div field="name" on-click="_customClick">CLICK {{model.name}}</div>
</mm-grid-item>
</template>
</mm-grid>

<script type="text/javascript">
var data = [],
columns = [
{field: "id", label:"id", width:"10%"},
{field: "name", label: "Name Here", width: "25%", resize: true, info: true},
{field: "advertiser", label: "Advertiser", width: "20%", resize: true, align:"right"},
{field: "concept", label: "Concept", width: "15%", resize: true, align:"right"},
{field: "type", label: "Type", width: "15%", resize: true, align:"right"},
{field: "dimensions", label: "Dimensions", width: "15%", align:"right"}
],
app,
NUM_ITEMS = 300;

for(var i=0; i<NUM_ITEMS; i++) {
generateItem();
}

if (data.length) {
data[0].content = new Array(100).join(" "+data[0].name);
data[0|(data.length / 2)].content = new Array(100).join(" "+data[0|(data.length / 2)].name);
data[data.length - 1].content = new Array(100).join(" "+data[data.length - 1].name);
}

function generateItem() {
data.push({
id: i,
name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi odio justo, vulputate eu consequat quis, ullamcorper sed ipsum. Nullam molestie, urna sed facilisis suscipit, leo urna molestie lorem, nec tincidunt ipsum tellus sit amet velit. Nulla pulvinar mi et commodo blandit.',
content: Math.random().toString(36).substring(7),
advertiser: "Advertiser No. " + i,
concept: "Concept No. " + i,
type: "swf",
file: "Creative_File_" + i + ".swf",
dimensions: String(Math.floor(Math.random()*300) + " x " + Math.floor(Math.random()*300)),
selected: false,
expanded: false,
});
}

window.addEventListener("WebComponentsReady", function() {
app = document.querySelector("#mmTestGrid");
app.mixinsForValue = function (value) {
if (value.id === "potentiallyUsedForDifferentiatingInvocationsTo-mixinsForValue" &&
value instanceof HTMLTemplateElement) {
return [{ // one or more "mixin" objects can be returned
_customClick: function (ev) {
console.log("_customClick", ev);
},
_customHover: function (ev) {
console.log("_customHover", ev);
},
}, {
_customDrag: function (ev) {
console.log("_customDrag", ev);
},
}];
}
};
app.data = data;

});
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion src/mm-grid/mm-grid.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
-->
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../shared/behaviors/resolvable.html">
<link rel="import" href="../shared/behaviors/mixinfindable.html"/>
<link rel="import" href="../shared/behaviors/templatefindable.html"/>
<link rel="import" href="../mm-item-recycler/mm-item-recycler.html">
<link rel="import" href="../mm-grid-column/mm-grid-column.html">
Expand All @@ -18,7 +19,7 @@
<dom-module id="mm-grid">
<link rel="import" type="css" href="mm-grid.css"/>
<template>
<mm-item-recycler id="viewport" gpu="{{gpu}}" data="{{data}}" scope="{{scope}}" index="{{index}}" template-finder="{{templateFinder}}" template-findable="{{templateFindable}}">
<mm-item-recycler id="viewport" gpu="{{gpu}}" data="{{data}}" scope="{{scope}}" index="{{index}}" template-finder="{{templateFinder}}" template-findable="{{templateFindable}}" mixin-findable="{{mixinFindable}}">
<div id="header">
<div class="_mm_column checkbox" hidden$="{{!selectable}}">
<mm-checkbox id="selectAll" on-tap="_toggleAllSelections" state="{{_selectAllState}}"></mm-checkbox>
Expand Down
1 change: 1 addition & 0 deletions src/mm-grid/mm-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

behaviors: [
StrandTraits.Resolvable,
StrandTraits.MixinFindable,
StrandTraits.TemplateFindable,
StrandTraits.Refable
],
Expand Down
Loading

0 comments on commit 20a3d4a

Please sign in to comment.