]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ClientSideShorten/shorten.js
add ClientSideShortenPlugin
[quix0rs-gnu-social.git] / plugins / ClientSideShorten / shorten.js
1 // smart(x) from Paul Irish
2 // http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/
3
4 (function($,sr){
5
6     // debouncing function from John Hann
7     // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
8     var debounce = function (func, threshold, execAsap) {
9         var timeout;
10
11         return function debounced () {
12             var obj = this, args = arguments;
13             function delayed () {
14                 if (!execAsap)
15                     func.apply(obj, args);
16                     timeout = null; 
17             };
18
19             if (timeout)
20                 clearTimeout(timeout);
21             else if (execAsap)
22                 func.apply(obj, args);
23
24             timeout = setTimeout(delayed, threshold || 100); 
25         };
26     }
27     jQuery.fn[sr] = function(fn){  return fn ? this.bind('keypress', debounce(fn, 1000)) : this.trigger(sr); };
28
29 })(jQuery,'smartkeypress');
30
31 $(document).ready(function(){
32     $('#notice_data-text').smartkeypress(function(e){  
33         var original = $('#notice_data-text').val();
34         $.ajax({
35             url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten',
36             data: { text: $('#notice_data-text').val() },
37             dataType: 'text',
38             success: function(data) {
39                 if(original == $('#notice_data-text').val()) {
40                     $('#notice_data-text').val(data);
41                     $('#notice_data-text').keyup();
42                 }
43             }
44         });
45     });
46 });