Enhance alerting to coalesce repeated alerts with the same text

This commit is contained in:
Jermolene 2014-02-14 10:29:26 +00:00
parent 44678b2ea2
commit 4836cf83e2
3 changed files with 35 additions and 7 deletions

View File

@ -12,6 +12,8 @@ A basic logging implementation
/*global $tw: false */
"use strict";
var ALERT_TAG = "$:/tags/Alert";
/*
Make a new logger
*/
@ -32,15 +34,37 @@ Logger.prototype.log = function(/* args */) {
Alert a message
*/
Logger.prototype.alert = function(/* args */) {
var text = Array.prototype.join.call(arguments," "),
fields = {
// Prepare the text of the alert
var text = Array.prototype.join.call(arguments," ");
// Check if there is an existing alert with the same text and the same component
var existingAlerts = $tw.wiki.getTiddlersWithTag(ALERT_TAG),
alertFields,
existingCount,
self = this;
$tw.utils.each(existingAlerts,function(title) {
var tiddler = $tw.wiki.getTiddler(title);
if(tiddler.fields.text === text && tiddler.fields.component === self.componentName && tiddler.fields.modified && (!alertFields || tiddler.fields.modified < alertFields.modified)) {
alertFields = $tw.utils.extend({},tiddler.fields);
}
});
if(alertFields) {
existingCount = alertFields.count || 1;
} else {
alertFields = {
title: $tw.wiki.generateNewTitle("$:/temp/alerts/alert",{prefix: ""}),
text: text,
tags: ["$:/tags/Alert"],
component: this.componentName,
modified: new Date()
tags: [ALERT_TAG],
component: this.componentName
};
$tw.wiki.addTiddler(new $tw.Tiddler(fields));
existingCount = 0;
}
alertFields.modified = new Date();
if(++existingCount > 1) {
alertFields.count = existingCount;
} else {
alertFields.count = undefined;
}
$tw.wiki.addTiddler(new $tw.Tiddler(alertFields));
// Log it too
this.log.apply(this,Array.prototype.slice.call(arguments,0));
};

View File

@ -1,3 +1,4 @@
title: $:/core/ui/AlertTemplate
<div class="tw-alert"><div class="tw-alert-toolbar"><$button message="tw-delete-tiddler" class="btn-invisible">{{$:/core/images/delete-button}}</$button></div><div class="tw-alert-subtitle"><$view field="component"/> - <$view field="modified" format="date" template="0hh:0mm:0ss DD MM YYYY"/></div><div class="tw-alert-body"><$view field="text"/></div></div>
<div class="tw-alert"><div class="tw-alert-toolbar"><$button message="tw-delete-tiddler" class="btn-invisible">{{$:/core/images/delete-button}}</$button></div><div class="tw-alert-subtitle"><$view field="component"/> - <$view field="modified" format="date" template="0hh:0mm:0ss DD MM YYYY"/> <$reveal type="nomatch" state="!!count" text=""><span class="tw-alert-highlight">(count: <$view field="count"/>)</span></$reveal></div><div class="tw-alert-body"><$view field="text"/></div></div>

View File

@ -950,6 +950,9 @@ canvas.tw-edit-bitmapeditor {
font-weight: bold;
}
.tw-alert-highlight {
color: #812;
}
.tw-static-alert {
position: relative;