Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with jQuery 1.9 and changes to dropdown position #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions jquery-textntags.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
background: #fff;
border: 1px solid #b2b2b2;
position: absolute;
left: 0;
right: 0;
z-index: 10000;
margin-top: -2px;

Expand All @@ -61,6 +59,7 @@

.textntags-wrapper .textntags-tag-list li {
background-color: #fff;
color: black;
padding: 0 5px;
margin: 0;
width: auto;
Expand Down
164 changes: 130 additions & 34 deletions jquery-textntags.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
templates : {
wrapper : _.template('<div class="textntags-wrapper"></div>'),
beautifier : _.template('<div class="textntags-beautifier"><div></div></div>'),
tagHighlight : _.template('<strong class="<%= class_name %>"><span>$<%= idx %></span></strong>'),
tagHighlight : _.template('<strong class="<%= class_name %>"><span><%- title %></span></strong>'),
tagList : _.template('<div class="textntags-tag-list"></div>'),
tagsListItem : _.template('<li><%= title %></li>'),
tagsListItemImage : _.template('<img src="<%= img %>" />'),
Expand Down Expand Up @@ -63,27 +63,71 @@
}
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
},
setCaratPosition: function (domNode, caretPos) {
if (domNode.createTextRange) {
var range = domNode.createTextRange();
range.move('character', caretPos);
range.select();
} else {
if (domNode.selectionStart) {
domNode.focus();
domNode.setSelectionRange(caretPos, caretPos);
} else {
domNode.focus();
// Derived from jQuery.selection by Iwasaki Koji (@madapaja) http://blog.madapaja.net
setCaretPosition: function (element, caretPos) {
element.each(function () {
this.focus();
try {
if (this.createTextRange) {
var range = this.createTextRange();

if (window.navigator.userAgent.toLowerCase().indexOf('msie') >= 0) {
caretPos = this.value.substr(0, caretPos).replace(/\r/g, '').length;
}

range.collapse(true);
range.moveStart('character', caretPos);
range.moveEnd('character', 0);

range.select();
} else if (this.setSelectionRange) {
this.setSelectionRange(caretPos, caretPos);
}
} catch (e) {}
});
},
getCaretPosition: function (element) {
var result = {start: 0, end: 0};

if (element.length < 1) return result;

element = element[0];

if (!element.value) return result;

try {
if (window.getSelection) {
result.start = element.selectionStart;
result.end = element.selectionEnd;
} else if (document.selection) {
element.focus();

var range = document.selection.createRange(),
range2 = document.body.createTextRange(),
tmpLength;

try {
range2.moveToElementText(element);
range2.setEndPoint('StartToStart', range);
} catch (e) {
range2 = element.createTextRange();
range2.setEndPoint('StartToStart', range);
}

result.start = element.value.length - range2.text.length;
result.end = result.start + range.text.length;
}
}
} catch (e) {}

return result;
}
};

var TextNTags = function (editor) {
var settings = null, templates;
var elContainer, elEditor, elBeautifier, elTagList, elTagListItemActive;
var tagsCollection;
var currentTriggerChar, currentDataQuery;
var currentTriggerChar, currentDataQuery, currentTagPosition = 0;
var editorSelectionLength = 0, editorTextLength = 0, editorKeyCode = 0, editorAddingTag = false;
var editorInPasteMode = false, editorPasteStartPosition = 0, editorPasteCutCharacters = 0;
var REGEX_ESCAPE_CHARS = ['[', '^', '$', '.', '|', '?', '*', '+', '(', ')', '\\'];
Expand Down Expand Up @@ -156,18 +200,47 @@
function getEditorValue () {
return elEditor.val();
}

function beautifiedReplace(text) {
return text.replace(/\n/g, '<br />&shy;').replace(/ {2}/g, ' &nbsp;');
}

function pushDiffText(text, diff_text, startPosition, endPosition) {
if (currentTagPosition >= startPosition && currentTagPosition < endPosition) {
text.push(beautifiedReplace(_.escape(diff_text.substr(0, currentTagPosition - startPosition))),
'<span class="textntags-caret-position"></span>',
beautifiedReplace(_.escape(diff_text.substr(currentTagPosition - startPosition))));
} else {
text.push(beautifiedReplace(_.escape(diff_text)));
}
}

function getBeautifiedText (tagged_text) {
var beautified_text = tagged_text || getTaggedText();
var plain_text = getEditorValue(),
position = 0, beautified_text, triggers = settings.triggers;

beautified_text = _.map(tagsCollection, function (tagPos) {
var text = [],
diff_pos = tagPos[0] - position,
diff_text = diff_pos > 0 ? plain_text.substr(position, diff_pos) : '',
objPropTransformer = transformObjectProperties(triggers[tagPos[2]].keys_map),
tagMarkup = templates.tagHighlight({
title: objPropTransformer(tagPos[3], false).title,
class_name: triggers[tagPos[2]].classes.tagHighlight
});

pushDiffText(text, diff_text, position, tagPos[0]);

text.push(tagMarkup);

_.each(settings.triggers, function (trigger) {
var markup = templates.tagHighlight({idx: trigger.parserGroups.title, class_name: trigger.classes.tagHighlight});
beautified_text = beautified_text.replace(trigger.parser, markup);
position = tagPos[0] + tagPos[1];

return text.join('');
});
beautified_text = beautified_text.replace(/\n/g, '<br />&shy;');
beautified_text = beautified_text.replace(/ {2}/g, ' &nbsp;') + '&shy;';
return beautified_text;

pushDiffText(beautified_text, plain_text.substr(position), position, plain_text.length);

return beautified_text.join('') + '&shy;';
}

function getTaggedText() {
Expand Down Expand Up @@ -235,14 +308,31 @@

function updateBeautifier () {
elBeautifier.find('div').html(getBeautifiedText());
var el = elBeautifier.find('span.textntags-caret-position');

if (el.length > 0) {
var pos = el.position(), ofs = el.offset();
if (ofs.top > ($(document).height() * 0.75)) {
pos = {
top: 'auto',
left: pos.left + 'px',
bottom: (el.offsetParent().height() - pos.top + 30) + 'px'
};
} else {
pos = {
top: pos.top + 'px',
left: pos.left + 'px'
};
}
elTagList.css(pos);
}
elEditor.css('height', elBeautifier.outerHeight() + 'px');
}

function checkForTrigger(look_ahead) {
look_ahead = look_ahead || 0;

var selectionStartFix = $.browser.webkit ? 0 : -1,
sStart = elEditor[0].selectionStart + selectionStartFix,
var sStart = utils.getCaretPosition(elEditor).start,
left_text = elEditor.val().substr(0, sStart + look_ahead),
found_trigger, found_trigger_char = null, query;

Expand All @@ -265,6 +355,7 @@
} else {
currentDataQuery = query;
currentTriggerChar = found_trigger_char;
currentTagPosition = left_text.length - query.length - found_trigger_char.length;
_.defer(_.bind(searchTags, this, currentDataQuery, found_trigger_char));
}
}
Expand All @@ -275,8 +366,9 @@

function onEditorKeyDown (e) {
var keys = KEY, // store in local var for faster lookup
sStart = elEditor[0].selectionStart,
sEnd = elEditor[0].selectionEnd,
selection = utils.getCaretPosition(elEditor),
sStart = selection.start,
sEnd = selection.end,
plain_text = elEditor.val();

editorSelectionLength = sEnd - sStart;
Expand Down Expand Up @@ -374,29 +466,31 @@
return;
}

var sStart = elEditor[0].selectionStart,
sEnd = elEditor[0].selectionEnd;
var selection = utils.getCaretPosition(elEditor),
sStart = selection.start,
sEnd = selection.end;

shiftTagsPosition(editorPasteStartPosition, sEnd - editorPasteStartPosition - editorPasteCutCharacters);
updateBeautifier();
}
}

function onEditorInput (e) {
var selectionStartFix = $.browser.webkit ? 0 : -1;
if (editorKeyCode != KEY.BACKSPACE && editorKeyCode != KEY['DELETE']) {
if (editorSelectionLength > 0) {
// delete of selection occured
var sStart = elEditor[0].selectionStart + selectionStartFix,
var selection = utils.getCaretPosition(elEditor),
sStart = selection.start,
selectionLength = editorSelectionLength,
sEnd = sStart + selectionLength,
tags_shift_positions = elEditor.val().length - editorTextLength;
removeTagsInRange(sStart, sEnd);
shiftTagsPosition(sEnd, tags_shift_positions);
} else if (!editorInPasteMode) {
// char input - shift with 1
var sStart = elEditor[0].selectionStart + selectionStartFix,
sEnd = elEditor[0].selectionEnd + selectionStartFix,
var selection = utils.getCaretPosition(elEditor),
sStart = selection.start,
sEnd = selection.end,
selectionLength = sEnd - sStart;

if (editorKeyCode == KEY.RETURN) {
Expand Down Expand Up @@ -458,7 +552,7 @@
objPropTransformer = transformObjectProperties(trigger.keys_map),
localTag = objPropTransformer(tag, false),
plain_text = getEditorValue(),
sStart = elEditor[0].selectionStart,
sStart = utils.getCaretPosition(elEditor).start,
tagStart = sStart - currentTriggerChar.length - currentDataQuery.length,
newCaretPosition = tagStart + localTag.title.length,
left_text = plain_text.substr(0, tagStart),
Expand All @@ -482,7 +576,7 @@
updateBeautifier();

elEditor.focus();
utils.setCaratPosition(elEditor[0], newCaretPosition);
utils.setCaretPosition(elEditor, newCaretPosition);

elEditor.trigger('tagsAdded.textntags', [[tag]]);
}
Expand All @@ -501,6 +595,8 @@
function populateTagList (query, triggerChar, results) {
var trigger = settings.triggers[triggerChar];

updateBeautifier();

if (trigger.uniqueTags) {
// Filter items that has already been mentioned
var id_key = trigger.keys_map.id, tagIds = _.map(tagsCollection, function (tagPos) { return tagPos[3][id_key]; });
Expand Down