From f900e6451cdfb67ee2c497207075c7ea5c7395b9 Mon Sep 17 00:00:00 2001 From: Negue Date: Thu, 1 Jan 2015 18:20:33 +0100 Subject: [PATCH] add remove-watch and use-timeout attribute to the markdown directive --- script/directives.js | 48 +++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/script/directives.js b/script/directives.js index 51c0dc170c..f6335bbd5d 100644 --- a/script/directives.js +++ b/script/directives.js @@ -85,26 +85,46 @@ }; }(); - habitrpg.directive('markdown', function() { + habitrpg.directive('markdown', ['$timeout', function($timeout) { return { restrict: 'E', link: function(scope, element, attrs) { - scope.$watch(attrs.ngModel, function(value, oldValue) { - var markdown = value; - var linktarget = attrs.target || '_self'; - var userName = scope.User.user.profile.name; - var userHighlight = "@"+userName; - var html = md.toHtml(markdown); - - html = html.replace(userHighlight, "@"+userName+""); - - html = html.replace(' href',' target="'+linktarget+'" href'); - element.html(html); + var removeWatch = !!scope.$eval(attrs.removeWatch); + var useTimeout = !!scope.$eval(attrs.useTimeout); + + var doRemoveWatch = scope.$watch(attrs.ngModel, function(value, oldValue) { + var replaceMarkdown = function(){ + + var markdown = value; + var linktarget = attrs.target || '_self'; + var userName = scope.User.user.profile.name; + var userHighlight = "@"+userName; + var html = md.toHtml(markdown); + + html = html.replace(userHighlight, "@"+userName+""); + + html = html.replace(' href',' target="'+linktarget+'" href'); + element.html(html); + + if(removeWatch) + { + doRemoveWatch(); + } + }; + + if(useTimeout) + { + $timeout(replaceMarkdown, 0); + } + else + { + replaceMarkdown(); + } }); } }; - }); - + }]); + habitrpg.filter('markdown', function() { return function(input){ var html = md.toHtml(input);