diff --git a/plugins/mentions/plugin.js b/plugins/mentions/plugin.js index 04d3b54..bf1627f 100644 --- a/plugins/mentions/plugin.js +++ b/plugins/mentions/plugin.js @@ -1,4 +1,4 @@ -´╗┐ /** +/** * @file * Written by Albert Skibinski * http://www.merge.nl @@ -19,6 +19,7 @@ function CKEDITOR_mentions (editor) { this.editor = editor; this.observe = 0; this.char_input = []; + this.timeout_id = null; if ( CKEDITOR_mentions.caller != CKEDITOR_mentions.get_instance ) { throw new Error("This object cannot be instanciated"); @@ -27,12 +28,23 @@ function CKEDITOR_mentions (editor) { /* * Collection of pairs editor id / instance of CKEDITOR_mentions - * * @type Array */ CKEDITOR_mentions.instances = []; /* + * Delay of the timeout between the last key pressed and the ajax query. It's use to prevent ajax flooding when user types fast. + * @type Number + */ +CKEDITOR_mentions.timeout_delay = 500; + +/* + * Minimum number of characters needed to start searching for users (includes the @). + * @type Number + */ +CKEDITOR_mentions.start_observe_count = 3; + +/* * Method used to get an instance of CKEDITOR_mentions linked to an instance of CKEDITOR. * Its design is based on the singleton design pattern. * @@ -88,34 +100,48 @@ CKEDITOR_mentions.prototype.stop_observing = function () { }; /* - * This methods send an ajax query to durpal ckeditor_mentions module and retrieve matching user. + * This is the handle to fill the tooltip. It trigger a timeout which will call timeout_callback. * * @returns {null} */ CKEDITOR_mentions.prototype.get_people = function () { - //if less than 3 char are input (including @) we don't try to get people - if (this.char_input.length < 3) { - this.delete_tooltip(); - return; + if (null !== this.timeout_id) { + clearTimeout(this.timeout_id); } + this.timeout_id = setTimeout(this.timeout_callback(), CKEDITOR_mentions.timeout_delay); +}; + +/* + * This methods send an ajax query to durpal ckeditor_mentions module and retrieve matching user. + * + * @returns {null} + */ +CKEDITOR_mentions.prototype.timeout_callback = function () { + console.log(this.char_input); + //if less than 3 char are input (including @) we don't try to get people + if (this.char_input.length < CKEDITOR_mentions.start_observe_count) { + this.delete_tooltip(); + return; + } + + // make a copy of the array instead of a reference + var clone = this.char_input.slice(0); + str = clone.join(''); + + var element_settings = {}; + + element_settings.progress = { 'type': 'none' }; + element_settings.url = '/ckeditor/mentions'; + + var element = 'mentions_getpeople'; + var base = 'mentions_getpeople'; + element_settings.submit = { + 'typed': str, + 'element': this.editor.element.getId() + }; + ajax = new Drupal.ajax(base, element, element_settings); + ajax.eventResponse(jQuery(ajax.element)); - // make a copy of the array instead of a reference - var clone = this.char_input.slice(0); - str = clone.join(''); - - var element_settings = {}; - - element_settings.progress = { 'type': 'none' }; - element_settings.url = '/ckeditor/mentions'; - - var element = 'mentions_getpeople'; - var base = 'mentions_getpeople'; - element_settings.submit = { - 'typed': str, - 'element': this.editor.element.getId() - }; - ajax = new Drupal.ajax(base, element, element_settings); - ajax.eventResponse(jQuery(ajax.element)); }; /*