(function() {
'use strict';
angular
.module('misiu.providers', [])
.provider('simpleConsole', function() {
var events = [];
var separator = '::'; //default
//public methods
this.$get = function() {
return {
addLog: function(data) {
addEvent('log', 'Log' + separator + data);
},
addError: function(data) {
addEvent('error', 'Error' + separator + data);
},
clear: function() {
events.length = 0;//why events=[] is not working
},
events: events
};
};
//private method
var addEvent = function(type, data) {
if (events.length > 0 && events[0].type === type && events[0].data === data) {
events[0].count++;
} else {
events.unshift({
type: type,
data: data,
count: 1
});
}
};
//this is available only in config
this.setSeparator = function(value) {
separator = value;
};
});
angular
.module('misiu.directives', ['misiu.providers'])
.directive('simpleConsole', ['simpleConsole', function(simpleConsole) {
return {
restrict: 'E',
template: [
'<div class="console animated">',
'<div ng-repeat="event in events" ng-class="{\'error\': event.type == \'error\', \'log\': event.type == \'log\'}">',
'{{event.data}}<span ng-if="event.count>1" class="count">{{event.count}}</span>',
'</div>',
'</div>'
].join(''),
link: function($scope, element) {
$scope.events = simpleConsole.events;
}
};
}]);
})();