This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): Modify chat module to implement johnpapa styleguide.
- Loading branch information
1 parent
b12be5f
commit 5d15e64
Showing
6 changed files
with
96 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
'use strict'; | ||
(function (app) { | ||
'use strict'; | ||
|
||
// Use Applicaion configuration module to register a new module | ||
ApplicationConfiguration.registerModule('chat'); | ||
app.registerModule('chat'); | ||
app.registerModule('chat.routes', ['ui.router']); | ||
})(ApplicationConfiguration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
'use strict'; | ||
(function () { | ||
'use strict'; | ||
|
||
// Configuring the Chat module | ||
angular.module('chat').run(['Menus', | ||
function (Menus) { | ||
angular | ||
.module('chat') | ||
.run(menuConfig); | ||
|
||
menuConfig.$inject = ['Menus']; | ||
|
||
function menuConfig(Menus) { | ||
// Set top bar menu items | ||
Menus.addMenuItem('topbar', { | ||
title: 'Chat', | ||
state: 'chat' | ||
}); | ||
} | ||
]); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
'use strict'; | ||
(function () { | ||
'use strict'; | ||
|
||
// Configure the 'chat' module routes | ||
angular.module('chat').config(['$stateProvider', | ||
function ($stateProvider) { | ||
angular | ||
.module('chat.routes') | ||
.config(routeConfig); | ||
|
||
routeConfig.$inject = ['$stateProvider']; | ||
|
||
function routeConfig($stateProvider) { | ||
$stateProvider | ||
.state('chat', { | ||
url: '/chat', | ||
templateUrl: 'modules/chat/client/views/chat.client.view.html', | ||
controller: 'ChatController', | ||
controllerAs: 'vm', | ||
data: { | ||
roles: ['user', 'admin'] | ||
} | ||
}); | ||
} | ||
]); | ||
})(); |
68 changes: 40 additions & 28 deletions
68
modules/chat/client/controllers/chat.client.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
'use strict'; | ||
(function () { | ||
'use strict'; | ||
|
||
// Create the 'chat' controller | ||
angular.module('chat').controller('ChatController', ['$scope', '$location', 'Authentication', 'Socket', | ||
function ($scope, $location, Authentication, Socket) { | ||
// Create a messages array | ||
$scope.messages = []; | ||
angular | ||
.module('chat') | ||
.controller('ChatController', ChatController); | ||
|
||
// If user is not signed in then redirect back home | ||
if (!Authentication.user) { | ||
$location.path('/'); | ||
} | ||
ChatController.$inject = ['$scope', '$state', 'Authentication', 'Socket']; | ||
|
||
// Make sure the Socket is connected | ||
if (!Socket.socket) { | ||
Socket.connect(); | ||
} | ||
function ChatController($scope, $state, Authentication, Socket) { | ||
var vm = this; | ||
|
||
vm.messages = []; | ||
vm.messageText = ''; | ||
vm.sendMessage = sendMessage; | ||
|
||
init(); | ||
|
||
function init() { | ||
// If user is not signed in then redirect back home | ||
if (!Authentication.user) { | ||
$state.go('home'); | ||
} | ||
|
||
// Add an event listener to the 'chatMessage' event | ||
Socket.on('chatMessage', function (message) { | ||
$scope.messages.unshift(message); | ||
}); | ||
// Make sure the Socket is connected | ||
if (!Socket.socket) { | ||
Socket.connect(); | ||
} | ||
|
||
// Add an event listener to the 'chatMessage' event | ||
Socket.on('chatMessage', function (message) { | ||
vm.messages.unshift(message); | ||
}); | ||
|
||
// Remove the event listener when the controller instance is destroyed | ||
$scope.$on('$destroy', function () { | ||
Socket.removeListener('chatMessage'); | ||
}); | ||
} | ||
|
||
// Create a controller method for sending messages | ||
$scope.sendMessage = function () { | ||
function sendMessage() { | ||
// Create a new message object | ||
var message = { | ||
text: this.messageText | ||
text: vm.messageText | ||
}; | ||
|
||
// Emit a 'chatMessage' message event | ||
Socket.emit('chatMessage', message); | ||
|
||
// Clear the message text | ||
this.messageText = ''; | ||
}; | ||
|
||
// Remove the event listener when the controller instance is destroyed | ||
$scope.$on('$destroy', function () { | ||
Socket.removeListener('chatMessage'); | ||
}); | ||
vm.messageText = ''; | ||
} | ||
} | ||
]); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters