From 4836cf83e21e323dcef99ff70857c21b6ec394ff Mon Sep 17 00:00:00 2001 From: Jermolene Date: Fri, 14 Feb 2014 10:29:26 +0000 Subject: [PATCH] Enhance alerting to coalesce repeated alerts with the same text --- core/modules/utils/logger.js | 36 +++++++++++++++++++++++++----- core/ui/AlertTemplate.tid | 3 ++- themes/tiddlywiki/vanilla/base.tid | 3 +++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/core/modules/utils/logger.js b/core/modules/utils/logger.js index bf320ae42..f89e40304 100644 --- a/core/modules/utils/logger.js +++ b/core/modules/utils/logger.js @@ -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)); }; diff --git a/core/ui/AlertTemplate.tid b/core/ui/AlertTemplate.tid index b9242f2e3..234a4d29a 100644 --- a/core/ui/AlertTemplate.tid +++ b/core/ui/AlertTemplate.tid @@ -1,3 +1,4 @@ title: $:/core/ui/AlertTemplate -
<$button message="tw-delete-tiddler" class="btn-invisible">{{$:/core/images/delete-button}}
<$view field="component"/> - <$view field="modified" format="date" template="0hh:0mm:0ss DD MM YYYY"/>
<$view field="text"/>
\ No newline at end of file +
<$button message="tw-delete-tiddler" class="btn-invisible">{{$:/core/images/delete-button}}
<$view field="component"/> - <$view field="modified" format="date" template="0hh:0mm:0ss DD MM YYYY"/> <$reveal type="nomatch" state="!!count" text="">(count: <$view field="count"/>)
<$view field="text"/>
+ diff --git a/themes/tiddlywiki/vanilla/base.tid b/themes/tiddlywiki/vanilla/base.tid index ca48710ab..fab3e6315 100644 --- a/themes/tiddlywiki/vanilla/base.tid +++ b/themes/tiddlywiki/vanilla/base.tid @@ -950,6 +950,9 @@ canvas.tw-edit-bitmapeditor { font-weight: bold; } +.tw-alert-highlight { + color: #812; +} .tw-static-alert { position: relative;