Browse Source

Merge pull request #238 from slr/tasty-fixes-4

fix of post splitting, $.fn.caret, licenses for caret, franc and notify.js
master
miguelfreitas 10 years ago
parent
commit
e8a8e91bbd
  1. 225
      js/interface_common.js
  2. 80
      js/jQueryPlugins.js
  3. 30
      licenses/caret.license
  4. 25
      licenses/franc.license
  5. 9
      licenses/notify.js.license

225
js/interface_common.js

@ -752,62 +752,76 @@ function checkPostForMentions(post, mentions, max) { @@ -752,62 +752,76 @@ function checkPostForMentions(post, mentions, max) {
var splitedPostsCount = 1;
var usePostSpliting = false;
function replyTextKeypress(e) {
function replyTextInput(e) {
e = e || event;
var $this = $( this );
var tweetForm = $this.parents("form");
if( tweetForm != undefined ) {
if ($.Options.getUnicodeConversionOpt() !== "disable")
$this.val(convert2Unicodes($this.val(), $this));
var c = 140 - $this.val().length;
if (usePostSpliting && !$this.parents('.directMessages').length) {
var $tas = tweetForm.find("textarea");
splitedPostsCount = $tas.length;
if ($this.hasClass('splited-post'))
$this.css('height', '28px');
var icurrentta = $tas.index(this); // current textarea $tas index
if (splitedPostsCount > 1)
var pml = getPostSplitingPML();
else
var pml = 140;
var cci = getPostSpittingCI(icurrentta);
var caretPos = $this.caret();
var reply_to = $this.attr('data-reply-to');
for (var i = 0; i < $tas.length; i++) {
var pml = 140 - (i+1).toString().length - splitedPostsCount.toString().length - 4;
//if mention exists, we shouldn't add it while posting.
if (typeof(reply_to) !== 'undefined' &&
!checkPostForMentions($tas[i].value, reply_to, pml - reply_to.length)) {
pml -= reply_to.length;
}
for (var i = 0; i < $tas.length; i++) {
if ($tas[i].value.length > pml) {
var endings = $tas[i].value.match(/ |,|;|\.|:|\/|\?|\!|\\|'|"|\n|\t/g);
var ci = $tas[i].value.lastIndexOf(endings[endings.length - 1]);
for (var j = endings.length - 2; j >= 0 && ci > pml; j--) {
ci = $tas[i].value.lastIndexOf(endings[j], ci - 1);
}
ci = (ci > pml ? pml : ci);
if (pml === 140)
pml = getPostSplitingPML();
var ci = getPostSpittingCI(i);
if (i < splitedPostsCount - 1) {
$tas[i + 1].value = $tas[i].value.substr(ci) + $tas[i + 1].value;
$tas[i].value = $tas[i].value.substr(0, ci);
if (caretPos > cci) {
caretPos -= ci;
icurrentta += 1;
cci = getPostSpittingCI(icurrentta);
var $targetta = $($tas[icurrentta]);
} else if (i === icurrentta)
$($tas[i]).caret(caretPos);
} else {
var $oldta = $($tas[i]);
if (typeof($.fn.textcomplete) === 'function')
if (typeof($.fn.textcomplete) === 'function') {
$oldta.textcomplete('destroy');
e.stopImmediatePropagation(); // something goes wrong in $.fn.textcomplete if we don't stop this immediately
}
var $newta = $($oldta).clone(true);
var cp = $oldta.val();
$oldta.val(cp.substr(0, ci));
$oldta.on("click", function(e) {
e.stopPropagation();
this.style.height = '80px';
});
e.stopImmediatePropagation();
$oldta.off('keyup');
$oldta.on('focusout', function() {this.style.height = '28px';}); // FIXME move this to CSS
$oldta[0].style.height = '28px'; // FIXME move this to CSS
$oldta.addClass('splited-post');
$oldta.after($newta);
$newta.val(cp.substr(ci));
$oldta.val(cp.substr(0, ci));
$tas = tweetForm.find("textarea");
splitedPostsCount = $tas.length;
pml = getPostSplitingPML();
$oldta.on('focus', function() {
this.style.height = '80px';
});
$oldta.addClass('splited-post');
$oldta.on('focusout', function() {this.style.height = '28px';}); // FIXME move this to CSS
if (caretPos > cci) {
caretPos -= ci;
icurrentta += 1;
cci = getPostSpittingCI(icurrentta);
var $targetta = $newta;
$oldta[0].style.height = '28px'; // FIXME move this to CSS
} else if (i === icurrentta) {
$($tas[i]).caret(caretPos);
replyTextUpdateRemaining($tas[i]);
if (typeof($.fn.textcomplete) === 'function')
setTextcompleteOn($tas[i]);
}
}
} else if ($tas.length > 1 && $tas[i].value.length === 0) {
@ -819,36 +833,113 @@ function replyTextKeypress(e) { @@ -819,36 +833,113 @@ function replyTextKeypress(e) {
}
$tas = tweetForm.find("textarea");
splitedPostsCount = $tas.length;
if (splitedPostsCount > 1)
pml = getPostSplitingPML();
else
pml = 140;
caretPos = -1;
if (icurrentta >= i && icurrentta > 0) {
icurrentta -= 1;
cci = getPostSpittingCI(icurrentta);
}
var $targetta = $($tas[icurrentta]);
}
}
if (typeof($newta) !== 'undefined' && $newta[0] !== document.activeElement)
$newta.focus();
c = 140 - $tas[$tas.length - 1].value.length - (2 * splitedPostsCount.toString().length) - 4;
if (typeof(reply_to) !== 'undefined' &&
!checkPostForMentions($tas[$tas.length - 1].value, reply_to, 140 - c - reply_to.length))
c -= reply_to.length;
if (typeof($targetta) !== 'undefined' && $targetta[0] !== document.activeElement) {
$this = $targetta;
$this.focus();
$this.caret(caretPos);
}
}
var remainingCount = tweetForm.find(".post-area-remaining");
}
if( c < 0 )
remainingCount.addClass("warn");
else
remainingCount.removeClass("warn");
function getPostSplitingPML() {
var pml = 140 -(i+1).toString().length -splitedPostsCount.toString().length -4;
if (usePostSpliting && !$this.parents('.directMessages').length)
remainingCount.text(splitedPostsCount.toString() + ". post: " + c.toString());
else
remainingCount.text(c.toString());
// if mention exists, we shouldn't add it while posting.
if (typeof(reply_to) !== 'undefined' &&
!checkPostForMentions($tas[i].value, reply_to, pml -reply_to.length)) {
pml -= reply_to.length;
}
return pml;
}
function getPostSpittingCI(ita) {
var ci;
var endings = $tas[ita].value.match(/ |,|;|\.|:|\/|\?|\!|\\|'|"|\n|\t/g);
if (endings) {
ci = $tas[ita].value.lastIndexOf(endings[endings.length - 1]);
for (var j = endings.length - 2; j >= 0 && ci > pml; j--) {
ci = $tas[ita].value.lastIndexOf(endings[j], ci - 1);
}
}
if (!(ci > 0))
ci = pml;
return (ci > pml) ? pml : ci;
}
}
function replyTextUpdateRemaining(ta) {
if (ta === document.activeElement ) {
var $this = $(ta);
var tweetForm = $this.closest('form');
if( tweetForm != undefined ) {
var remainingCount = tweetForm.find(".post-area-remaining");
var c = replyTextCountRemaining(ta);
if (usePostSpliting && !$this.parents('.directMessages').length && splitedPostsCount > 1)
remainingCount.text((tweetForm.find("textarea").index(ta)+1).toString() +'/' +splitedPostsCount.toString() +": " +c.toString());
else
remainingCount.text(c.toString());
var tweetAction = tweetForm.find(".post-submit");
if( !tweetAction.length ) tweetAction = tweetForm.find(".dm-submit");
var disable = false;
$this.closest('form').find("textarea").each(function() {
if (replyTextCountRemaining(this) < 0) {
disable = true; // alternatively we could call replyTextInput()
return false;
}
});
if (!disable && c >= 0 && c < 140 && $this.val() != $this.attr("data-reply-to")) {
remainingCount.removeClass("warn");
$.MAL.enableButton(tweetAction);
} else {
if (disable)
remainingCount.addClass("warn");
$.MAL.disableButton(tweetAction);
}
}
}
}
function replyTextCountRemaining(ta) {
var $this = $(ta);
var c;
if (usePostSpliting && !$this.parents('.directMessages').length && splitedPostsCount > 1) {
c = 140 -ta.value.length -($this.closest('form').find("textarea").index(ta)+1).toString().length -splitedPostsCount.toString().length -4;
var reply_to = $this.attr('data-reply-to');
if (typeof(reply_to) !== 'undefined' &&
!checkPostForMentions(ta.value, reply_to, 140 -c -reply_to.length))
c -= reply_to.length;
} else
c = 140 - ta.value.length;
return c;
}
function replyTextKeySend(e) {
e = e || event;
var $this = $( this );
var tweetForm = $this.parents('form');
if( tweetForm != undefined ) {
var tweetAction = tweetForm.find(".post-submit");
if( !tweetAction.length ) tweetAction = tweetForm.find(".dm-submit");
if( c >= 0 && c < 140 &&
$this.val() != $this.attr("data-reply-to") ) {
$.MAL.enableButton(tweetAction);
} else {
$.MAL.disableButton(tweetAction);
}
if( $.Options.keyEnterToSend() && $('.dropdown-menu').css('display') == 'none'){
if (e.keyCode === 13 && (!e.metaKey && !e.ctrlKey)) {
@ -1510,7 +1601,9 @@ function initInterfaceCommon() { @@ -1510,7 +1601,9 @@ function initInterfaceCommon() {
$( ".undo-unicode" ).click( undoLastUnicode );
var $replyText = $( ".post-area-new textarea" );
$replyText.on("keyup", replyTextKeypress );
$replyText.on('input', replyTextInput); // input event fires in modern browsers (IE9+) on any changes in textarea (and copypasting with mouse too)
$replyText.on('input focus', function() {replyTextUpdateRemaining(this);});
$replyText.on('keyup', replyTextKeySend);
$( ".open-profile-modal").bind( "click", function(e){ e.stopPropagation(); } );
//$( ".open-hashtag-modal").bind( "click", openHashtagModal );
@ -1530,25 +1623,25 @@ function initInterfaceCommon() { @@ -1530,25 +1623,25 @@ function initInterfaceCommon() {
if (typeof($.fn.textcomplete) === 'function') {
$('textarea')
.on('focus', function () {
var element = this;
// Cursor has not set yet. And wait 100ms to skip global click event.
setTimeout(function () {
// Cursor is ready.
$(element).textcomplete(getMentionsForAutoComplete(), {
'appendTo': ($(element).parents('.dashboard').length > 0) ? $(element).parent() : $('body'),
'listPosition': setTextcompleteDropdownListPos
});
}, 100);
})
.on('focusout', function () {
$(this).textcomplete('destroy');
})
.on('focus', function () { setTextcompleteOn(this); })
.on('focusout', function () { $(this).textcomplete('destroy'); })
;
}
}
function setTextcompleteOn(element) {
var $this = $(element);
// Cursor has not set yet. And wait 100ms to skip global click event.
setTimeout(function () {
// Cursor is ready.
$this.textcomplete(getMentionsForAutoComplete(), {
'appendTo': ($this.parents('.dashboard').length > 0) ? $this.parent() : $('body'),
'listPosition': setTextcompleteDropdownListPos
});
}, 100);
}
// following workaround function is for calls from $.fn.textcomplete only
// we need this because currently implementation of caret position detection is way too imperfect

80
js/jQueryPlugins.js

@ -12,3 +12,83 @@ @@ -12,3 +12,83 @@
return $(this);
}
})(jQuery);
(function($) {
$.fn.caret = function(pos) {
var target = this[0];
var isContentEditable = target.contentEditable === 'true';
//get
if (arguments.length == 0) {
//HTML5
if (window.getSelection) {
//contenteditable
if (isContentEditable) {
target.focus();
var range1 = window.getSelection().getRangeAt(0),
range2 = range1.cloneRange();
range2.selectNodeContents(target);
range2.setEnd(range1.endContainer, range1.endOffset);
return range2.toString().length;
}
//textarea
return target.selectionStart;
}
//IE<9
if (document.selection) {
target.focus();
//contenteditable
if (isContentEditable) {
var range1 = document.selection.createRange(),
range2 = document.body.createTextRange();
range2.moveToElementText(target);
range2.setEndPoint('EndToEnd', range1);
return range2.text.length;
}
//textarea
var pos = 0,
range = target.createTextRange(),
range2 = document.selection.createRange().duplicate(),
bookmark = range2.getBookmark();
range.moveToBookmark(bookmark);
while (range.moveStart('character', -1) !== 0) pos++;
return pos;
}
// Addition for jsdom support
if (target.selectionStart)
return target.selectionStart;
//not supported
return 0;
}
//set
if (pos == -1)
pos = this[isContentEditable? 'text' : 'val']().length;
//HTML5
if (window.getSelection) {
//contenteditable
if (isContentEditable) {
target.focus();
window.getSelection().collapse(target.firstChild, pos);
}
//textarea
else
target.setSelectionRange(pos, pos);
}
//IE<9
else if (document.body.createTextRange) {
if (isContentEditable) {
var range = document.body.createTextRange();
range.moveToElementText(target);
range.moveStart('character', pos);
range.collapse(true);
range.select();
} else {
var range = target.createTextRange();
range.move('character', pos);
range.select();
}
}
if (!isContentEditable)
target.focus();
return pos;
}
})(jQuery);

30
licenses/caret.license

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
Copyright (c) 2009, Gideon Sireling
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Gideon Sireling nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

25
licenses/franc.license

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
(The MIT License)
Copyright (c) 2014-2015 Titus Wormer <tituswormer@gmail.com>
Copyright (c) 2008 Kent S Johnson
Copyright (c) 2006 Jacob R Rideout <kde@jacobrideout.net>
Copyright (c) 2004 Maciej Ceglowski
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

9
licenses/notify.js.license

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
Copyright (c) 2014 Alex Gibson
http://alxgbsn.co.uk/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction except as noted below, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sublicense, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
Loading…
Cancel
Save