<!doctype html>
<html ng-app="angular_chat">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.4.0.js"></script>
<div pub-key="pub-c-a6378144-60dd-4b70-9d35-ce3ca44578b5" sub-key="sub-c-2ef302e0-373e-11e3-a5ee-02ee2ddab7fe" ssl="off" origin="pubsub.pubnub.com" id="pubnub"></div>
<script src="http://cdn.pubnub.com/pubnub-3.1.min.js"></script>
<script src="angular-chat.js"></script>
<link rel="stylesheet" href="style.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<div class='container-fluid' ng-controller="chatCtrl">
<div ng-include="'chatHeader.html'"></div>
<div ng-Show="!loggedIn" id="login" class="alert text-center">
<h2>Login</h2>
<label for="username">
<b>Username:</b>
</label>
<label>
<input type="text" ng-model="message.username" />
</label>
<span ng-click="attemptLogin()" class="btn">
<i class="icon-user"></i> Go Chat
</span>
</div>
<div ng-Show="loggedIn" id="chat" class="row table-responsive">
<table class="table table-bordered table-condensed table-hover table-striped">
<tr ng-repeat="chat in chatMessages">
<td style="width: 30%;">
<b>{{chat.username}}</b><br/>
<small>
{{chat.date}} @ {{chat.time}}
</small>
</td>
<td colspan="2">{{chat.text}}</td>
</tr>
<tr ng-show="chatMessages.length == 0">
<td colspan="3">No messages yet!</td>
</tr>
</table>
</div>
<form ng-Show="loggedIn" class="form-inline text-center" ui-keypress="{13:'postMessage()'}">
<div id="inputMessage" class="control-group">
<input type="text" placeholder="Say hello!" ng-model="message.text" />
<span ng-click="postMessage()" class="btn">
<i class="icon-comment"></i> Post
</span>
</div>
</form>
<div ng-include="'chatFooter.html'"></div>
</div>
</body>
</html>
/***
* File: angular-chat.js
* Author: Jade Krafsig
* Source: design1online.com, LLC
* License: GNU Public License
***/
/***
* Purpose: load bootstrap ui angular modules
* Precondition: none
* Postcondition: modules loaded
***/
angular.module('angular_chat', ['ui.bootstrap']);
/***
* Purpose: load the existing chat logs
* Precondition: none
* Postcondition: chat logs have been loaded
***/
function chatCtrl($scope, $http) {
/***
* Configurable global variables
***/
$scope.chatChannel = "angular_chat";
$scope.messageLimit = 50;
$scope.defaultUsername = "Guest";
/***
* Static global variables
***/
$scope.loggedIn = false;
$scope.errorMsg;
$scope.realtimeStatus = 0;
/***
* Purpose: clear the message object
* Precondition: none
* Postcondition: message object has been reset
***/
$scope.clearMsg = function() {
$scope.message = {
username: $scope.defaultUsername,
email: 'n/a',
text: ''
};
}
$scope.clearMsg();
/***
* Purpose: load the existing chat logs
* Precondition: none
* Postcondition: chat logs have been loaded
***/
$scope.chatLogs = function() {
PUBNUB.history( {
channel : $scope.chatChannel,
limit : $scope.messageLimit
}, function(messages) {
// Shows All Messages
$scope.$apply(function(){
$scope.chatMessages = messages.reverse();
});
});
}
/***
* Purpose: load the existing chat logs
* Precondition: none
* Postcondition: chat logs have been loaded
***/
$scope.attemptLogin = function() {
$scope.errorMsg = "";
if (!$scope.message.username) {
$scope.errorMsg = "You must enter a username.";
return;
}
if (!$scope.realtimeStatus) {
$scope.errorMsg = "You're not connect to PubNub.";
return;
}
$scope.loggedIn = true;
}
/***
* Purpose: remove error message formatting when the message input changes
* Precondition: none
* Postcondition: error message class removed from message input
***/
$scope.$watch('message.text', function(newValue, oldValue) {
if (newValue)
$("#inputMessage").removeClass("error");
$scope.errorMsg = "";
}, true);
/***
* Purpose: trying to post a message to the chat
* Precondition: loggedIn
* Postcondition: message added to chatMessages and sent to chatLog
***/
$scope.postMessage = function() {
//make sure they are logged in
if (!$scope.loggedIn) {
$scope.errorMsg = "You must login first.";
return;
}
//make sure they enter a chat message
if (!$scope.message.text) {
$scope.errorMsg = "You must enter a message.";
$("#inputMessage").addClass("error");
return;
}
//set the message date
d = new Date();
$scope.message.date = d.getDay() + "/" + d.getMonth() + "/" + d.getFullYear();
$scope.message.time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
PUBNUB.publish({
channel : $scope.chatChannel,
message : $scope.message
});
$scope.message.text = "";
};
/***
* Purpose: connect and access pubnub channel
* Preconditions: pubnub js file init
* Postconditions: pubnub is waiting and ready
***/
PUBNUB.subscribe({
channel : $scope.chatChannel,
restore : false,
callback : function(message) {
//update messages with the new message
$scope.$apply(function(){
$scope.chatMessages.unshift(message);
});
},
error : function(data) {
$scope.errorMsg = data;
},
disconnect : function() {
$scope.$apply(function(){
$scope.realtimeStatus = 0;
});
},
reconnect : function() {
$scope.$apply(function(){
$scope.realtimeStatus = 1;
});
},
connect : function() {
$scope.$apply(function(){
$scope.realtimeStatus = 2;
//load the chat logs
$scope.chatLogs();
});
}
});
/***
* Purpose: trying to post a message to the chat
* Precondition: loggedIn
* Postcondition: message added to chatMessages and sent to chatLog
***/
$scope.attemptLogout = function() {
$("#inputMessage").removeClass("error");
$scope.clearMsg();
$scope.loggedIn = false;
}
}
#chat {
height: 33em;
overflow: auto;
background-color: #EEE;
border-radius: .25em;
margin-bottom: 1em;
}
.status {
float: left;
margin-top: -1em;
}
#logout {
float: right;
margin-top: -1.2em;
}
<div class="text-center">
2013 © design1online.com
</div>
<div class="text-center">
<h1>Angular Chat</h1>
<h5><i class="icon-info-sign"></i> using Bootstrap and PubNub</h5>
<p> </p>
<div ng-show="realtimeStatus == 0" class="status">
<span class="alert alert-error"><i class="icon-download"></i> Disconnected</span>
</div>
<div ng-show="realtimeStatus == 1" class="status">
<span class="alert alert-info"><i class="icon-refresh"></i> Connecting...</span>
</div>
<div ng-show="realtimeStatus == 2" class="status">
<span class="alert alert-success"><i class="icon-upload"></i> Connected</span>
</div>
<div ng-show="loggedIn" id="logout">
<span class="btn" ng-click="attemptLogout()"><i class="icon-off"></i> Logout</span>
</div>
</div>
<p> </p>
<div ng-show="errorMsg" class="alert alert-error">
<i class="icon-warning-sign"></i> <b>Error:</b> {{errorMsg}}
</div>
This is a test, this is only a test!!
*** WORKING BASIC ANGULAR CHAT ***
** Features Include **
* Login/logout
* PubNub messaging (no server or database required)
* Uses bootstrap
* Realtime chatting
See the tutorial at http://jadendreamer.wordpress.com