|
| 1 | +(function () { |
| 2 | + //trim polyfill |
| 3 | + if (!String.prototype.trim) { |
| 4 | + String.prototype.trim = function () { |
| 5 | + return this.replace(/^\s+|\s+$/g, ''); |
| 6 | + }; |
| 7 | + } |
| 8 | + |
| 9 | + //a custom binding to handle the enter key (could go in a separate library) |
| 10 | + ko.bindingHandlers.enterKey = { |
| 11 | + init:function (element, valueAccessor, allBindingsAccessor, data) { |
| 12 | + var wrappedHandler, newValueAccessor; |
| 13 | + |
| 14 | + //wrap the handler with a check for the enter key |
| 15 | + wrappedHandler = function (data, event) { |
| 16 | + if (event.keyCode === 13) { |
| 17 | + valueAccessor().call(this, data, event); |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + //create a valueAccessor with the options that we would want to pass to the event binding |
| 22 | + newValueAccessor = function () { |
| 23 | + return { keyup:wrappedHandler }; |
| 24 | + }; |
| 25 | + |
| 26 | + //call the real event binding's init function |
| 27 | + ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + //wrapper to hasfocus that also selects text and applies focus async |
| 32 | + ko.bindingHandlers.selectAndFocus = { |
| 33 | + init:function (element, valueAccessor, allBindingsAccessor) { |
| 34 | + ko.bindingHandlers.hasfocus.init(element, valueAccessor, allBindingsAccessor); |
| 35 | + ko.utils.registerEventHandler(element, "focus", function () { |
| 36 | + element.select(); |
| 37 | + }); |
| 38 | + }, |
| 39 | + update:function (element, valueAccessor) { |
| 40 | + ko.utils.unwrapObservable(valueAccessor()); //for dependency |
| 41 | + //ensure that element is visible before trying to focus |
| 42 | + setTimeout(function () { |
| 43 | + ko.bindingHandlers.hasfocus.update(element, valueAccessor); |
| 44 | + }, 0); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + //alternative to "visible" binding that will specifically set "block" to override what is in css |
| 49 | + ko.bindingHandlers.block = { |
| 50 | + update:function (element, valueAccessor) { |
| 51 | + var value = ko.utils.unwrapObservable(valueAccessor()); |
| 52 | + element.style.display = value ? "block" : "none"; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + //alternative to "visible" binding that will specifically set "inline" to override what is in css |
| 57 | + ko.bindingHandlers.inline = { |
| 58 | + update:function (element, valueAccessor) { |
| 59 | + var value = ko.utils.unwrapObservable(valueAccessor()); |
| 60 | + element.style.display = value ? "inline" : "none"; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + //represent a single todo item |
| 65 | + var Todo = function (content, done) { |
| 66 | + this.content = ko.observable(content); |
| 67 | + this.done = ko.observable(done); |
| 68 | + this.editing = ko.observable(false); |
| 69 | + }; |
| 70 | + |
| 71 | + //our main view model |
| 72 | + var ViewModel = function (todos) { |
| 73 | + var self = this; |
| 74 | + //map array of passed in todos to an observableArray of Todo objects |
| 75 | + self.todos = ko.observableArray(ko.utils.arrayMap(todos, function (todo) { |
| 76 | + return new Todo(todo.content, todo.done); |
| 77 | + })); |
| 78 | + |
| 79 | + //store the new todo value being entered |
| 80 | + self.current = ko.observable(); |
| 81 | + |
| 82 | + //add a new todo, when enter key is pressed |
| 83 | + self.add = function (data, event) { |
| 84 | + var newTodo, current = self.current().trim(); |
| 85 | + if (current) { |
| 86 | + newTodo = new Todo(current); |
| 87 | + self.todos.push(newTodo); |
| 88 | + self.current(""); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + //remove a single todo |
| 93 | + self.remove = function (todo) { |
| 94 | + self.todos.remove(todo); |
| 95 | + }; |
| 96 | + |
| 97 | + //remove all completed todos |
| 98 | + self.removeCompleted = function () { |
| 99 | + self.todos.remove(function (todo) { |
| 100 | + return todo.done(); |
| 101 | + }); |
| 102 | + }; |
| 103 | + |
| 104 | + //edit an item |
| 105 | + self.editItem = function(item) { |
| 106 | + item.editing(true); |
| 107 | + }; |
| 108 | + |
| 109 | + //stop editing an item. Remove the item, if it is now empty |
| 110 | + self.stopEditing = function(item) { |
| 111 | + item.editing(false); |
| 112 | + if (!item.content().trim()) { |
| 113 | + self.remove(item); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + //count of all completed todos |
| 118 | + self.completedCount = ko.computed(function () { |
| 119 | + return ko.utils.arrayFilter(self.todos(), |
| 120 | + function (todo) { |
| 121 | + return todo.done(); |
| 122 | + }).length; |
| 123 | + }); |
| 124 | + |
| 125 | + //count of todos that are not complete |
| 126 | + self.remainingCount = ko.computed(function () { |
| 127 | + return self.todos().length - self.completedCount(); |
| 128 | + }); |
| 129 | + |
| 130 | + //writeable computed observable to handle marking all complete/incomplete |
| 131 | + self.allCompleted = ko.computed({ |
| 132 | + //always return true/false based on the done flag of all todos |
| 133 | + read:function () { |
| 134 | + return !self.remainingCount(); |
| 135 | + }, |
| 136 | + //set all todos to the written value (true/false) |
| 137 | + write:function (newValue) { |
| 138 | + ko.utils.arrayForEach(self.todos(), function (todo) { |
| 139 | + //set even if value is the same, as subscribers are not notified in that case |
| 140 | + todo.done(newValue); |
| 141 | + }); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + //helper function to keep expressions out of markup |
| 146 | + self.getLabel = function (count) { |
| 147 | + return ko.utils.unwrapObservable(count) === 1 ? "item" : "items"; |
| 148 | + }; |
| 149 | + |
| 150 | + //internal computed observable that fires whenever anything changes in our todos |
| 151 | + ko.computed( |
| 152 | + function () { |
| 153 | + //get a clean copy of the todos, which also creates a dependency on the observableArray and all observables in each item |
| 154 | + var todos = ko.toJS(self.todos); |
| 155 | + |
| 156 | + //store to local storage |
| 157 | + amplify.store("todos-knockout", todos); |
| 158 | + }).extend({ throttle:500 }); //save at most once per second |
| 159 | + }; |
| 160 | + |
| 161 | + //check local storage for todos |
| 162 | + var todos = amplify.store("todos-knockout"); |
| 163 | + |
| 164 | + //bind a new instance of our view model to the page |
| 165 | + ko.applyBindings(new ViewModel(todos || [])); |
| 166 | +})(); |
0 commit comments