From 89bd53874202e682c1b803815770bc17621b8958 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 1 Mar 2017 12:56:29 -0500 Subject: [PATCH] add callback priority to tags html --- .../javascripts/discourse/lib/plugin-api.js.es6 | 7 ++++--- .../javascripts/discourse/lib/render-tags.js.es6 | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index e2e79d5b04b..de6576d10af 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -392,6 +392,7 @@ class PluginApi { /** * Register a callback to be called every time tags render + * highest priority callbacks are called first * example: * * callback = function(topic, params) { @@ -400,11 +401,11 @@ class PluginApi { * } * } * - * api.addTagsHtmlCallback(callback); + * api.addTagsHtmlCallback(callback, {priority: 100}); * **/ - addTagsHtmlCallback(callback) { - addTagsHtmlCallback(callback); + addTagsHtmlCallback(callback, options) { + addTagsHtmlCallback(callback, options); }; /** diff --git a/app/assets/javascripts/discourse/lib/render-tags.js.es6 b/app/assets/javascripts/discourse/lib/render-tags.js.es6 index 9f66eb36cdc..eb9934d15b6 100644 --- a/app/assets/javascripts/discourse/lib/render-tags.js.es6 +++ b/app/assets/javascripts/discourse/lib/render-tags.js.es6 @@ -1,10 +1,20 @@ import renderTag from 'discourse/lib/render-tag'; let callbacks = null; +let priorities = null; -export function addTagsHtmlCallback(callback) { +export function addTagsHtmlCallback(callback, options) { callbacks = callbacks || []; - callbacks.push(callback); + priorities = priorities || []; + const priority = (options && options.priority) || 0; + + let i = 0; + while(i < priorities.length && priorities[i] > priority) { + i += 1; + } + + priorities.splice(i, 0, priority); + callbacks.splice(i, 0, callback); }; export default function(topic, params){