]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ClientSideShorten/shorten.js
Shorten text after paste operation
[quix0rs-gnu-social.git] / plugins / ClientSideShorten / shorten.js
1 //wrap everything in a self-executing anonymous function to avoid conflicts
2 (function(){
3
4     // smart(x) from Paul Irish
5     // http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/
6
7     (function($,sr){
8
9         // debouncing function from John Hann
10         // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
11         var debounce = function (func, threshold, execAsap) {
12             var timeout;
13
14             return function debounced () {
15                 var obj = this, args = arguments;
16                 function delayed () {
17                     if (!execAsap)
18                         func.apply(obj, args);
19                         timeout = null; 
20                 };
21
22                 if (timeout)
23                     clearTimeout(timeout);
24                 else if (execAsap)
25                     func.apply(obj, args);
26
27                 timeout = setTimeout(delayed, threshold || 100); 
28             };
29         }
30         jQuery.fn[sr] = function(fn){  return fn ? this.bind('keypress', debounce(fn, 1000)) : this.trigger(sr); };
31
32     })(jQuery,'smartkeypress');
33
34     function shorten()
35     {
36         $noticeDataText = $('#'+SN.C.S.NoticeDataText);
37         var original = $noticeDataText.val();
38         shortenAjax = $.ajax({
39             url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten',
40             data: { text: $noticeDataText.val() },
41             dataType: 'text',
42             success: function(data) {
43                 if(original == $noticeDataText.val()) {
44                     $noticeDataText.val(data).keyup();
45                 }
46             }
47         });
48     }
49
50     $(document).ready(function(){
51         $noticeDataText = $('#'+SN.C.S.NoticeDataText);
52         $noticeDataText.smartkeypress(function(e){
53             if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort();
54             if(e.charCode == '32') {
55                 shorten();
56             }
57         });
58         $noticeDataText.bind('paste', function() {
59             if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort();
60             setTimeout(shorten,1);
61         });
62     });
63
64 })();