mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
-Refactored socket state to use growl.
This commit is contained in:
parent
9765a5759e
commit
dd38b478de
10 changed files with 95 additions and 70 deletions
|
@ -6,4 +6,6 @@ require('angular-growl-v2/build/angular-growl.js')
|
||||||
module.exports = angular.module('stf/common-ui/notifications', [
|
module.exports = angular.module('stf/common-ui/notifications', [
|
||||||
'ngAnimate',
|
'ngAnimate',
|
||||||
'angular-growl'
|
'angular-growl'
|
||||||
])
|
]).config(['growlProvider', function (growlProvider) {
|
||||||
|
growlProvider.globalEnableHtml(true)
|
||||||
|
}])
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
module.exports = angular.module('stf/socket', [])
|
module.exports = angular.module('stf/socket', [])
|
||||||
.factory('socket', require('./socket-service'))
|
.factory('socket', require('./socket-service'))
|
||||||
.directive('socketState', require('./socket-state-directive'))
|
|
||||||
|
|
|
@ -24,53 +24,5 @@ module.exports = function SocketFactory($rootScope) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('connect', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'connect'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('connecting', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'connecting'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('disconnect', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'disconnect'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('connect_failed', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'connect_failed'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('error', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'error'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('reconnect_failed', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'reconnect_failed'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('reconnect', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'reconnect'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('reconnecting', function() {
|
|
||||||
$rootScope.$apply(function() {
|
|
||||||
socket.state = 'reconnecting'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return socket
|
return socket
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
module.exports = function SocketStateDirectiveFactory(socket) {
|
|
||||||
return {
|
|
||||||
restrict: 'E'
|
|
||||||
, template: require('./socket-state.jade')
|
|
||||||
, scope: {
|
|
||||||
}
|
|
||||||
, link: function (scope) {
|
|
||||||
scope.$watch(
|
|
||||||
function() {
|
|
||||||
return socket.state
|
|
||||||
}
|
|
||||||
, function(state) {
|
|
||||||
scope.state = state
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
div {{ state }}
|
|
6
res/app/components/stf/socket/socket-state/index.js
Normal file
6
res/app/components/stf/socket/socket-state/index.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module.exports = angular.module('stf/socket/socket-state', [
|
||||||
|
require('stf/socket').name,
|
||||||
|
require('stf/common-ui/notifications').name
|
||||||
|
])
|
||||||
|
.directive('socketState', require('./socket-state-directive'))
|
||||||
|
.controller('SocketStateCtrl', require('./socket-state-controller'))
|
|
@ -0,0 +1,75 @@
|
||||||
|
module.exports = function ($scope, $element, $attrs, $transclude, socket, growl, gettext) {
|
||||||
|
var hasFailedOnce = false
|
||||||
|
|
||||||
|
socket.on('connect', function () {
|
||||||
|
$scope.socketState = 'connect'
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('connecting', function () {
|
||||||
|
$scope.socketState = 'connecting'
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('disconnect', function () {
|
||||||
|
$scope.socketState = 'disconnect'
|
||||||
|
hasFailedOnce = true
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('connect_failed', function () {
|
||||||
|
$scope.socketState = 'connect_failed'
|
||||||
|
hasFailedOnce = true
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('error', function () {
|
||||||
|
$scope.socketState = 'error'
|
||||||
|
hasFailedOnce = true
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('reconnect_failed', function () {
|
||||||
|
$scope.socketState = 'reconnect_failed'
|
||||||
|
hasFailedOnce = true
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('reconnect', function () {
|
||||||
|
$scope.socketState = 'reconnect'
|
||||||
|
hasFailedOnce = true
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('reconnecting', function () {
|
||||||
|
$scope.socketState = 'reconnecting'
|
||||||
|
hasFailedOnce = true
|
||||||
|
})
|
||||||
|
|
||||||
|
$scope.$watch('socketState', function (newValue, oldValue) {
|
||||||
|
console.log(newValue)
|
||||||
|
if (newValue) {
|
||||||
|
if (newValue === 'connecting' && oldValue) {
|
||||||
|
growl.info(gettext('<h4>WebSocket</h4> Connecting...'), {ttl: 1000})
|
||||||
|
} else if (newValue === 'connect' && oldValue === 'connecting') {
|
||||||
|
if (hasFailedOnce) {
|
||||||
|
growl.success(gettext('<h4>WebSocket</h4> Connected successfully.'), {ttl: 2000})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (newValue) {
|
||||||
|
case 'disconnect':
|
||||||
|
growl.info(gettext('<h4>WebSocket</h4> Disconnected.'), {ttl: 2000})
|
||||||
|
break;
|
||||||
|
case 'connect_failed':
|
||||||
|
growl.error(gettext('<h4>WebSocket</h4> Error while connecting.'), {ttl: 2000})
|
||||||
|
break;
|
||||||
|
case 'error':
|
||||||
|
growl.error(gettext('<h4>WebSocket</h4> Error.'), {ttl: 2000})
|
||||||
|
break;
|
||||||
|
case 'reconnect_failed':
|
||||||
|
growl.error(gettext('<h4>WebSocket</h4> Error while reconnecting.'), {ttl: 2000})
|
||||||
|
break;
|
||||||
|
case 'reconnect':
|
||||||
|
growl.success(gettext('<h4>WebSocket</h4> Reconnected successfully.'), {ttl: 2000})
|
||||||
|
break;
|
||||||
|
case 'reconnecting':
|
||||||
|
growl.info(gettext('<h4>WebSocket</h4> Reconnecting...'), {ttl: 1000})
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
module.exports = function SocketStateDirectiveFactory() {
|
||||||
|
return {
|
||||||
|
restrict: 'EA',
|
||||||
|
template: require('./socket-state.jade'),
|
||||||
|
controller: 'SocketStateCtrl',
|
||||||
|
scope: {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ require('./small.css')
|
||||||
|
|
||||||
module.exports = angular.module('layout', [
|
module.exports = angular.module('layout', [
|
||||||
'fa.directive.borderLayout',
|
'fa.directive.borderLayout',
|
||||||
require('stf/common-ui').name
|
require('stf/common-ui').name,
|
||||||
|
require('stf/socket/socket-state').name
|
||||||
])
|
])
|
||||||
.controller('LayoutCtrl', require('./layout-controller'))
|
.controller('LayoutCtrl', require('./layout-controller'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue