Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
Feature/demo app. Closes #80. (#82)
Browse files Browse the repository at this point in the history
* New demo app. Initial check-in.

* Update.

* Progress checkin.

* Progress checkin.

* Progress check-in.

* Ready for review.

* Adds comments to demo script.

* Ready for review.
  • Loading branch information
chriscox committed Mar 21, 2017
1 parent 27c95fc commit 095254e
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 78 deletions.
91 changes: 41 additions & 50 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,64 @@

<head>
<meta charset="UTF-8" />
<title>Remixer Examples</title>

<title>Remixer Example</title>
<link rel="icon" href="src/favicon.ico">
<link rel="stylesheet" href="./node_modules/material-design-lite/material.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="src/demo-styles.css">
<script src="./node_modules/material-design-lite/material.min.js"></script>

<!-- Fake Date Generator -->
<script src="./node_modules/jquery/dist/jquery.min.js"></script>
<script src="./node_modules/handlebars/dist/handlebars.min.js"></script>
<script src="src/faker.min.js"></script>
<script src="src/demo-app.js"></script>

<!-- Remixer -->
<script src="/assets/remixer.js"></script>

</head>

<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<span id="remixer-demo-title" class="mdl-layout-title">Remixer Demo</span>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout-title">Remixer Demo</span>
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="">Example Link 1</a>
<a class="mdl-navigation__link" href="">Example Link 2</a>
</nav>
</div>
<main class="mdl-layout__content">
<div class="page-content">
<div id="box"></div>
</div>
</main>
</div>
<div id="demo-app"></div>

<script>
// Start Remixer.
remixer.start();

// Add a string variable to generate a text field in the overlay.
remixer.addStringVariable("Title", "Remixer Demo", null, function(variable) {
document.getElementById("remixer-demo-title").innerText = variable.selectedValue;
});

// Add a boolean variable to generate a toggle switch in the overlay.
remixer.addBooleanVariable("Show box", true, function(variable) {
document.getElementById("box").style.display = variable.selectedValue ? "block" : "none";
});

// Add a range variable to generate a slider in the overlay.
remixer.addRangeVariable("Box opacity", 1, 0, 1, 0.1, function(variable) {
document.getElementById("box").style.opacity = variable.selectedValue;
loadTemplate().then(function() {
addRemixerVariables();
});

// Add a string variable with 3+ possible values to generate a dropdown selector in the overlay.
remixer.addStringVariable("Box width", "200px", ["200px", "300px", "400px", "600px"], function(variable) {
document.getElementById("box").style.width = variable.selectedValue;
});

// Add an item list variable with colors to generate a color picker in the overlay.
remixer.addColorVariable("Box Color", "#4285F4", ["#4285F4", "#0F9D58", "#DB4437"], function(variable) {
document.getElementById("box").style.backgroundColor = variable.selectedValue;
});

// Add a string variable with 2 possible values to generate a radio selector in the overlay.
remixer.addStringVariable("Theme", "Light", ["Light", "Dark"], function(variable) {
var color = variable.selectedValue === "Light" ? "white" : "darkGray";
document.body.style.backgroundColor = color;
});
function addRemixerVariables() {

// Add a string variable to generate a text field in the overlay.
remixer.addStringVariable("Title", "Transactions", null, function(variable) {
$(".left-panel .panel-header-top h2").text(variable.selectedValue);
});

// Add a boolean variable to generate a toggle switch in the overlay.
remixer.addBooleanVariable("Show transaction subtitles", true, function(variable) {
$(".mdl-list__item-sub-title").css("display", variable.selectedValue ? "block" : "none");
$(".transaction-list-item").toggleClass("mdl-list__item--two-line", variable.selectedValue);
});

remixer.addBooleanVariable("Show transaction date", true, function(variable) {
$(".transaction-list-item .date").css("display", variable.selectedValue ? "inline-block" : "none");
});

// Add a range variable to generate a slider in the overlay.
remixer.addRangeVariable("Icon Size", 24, 20, 40, 2, function(variable) {
$(".mdl-list__item-avatar").css("fontSize", variable.selectedValue + "px")
});

// Add an item list variable with colors to generate a color picker in the overlay.
remixer.addColorVariable("Selected Color", "#ffe3b4", ["#ffe3b4", "#bddbfb", "#f78a81", "#eaded9"], function(variable) {
var color = variable.selectedValue;
var cssRule = $("<style> .transaction-list-item.selected {background-color: " + color + ";} </style>");
$('html > head').append(cssRule);
});
}
</script>
</body>

Expand Down
9 changes: 6 additions & 3 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"start": "webpack-dev-server --open"
},
"devDependencies": {
"webpack": "^2.2.0-rc.3",
"webpack-dev-server": "^2.2.0-rc.0"
"webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0"
},
"dependencies": {
"faker": "^4.1.0",
"handlebars": "^4.0.6",
"jquery": "^3.1.1",
"material-design-lite": "^1.2.1",
"material-remixer": "^0.5.1"
"material-remixer": "^0.5.8"
}
}
142 changes: 142 additions & 0 deletions examples/src/demo-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/** @license
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

/** The handlebars page template. */
var template;

/** The data view structure. */
var dataView = {};

/**
* Loads the demo template.
* @return {Promise} Returns promise for loading template as successful/failure.
*/
function loadTemplate() {
generateData();

return new Promise(function(resolve, reject) {

// Load handlebars page template.
$.get('src/demo-template.html', function(response) {
template = response;
render();
resolve();
}).fail(function() {
reject();
});
});
}

/** Generates fake data using faker.js script. */
function generateData() {
var totalTransactions = 20;
var totalAmount = 0;
var transactions = [];

// Generate transactions
for (var i = 0; i < totalTransactions; i++) {
// Transactions
var transaction = faker.helpers.createTransaction();
transaction.date = getDate();
totalAmount += parseFloat(transaction.amount);

// Other transactions
transaction.other = [];
for (var j = 0; j < totalTransactions; j++) {
other = faker.helpers.createTransaction();
other.date = getDate();
transaction.other.push(other);
}

transactions.push(transaction);
}

dataView.transactions = transactions.sort(compare);
dataView.selectedTransaction = transactions[0];
dataView.totalAmount = toCurrency(totalAmount);
}

/** Sorts by date. */
function compare(a, b) {
var date1 = new Date(a.date);
var date2 = new Date(b.date);
if (date1 < date2) {
return -1;
}
if (date1 > date2) {
return 1;
}
return 0;
};

/** Returns a currency string for given value. */
function toCurrency(value) {
return value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString();
}

/** Returns a random date between Jan 1st, 2017 and now. */
function getDate() {
var date = faker.date.between('2017-01-01', new Date());
return date.toLocaleDateString();
}

/** Renders the tempate to the #demo-app div. */
function render() {
var content = $($.parseHTML(template));
var compiler = Handlebars.compile(content.filter("#layout").html());

Handlebars.registerPartial({
leftPanel: content.filter("#left-panel").html(),
rightPanel: content.filter("#right-panel").html(),
transactionList: content.filter("#transaction-list").html(),
transactionDetails: content.filter("#transaction-details").html(),
transactionOther: content.filter("#transaction-other").html(),
});

// Adds selected class to selected transaction item.
Handlebars.registerHelper('isSelected', function(options) {
var resp =
(this.business === dataView.selectedTransaction.business) ?
"selected" : "";
return options.fn(resp);
});

// Returns fake detail monthly total.
Handlebars.registerHelper('monthlyTotal', function(options) {
var total = toCurrency(parseFloat(this.selectedTransaction.amount) * 12);
return options.fn(total);
});

// Returns fake detail yearly total.
Handlebars.registerHelper('yearlyTotal', function(options) {
var total = toCurrency(parseFloat(this.selectedTransaction.amount) * 40);
return options.fn(total);
});

var html = compiler(dataView);
$("#demo-app").html(html);

addClickHandler();
}

// Adds a click listener for transaction list items.
function addClickHandler() {
$('.transaction-list-item').click(function() {
var index = $('.transaction-list-item').index(this);
dataView.selectedTransaction = dataView.transactions[index];
render();
});
}
Loading

0 comments on commit 095254e

Please sign in to comment.