Skip to content

Commit

Permalink
Replaced $timeout by $interval to allow tests with protractor. See th…
Browse files Browse the repository at this point in the history
…e default issue here : angular/protractor#169
  • Loading branch information
Cédric Mourouvin committed Jul 31, 2014
1 parent 717a171 commit 9c58d3a
Showing 1 changed file with 140 additions and 134 deletions.
274 changes: 140 additions & 134 deletions toaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@
* AngularJS Toaster
* Version: 0.4.7
*
* Copyright 2013 Jiri Kavulak.
* All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* Copyright 2013 Jiri Kavulak.
* All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
*
* Author: Jiri Kavulak
* Related to project of John Papa and Hans Fjällemark
*/

angular.module('toaster', ['ngAnimate'])
.service('toaster', ['$rootScope', function ($rootScope) {
this.pop = function (type, title, body, timeout, bodyOutputType, clickHandler) {
.service('toaster', ['$rootScope',
function($rootScope) {
this.pop = function(type, title, body, timeout, bodyOutputType, clickHandler) {
this.toast = {
type: type,
title: title,
body: body,
timeout: timeout,
bodyOutputType: bodyOutputType,
clickHandler: clickHandler
type: type,
title: title,
body: body,
timeout: timeout,
bodyOutputType: bodyOutputType,
clickHandler: clickHandler
};
$rootScope.$broadcast('toaster-newToast');
};
};

this.clear = function () {
this.clear = function() {
$rootScope.$broadcast('toaster-clearToasts');
};
}])
.constant('toasterConfig', {
'limit': 0, // limits max number of toasts
};
}
])
.constant('toasterConfig', {
'limit': 0, // limits max number of toasts
'tap-to-dismiss': true,
'close-button': false,
'newest-on-top': true,
Expand All @@ -43,143 +45,147 @@ angular.module('toaster', ['ngAnimate'])
//'extended-time-out': 1000, // not implemented
'time-out': 5000, // Set timeOut and extendedTimeout to 0 to make it sticky
'icon-classes': {
error: 'toast-error',
info: 'toast-info',
wait: 'toast-wait',
success: 'toast-success',
warning: 'toast-warning'
error: 'toast-error',
info: 'toast-info',
wait: 'toast-wait',
success: 'toast-success',
warning: 'toast-warning'
},
'body-output-type': '', // Options: '', 'trustedHtml', 'template'
'body-template': 'toasterBodyTmpl.html',
'icon-class': 'toast-info',
'position-class': 'toast-top-right',
'title-class': 'toast-title',
'message-class': 'toast-message'
})
.directive('toasterContainer', ['$compile', '$timeout', '$sce', 'toasterConfig', 'toaster',
function ($compile, $timeout, $sce, toasterConfig, toaster) {
return {
})
.directive('toasterContainer', ['$compile', '$interval', '$sce', 'toasterConfig', 'toaster',
function($compile, $interval, $sce, toasterConfig, toaster) {
return {
replace: true,
restrict: 'EA',
scope: true, // creates an internal scope for this directive
link: function (scope, elm, attrs) {

var id = 0,
mergedConfig;

mergedConfig = angular.extend({}, toasterConfig, scope.$eval(attrs.toasterOptions));

scope.config = {
position: mergedConfig['position-class'],
title: mergedConfig['title-class'],
message: mergedConfig['message-class'],
tap: mergedConfig['tap-to-dismiss'],
closeButton: mergedConfig['close-button']
};

scope.configureTimer = function configureTimer(toast) {
var timeout = typeof (toast.timeout) == "number" ? toast.timeout : mergedConfig['time-out'];
if (timeout > 0)
setTimeout(toast, timeout);
};

function addToast(toast) {
toast.type = mergedConfig['icon-classes'][toast.type];
if (!toast.type)
toast.type = mergedConfig['icon-class'];

id++;
angular.extend(toast, { id: id });

// Set the toast.bodyOutputType to the default if it isn't set
toast.bodyOutputType = toast.bodyOutputType || mergedConfig['body-output-type'];
switch (toast.bodyOutputType) {
case 'trustedHtml':
toast.html = $sce.trustAsHtml(toast.body);
break;
case 'template':
toast.bodyTemplate = toast.body || mergedConfig['body-template'];
break;
}

scope.configureTimer(toast);
link: function(scope, elm, attrs) {

var id = 0,
mergedConfig;

mergedConfig = angular.extend({}, toasterConfig, scope.$eval(attrs.toasterOptions));

scope.config = {
position: mergedConfig['position-class'],
title: mergedConfig['title-class'],
message: mergedConfig['message-class'],
tap: mergedConfig['tap-to-dismiss'],
closeButton: mergedConfig['close-button']
};

scope.configureTimer = function configureTimer(toast) {
var timeout = typeof(toast.timeout) == "number" ? toast.timeout : mergedConfig['time-out'];
if (timeout > 0)
setTimeout(toast, timeout);
};

function addToast(toast) {
toast.type = mergedConfig['icon-classes'][toast.type];
if (!toast.type)
toast.type = mergedConfig['icon-class'];

id++;
angular.extend(toast, {
id: id
});

if (mergedConfig['newest-on-top'] === true) {
scope.toasters.unshift(toast);
if (mergedConfig['limit'] > 0 && scope.toasters.length > mergedConfig['limit']) {
scope.toasters.pop();
}
} else {
scope.toasters.push(toast);
if (mergedConfig['limit'] > 0 && scope.toasters.length > mergedConfig['limit']) {
scope.toasters.shift();
}
}
// Set the toast.bodyOutputType to the default if it isn't set
toast.bodyOutputType = toast.bodyOutputType || mergedConfig['body-output-type'];
switch (toast.bodyOutputType) {
case 'trustedHtml':
toast.html = $sce.trustAsHtml(toast.body);
break;
case 'template':
toast.bodyTemplate = toast.body || mergedConfig['body-template'];
break;
}

function setTimeout(toast, time) {
toast.timeout = $timeout(function () {
scope.removeToast(toast.id);
}, time);
scope.configureTimer(toast);

if (mergedConfig['newest-on-top'] === true) {
scope.toasters.unshift(toast);
if (mergedConfig['limit'] > 0 && scope.toasters.length > mergedConfig['limit']) {
scope.toasters.pop();
}
} else {
scope.toasters.push(toast);
if (mergedConfig['limit'] > 0 && scope.toasters.length > mergedConfig['limit']) {
scope.toasters.shift();
}
}

scope.toasters = [];
scope.$on('toaster-newToast', function () {
addToast(toaster.toast);
});

scope.$on('toaster-clearToasts', function () {
scope.toasters.splice(0, scope.toasters.length);
});
}

function setTimeout(toast, time) {
toast.timeout = $interval(function() {
scope.removeToast(toast.id);
}, time, 1);
}

scope.toasters = [];
scope.$on('toaster-newToast', function() {
addToast(toaster.toast);
});

scope.$on('toaster-clearToasts', function() {
scope.toasters.splice(0, scope.toasters.length);
});
},
controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {

$scope.stopTimer = function (toast) {
if (toast.timeout) {
$timeout.cancel(toast.timeout);
toast.timeout = null;
}
controller: ['$scope', '$element', '$attrs',
function($scope, $element, $attrs) {

$scope.stopTimer = function(toast) {
if (toast.timeout) {
$interval.cancel(toast.timeout);
toast.timeout = null;
}
};

$scope.restartTimer = function (toast) {
if (!toast.timeout)
$scope.configureTimer(toast);
$scope.restartTimer = function(toast) {
if (!toast.timeout)
$scope.configureTimer(toast);
};

$scope.removeToast = function (id) {
var i = 0;
for (i; i < $scope.toasters.length; i++) {
if ($scope.toasters[i].id === id)
break;
}
$scope.toasters.splice(i, 1);
$scope.removeToast = function(id) {
var i = 0;
for (i; i < $scope.toasters.length; i++) {
if ($scope.toasters[i].id === id)
break;
}
$scope.toasters.splice(i, 1);
};

$scope.click = function (toaster) {
if ($scope.config.tap === true) {
if (toaster.clickHandler && angular.isFunction($scope.$parent.$eval(toaster.clickHandler))) {
var result = $scope.$parent.$eval(toaster.clickHandler)(toaster);
if (result === true)
$scope.removeToast(toaster.id);
} else {
if (angular.isString(toaster.clickHandler))
console.log("TOAST-NOTE: Your click handler is not inside a parent scope of toaster-container.");
$scope.removeToast(toaster.id);
}
$scope.click = function(toaster) {
if ($scope.config.tap === true) {
if (toaster.clickHandler && angular.isFunction($scope.$parent.$eval(toaster.clickHandler))) {
var result = $scope.$parent.$eval(toaster.clickHandler)(toaster);
if (result === true)
$scope.removeToast(toaster.id);
} else {
if (angular.isString(toaster.clickHandler))
console.log("TOAST-NOTE: Your click handler is not inside a parent scope of toaster-container.");
$scope.removeToast(toaster.id);
}
}
};
}],
template:
'<div id="toast-container" ng-class="config.position">' +
'<div ng-repeat="toaster in toasters" class="toast" ng-class="toaster.type" ng-click="click(toaster)" ng-mouseover="stopTimer(toaster)" ng-mouseout="restartTimer(toaster)">' +
'<button class="toast-close-button" ng-show="config.closeButton">&times;</button>' +
'<div ng-class="config.title">{{toaster.title}}</div>' +
'<div ng-class="config.message" ng-switch on="toaster.bodyOutputType">' +
'<div ng-switch-when="trustedHtml" ng-bind-html="toaster.html"></div>' +
'<div ng-switch-when="template"><div ng-include="toaster.bodyTemplate"></div></div>' +
'<div ng-switch-default >{{toaster.body}}</div>' +
'</div>' +
'</div>' +
'</div>'
};
}]);
}
],
template: '<div id="toast-container" ng-class="config.position">' +
'<div ng-repeat="toaster in toasters" class="toast" ng-class="toaster.type" ng-click="click(toaster)" ng-mouseover="stopTimer(toaster)" ng-mouseout="restartTimer(toaster)">' +
'<button class="toast-close-button" ng-show="config.closeButton">&times;</button>' +
'<div ng-class="config.title">{{toaster.title}}</div>' +
'<div ng-class="config.message" ng-switch on="toaster.bodyOutputType">' +
'<div ng-switch-when="trustedHtml" ng-bind-html="toaster.html"></div>' +
'<div ng-switch-when="template"><div ng-include="toaster.bodyTemplate"></div></div>' +
'<div ng-switch-default >{{toaster.body}}</div>' +
'</div>' +
'</div>' +
'</div>'
};
}
]);

0 comments on commit 9c58d3a

Please sign in to comment.