From cccb86bd26a6c16f04d40be6d87f12cbb5f4ce27 Mon Sep 17 00:00:00 2001 From: Bryan-Kirk Reinhardt Date: Sat, 2 May 2015 13:53:22 -0700 Subject: [PATCH 1/3] Refactor --- README.md | 29 +++++++++++++-- client/README.md | 35 +----------------- client/angular/README.md | 34 +++++++++++++++++ client/{ => angular}/app/404.html | 0 client/{ => angular}/app/favicon.ico | Bin .../fonts/glyphicons-halflings-regular.eot | Bin .../fonts/glyphicons-halflings-regular.svg | 0 .../fonts/glyphicons-halflings-regular.ttf | Bin .../fonts/glyphicons-halflings-regular.woff | Bin client/{ => angular}/app/index.html | 0 client/{ => angular}/app/scripts/app.js | 0 .../app/scripts/controllers/main.js | 0 .../{ => angular}/app/scripts/services/api.js | 0 client/{ => angular}/app/styles/main.css | 0 client/{ => angular}/app/views/main.html | 0 15 files changed, 62 insertions(+), 36 deletions(-) mode change 100755 => 100644 client/README.md create mode 100755 client/angular/README.md rename client/{ => angular}/app/404.html (100%) rename client/{ => angular}/app/favicon.ico (100%) rename client/{ => angular}/app/fonts/glyphicons-halflings-regular.eot (100%) rename client/{ => angular}/app/fonts/glyphicons-halflings-regular.svg (100%) rename client/{ => angular}/app/fonts/glyphicons-halflings-regular.ttf (100%) rename client/{ => angular}/app/fonts/glyphicons-halflings-regular.woff (100%) rename client/{ => angular}/app/index.html (100%) rename client/{ => angular}/app/scripts/app.js (100%) rename client/{ => angular}/app/scripts/controllers/main.js (100%) rename client/{ => angular}/app/scripts/services/api.js (100%) rename client/{ => angular}/app/styles/main.css (100%) rename client/{ => angular}/app/views/main.html (100%) diff --git a/README.md b/README.md index aa08a21..dddee8d 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,30 @@ The socket connection endpoint is: ``` ws://{host:port}/v1.0/chat ``` -Following are some examples for using Chrome/Dark Websocket. + +The basic json format of a request is as follows: + +``` +{ + "roomName":"Name of the room to access", + "reqType":999, + "content":"command, metadata, msg etc." +} +``` + +The basic json format of a response back from the server is as follows: + +``` +{ + "roomName":"Name of the room responding.", + "rspType":999, + "content":"confirmation data, message, errors, etc" + "list":["data","in","array","form","such","as","roomnames"] +} +``` +See server/chat_request.go and server/chat_response.go for more details on types. + +The following are some examples for using Chrome/Dark Websocket. Spaces must be encoded in JSON calls. ``` @@ -124,8 +147,8 @@ Spaces must be encoded in JSON calls. ``` -A client javascript/html demo is provided under the /client directory. -Please see that directories README.md for more information. +Clientside demos are provided under the /client directory. +Please see those directory README.md files for more information. ## HTTP API for Alive and Stats diff --git a/client/README.md b/client/README.md old mode 100755 new mode 100644 index 0e5de19..223c5bb --- a/client/README.md +++ b/client/README.md @@ -1,34 +1,3 @@ -## ChattyPantz Client Demo +## ChattyPantz Client Demos -This folder contains a simple demonstration of a client connection to the server using javascript and html. - -### Dependencies - -* You must be connected to the internet to load CDN angular.js, semnatic-ui.js, and jquery.js. -* The server should be running on localhost:6660 - -### Instructions - -Assumption: server is up and running on localhost:6660. - -If needed, you can change the API host and port endpoint in scripts/services/api.js: -``` -var server = "ws://127.0.0.1:6660/v1.0/chat"; -``` -1. Load app/index.html in your browser. -2. Enter a nickname. -3. Connect to the server. - -You will be placed into room "Demo". A list of user nicknames from the room will also be displayed. -NOTE: If your nickname already exists when logging into the room, you will be warned and disconnected. - -4. Now, send your messages. -5. When you are done, disconnect from the server. - -### Enhancements - -The application doesn't demonstrate multi-room management, nor does it demonstrate the following request types: -* GET_NICKNAME: 102 -* LIST_ROOMS: 103 -* HIDE: 106 -* UNHIDE: 107 +Subfolders in this directory demonstrate client side connections to the Chattypantz server. diff --git a/client/angular/README.md b/client/angular/README.md new file mode 100755 index 0000000..051322c --- /dev/null +++ b/client/angular/README.md @@ -0,0 +1,34 @@ +## ChattyPantz Client Demo - Angular.js + +This folder contains a simple demonstration of a client connection to the server using javascript and html. + +### Dependencies + +* You must be connected to the internet to load CDN angular.js, semnatic-ui.js, and jquery.js. +* The server should be running on localhost:6660 + +### Instructions + +Assumption: server is up and running on localhost:6660. + +If needed, you can change the API host and port endpoint in scripts/services/api.js: +``` +var server = "ws://127.0.0.1:6660/v1.0/chat"; +``` +1. Load app/index.html in your browser. +2. Enter a nickname. +3. Connect to the server. + +You will be placed into room "Demo". A list of user nicknames from the room will also be displayed. +NOTE: If your nickname already exists when logging into the room, you will be warned and disconnected. + +4. Now, send your messages. +5. When you are done, disconnect from the server. + +### Enhancements + +The application doesn't demonstrate multi-room management, nor does it demonstrate the following request types: +* GET_NICKNAME: 102 +* LIST_ROOMS: 103 +* HIDE: 106 +* UNHIDE: 107 diff --git a/client/app/404.html b/client/angular/app/404.html similarity index 100% rename from client/app/404.html rename to client/angular/app/404.html diff --git a/client/app/favicon.ico b/client/angular/app/favicon.ico similarity index 100% rename from client/app/favicon.ico rename to client/angular/app/favicon.ico diff --git a/client/app/fonts/glyphicons-halflings-regular.eot b/client/angular/app/fonts/glyphicons-halflings-regular.eot similarity index 100% rename from client/app/fonts/glyphicons-halflings-regular.eot rename to client/angular/app/fonts/glyphicons-halflings-regular.eot diff --git a/client/app/fonts/glyphicons-halflings-regular.svg b/client/angular/app/fonts/glyphicons-halflings-regular.svg similarity index 100% rename from client/app/fonts/glyphicons-halflings-regular.svg rename to client/angular/app/fonts/glyphicons-halflings-regular.svg diff --git a/client/app/fonts/glyphicons-halflings-regular.ttf b/client/angular/app/fonts/glyphicons-halflings-regular.ttf similarity index 100% rename from client/app/fonts/glyphicons-halflings-regular.ttf rename to client/angular/app/fonts/glyphicons-halflings-regular.ttf diff --git a/client/app/fonts/glyphicons-halflings-regular.woff b/client/angular/app/fonts/glyphicons-halflings-regular.woff similarity index 100% rename from client/app/fonts/glyphicons-halflings-regular.woff rename to client/angular/app/fonts/glyphicons-halflings-regular.woff diff --git a/client/app/index.html b/client/angular/app/index.html similarity index 100% rename from client/app/index.html rename to client/angular/app/index.html diff --git a/client/app/scripts/app.js b/client/angular/app/scripts/app.js similarity index 100% rename from client/app/scripts/app.js rename to client/angular/app/scripts/app.js diff --git a/client/app/scripts/controllers/main.js b/client/angular/app/scripts/controllers/main.js similarity index 100% rename from client/app/scripts/controllers/main.js rename to client/angular/app/scripts/controllers/main.js diff --git a/client/app/scripts/services/api.js b/client/angular/app/scripts/services/api.js similarity index 100% rename from client/app/scripts/services/api.js rename to client/angular/app/scripts/services/api.js diff --git a/client/app/styles/main.css b/client/angular/app/styles/main.css similarity index 100% rename from client/app/styles/main.css rename to client/angular/app/styles/main.css diff --git a/client/app/views/main.html b/client/angular/app/views/main.html similarity index 100% rename from client/app/views/main.html rename to client/angular/app/views/main.html From 17bd5484a38601ae4a6633c7214506f2301579f7 Mon Sep 17 00:00:00 2001 From: Bryan-Kirk Reinhardt Date: Sat, 2 May 2015 15:14:14 -0700 Subject: [PATCH 2/3] react demo - WIP --- client/react/README.md | 34 ++++ client/react/app/index.html | 40 +++++ .../app/js/actions/ChattypantzActions.js | 85 +++++++++ client/react/app/js/app.js | 8 + .../app/js/constants/ChattypantzConstants.js | 13 ++ .../react/app/js/dispatcher/AppDispatcher.js | 3 + .../react/app/js/stores/ChattypantzStore.js | 165 ++++++++++++++++++ 7 files changed, 348 insertions(+) create mode 100644 client/react/README.md create mode 100644 client/react/app/index.html create mode 100644 client/react/app/js/actions/ChattypantzActions.js create mode 100644 client/react/app/js/app.js create mode 100644 client/react/app/js/constants/ChattypantzConstants.js create mode 100644 client/react/app/js/dispatcher/AppDispatcher.js create mode 100644 client/react/app/js/stores/ChattypantzStore.js diff --git a/client/react/README.md b/client/react/README.md new file mode 100644 index 0000000..4496fae --- /dev/null +++ b/client/react/README.md @@ -0,0 +1,34 @@ +## ChattyPantz Client Demo - React.js + +This folder contains a simple demonstration of a client connection to the server using javascript and html. + +### Dependencies + +* You must be connected to the internet to load CDN react.js, semnatic-ui.js, and jquery.js. +* The server should be running on localhost:6660 + +### Instructions + +Assumption: server is up and running on localhost:6660. + +If needed, you can change the API host and port endpoint in scripts/services/api.js: +``` +var server = "ws://127.0.0.1:6660/v1.0/chat"; +``` +1. Load app/index.html in your browser. +2. Enter a nickname. +3. Connect to the server. + +You will be placed into room "Demo". A list of user nicknames from the room will also be displayed. +NOTE: If your nickname already exists when logging into the room, you will be warned and disconnected. + +4. Now, send your messages. +5. When you are done, disconnect from the server. + +### Enhancements + +The application doesn't demonstrate multi-room management, nor does it demonstrate the following request types: +* GET_NICKNAME: 102 +* LIST_ROOMS: 103 +* HIDE: 106 +* UNHIDE: 107 diff --git a/client/react/app/index.html b/client/react/app/index.html new file mode 100644 index 0000000..d6b433e --- /dev/null +++ b/client/react/app/index.html @@ -0,0 +1,40 @@ + + + + + + + + + + + + Chattypantz Client Demo + + + + + + + + +
+
+

ChattyPantz Client Demo

+
+
+
+ + + + + + + + + diff --git a/client/react/app/js/actions/ChattypantzActions.js b/client/react/app/js/actions/ChattypantzActions.js new file mode 100644 index 0000000..5d465df --- /dev/null +++ b/client/react/app/js/actions/ChattypantzActions.js @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * TodoActions + */ + +var AppDispatcher = require('../dispatcher/AppDispatcher'); +var TodoConstants = require('../constants/ChattypantzConstants'); + +var TodoActions = { + + /** + * @param {string} text + */ + create: function(text) { + AppDispatcher.dispatch({ + actionType: TodoConstants.TODO_CREATE, + text: text + }); + }, + + /** + * @param {string} id The ID of the ToDo item + * @param {string} text + */ + updateText: function(id, text) { + AppDispatcher.dispatch({ + actionType: TodoConstants.TODO_UPDATE_TEXT, + id: id, + text: text + }); + }, + + /** + * Toggle whether a single ToDo is complete + * @param {object} todo + */ + toggleComplete: function(todo) { + var id = todo.id; + var actionType = todo.complete ? + TodoConstants.TODO_UNDO_COMPLETE : + TodoConstants.TODO_COMPLETE; + + AppDispatcher.dispatch({ + actionType: actionType, + id: id + }); + }, + + /** + * Mark all ToDos as complete + */ + toggleCompleteAll: function() { + AppDispatcher.dispatch({ + actionType: TodoConstants.TODO_TOGGLE_COMPLETE_ALL + }); + }, + + /** + * @param {string} id + */ + destroy: function(id) { + AppDispatcher.dispatch({ + actionType: TodoConstants.TODO_DESTROY, + id: id + }); + }, + + /** + * Delete all the completed ToDos + */ + destroyCompleted: function() { + AppDispatcher.dispatch({ + actionType: TodoConstants.TODO_DESTROY_COMPLETED + }); + } + +}; + +module.exports = TodoActions; diff --git a/client/react/app/js/app.js b/client/react/app/js/app.js new file mode 100644 index 0000000..bed0852 --- /dev/null +++ b/client/react/app/js/app.js @@ -0,0 +1,8 @@ +var React = require('react'); + +var TodoApp = require('./components/ChattypantzApp.react'); + +React.render( + , + document.getElementById('chattypantzapp') +); diff --git a/client/react/app/js/constants/ChattypantzConstants.js b/client/react/app/js/constants/ChattypantzConstants.js new file mode 100644 index 0000000..506fd64 --- /dev/null +++ b/client/react/app/js/constants/ChattypantzConstants.js @@ -0,0 +1,13 @@ +var requestType = require('requestType'); + +module.exports = requestType({ + SET_NICKNAME: 101, + GET_NICKNAME: 102, + LIST_ROOMS: 103, + JOIN: 104, + LIST_NAMES: 105, + HIDE: 106, + UNHIDE: 107, + MSG: 108, + LEAVE: 109 + }); diff --git a/client/react/app/js/dispatcher/AppDispatcher.js b/client/react/app/js/dispatcher/AppDispatcher.js new file mode 100644 index 0000000..5231a4e --- /dev/null +++ b/client/react/app/js/dispatcher/AppDispatcher.js @@ -0,0 +1,3 @@ +var Dispatcher = require('flux').Dispatcher; + +module.exports = new Dispatcher(); diff --git a/client/react/app/js/stores/ChattypantzStore.js b/client/react/app/js/stores/ChattypantzStore.js new file mode 100644 index 0000000..5a63072 --- /dev/null +++ b/client/react/app/js/stores/ChattypantzStore.js @@ -0,0 +1,165 @@ +var AppDispatcher = require('../dispatcher/AppDispatcher'); +var EventEmitter = require('events').EventEmitter; +var ChattypantzConstants = require('../constants/ChattypantzConstants'); +var assign = require('object-assign'); + +var CHANGE_EVENT = 'change'; + +var _todos = {}; + +/** + * Create a TODO item. + * @param {string} text The content of the TODO + */ +function create(text) { + // Hand waving here -- not showing how this interacts with XHR or persistent + // server-side storage. + // Using the current timestamp + random number in place of a real id. + var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36); + _todos[id] = { + id: id, + complete: false, + text: text + }; +} + +/** + * Update a TODO item. + * @param {string} id + * @param {object} updates An object literal containing only the data to be + * updated. + */ +function update(id, updates) { + _todos[id] = assign({}, _todos[id], updates); +} + +/** + * Update all of the TODO items with the same object. + * the data to be updated. Used to mark all TODOs as completed. + * @param {object} updates An object literal containing only the data to be + * updated. + + */ +function updateAll(updates) { + for (var id in _todos) { + update(id, updates); + } +} + +/** + * Delete a TODO item. + * @param {string} id + */ +function destroy(id) { + delete _todos[id]; +} + +/** + * Delete all the completed TODO items. + */ +function destroyCompleted() { + for (var id in _todos) { + if (_todos[id].complete) { + destroy(id); + } + } +} + +var TodoStore = assign({}, EventEmitter.prototype, { + + /** + * Tests whether all the remaining TODO items are marked as completed. + * @return {boolean} + */ + areAllComplete: function() { + for (var id in _todos) { + if (!_todos[id].complete) { + return false; + } + } + return true; + }, + + /** + * Get the entire collection of TODOs. + * @return {object} + */ + getAll: function() { + return _todos; + }, + + emitChange: function() { + this.emit(CHANGE_EVENT); + }, + + /** + * @param {function} callback + */ + addChangeListener: function(callback) { + this.on(CHANGE_EVENT, callback); + }, + + /** + * @param {function} callback + */ + removeChangeListener: function(callback) { + this.removeListener(CHANGE_EVENT, callback); + } +}); + +// Register callback to handle all updates +AppDispatcher.register(function(action) { + var text; + + switch(action.actionType) { + case TodoConstants.TODO_CREATE: + text = action.text.trim(); + if (text !== '') { + create(text); + TodoStore.emitChange(); + } + break; + + case TodoConstants.TODO_TOGGLE_COMPLETE_ALL: + if (TodoStore.areAllComplete()) { + updateAll({complete: false}); + } else { + updateAll({complete: true}); + } + TodoStore.emitChange(); + break; + + case TodoConstants.TODO_UNDO_COMPLETE: + update(action.id, {complete: false}); + TodoStore.emitChange(); + break; + + case TodoConstants.TODO_COMPLETE: + update(action.id, {complete: true}); + TodoStore.emitChange(); + break; + + case TodoConstants.TODO_UPDATE_TEXT: + text = action.text.trim(); + if (text !== '') { + update(action.id, {text: text}); + TodoStore.emitChange(); + } + break; + + case TodoConstants.TODO_DESTROY: + destroy(action.id); + TodoStore.emitChange(); + break; + + case TodoConstants.TODO_DESTROY_COMPLETED: + destroyCompleted(); + TodoStore.emitChange(); + break; + + default: + // no op + } +}); + +module.exports = ChattypantzStore; From fb036d69ec3358a323fac244f08bdc5fdd594af1 Mon Sep 17 00:00:00 2001 From: Bryan-Kirk Reinhardt Date: Mon, 4 May 2015 14:32:37 -0700 Subject: [PATCH 3/3] removed react - no long support for semantic --- client/react/README.md | 34 ---- client/react/app/index.html | 40 ----- .../app/js/actions/ChattypantzActions.js | 85 --------- client/react/app/js/app.js | 8 - .../app/js/constants/ChattypantzConstants.js | 13 -- .../react/app/js/dispatcher/AppDispatcher.js | 3 - .../react/app/js/stores/ChattypantzStore.js | 165 ------------------ 7 files changed, 348 deletions(-) delete mode 100644 client/react/README.md delete mode 100644 client/react/app/index.html delete mode 100644 client/react/app/js/actions/ChattypantzActions.js delete mode 100644 client/react/app/js/app.js delete mode 100644 client/react/app/js/constants/ChattypantzConstants.js delete mode 100644 client/react/app/js/dispatcher/AppDispatcher.js delete mode 100644 client/react/app/js/stores/ChattypantzStore.js diff --git a/client/react/README.md b/client/react/README.md deleted file mode 100644 index 4496fae..0000000 --- a/client/react/README.md +++ /dev/null @@ -1,34 +0,0 @@ -## ChattyPantz Client Demo - React.js - -This folder contains a simple demonstration of a client connection to the server using javascript and html. - -### Dependencies - -* You must be connected to the internet to load CDN react.js, semnatic-ui.js, and jquery.js. -* The server should be running on localhost:6660 - -### Instructions - -Assumption: server is up and running on localhost:6660. - -If needed, you can change the API host and port endpoint in scripts/services/api.js: -``` -var server = "ws://127.0.0.1:6660/v1.0/chat"; -``` -1. Load app/index.html in your browser. -2. Enter a nickname. -3. Connect to the server. - -You will be placed into room "Demo". A list of user nicknames from the room will also be displayed. -NOTE: If your nickname already exists when logging into the room, you will be warned and disconnected. - -4. Now, send your messages. -5. When you are done, disconnect from the server. - -### Enhancements - -The application doesn't demonstrate multi-room management, nor does it demonstrate the following request types: -* GET_NICKNAME: 102 -* LIST_ROOMS: 103 -* HIDE: 106 -* UNHIDE: 107 diff --git a/client/react/app/index.html b/client/react/app/index.html deleted file mode 100644 index d6b433e..0000000 --- a/client/react/app/index.html +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - Chattypantz Client Demo - - - - - - - - -
-
-

ChattyPantz Client Demo

-
-
-
- - - - - - - - - diff --git a/client/react/app/js/actions/ChattypantzActions.js b/client/react/app/js/actions/ChattypantzActions.js deleted file mode 100644 index 5d465df..0000000 --- a/client/react/app/js/actions/ChattypantzActions.js +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2014-2015, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * TodoActions - */ - -var AppDispatcher = require('../dispatcher/AppDispatcher'); -var TodoConstants = require('../constants/ChattypantzConstants'); - -var TodoActions = { - - /** - * @param {string} text - */ - create: function(text) { - AppDispatcher.dispatch({ - actionType: TodoConstants.TODO_CREATE, - text: text - }); - }, - - /** - * @param {string} id The ID of the ToDo item - * @param {string} text - */ - updateText: function(id, text) { - AppDispatcher.dispatch({ - actionType: TodoConstants.TODO_UPDATE_TEXT, - id: id, - text: text - }); - }, - - /** - * Toggle whether a single ToDo is complete - * @param {object} todo - */ - toggleComplete: function(todo) { - var id = todo.id; - var actionType = todo.complete ? - TodoConstants.TODO_UNDO_COMPLETE : - TodoConstants.TODO_COMPLETE; - - AppDispatcher.dispatch({ - actionType: actionType, - id: id - }); - }, - - /** - * Mark all ToDos as complete - */ - toggleCompleteAll: function() { - AppDispatcher.dispatch({ - actionType: TodoConstants.TODO_TOGGLE_COMPLETE_ALL - }); - }, - - /** - * @param {string} id - */ - destroy: function(id) { - AppDispatcher.dispatch({ - actionType: TodoConstants.TODO_DESTROY, - id: id - }); - }, - - /** - * Delete all the completed ToDos - */ - destroyCompleted: function() { - AppDispatcher.dispatch({ - actionType: TodoConstants.TODO_DESTROY_COMPLETED - }); - } - -}; - -module.exports = TodoActions; diff --git a/client/react/app/js/app.js b/client/react/app/js/app.js deleted file mode 100644 index bed0852..0000000 --- a/client/react/app/js/app.js +++ /dev/null @@ -1,8 +0,0 @@ -var React = require('react'); - -var TodoApp = require('./components/ChattypantzApp.react'); - -React.render( - , - document.getElementById('chattypantzapp') -); diff --git a/client/react/app/js/constants/ChattypantzConstants.js b/client/react/app/js/constants/ChattypantzConstants.js deleted file mode 100644 index 506fd64..0000000 --- a/client/react/app/js/constants/ChattypantzConstants.js +++ /dev/null @@ -1,13 +0,0 @@ -var requestType = require('requestType'); - -module.exports = requestType({ - SET_NICKNAME: 101, - GET_NICKNAME: 102, - LIST_ROOMS: 103, - JOIN: 104, - LIST_NAMES: 105, - HIDE: 106, - UNHIDE: 107, - MSG: 108, - LEAVE: 109 - }); diff --git a/client/react/app/js/dispatcher/AppDispatcher.js b/client/react/app/js/dispatcher/AppDispatcher.js deleted file mode 100644 index 5231a4e..0000000 --- a/client/react/app/js/dispatcher/AppDispatcher.js +++ /dev/null @@ -1,3 +0,0 @@ -var Dispatcher = require('flux').Dispatcher; - -module.exports = new Dispatcher(); diff --git a/client/react/app/js/stores/ChattypantzStore.js b/client/react/app/js/stores/ChattypantzStore.js deleted file mode 100644 index 5a63072..0000000 --- a/client/react/app/js/stores/ChattypantzStore.js +++ /dev/null @@ -1,165 +0,0 @@ -var AppDispatcher = require('../dispatcher/AppDispatcher'); -var EventEmitter = require('events').EventEmitter; -var ChattypantzConstants = require('../constants/ChattypantzConstants'); -var assign = require('object-assign'); - -var CHANGE_EVENT = 'change'; - -var _todos = {}; - -/** - * Create a TODO item. - * @param {string} text The content of the TODO - */ -function create(text) { - // Hand waving here -- not showing how this interacts with XHR or persistent - // server-side storage. - // Using the current timestamp + random number in place of a real id. - var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36); - _todos[id] = { - id: id, - complete: false, - text: text - }; -} - -/** - * Update a TODO item. - * @param {string} id - * @param {object} updates An object literal containing only the data to be - * updated. - */ -function update(id, updates) { - _todos[id] = assign({}, _todos[id], updates); -} - -/** - * Update all of the TODO items with the same object. - * the data to be updated. Used to mark all TODOs as completed. - * @param {object} updates An object literal containing only the data to be - * updated. - - */ -function updateAll(updates) { - for (var id in _todos) { - update(id, updates); - } -} - -/** - * Delete a TODO item. - * @param {string} id - */ -function destroy(id) { - delete _todos[id]; -} - -/** - * Delete all the completed TODO items. - */ -function destroyCompleted() { - for (var id in _todos) { - if (_todos[id].complete) { - destroy(id); - } - } -} - -var TodoStore = assign({}, EventEmitter.prototype, { - - /** - * Tests whether all the remaining TODO items are marked as completed. - * @return {boolean} - */ - areAllComplete: function() { - for (var id in _todos) { - if (!_todos[id].complete) { - return false; - } - } - return true; - }, - - /** - * Get the entire collection of TODOs. - * @return {object} - */ - getAll: function() { - return _todos; - }, - - emitChange: function() { - this.emit(CHANGE_EVENT); - }, - - /** - * @param {function} callback - */ - addChangeListener: function(callback) { - this.on(CHANGE_EVENT, callback); - }, - - /** - * @param {function} callback - */ - removeChangeListener: function(callback) { - this.removeListener(CHANGE_EVENT, callback); - } -}); - -// Register callback to handle all updates -AppDispatcher.register(function(action) { - var text; - - switch(action.actionType) { - case TodoConstants.TODO_CREATE: - text = action.text.trim(); - if (text !== '') { - create(text); - TodoStore.emitChange(); - } - break; - - case TodoConstants.TODO_TOGGLE_COMPLETE_ALL: - if (TodoStore.areAllComplete()) { - updateAll({complete: false}); - } else { - updateAll({complete: true}); - } - TodoStore.emitChange(); - break; - - case TodoConstants.TODO_UNDO_COMPLETE: - update(action.id, {complete: false}); - TodoStore.emitChange(); - break; - - case TodoConstants.TODO_COMPLETE: - update(action.id, {complete: true}); - TodoStore.emitChange(); - break; - - case TodoConstants.TODO_UPDATE_TEXT: - text = action.text.trim(); - if (text !== '') { - update(action.id, {text: text}); - TodoStore.emitChange(); - } - break; - - case TodoConstants.TODO_DESTROY: - destroy(action.id); - TodoStore.emitChange(); - break; - - case TodoConstants.TODO_DESTROY_COMPLETED: - destroyCompleted(); - TodoStore.emitChange(); - break; - - default: - // no op - } -}); - -module.exports = ChattypantzStore;