diff --git a/src/app.css b/src/app.css deleted file mode 100644 index 527a58f..0000000 --- a/src/app.css +++ /dev/null @@ -1,20 +0,0 @@ -.toggle-graph { - float: left; - margin-left: 16px; - cursor: pointer; - position: relative; - z-index: 1; -} -.toggle-graph svg { - height: 20px; - width: 20px; -} -.toggle-graph svg path { - fill: #777; -} - -.toggle-graph.active svg path, -.toggle-graph:hover svg path, -.toggle-graph:focus svg path { - fill: black; -} diff --git a/src/app.js b/src/app.js index 51a0571..bea3f58 100644 --- a/src/app.js +++ b/src/app.js @@ -7,23 +7,21 @@ var Model = require('./model') var Store = require('./store') var Template = require('./template') -import {$on} from './helpers' -import {updateTodo} from './todo' -import toggleGraph from './graph' - -export function onLoad() { // eslint-disable-line import/prefer-default-export - updateTodo() - const toggleGraphButton = document.querySelector('.toggle-graph') - $on( - toggleGraphButton, - 'click', - () => { - const active = toggleGraph() - if (active) { - toggleGraphButton.classList.add('active') - } else { - toggleGraphButton.classList.remove('active') - } - }, - ) +/** +* Sets up a brand new Todo list. +* +* @param {string} name The name of your new to do list. +*/ +function Todo(name) { + this.storage = new Store(name) + this.model = new Model(this.storage) + this.template = new Template() + this.view = new View(this.template) + this.controller = new Controller(this.model, this.view) +} + +module.exports.onLoad = function onLoad() { + var todo = new Todo('todos-vanillajs') + todo.controller.setView(document.location.hash) + helpers.log('view set') } diff --git a/src/controller.js b/src/controller.js index 8a48c90..532c5f6 100644 --- a/src/controller.js +++ b/src/controller.js @@ -1,4 +1,4 @@ -export default Controller +module.exports = Controller /** * Takes a model and view and acts as the controller between them @@ -110,7 +110,7 @@ Controller.prototype.addItem = function(title) { Controller.prototype.editItem = function(id) { var that = this that.model.read(id, function(data) { - that.view.render('editItem', {id, title: data[0].title}) + that.view.render('editItem', {id: id, title: data[0].title}) }) } @@ -120,8 +120,8 @@ Controller.prototype.editItem = function(id) { Controller.prototype.editItemSave = function(id, title) { var that = this if (title.trim()) { - that.model.update(id, {title}, function() { - that.view.render('editItemDone', {id, title}) + that.model.update(id, {title: title}, function() { + that.view.render('editItemDone', {id: id, title: title}) }) } else { that.removeItem(id) @@ -134,7 +134,7 @@ Controller.prototype.editItemSave = function(id, title) { Controller.prototype.editItemCancel = function(id) { var that = this that.model.read(id, function(data) { - that.view.render('editItemDone', {id, title: data[0].title}) + that.view.render('editItemDone', {id: id, title: data[0].title}) }) } @@ -179,10 +179,10 @@ Controller.prototype.removeCompletedItems = function() { */ Controller.prototype.toggleComplete = function(id, completed, silent) { var that = this - that.model.update(id, {completed}, function() { + that.model.update(id, {completed: completed}, function() { that.view.render('elementComplete', { - id, - completed, + id: id, + completed: completed }) }) @@ -230,7 +230,6 @@ Controller.prototype._updateCount = function() { */ Controller.prototype._filter = function(force) { var activeRoute = this._activeRoute.charAt(0).toUpperCase() + this._activeRoute.substr(1) - // Update the elements on the page, which change with each completed todo this._updateCount() @@ -250,7 +249,6 @@ Controller.prototype._filter = function(force) { Controller.prototype._updateFilterState = function(currentPage) { // Store a reference to the active route, allowing us to re-filter todo // items as they are marked complete or incomplete. - currentPage = currentPage.split('?')[0] this._activeRoute = currentPage if (currentPage === '') { diff --git a/src/graph/index.js b/src/graph/index.js deleted file mode 100644 index 7957117..0000000 --- a/src/graph/index.js +++ /dev/null @@ -1,53 +0,0 @@ -import {subscribe, getTodo} from '../todo' - -let graphArea -const unsubscribe = { - store: null, - todo: null, -} - -export default toggleGraph - -function toggleGraph() { - if (graphArea) { - graphArea.remove() - graphArea = null - unsubscribe.store() - unsubscribe.todo() - return false - } else { - graphArea = document.createElement('div') - document.body.querySelector('.graph-area-container').appendChild(graphArea) - const {storage} = getTodo() - loadAndRenderGraph(graphArea, storage) - updateTodoSubscription() - updateStoreSubscription(storage) - return true - } -} - -function updateTodoSubscription() { - if (unsubscribe.todo) { - unsubscribe.todo() - } - unsubscribe.todo = subscribe(function onTodoUpdate() { - const {storage} = getTodo() - updateStoreSubscription(storage) - loadAndRenderGraph(graphArea, storage) - }) -} - -function updateStoreSubscription(store) { - if (unsubscribe.store) { - unsubscribe.store() - } - unsubscribe.store = store.subscribe(function onStoreUpdate() { - loadAndRenderGraph(graphArea, store) - }) -} - -function loadAndRenderGraph(node, store) { - System.import('./render').then(({default: renderGraph}) => { - renderGraph(node, store) - }) -} diff --git a/src/graph/render.js b/src/graph/render.js deleted file mode 100644 index 5ff1555..0000000 --- a/src/graph/render.js +++ /dev/null @@ -1,44 +0,0 @@ -import React from 'react' -import ReactDOM from 'react-dom' -import {PieChart} from 'rd3' -import {chain} from 'lodash' - -export default updateGraph - -function updateGraph(node, store) { - store.findAll(todos => { - ReactDOM.render( - , - node, - ) - }) -} - -function Graph({todos}) { - const data = chain(todos) - .groupBy('completed') - .map((group, complete) => ({ - label: complete === 'true' ? 'Complete' : 'Incomplete', - value: Math.round(group.length / todos.length * 10000) / 100 - })) - .value() - return ( -
- There are {todos.length} total todos -
- -
-
- ) -} -Graph.propTypes = { - todos: React.PropTypes.array, -} diff --git a/src/helpers.js b/src/helpers.js index e88ecb8..b8eb212 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,4 +1,4 @@ -export {qs, qsa, $on, $delegate, $parent, remove} +module.exports = {qs, qsa, log, $on, $delegate, $parent, remove, leftPad} // Get element(s) by CSS selector: function qs(selector, scope) { @@ -9,6 +9,12 @@ function qsa(selector, scope) { return (scope || document).querySelectorAll(selector) } +function log() { + if (window.console && window.console.log) { + window.console.log.apply(window.console, arguments) // eslint-disable-line + } +} + // addEventListener wrapper: function $on(target, type, callback, useCapture) { target.addEventListener(type, callback, !!useCapture) @@ -17,10 +23,6 @@ function $on(target, type, callback, useCapture) { // Attach a handler to event for all elements that match the selector, // now or in the future, based on a root element function $delegate(target, selector, type, handler) { - // https://developer.mozilla.org/en-US/docs/Web/Events/blur - var useCapture = type === 'blur' || type === 'focus' - $on(target, type, dispatchEvent, useCapture) - function dispatchEvent(event) { var targetElement = event.target var potentialElements = qsa(selector, target) @@ -30,13 +32,18 @@ function $delegate(target, selector, type, handler) { handler.call(targetElement, event) } } + + // https://developer.mozilla.org/en-US/docs/Web/Events/blur + var useCapture = type === 'blur' || type === 'focus' + + $on(target, type, dispatchEvent, useCapture) } // Find the element's parent with the given tag name: // $parent(qs('a'), 'div'); function $parent(element, tagName) { if (!element.parentNode) { - return undefined + return } if (element.parentNode.tagName.toLowerCase() === tagName.toLowerCase()) { return element.parentNode @@ -56,6 +63,16 @@ function remove(array, thing) { array.splice(index, 1) } +// pad the left of the given string by the given size with the given character +// leftPad('10', 3, '0') -> 010 +function leftPad(str, size, padWith) { + if (size <= str.length) { + return str + } else { + return Array(size - str.length + 1).join(padWith || '0') + str + } +} + // Allow for looping on nodes by chaining: // qsa('.foo').forEach(function () {}) NodeList.prototype.forEach = Array.prototype.forEach diff --git a/src/index.html b/src/index.html old mode 100644 new mode 100755 index 69d8dbe..896d463 --- a/src/index.html +++ b/src/index.html @@ -18,11 +18,6 @@ -