From 2acd73c18df4448d6743978f41795e233366d0c3 Mon Sep 17 00:00:00 2001 From: Piotr Szmyd Date: Thu, 29 Jan 2015 19:55:23 +0100 Subject: [PATCH] Refactored tokens dropdown implementation. Textboxes can now have the tokens dropdown attached ad-hoc by using $(selector).tokenized() method. Previously it only happened at page load, making it impossible to attach tokens dropdown to textboxes created at runtime. --- .../Scripts/orchard-tokens-admin.js | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Tokens/Scripts/orchard-tokens-admin.js b/src/Orchard.Web/Modules/Orchard.Tokens/Scripts/orchard-tokens-admin.js index 11e70cfdb..e357cd218 100644 --- a/src/Orchard.Web/Modules/Orchard.Tokens/Scripts/orchard-tokens-admin.js +++ b/src/Orchard.Web/Modules/Orchard.Tokens/Scripts/orchard-tokens-admin.js @@ -25,15 +25,27 @@ } }); -jQuery(function ($) { +jQuery.fn.extend({ + tokenized : function () { + return $(this).each(function () { + var _this = $(this); - // provide autocomplete behavior to tokenized inputs - // tokensUrl is initialized from the view - $.get(tokensUrl, function (data) { - $('.tokenized') - .autocomplete({ + // add an icon to tokenized inputs + _this.wrap(''); + var popup = $('
 
') + _this.parent().prepend(popup); + + // show the full list of tokens when the icon is clicked + popup.children('.tokenized-popup').click(function () { + var input = $(this).parent().next(); + // pass empty string as value to search for, displaying all results + input.autocomplete("search", ""); + input.focus(); + }); + + $(this).autocomplete({ minLength: 0, - source: data, + source: $.tokens, select: function (event, ui) { $(this).insertAtCaret(ui.item.value); return false; @@ -50,18 +62,18 @@ jQuery(function ($) { .appendTo(ul); }; }); - }); + }); + }, +}); - // add an icon to tokenized inputs - $('.tokenized').wrap(''); +$(function() { - $('.token-wrapper').prepend('
 
'); + $.tokens = {}; - // show the full list of tokens when the icon is clicked - $('.tokenized-popup').click(function () { - var input = $(this).parent().next(); - // pass empty string as value to search for, displaying all results - input.autocomplete("search", ""); - input.focus(); + // provide autocomplete behavior to tokenized inputs + // tokensUrl is initialized from the view + $.get(tokensUrl, function (data) { + $.tokens = data; + $('.tokenized').tokenized(); }); });