diff --git a/core/misc/machine-name.js b/core/misc/machine-name.js index 559bc88..7214a27 100644 --- a/core/misc/machine-name.js +++ b/core/misc/machine-name.js @@ -249,13 +249,38 @@ * The transliterated source string. */ transliterate: function (source, settings) { - return $.get(drupalSettings.path.basePath + 'machine_name/transliterate', { + var now = Date.now(); + + // Number of milliseconds to wait between calls to the server. This avoids + // flooding the server with requests as a user is typing. + // @TODO: make this configurable? + this.delay = 250; + + if ('undefined' != typeof this.lastCalled && 'undefined' != typeof this.cachedResult && (now - this.lastCalled <= this.delay)) { + // Make sure we make one final call to the server to capture the final + // characters typed. + var self = this; + if ('undefined' != typeof this.finalCallback) { + clearTimeout(this.finalCallback); + } + this.finalCallback = setTimeout(function() { + self.transliterate(source, settings); + }, this.delay); + + return this.cachedResult; + } + + // Updated cached results with the latest from the server. + this.lastCalled = now; + this.cachedResult = $.get(drupalSettings.path.basePath + 'machine_name/transliterate', { text: source, langcode: drupalSettings.langcode, replace_pattern: settings.replace_pattern, replace: settings.replace, lowercase: true }); + + return this.cachedResult; } });