Skip to content

Commit

Permalink
Merge branch 't/9816'
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Aug 26, 2013
2 parents e6f88ac + 2252695 commit 711cec9
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 195 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -3,6 +3,8 @@ CKEditor 4 Changelog

## CKEditor 4.2.1

* [#9816](http://dev.ckeditor.com/ticket/9816): Floating toolbar does not reposition vertically in several cases.

## CKEditor 4.2

**Important Notes:**
Expand Down
67 changes: 67 additions & 0 deletions core/tools.js
Expand Up @@ -1012,6 +1012,73 @@
}

return !!domain;
},

/**
* Buffers `input` events (or any `input` calls)
* and triggers `output` not more often than once per `minInterval`.
*
* var buffer = CKEDITOR.tools.eventsBuffer( 200, function() {
* console.log( 'foo!' );
* } );
*
* buffer.input();
* // 'foo!' logged immediately
* buffer.input();
* // nothing logged
* buffer.input();
* // nothing logged
* // ... after 200ms single 'foo!' will be logged
*
* Can be easily used with event:
*
* var buffer = CKEDITOR.tools.eventsBuffer( 200, function() {
* console.log( 'foo!' );
* } );
*
* editor.on( 'key', buffer.input );
* // Note: there's no need to bind buffer as a context.
*
* @since 4.2.1
* @param {Number} minInterval Minimum interval between `output` calls in milliseconds.
* @param {Function} output Function that will be executed as `output`.
* @returns {Object}
* @returns {Function} return.input Buffer's input method.
* @returns {Function} return.reset Resets buffered events – `output` will not be executed
* until next `input` is triggered.
*/
eventsBuffer: function( minInterval, output ) {
var scheduled,
lastOutput = 0;

function triggerOutput() {
lastOutput = ( new Date() ).getTime();
scheduled = false;
output();
}

return {
input: function() {
if ( scheduled )
return;

var diff = ( new Date() ).getTime() - lastOutput;

// If less than minInterval passed after last check,
// schedule next for minInterval after previous one.
if ( diff < minInterval )
scheduled = setTimeout( triggerOutput, minInterval - diff );
else
triggerOutput();
},

reset: function() {
if ( scheduled )
clearTimeout( scheduled );

scheduled = lastOutput = 0;
}
};
}
};
})();
Expand Down

0 comments on commit 711cec9

Please sign in to comment.