From 65796ba03ba4cd0470a1e9790d2234c8b42d6cba Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Wed, 21 Apr 2010 17:14:54 -0400 Subject: add ClientSideShortenPlugin see plugins/ClientSideShorten/README --- plugins/ClientSideShorten/shorten.js | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 plugins/ClientSideShorten/shorten.js (limited to 'plugins/ClientSideShorten/shorten.js') diff --git a/plugins/ClientSideShorten/shorten.js b/plugins/ClientSideShorten/shorten.js new file mode 100644 index 000000000..8e0721756 --- /dev/null +++ b/plugins/ClientSideShorten/shorten.js @@ -0,0 +1,46 @@ +// smart(x) from Paul Irish +// http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/ + +(function($,sr){ + + // debouncing function from John Hann + // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ + var debounce = function (func, threshold, execAsap) { + var timeout; + + return function debounced () { + var obj = this, args = arguments; + function delayed () { + if (!execAsap) + func.apply(obj, args); + timeout = null; + }; + + if (timeout) + clearTimeout(timeout); + else if (execAsap) + func.apply(obj, args); + + timeout = setTimeout(delayed, threshold || 100); + }; + } + jQuery.fn[sr] = function(fn){ return fn ? this.bind('keypress', debounce(fn, 1000)) : this.trigger(sr); }; + +})(jQuery,'smartkeypress'); + +$(document).ready(function(){ + $('#notice_data-text').smartkeypress(function(e){ + var original = $('#notice_data-text').val(); + $.ajax({ + url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten', + data: { text: $('#notice_data-text').val() }, + dataType: 'text', + success: function(data) { + if(original == $('#notice_data-text').val()) { + $('#notice_data-text').val(data); + $('#notice_data-text').keyup(); + } + } + }); + }); +}); -- cgit v1.2.3-54-g00ecf From 809e597841d1337b641784eee21d5e9b5dc297e1 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 22 Apr 2010 17:57:57 -0400 Subject: Only shorten after the user presses space, or following a paste operation --- plugins/ClientSideShorten/shorten.js | 75 +++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 31 deletions(-) (limited to 'plugins/ClientSideShorten/shorten.js') diff --git a/plugins/ClientSideShorten/shorten.js b/plugins/ClientSideShorten/shorten.js index 8e0721756..b3a614633 100644 --- a/plugins/ClientSideShorten/shorten.js +++ b/plugins/ClientSideShorten/shorten.js @@ -1,46 +1,59 @@ -// smart(x) from Paul Irish -// http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/ +//wrap everything in a self-executing anonymous function to avoid conflicts +(function(){ -(function($,sr){ + // smart(x) from Paul Irish + // http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/ - // debouncing function from John Hann - // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ - var debounce = function (func, threshold, execAsap) { - var timeout; + (function($,sr){ - return function debounced () { - var obj = this, args = arguments; - function delayed () { - if (!execAsap) + // debouncing function from John Hann + // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ + var debounce = function (func, threshold, execAsap) { + var timeout; + + return function debounced () { + var obj = this, args = arguments; + function delayed () { + if (!execAsap) + func.apply(obj, args); + timeout = null; + }; + + if (timeout) + clearTimeout(timeout); + else if (execAsap) func.apply(obj, args); - timeout = null; - }; - if (timeout) - clearTimeout(timeout); - else if (execAsap) - func.apply(obj, args); + timeout = setTimeout(delayed, threshold || 100); + }; + } + jQuery.fn[sr] = function(fn){ return fn ? this.bind('keypress', debounce(fn, 1000)) : this.trigger(sr); }; - timeout = setTimeout(delayed, threshold || 100); - }; - } - jQuery.fn[sr] = function(fn){ return fn ? this.bind('keypress', debounce(fn, 1000)) : this.trigger(sr); }; + })(jQuery,'smartkeypress'); -})(jQuery,'smartkeypress'); + $(document).ready(function(){ + $noticeDataText = $('#'+SN.C.S.NoticeDataText); + $noticeDataText.smartkeypress(function(e){ + if(e.charCode == '32') { + shorten(); + } + }); + $noticeDataText.bind('paste', shorten); + }); -$(document).ready(function(){ - $('#notice_data-text').smartkeypress(function(e){ - var original = $('#notice_data-text').val(); + function shorten() + { + $noticeDataText = $('#'+SN.C.S.NoticeDataText); + var original = $noticeDataText.val(); $.ajax({ url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten', - data: { text: $('#notice_data-text').val() }, + data: { text: $noticeDataText.val() }, dataType: 'text', success: function(data) { - if(original == $('#notice_data-text').val()) { - $('#notice_data-text').val(data); - $('#notice_data-text').keyup(); + if(original == $noticeDataText.val()) { + $noticeDataText.val(data).keyup(); } } }); - }); -}); + } +})(); -- cgit v1.2.3-54-g00ecf From 1de8877cad94e1f9252b1f6ccdea57ff3339c367 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 23 Apr 2010 12:13:46 -0400 Subject: Shorten text after paste operation Abort ajax shorten request if the user pastes/pushes another key --- plugins/ClientSideShorten/shorten.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'plugins/ClientSideShorten/shorten.js') diff --git a/plugins/ClientSideShorten/shorten.js b/plugins/ClientSideShorten/shorten.js index b3a614633..0db78ecea 100644 --- a/plugins/ClientSideShorten/shorten.js +++ b/plugins/ClientSideShorten/shorten.js @@ -31,21 +31,11 @@ })(jQuery,'smartkeypress'); - $(document).ready(function(){ - $noticeDataText = $('#'+SN.C.S.NoticeDataText); - $noticeDataText.smartkeypress(function(e){ - if(e.charCode == '32') { - shorten(); - } - }); - $noticeDataText.bind('paste', shorten); - }); - function shorten() { $noticeDataText = $('#'+SN.C.S.NoticeDataText); var original = $noticeDataText.val(); - $.ajax({ + shortenAjax = $.ajax({ url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten', data: { text: $noticeDataText.val() }, dataType: 'text', @@ -56,4 +46,19 @@ } }); } + + $(document).ready(function(){ + $noticeDataText = $('#'+SN.C.S.NoticeDataText); + $noticeDataText.smartkeypress(function(e){ + if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort(); + if(e.charCode == '32') { + shorten(); + } + }); + $noticeDataText.bind('paste', function() { + if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort(); + setTimeout(shorten,1); + }); + }); + })(); -- cgit v1.2.3-54-g00ecf From edc6cf39a0acf9ac77626bb4c11a60a11878d65d Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Mon, 26 Apr 2010 15:58:35 -0400 Subject: Only shorten when notice length exceeds max length --- .../ClientSideShorten/ClientSideShortenPlugin.php | 1 + plugins/ClientSideShorten/shorten.js | 26 ++++++++++++---------- plugins/ClientSideShorten/shorten.php | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) (limited to 'plugins/ClientSideShorten/shorten.js') diff --git a/plugins/ClientSideShorten/ClientSideShortenPlugin.php b/plugins/ClientSideShorten/ClientSideShortenPlugin.php index 21763d5b7..ba1f7d3a7 100644 --- a/plugins/ClientSideShorten/ClientSideShortenPlugin.php +++ b/plugins/ClientSideShorten/ClientSideShortenPlugin.php @@ -51,6 +51,7 @@ class ClientSideShortenPlugin extends Plugin } function onEndShowScripts($action){ + $action->inlineScript('var Notice_maxContent = ' . Notice::maxContent()); if (common_logged_in()) { $action->script('plugins/ClientSideShorten/shorten.js'); } diff --git a/plugins/ClientSideShorten/shorten.js b/plugins/ClientSideShorten/shorten.js index 0db78ecea..856c7f05f 100644 --- a/plugins/ClientSideShorten/shorten.js +++ b/plugins/ClientSideShorten/shorten.js @@ -34,29 +34,31 @@ function shorten() { $noticeDataText = $('#'+SN.C.S.NoticeDataText); - var original = $noticeDataText.val(); - shortenAjax = $.ajax({ - url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten', - data: { text: $noticeDataText.val() }, - dataType: 'text', - success: function(data) { - if(original == $noticeDataText.val()) { - $noticeDataText.val(data).keyup(); + if(Notice_maxContent > 0 && $noticeDataText.val().length > Notice_maxContent){ + var original = $noticeDataText.val(); + shortenAjax = $.ajax({ + url: $('address .url')[0].href+'/plugins/ClientSideShorten/shorten', + data: { text: $noticeDataText.val() }, + dataType: 'text', + success: function(data) { + if(original == $noticeDataText.val()) { + $noticeDataText.val(data).keyup(); + } } - } - }); + }); + } } $(document).ready(function(){ $noticeDataText = $('#'+SN.C.S.NoticeDataText); $noticeDataText.smartkeypress(function(e){ - if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort(); + //if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort(); if(e.charCode == '32') { shorten(); } }); $noticeDataText.bind('paste', function() { - if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort(); + //if(typeof(shortenAjax) !== 'undefined') shortenAjax.abort(); setTimeout(shorten,1); }); }); diff --git a/plugins/ClientSideShorten/shorten.php b/plugins/ClientSideShorten/shorten.php index 4905c62c2..07c19e2e7 100644 --- a/plugins/ClientSideShorten/shorten.php +++ b/plugins/ClientSideShorten/shorten.php @@ -61,7 +61,7 @@ class ShortenAction extends Action { parent::handle($args); header('Content-Type: text/plain'); - $shortened_text = common_shorten_links($this->text, true); + $shortened_text = common_shorten_links($this->text); print $shortened_text; } } -- cgit v1.2.3-54-g00ecf