PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/assignments/Week-2/ToDo/index.html

https://github.com/rockymountainhigh1943/WDIM387
HTML | 235 lines | 216 code | 19 blank | 0 comment | 0 complexity | 248e349fe14fa0410894f2f5e943492b MD5 | raw file
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>JavaScript MVC App: ToDo List w/ localStorage</title>
  5. <script type="text/javascript" src="scripts/json2.js"></script>
  6. <script type="text/javascript" src="scripts/jquery-1.6.2.min.js"></script>
  7. <script type="text/javascript" src="scripts/underscore-1.1.6.js"></script>
  8. <script type="text/javascript" src="scripts/backbone.js"></script>
  9. <script type="text/javascript" src="scripts/backbone-localstorage.js"></script>
  10. <script type="text/javascript">
  11. $(document).ready(function(){
  12. // Model: New Task
  13. window.Task = Backbone.Model.extend({
  14. defaults: {
  15. content: 'New Task',
  16. done: false
  17. },
  18. initialize: function(){
  19. if(!this.get('content')){
  20. this.set({'content': this.defaults.content});
  21. }
  22. },
  23. toggle: function(){
  24. this.save({done: !this.get('done')});
  25. },
  26. clear: function(){
  27. this.destroy();
  28. this.view.remove();
  29. }
  30. });
  31. // Collection: Task List
  32. window.TaskList = Backbone.Collection.extend({
  33. model: Task,
  34. localStorage: new Store('todos'),
  35. done: function(){
  36. return this.filter(function(task){ return task.get('done'); });
  37. },
  38. remaining: function(){
  39. return this.without.apply(this, this.done());
  40. },
  41. nextOrder: function(){
  42. if(!this.length) return 1;
  43. return this.last().get('order') + 1;
  44. },
  45. comparator: function(task){
  46. return task.get('order');
  47. }
  48. });
  49. // Create global collection of tasks
  50. window.Tasks = new TaskList;
  51. // View: Task List Item
  52. window.TaskView = Backbone.View.extend({
  53. tagName: 'li',
  54. template: _.template($('#item-template').html()),
  55. events:
  56. {
  57. 'click .check' : 'toggleDone',
  58. 'dblclick div.task-content' : 'edit',
  59. 'click span.task-destroy' : 'clear',
  60. 'keypress .todo-input' : 'updateOnEnter'
  61. },
  62. initialize: function(){
  63. this.model.bind('change', this.render, this);
  64. this.model.view = this;
  65. },
  66. render: function(){
  67. $(this.el).html(this.template(this.model.toJSON()));
  68. return this;
  69. },
  70. setContent: function(){
  71. var content = this.model.get('content');
  72. this.$('.todo-content').text(content);
  73. this.input = this.$('.todo-input');
  74. this.input.bind('blur', _.bind(this.close, this));
  75. this.input.val(content);
  76. },
  77. toggleDone: function(){
  78. this.model.toggle();
  79. },
  80. edit: function(){
  81. $(this.el).addClass('editing');
  82. this.input.focus();
  83. },
  84. close: function(){
  85. this.model.save({content: this.input.val()});
  86. $(this.el).removeClass('editing');
  87. },
  88. updateOnEnter: function(e){
  89. if(e.keyCode == 13) this.close();
  90. },
  91. remove: function(){
  92. $(this.el).remove();
  93. },
  94. clear: function(){
  95. this.model.clear();
  96. }
  97. });
  98. // View: The App
  99. window.AppView = Backbone.View.extend({
  100. el: $('#todoapp'),
  101. statsTemplate: _.template($('#stats-template').html()),
  102. events: {
  103. 'keypress #new-todo' : 'createOnEnter',
  104. 'keyup #new-todo' : 'showToolTip',
  105. 'click .todo-clear' : 'clearCompleted'
  106. },
  107. initialize: function(){
  108. this.input = this.$('#new-todo');
  109. Tasks.bind('add', this.addOne, this);
  110. Tasks.bind('reset', this.addAll, this);
  111. Tasks.bind('all', this.render, this);
  112. Tasks.fetch();
  113. },
  114. render: function(){
  115. this.$('#todo-stats').html(this.statsTemplate({
  116. total: Tasks.length,
  117. done: Tasks.done().length,
  118. remaining: Tasks.remaining().length
  119. }));
  120. },
  121. addOne: function(task){
  122. var view = new TaskView({model: task});
  123. this.$("#todo-list").append(view.render().el);
  124. },
  125. addAll: function(){
  126. Tasks.each(this.addOne);
  127. },
  128. newAttributes: function(){
  129. return {
  130. content: this.input.val(),
  131. order: Tasks.nextOrder(),
  132. done: false
  133. };
  134. },
  135. createOnEnter: function(e){
  136. if (e.keyCode != 13) return;
  137. Tasks.create(this.newAttributes());
  138. this.input.val('');
  139. },
  140. clearCompleted: function(){
  141. _.each(Tasks.done(), function(task){ task.clear(); });
  142. },
  143. showToolTip: function(e){
  144. var tooltip = this.$('.ui-tooltip-top');
  145. var val = this.input.val();
  146. tooltip.fadeOut();
  147. if (this.tooltipTimeout) clearTimeout(this.tooltipTimeout);
  148. if (val == '' || val == this.input.attr('placeholder')) return;
  149. var show = function(){
  150. tooltip.show().fadeIn();
  151. };
  152. this.tooltipTimeout = _.delay(show, 1000);
  153. }
  154. });
  155. window.App = new AppView;
  156. });
  157. </script>
  158. <style type="text/css">
  159. body {
  160. font-family:Arial, sans-serif;
  161. font-size:14px;
  162. }
  163. h1 {
  164. font-size:24px;
  165. color:#099FF;
  166. margin-bottom:10px;
  167. }
  168. </style>
  169. </head>
  170. <body>
  171. <div id="todoapp">
  172. <div class="title">
  173. <h1>Task List</h1>
  174. <p>Add items to your ToDo list below:</p>
  175. </div>
  176. <div class="content">
  177. <div id="create-todo">
  178. <input id="new-todo" placeholder="What needs to be done?" type="text" />
  179. <span class="ui-tooltip-top" style="display:none;">Press Enter to save this task</span>
  180. </div>
  181. <div id="todos">
  182. <ul id="todo-list"></ul>
  183. </div>
  184. <div id="todo-stats"></div>
  185. </div>
  186. </div>
  187. <script type="text/template" id="item-template">
  188. <div class="todo <%= done ? 'done' : '' %>">
  189. <div class="display">
  190. <input class="check" type="checkbox" <%= done ? 'checked="checked"' : '' %> />
  191. <div class="todo-content"></div>
  192. <span class="todo-destroy"></span>
  193. </div>
  194. <div class="edit">
  195. <input class="todo-input" type="text" value="" />
  196. </div>
  197. </div>
  198. </script>
  199. <script type="text/template" id="stats-template">
  200. <% if (total) { %>
  201. <span class="todo-count">
  202. <span class="number"><%= remaining %></span>
  203. <span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left.
  204. </span>
  205. <% } %>
  206. <% if (done) { %>
  207. <span class="todo-clear">
  208. <a href="#">
  209. Clear <span class="number-done"><%= done %></span>
  210. completed <span class="word-done"><%= done == 1 ? 'item' : 'items' %></span>
  211. </a>
  212. </span>
  213. <% } %>
  214. </script>
  215. </body>
  216. </html>