Skip to content

Commit

Permalink
in some cases, there's no selection
Browse files Browse the repository at this point in the history
Need to protect against no selection in the browser.
  • Loading branch information
Jeff Collins committed Nov 26, 2014
1 parent 9c0239e commit a17f3d4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
27 changes: 16 additions & 11 deletions dist/mentio.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ angular.module('mentio', [])
}],
link: function (scope, element, attrs) {
scope.triggerCharMap = {};

scope.targetElement = element;
attrs.$set('autocomplete','off');

Expand Down Expand Up @@ -300,9 +301,11 @@ angular.module('mentio', [])
)
{
/** save selection info about the target control for later re-selection */
scope.targetElement = mentionInfo.mentionSelectedElement;
scope.targetElementPath = mentionInfo.mentionSelectedPath;
scope.targetElementSelectedOffset = mentionInfo.mentionSelectedOffset;
if (mentionInfo.mentionSelectedElement) {
scope.targetElement = mentionInfo.mentionSelectedElement;
scope.targetElementPath = mentionInfo.mentionSelectedPath;
scope.targetElementSelectedOffset = mentionInfo.mentionSelectedOffset;
}

/* publish to external ngModel */
scope.setTriggerText(mentionInfo.mentionText);
Expand Down Expand Up @@ -603,14 +606,16 @@ angular.module('mentio')
function selectElement (targetElement, path, offset) {
var range;
var elem = targetElement;
for (var i = 0; i < path.length; i++) {
elem = elem.childNodes[path[i]];
if (elem === undefined) {
return;
}
while (elem.length < offset) {
offset -= elem.length;
elem = elem.nextSibling;
if (path) {
for (var i = 0; i < path.length; i++) {
elem = elem.childNodes[path[i]];
if (elem === undefined) {
return;
}
while (elem.length < offset) {
offset -= elem.length;
elem = elem.nextSibling;
}
}
}
if (document.selection && document.selection.createRange) {
Expand Down
2 changes: 1 addition & 1 deletion dist/mentio.min.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ gulp.task('site', ['dist'], function () {

gulp.task('dist', ['tpl'], function () {
return gulp.src([
// 'bower_components/textarea-caret-position/index.js',
'src/mentio.directive.js',
'src/mentio.service.js',
'src/templates.js'
Expand All @@ -58,9 +57,6 @@ gulp.task('dist', ['tpl'], function () {
.pipe(gjshint.reporter(stylish))
.pipe(ngAnnotate())
.pipe(concat('mentio.js'))
// // hack to make componentjs libs to work
// .pipe(wrap('(function () {\n\n\'use strict\';\n\nvar Package = \'\';' +
// '\n\nvar getCaretCoordinates ;\n\n<%= contents %>\n\n})();'))
.pipe(gulp.dest('dist'))
.pipe(uglify())
.pipe(concat('mentio.min.js'))
Expand Down
9 changes: 6 additions & 3 deletions src/mentio.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ angular.module('mentio', [])
},
link: function (scope, element, attrs) {
scope.triggerCharMap = {};

scope.targetElement = element;
attrs.$set('autocomplete','off');

Expand Down Expand Up @@ -300,9 +301,11 @@ angular.module('mentio', [])
)
{
/** save selection info about the target control for later re-selection */
scope.targetElement = mentionInfo.mentionSelectedElement;
scope.targetElementPath = mentionInfo.mentionSelectedPath;
scope.targetElementSelectedOffset = mentionInfo.mentionSelectedOffset;
if (mentionInfo.mentionSelectedElement) {
scope.targetElement = mentionInfo.mentionSelectedElement;
scope.targetElementPath = mentionInfo.mentionSelectedPath;
scope.targetElementSelectedOffset = mentionInfo.mentionSelectedOffset;
}

/* publish to external ngModel */
scope.setTriggerText(mentionInfo.mentionText);
Expand Down
18 changes: 10 additions & 8 deletions src/mentio.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ angular.module('mentio')
function selectElement (targetElement, path, offset) {
var range;
var elem = targetElement;
for (var i = 0; i < path.length; i++) {
elem = elem.childNodes[path[i]];
if (elem === undefined) {
return;
}
while (elem.length < offset) {
offset -= elem.length;
elem = elem.nextSibling;
if (path) {
for (var i = 0; i < path.length; i++) {
elem = elem.childNodes[path[i]];
if (elem === undefined) {
return;
}
while (elem.length < offset) {
offset -= elem.length;
elem = elem.nextSibling;
}
}
}
if (document.selection && document.selection.createRange) {
Expand Down

0 comments on commit a17f3d4

Please sign in to comment.