Commit e714fd00 authored by matthew's avatar matthew

add reagent example

parent 36443315
target/
node_modules/todomvc-app-css/*
!node_modules/todomvc-app-css/index.css
node_modules/todomvc-common/*
!node_modules/todomvc-common/base.css
!node_modules/todomvc-common/base.js
<!doctype html>
<html lang="en" data-framework="reagent">
<head>
<meta charset="utf-8">
<title>Reagent • TodoMVC</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="node_modules/todomvc-common/base.css">
<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
</head>
<body>
<div id="app">Loading...</div>
<script src="node_modules/todomvc-common/base.js"></script>
<script src="js/app.js"></script>
<script>
todomvc.core.run();
</script>
</body>
</html>
This source diff could not be displayed because it is too large. You can view the blob instead.
html,
body {
margin: 0;
padding: 0;
}
button {
margin: 0;
padding: 0;
border: 0;
background: none;
font-size: 100%;
vertical-align: baseline;
font-family: inherit;
font-weight: inherit;
color: inherit;
-webkit-appearance: none;
appearance: none;
-webkit-font-smoothing: antialiased;
-moz-font-smoothing: antialiased;
font-smoothing: antialiased;
}
body {
font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif;
line-height: 1.4em;
background: #f5f5f5;
color: #4d4d4d;
min-width: 230px;
max-width: 550px;
margin: 0 auto;
-webkit-font-smoothing: antialiased;
-moz-font-smoothing: antialiased;
font-smoothing: antialiased;
font-weight: 300;
}
button,
input[type="checkbox"] {
outline: none;
}
.hidden {
display: none;
}
#todoapp {
background: #fff;
margin: 130px 0 40px 0;
position: relative;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2),
0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
#todoapp input::-webkit-input-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
#todoapp input::-moz-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
#todoapp input::input-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
#todoapp h1 {
position: absolute;
top: -155px;
width: 100%;
font-size: 100px;
font-weight: 100;
text-align: center;
color: rgba(175, 47, 47, 0.15);
-webkit-text-rendering: optimizeLegibility;
-moz-text-rendering: optimizeLegibility;
text-rendering: optimizeLegibility;
}
#new-todo,
.edit {
position: relative;
margin: 0;
width: 100%;
font-size: 24px;
font-family: inherit;
font-weight: inherit;
line-height: 1.4em;
border: 0;
outline: none;
color: inherit;
padding: 6px;
border: 1px solid #999;
box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-font-smoothing: antialiased;
font-smoothing: antialiased;
}
#new-todo {
padding: 16px 16px 16px 60px;
border: none;
background: rgba(0, 0, 0, 0.003);
box-shadow: inset 0 -2px 1px rgba(0,0,0,0.03);
}
#main {
position: relative;
z-index: 2;
border-top: 1px solid #e6e6e6;
}
label[for='toggle-all'] {
display: none;
}
#toggle-all {
position: absolute;
top: -55px;
left: -12px;
width: 60px;
height: 34px;
text-align: center;
border: none; /* Mobile Safari */
}
#toggle-all:before {
content: '❯';
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
}
#toggle-all:checked:before {
color: #737373;
}
#todo-list {
margin: 0;
padding: 0;
list-style: none;
}
#todo-list li {
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
}
#todo-list li:last-child {
border-bottom: none;
}
#todo-list li.editing {
border-bottom: none;
padding: 0;
}
#todo-list li.editing .edit {
display: block;
width: 506px;
padding: 13px 17px 12px 17px;
margin: 0 0 0 43px;
}
#todo-list li.editing .view {
display: none;
}
#todo-list li .toggle {
text-align: center;
width: 40px;
/* auto, since non-WebKit browsers doesn't support input styling */
height: auto;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
border: none; /* Mobile Safari */
-webkit-appearance: none;
appearance: none;
}
#todo-list li .toggle:after {
content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="-10 -18 100 135"><circle cx="50" cy="50" r="50" fill="none" stroke="#ededed" stroke-width="3"/></svg>');
}
#todo-list li .toggle:checked:after {
content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="-10 -18 100 135"><circle cx="50" cy="50" r="50" fill="none" stroke="#bddad5" stroke-width="3"/><path fill="#5dc2af" d="M72 25L42 71 27 56l-4 4 20 20 34-52z"/></svg>');
}
#todo-list li label {
white-space: pre;
word-break: break-word;
padding: 15px 60px 15px 15px;
margin-left: 45px;
display: block;
line-height: 1.2;
transition: color 0.4s;
}
#todo-list li.completed label {
color: #d9d9d9;
text-decoration: line-through;
}
#todo-list li .destroy {
display: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 0;
font-size: 30px;
color: #cc9a9a;
margin-bottom: 11px;
transition: color 0.2s ease-out;
}
#todo-list li .destroy:hover {
color: #af5b5e;
}
#todo-list li .destroy:after {
content: '×';
}
#todo-list li:hover .destroy {
display: block;
}
#todo-list li .edit {
display: none;
}
#todo-list li.editing:last-child {
margin-bottom: -1px;
}
#footer {
color: #777;
padding: 10px 15px;
height: 20px;
text-align: center;
border-top: 1px solid #e6e6e6;
}
#footer:before {
content: '';
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 50px;
overflow: hidden;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),
0 8px 0 -3px #f6f6f6,
0 9px 1px -3px rgba(0, 0, 0, 0.2),
0 16px 0 -6px #f6f6f6,
0 17px 2px -6px rgba(0, 0, 0, 0.2);
}
#todo-count {
float: left;
text-align: left;
}
#todo-count strong {
font-weight: 300;
}
#filters {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
right: 0;
left: 0;
}
#filters li {
display: inline;
}
#filters li a {
color: inherit;
margin: 3px;
padding: 3px 7px;
text-decoration: none;
border: 1px solid transparent;
border-radius: 3px;
}
#filters li a.selected,
#filters li a:hover {
border-color: rgba(175, 47, 47, 0.1);
}
#filters li a.selected {
border-color: rgba(175, 47, 47, 0.2);
}
#clear-completed,
html #clear-completed:active {
float: right;
position: relative;
line-height: 20px;
text-decoration: none;
cursor: pointer;
position: relative;
}
#clear-completed:hover {
text-decoration: underline;
}
#info {
margin: 65px auto 0;
color: #bfbfbf;
font-size: 10px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
text-align: center;
}
#info p {
line-height: 1;
}
#info a {
color: inherit;
text-decoration: none;
font-weight: 400;
}
#info a:hover {
text-decoration: underline;
}
/*
Hack to remove background from Mobile Safari.
Can't use it globally since it destroys checkboxes in Firefox
*/
@media screen and (-webkit-min-device-pixel-ratio:0) {
#toggle-all,
#todo-list li .toggle {
background: none;
}
#todo-list li .toggle {
height: 40px;
}
#toggle-all {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
-webkit-appearance: none;
appearance: none;
}
}
@media (max-width: 430px) {
#footer {
height: 50px;
}
#filters {
bottom: 10px;
}
}
hr {
margin: 20px 0;
border: 0;
border-top: 1px dashed #c5c5c5;
border-bottom: 1px dashed #f7f7f7;
}
.learn a {
font-weight: normal;
text-decoration: none;
color: #b83f45;
}
.learn a:hover {
text-decoration: underline;
color: #787e7e;
}
.learn h3,
.learn h4,
.learn h5 {
margin: 10px 0;
font-weight: 500;
line-height: 1.2;
color: #000;
}
.learn h3 {
font-size: 24px;
}
.learn h4 {
font-size: 18px;
}
.learn h5 {
margin-bottom: 0;
font-size: 14px;
}
.learn ul {
padding: 0;
margin: 0 0 30px 25px;
}
.learn li {
line-height: 20px;
}
.learn p {
font-size: 15px;
font-weight: 300;
line-height: 1.3;
margin-top: 0;
margin-bottom: 0;
}
#issue-count {
display: none;
}
.quote {
border: none;
margin: 20px 0 60px 0;
}
.quote p {
font-style: italic;
}
.quote p:before {
content: '“';
font-size: 50px;
opacity: .15;
position: absolute;
top: -20px;
left: 3px;
}
.quote p:after {
content: '”';
font-size: 50px;
opacity: .15;
position: absolute;
bottom: -42px;
right: 3px;
}
.quote footer {
position: absolute;
bottom: -40px;
right: 0;
}
.quote footer img {
border-radius: 3px;
}
.quote footer a {
margin-left: 5px;
vertical-align: middle;
}
.speech-bubble {
position: relative;
padding: 10px;
background: rgba(0, 0, 0, .04);
border-radius: 5px;
}
.speech-bubble:after {
content: '';
position: absolute;
top: 100%;
right: 30px;
border: 13px solid transparent;
border-top-color: rgba(0, 0, 0, .04);
}
.learn-bar > .learn {
position: absolute;
width: 272px;
top: 8px;
left: -300px;
padding: 10px;
border-radius: 5px;
background-color: rgba(255, 255, 255, .6);
transition-property: left;
transition-duration: 500ms;
}
@media (min-width: 899px) {
.learn-bar {
width: auto;
padding-left: 300px;
}
.learn-bar > .learn {
left: 8px;
}
}
/* global _ */
(function () {
'use strict';
/* jshint ignore:start */
// Underscore's Template Module
// Courtesy of underscorejs.org
var _ = (function (_) {
_.defaults = function (object) {
if (!object) {
return object;
}
for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
var iterable = arguments[argsIndex];
if (iterable) {
for (var key in iterable) {
if (object[key] == null) {
object[key] = iterable[key];
}
}
}
}
return object;
}
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(text, data, settings) {
var render;
settings = _.defaults({}, settings, _.templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = new RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
}
if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
}
if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
index = offset + match.length;
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + "return __p;\n";
try {
render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
}
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
};
// Provide the compiled function source as a convenience for precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
return _;
})({});
if (location.hostname === 'todomvc.com') {
window._gaq = [['_setAccount','UA-31081062-1'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'));
}
/* jshint ignore:end */
function redirect() {
if (location.hostname === 'tastejs.github.io') {
location.href = location.href.replace('tastejs.github.io/todomvc', 'todomvc.com');
}
}
function findRoot() {
var base = location.href.indexOf('examples/');
return location.href.substr(0, base);
}
function getFile(file, callback) {
if (!location.host) {
return console.info('Miss the info bar? Run TodoMVC from a server to avoid a cross-origin error.');
}
var xhr = new XMLHttpRequest();
xhr.open('GET', findRoot() + file, true);
xhr.send();
xhr.onload = function () {
if (xhr.status === 200 && callback) {
callback(xhr.responseText);
}
};
}
function Learn(learnJSON, config) {
if (!(this instanceof Learn)) {
return new Learn(learnJSON, config);
}
var template, framework;
if (typeof learnJSON !== 'object') {
try {
learnJSON = JSON.parse(learnJSON);
} catch (e) {
return;
}
}
if (config) {
template = config.template;
framework = config.framework;
}
if (!template && learnJSON.templates) {
template = learnJSON.templates.todomvc;
}
if (!framework && document.querySelector('[data-framework]')) {
framework = document.querySelector('[data-framework]').dataset.framework;
}
this.template = template;
if (learnJSON.backend) {
this.frameworkJSON = learnJSON.backend;
this.frameworkJSON.issueLabel = framework;
this.append({
backend: true
});
} else if (learnJSON[framework]) {
this.frameworkJSON = learnJSON[framework];
this.frameworkJSON.issueLabel = framework;
this.append();
}
this.fetchIssueCount();
}
Learn.prototype.append = function (opts) {
var aside = document.createElement('aside');
aside.innerHTML = _.template(this.template, this.frameworkJSON);
aside.className = 'learn';
if (opts && opts.backend) {
// Remove demo link
var sourceLinks = aside.querySelector('.source-links');
var heading = sourceLinks.firstElementChild;
var sourceLink = sourceLinks.lastElementChild;
// Correct link path
var href = sourceLink.getAttribute('href');
sourceLink.setAttribute('href', href.substr(href.lastIndexOf('http')));
sourceLinks.innerHTML = heading.outerHTML + sourceLink.outerHTML;
} else {
// Localize demo links
var demoLinks = aside.querySelectorAll('.demo-link');
Array.prototype.forEach.call(demoLinks, function (demoLink) {
if (demoLink.getAttribute('href').substr(0, 4) !== 'http') {
demoLink.setAttribute('href', findRoot() + demoLink.getAttribute('href'));
}
});
}
document.body.className = (document.body.className + ' learn-bar').trim();
document.body.insertAdjacentHTML('afterBegin', aside.outerHTML);
};
Learn.prototype.fetchIssueCount = function () {
var issueLink = document.getElementById('issue-count-link');
if (issueLink) {
var url = issueLink.href.replace('https://github.com', 'https://api.github.com/repos');
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
var parsedResponse = JSON.parse(e.target.responseText);
if (parsedResponse instanceof Array) {
var count = parsedResponse.length
if (count !== 0) {
issueLink.innerHTML = 'This app has ' + count + ' open issues';
document.getElementById('issue-count').style.display = 'inline';
}
}
};
xhr.send();
}
};
redirect();
getFile('learn.json', Learn);
})();
{
"private": true,
"dependencies": {
"todomvc-app-css": "^1.0.0",
"todomvc-common": "^1.0.1"
}
}
(defproject todomvc "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-3058" :scope "provided"]
[reagent "0.5.0"]
[secretary "1.2.2"]
[alandipert/storage-atom "1.2.4"]]
:min-lein-version "2.5.0"
:plugins [[lein-cljsbuild "1.0.4"]]
:profiles {:dev {:cljsbuild {:builds [{:source-paths ["src/cljs"]
:compiler {:output-to "js/app.js"}}]}}
:prod {:cljsbuild {:builds [{:source-paths ["src/cljs"]
:compiler {:output-to "js/app.js"
:optimizations :advanced
:elide-asserts true
:pretty-print false}}]}}})
# Reagent TodoMVC Example
> Reagent provides a minimalistic interface between ClojureScript and React. It allows you to define efficient React components using nothing but plain ClojureScript functions and data, that describe your UI using a Hiccup-like syntax.
> _[Reagent - https://github.com/reagent-project/reagent](https://github.com/reagent-project/reagent)_
## Learning Reagent
The [Reagent website](http://reagent-project.github.io/) is a great resource for getting started.
Here are some links you may find helpful:
* [reagent-template](https://github.com/reagent-project/reagent-template)
* [reagent-cookbook](https://github.com/reagent-project/reagent-cookbook)
* [reagent-forms](https://github.com/reagent-project/reagent-forms)
* [yogthos blog](http://yogthos.net/)
* Twitter: [@ReagentProject](https://twitter.com/reagentproject)
* YouTube: [Reagent Project](https://www.youtube.com/channel/UC1UP5LiNNNf0a45dA9eDA0Q)
Get help from other reagent users:
* [Google Groups mailing list](https://groups.google.com/forum/#!forum/reagent-project)
* [SF Reagent Meetup](http://www.meetup.com/Reagent-Minimalistic-React-for-ClojureScript/)
## Components Summary
The following is a summary of the components directory.
![Components overview](components-overview.png)
## Running
Compile the clojurescript files.
```
$ lein cljsbuild once
```
*Note: to compile an optimized version, run `lein clean` followed by `lein with-profile prod cljsbuild once`*
Open `index.html` to view the example application.
## Credit
[Matthew Jaoudi](https://twitter.com/gadfly361), [Dmitri Sotnikov](https://twitter.com/yogthos), and [Dan Holmsand](https://twitter.com/holmsand)
(ns todomvc.actions
(:require [todomvc.session :as session]
[todomvc.helpers :as helpers]))
(defn add-todo [title default]
(let [id (helpers/create-todo-id)
trimmed-title (helpers/trim-title @title)]
(swap! session/todos assoc id {:id id
:title trimmed-title
:completed false})
(reset! title default)))
(defn toggle-todo [id]
(swap! session/todos update-in [id :completed] not))
(defn toggle-all-todos [bool]
(doseq [todo (helpers/todos-all @session/todos)]
(swap! session/todos assoc-in [(:id todo) :completed] (not bool))))
(defn delete-todo [id]
(swap! session/todos dissoc id))
(defn save-todo [id title editing]
(let [trimmed-title (helpers/trim-title @title)]
(if-not (empty? trimmed-title)
(swap! session/todos assoc-in [id :title] trimmed-title)
(delete-todo id))
(reset! editing false)))
(defn clear-completed-todos [todos]
(doseq [todo (helpers/todos-completed todos)]
(delete-todo (:id todo))))
(ns todomvc.components.footer)
(defn component []
[:footer#info
[:p "Double-click to edit a todo"]
[:p "Credits: "
[:a {:href "https://twitter.com/gadfly361"} "Matthew Jaoudi"] ", "
[:a {:href "https://twitter.com/yogthos"} "Dmitri Sotnikov"] ", and "
[:a {:href "https://twitter.com/holmsand"} "Dan Holmsand"]]
[:p "Part of " [:a {:href "http://todomvc.com"} "TodoMVC"]]])
(ns todomvc.components.title)
(defn component []
[:h1 "todos"])
(ns todomvc.components.todo-edit
(:require [reagent.core :as reagent]
[todomvc.actions :as actions]
[todomvc.helpers :as helpers]))
(defn on-key-down [k id title default editing]
(let [key-pressed (.-which k)]
(condp = key-pressed
helpers/enter-key (actions/save-todo id title editing)
helpers/escape-key (do (reset! title default)
(reset! editing false))
nil)))
(defn component-render [{:keys [id title completed]} editing]
(let [default title
edit-title (reagent/atom default)]
(fn []
[:input.edit {:type "text"
:style {:display (helpers/display-elem @editing)}
:value @edit-title
:on-change #(reset! edit-title (-> % .-target .-value))
:on-blur #(actions/save-todo id edit-title editing)
:on-key-down #(on-key-down % id edit-title default editing)}])))
(defn component-did-update [x]
(.focus (reagent/dom-node x)))
(defn component []
(reagent/create-class {:reagent-render component-render
:component-did-update component-did-update}))
(ns todomvc.components.todo-input
(:require [reagent.core :as reagent]
[todomvc.actions :as actions]
[todomvc.helpers :as helpers]))
(defn on-key-down [k title default]
(let [key-pressed (.-which k)]
(condp = key-pressed
helpers/enter-key (actions/add-todo title default)
nil)))
(defn component-render []
(let [default ""
title (reagent/atom default)]
(fn []
[:input#new-todo {:type "text"
:value @title
:placeholder "What needs to be done?"
:on-change #(reset! title (-> % .-target .-value))
:on-key-down #(on-key-down % title default)}])))
(defn component-did-mount [x]
(.focus (reagent/dom-node x)))
(defn component []
(reagent/create-class {:reagent-render component-render
:component-did-mount component-did-mount}))
(ns todomvc.components.todo-item
(:require [reagent.core :as reagent]
[todomvc.session :as session]
[todomvc.actions :as actions]
[todomvc.helpers :as helpers]
[todomvc.components.todo-edit :as todo-edit]))
(defn todo-item-class [completed editing]
(str (when completed "completed ")
(when @editing "editing")))
(defn todo-checkbox [id completed]
[:input.toggle {:type "checkbox"
:checked completed
:on-change #(actions/toggle-todo id)}])
(defn component [todo]
(let [editing (reagent/atom false)]
(fn [{:keys [id title completed] :as todo}]
[:li {:class (todo-item-class completed editing)
:style {:display (helpers/display-elem
(helpers/todo-display-filter completed @session/todos-display-type))}}
[:div.view
[todo-checkbox id completed]
[:label {:on-double-click #(reset! editing true)} title]
[:button.destroy {:on-click #(actions/delete-todo id)}]]
[todo-edit/component todo editing]])))
(ns todomvc.components.todos-clear
(:require [todomvc.session :as session]
[todomvc.actions :as actions]
[todomvc.helpers :as helpers]))
(defn component []
[:button#clear-completed {:on-click #(actions/clear-completed-todos @session/todos)
:style {:display (helpers/display-elem (helpers/todos-any-completed?
@session/todos))}}
"Clear completed"])
(ns todomvc.components.todos-count
(:require [todomvc.session :as session]
[todomvc.helpers :as helpers]))
(defn items-left [todos]
(let [active-count (count (helpers/todos-active todos))]
(str (if (= 1 active-count) " item " " items ")
"left")))
(defn component []
[:span#todo-count
[:strong (count (helpers/todos-active @session/todos))]
(items-left @session/todos)])
(ns todomvc.components.todos-filters
(:require [todomvc.session :as session]))
(defn selected-class [display-type todos-display-type]
(if (= display-type
todos-display-type)
"selected" ""))
(defn component []
[:ul#filters
[:li [:a {:class (selected-class :all @session/todos-display-type) :href "#/"} "All"]]
[:li [:a {:class (selected-class :active @session/todos-display-type) :href "#/active"} "Active"]]
[:li [:a {:class (selected-class :completed @session/todos-display-type) :href "#/completed"} "Completed"]]])
(ns todomvc.components.todos-list
(:require [todomvc.components.todo-item :as todo-item]))
(defn component [todos]
[:ul#todo-list
(for [todo todos]
^{:key (:id todo)}
[todo-item/component todo])])
(ns todomvc.components.todos-toggle
(:require [todomvc.session :as session]
[todomvc.actions :as actions]
[todomvc.helpers :as helpers]))
(defn component []
[:span
[:input#toggle-all {:type "checkbox"
:checked (helpers/todos-all-completed? @session/todos)
:on-change #(actions/toggle-all-todos
(helpers/todos-all-completed? @session/todos))}]
[:label {:for "toggle-all"} "Mark all as complete"]])
(ns todomvc.core
(:require [reagent.core :as reagent]
[todomvc.session :as session]
[todomvc.actions :as actions]
[todomvc.helpers :as helpers]
[todomvc.components.title :as title]
[todomvc.components.todo-input :as todo-input]
[todomvc.components.footer :as footer]
[todomvc.components.todos-toggle :as todos-toggle]
[todomvc.components.todos-list :as todos-list]
[todomvc.components.todos-count :as todos-count]
[todomvc.components.todos-filters :as todos-filters]
[todomvc.components.todos-clear :as todos-clear]))
(defn todo-app []
[:div
[:section#todoapp
[:header#header
[title/component]
[todo-input/component]]
[:div {:style
{:display (helpers/display-elem (helpers/todos-any?
@session/todos))}}
[:section#main
[todos-toggle/component]
[todos-list/component (helpers/todos-all @session/todos)]]
[:footer#footer
[todos-count/component]
[todos-filters/component]
[todos-clear/component]
]]]
[footer/component]])
(defn ^:export run []
(reagent/render [todo-app]
(js/document.getElementById "app")))
(ns todomvc.helpers
(:require [todomvc.session :as session]))
(def enter-key 13)
(def escape-key 27)
(defn trim-title [title]
(clojure.string/trim title))
(defn display-elem [bool]
(if bool "inline" "none"))
(defn todo-display-filter [completed display-type]
(case display-type
:completed completed
:active (not completed)
true))
(defn create-todo-id []
(swap! session/todos-counter inc))
(defn todos-all [todos]
(vals todos))
(defn todos-active [todos]
(let [todos-all (todos-all todos)]
(filter #(not (:completed %)) todos-all)))
(defn todos-completed [todos]
(let [todos-all (todos-all todos)]
(filter :completed todos-all)))
(defn todos-any? [todos]
(pos? (count (todos-all todos))))
(defn todos-any-completed? [todos]
(pos? (count (todos-completed todos))))
(defn todos-all-completed? [todos]
(= (count (todos-all todos))
(count (todos-completed todos))))
(ns todomvc.routes
(:require [todomvc.session :as session]
[secretary.core :as secretary :include-macros true]
[goog.events :as events]
[goog.history.EventType :as EventType])
(:import goog.History))
(secretary/set-config! :prefix "#")
(secretary/defroute "/" []
(reset! session/todos-display-type :all))
(secretary/defroute "/active" []
(reset! session/todos-display-type :active))
(secretary/defroute "/completed" []
(reset! session/todos-display-type :completed))
(doto (History.)
(events/listen
EventType/NAVIGATE
(fn [event]
(secretary/dispatch! (.-token event))))
(.setEnabled true))
(ns todomvc.session
(:require [reagent.core :as reagent]
[alandipert.storage-atom :refer [local-storage]]))
(reset! alandipert.storage-atom/storage-delay 0)
(def todos (local-storage (reagent/atom (sorted-map)) :todos-reagent))
;; will look like {id {:id _ :title _ :completed _ }}
(def todos-counter (local-storage (reagent/atom 0) :todos-counter-reagent))
;; will inc for each new todo
(def todos-display-type (reagent/atom :all))
;; the options are :all, :active, :completed
...@@ -164,6 +164,9 @@ ...@@ -164,6 +164,9 @@
<li> <li>
<a href="examples/serenadejs/" data-source="https://github.com/elabs/serenade.js" data-content="Serenade.js is yet another MVC client side JavaScript framework. Why do we indulge in recreating the wheel? We believe that Serenade.js more closely follows the ideas of classical MVC than competing frameworks.">Serenade.js</a> <a href="examples/serenadejs/" data-source="https://github.com/elabs/serenade.js" data-content="Serenade.js is yet another MVC client side JavaScript framework. Why do we indulge in recreating the wheel? We believe that Serenade.js more closely follows the ideas of classical MVC than competing frameworks.">Serenade.js</a>
</li> </li>
<li class="routing">
<a href="examples/reagent/" data-source="https://reagent-project.github.io/" data-content="Reagent provides a minimalistic interface between ClojureScript and React.">Reagent</a>
</li>
</ul> </ul>
</div> </div>
<div class="js-app-list" data-app-list="labs"> <div class="js-app-list" data-app-list="labs">
......
...@@ -1813,6 +1813,43 @@ ...@@ -1813,6 +1813,43 @@
}] }]
}] }]
}, },
"reagent": {
"name": "Reagent",
"description": "Reagent provides a minimalistic interface between ClojureScript and React. It allows you to define efficient React components using nothing but plain ClojureScript functions and data, that describe your UI using a Hiccup-like syntax.",
"homepage": "reagent-project.github.io/",
"examples": [{
"name": "Reagent Cookbook",
"url": "https://github.com/reagent-project/reagent-cookbook"
}],
"link_groups": [{
"heading": "Official Resources",
"links": [{
"name": "Reagent Template",
"url": "https://github.com/reagent-project/reagent-template"
}, {
"name": "Reagent Cookbook",
"url": "https://github.com/reagent-project/reagent-cookbook"
}, {
"name": "Reagent Forms",
"url": "https://github.com/reagent-project/reagent-forms"
}]
}, {
"heading": "Community",
"links": [{
"name": "Reagent on Stack Overflow",
"url": "http://stackoverflow.com/questions/tagged/reagent"
}, {
"name": "Google Groups Mailing List",
"url": "https://groups.google.com/forum/#!forum/reagent-project"
}, {
"name": "Reagent on Twitter",
"url": "https://twitter.com/ReagentProject"
}, {
"name": "Reagent YouTube Channel",
"url": "https://www.youtube.com/channel/UC1UP5LiNNNf0a45dA9eDA0Q"
}]
}]
},
"sammyjs": { "sammyjs": {
"name": "Sammy.js", "name": "Sammy.js",
"description": "A small web framework with class.", "description": "A small web framework with class.",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment