From 703fcb16684ecfba00ab5d98734ca698c5252f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Fri, 8 Aug 2014 14:39:50 +0100 Subject: [PATCH] Add support for asynchronous / lazy plugins Fixes #253 --- js/glowingbear.js | 6 ++++++ js/plugins.js | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index a1ac3d8..0efde02 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -1343,6 +1343,12 @@ weechat.directive('plugin', function($rootScope) { * Actual plugin content is only fetched when * content is shown. */ + + // If the plugin is asynchronous / lazy, execute it now and store + // the result. This ensures that the callback is executed only once + if ($scope.plugin.content instanceof Function) { + $scope.plugin.content = $scope.plugin.content(); + } $scope.displayedContent = $scope.plugin.content; $scope.plugin.visible = true; diff --git a/js/plugins.js b/js/plugins.js index afd31af..16a4545 100644 --- a/js/plugins.js +++ b/js/plugins.js @@ -60,8 +60,14 @@ plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) { if (num) { pluginName += " " + num; } + + // If content isn't a callback, it's HTML + if (!(content instanceof Function)) { + content = $sce.trustAsHtml(content); + } + message.metadata.push({ - 'content': $sce.trustAsHtml(content), + 'content': content, 'nsfw': nsfw, 'name': pluginName });