Skip to content

Commit

Permalink
Merge pull request #4 from naiky/master
Browse files Browse the repository at this point in the history
Migration
  • Loading branch information
fcroiseaux committed Nov 4, 2013
2 parents b57ffb0 + 77a28c8 commit 9d7f987
Show file tree
Hide file tree
Showing 16 changed files with 750 additions and 575 deletions.
4 changes: 4 additions & 0 deletions app/assets/javascripts/app.js
@@ -0,0 +1,4 @@
'use strict';

/** app level module which depends on services and controllers */
angular.module('liveChat', ['liveChat.controllers', 'ui.bootstrap']);
120 changes: 120 additions & 0 deletions app/assets/javascripts/controllers.js
@@ -0,0 +1,120 @@
'use strict';

/** Controllers */
angular.module('liveChat.controllers', []).
controller('ChatCtrl', function ($scope, $dialog, $http) {
$scope.messages = [];
$scope.inputText = "";
$scope.users = [];
$scope.currentMessage = "";
$scope.currentUser ="";
$scope.title= "toute l'actualité IT au Luxembourg";


$scope.connect = function (username) {
var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket;
// $scope.chatSocket = new WS("ws://localhost:9000/room/chat/" + username);

var url = jsRoutes.controllers.Application.chat(username).absoluteURL().replace("http://","ws://")
$scope.chatSocket = new WS(url);
$scope.chatSocket.onmessage = $scope.receiveEvent;

$http.get('/username/' + username).success(function(data, status, headers, config) {
$scope.currentUser = data;
});
};

$scope.sendText = function (textMsg) {
$scope.chatSocket.send(JSON.stringify(
{text: textMsg}
))
}

$scope.receiveEvent = function (event) {
var data = JSON.parse(event.data)

// Handle errors
if (data.error) {
chatSocket.close()
$("#onError span").text(data.error)
$("#onError").show();
return
} else {
$("#onChat").show();
}

var now = new Date();
var min = now.getMinutes() + "";
if (min.length == 1) {
min = "0" + min;
}
data.time = now.getHours() + ':' + min;
$scope.messages.push(data);
$scope.users = data.members;
$scope.$apply();
}


var alreadyExist = function (user) {
var resp = false
$($scope.users).each(function () {
if (this == user)
resp = true
})
return resp
}

$scope.changeUsername = function (username) {
if (username.indexOf("<") != -1) {
showErrorMsg("Si tu veux programmer en HTML, aide nous plutôt à faire évoluer le chat")
} else if (username.indexOf("@@") != -1) {
showErrorMsg("Le caractère @@ est réservé pour les comptes twitters")
} else if (alreadyExist(username)) {
showErrorMsg("Utilisateur déjà existant")
} else if (username.trim() == "") {
showErrorMsg("Merci de saisir un nom d'utilisateur")
} else {
myname = username
$scope.sendText("nickname:" + username)
$scope.currentUser = username;
}
}

$scope.setUserStyle = function(username) {
if (username == $scope.currentUser) {
return "{color : 'blue';}";
} else {
return "{}";
}
}

$scope.openChangeUsernameDlg = function () {
var dialogOptions = {
modal: true,
fade: true,
backdrop: true,
keyboard: true,
backdropClick: true,
templateUrl: '/changeuser',
controller: 'ChangeNameDlgCtrl'
};

var d = $dialog.dialog(dialogOptions);
d.open().then(function (username) {
if (username) {
$scope.changeUsername(username);
}
});
}

var myname = '';


});


function ChangeNameDlgCtrl($scope, dialog) {
$scope.close = function(result) {
dialog.close(result);
};
}
57 changes: 57 additions & 0 deletions app/assets/javascripts/directives.js
@@ -0,0 +1,57 @@
'use strict';

function fakeNgModel(initValue){
return {
$setViewValue: function(value){
this.$viewValue = value;
},
$viewValue: initValue
};
}


angular.module('liveChat').directive('ngEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if (event.which === 13) {
scope.$apply(function () {
scope.$eval(attrs.ngEnter);
});

event.preventDefault();
element.val('');
}
});
};
});


angular.module('liveChat').directive('scrollGlue', function () {
return {
priority: 1,
require: ['?ngModel'],
restrict: 'A',
link: function (scope, $el, attrs, ctrls) {
var el = $el[0],
ngModel = ctrls[0] || fakeNgModel(true);

function scrollToBottom() {
el.scrollTop = el.scrollHeight;
}

function shouldActivateAutoScroll() {
return el.scrollTop + el.clientHeight == el.scrollHeight;
}

scope.$watch(function () {
if (ngModel.$viewValue) {
scrollToBottom();
}
});

/* $el.bind('scroll', function () {
scope.$apply(ngModel.$setViewValue.bind(ngModel, shouldActivateAutoScroll()));
}); */
}
};
});

0 comments on commit 9d7f987

Please sign in to comment.