todos.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Todo</title>
  5. <meta charset="utf-8">
  6. <script src="../dist/seed.js"></script>
  7. <style type="text/css">
  8. .red {
  9. color: red;
  10. }
  11. .done {
  12. text-decoration: line-through;
  13. }
  14. #app.all .all {
  15. font-weight: bold;
  16. }
  17. #app.remaining .todo.done {
  18. display: none;
  19. }
  20. #app.remaining .remaining {
  21. font-weight: bold;
  22. }
  23. #app.completed .todo:not(.done) {
  24. display: none;
  25. }
  26. #app.completed .completed {
  27. font-weight: bold;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="app" sd-controller="Todos" sd-class="filter">
  33. <div>
  34. <input placeholder="What needs to be done?" sd-on="change:addTodo">
  35. </div>
  36. <ul sd-show="todos">
  37. <li class="todo" sd-each="todo:todos" sd-class="done:todo.done">
  38. <input type="checkbox" sd-checked="todo.done" sd-on="change:toggleTodo">
  39. <span sd-text="todo.text"></span>
  40. <a sd-on="click:removeTodo">X</a>
  41. </li>
  42. </ul>
  43. <div id="footer">
  44. Total: <span sd-text="total < todos"></span> |
  45. Remaining: <span sd-text="remaining < completed"></span> |
  46. Completed: <span sd-text="completed"></span>
  47. <br>
  48. <a class="all" sd-on="click:setFilter">Show All</a> |
  49. <a class="remaining" sd-on="click:setFilter">Show Remaining</a> |
  50. <a class="completed" sd-on="click:setFilter">Show Completed</a>
  51. <br>
  52. <a sd-on="click:removeCompleted">Remove Completed</a>
  53. </div>
  54. </div>
  55. <script>
  56. var Seed = require('seed')
  57. var todos = [
  58. { text: 'make nesting controllers work', done: true },
  59. { text: 'complete ArrayWatcher', done: false },
  60. { text: 'computed properties', done: false },
  61. { text: 'parse textnodes', done: false }
  62. ]
  63. Seed.controller('Todos', function (scope) {
  64. // regular properties
  65. scope.todos = todos
  66. scope.filter = 'all'
  67. scope.completed = todos.reduce(function (count, todo) {
  68. return count + (todo.done ? 1 : 0)
  69. }, 0)
  70. // computed properties
  71. scope.total = function () {
  72. return scope.todos.length
  73. }
  74. scope.remaining = function () {
  75. return scope.todos.length - scope.completed
  76. }
  77. // event handlers
  78. scope.addTodo = function (e) {
  79. var val = e.el.value
  80. if (val) {
  81. e.el.value = ''
  82. scope.todos.unshift({ text: val, done: false })
  83. }
  84. }
  85. scope.removeTodo = function (e) {
  86. scope.todos.remove(e.scope)
  87. scope.completed -= e.scope.done ? 1 : 0
  88. }
  89. scope.toggleTodo = function (e) {
  90. scope.completed += e.scope.done ? 1 : -1
  91. }
  92. scope.setFilter = function (e) {
  93. scope.filter = e.el.className
  94. }
  95. scope.removeCompleted = function () {
  96. scope.todos = scope.todos.filter(function (todo) {
  97. return !todo.done
  98. })
  99. }
  100. })
  101. var app = Seed.bootstrap()
  102. </script>
  103. </body>
  104. </html>