Skip to content

Commit

Permalink
#30 select() and promises
Browse files Browse the repository at this point in the history
the function specified in the mentio-select attribute can now return
Promises instead of only a string.  Both are supported.
  • Loading branch information
Jeff Collins committed Sep 16, 2014
1 parent bc4cdd1 commit ae82d52
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
25 changes: 14 additions & 11 deletions dist/mentio.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ angular.module('mentio', [])
}
};

$scope.replaceText = function (triggerChar, item) {
var remoteScope = $scope.triggerCharMap[triggerChar];
var text = remoteScope.select({
item: item
});
$scope.replaceText = function (text) {
$scope.hideAll();
mentioUtil.replaceTriggerText($scope.targetElement, $scope.targetElementPath,
$scope.targetElementSelectedOffset, $scope.triggerCharSet, text, $scope.requireLeadingSpace);
$scope.setTriggerText('');
Expand Down Expand Up @@ -190,7 +187,7 @@ angular.module('mentio', [])
'keydown keypress paste', function (event) {
var activeMenuScope = $scope.getActiveMenuScope();
if (activeMenuScope) {
if (event.which === 9) {
if (event.which === 9 || event.which === 13) {
event.preventDefault();
activeMenuScope.selectActive();
}
Expand All @@ -216,10 +213,9 @@ angular.module('mentio', [])
});
}

if (event.which === 13) {
if (event.which === 37 || event.which === 39) {
event.preventDefault();
activeMenuScope.selectActive();
}
}
}
}
);
Expand Down Expand Up @@ -363,8 +359,15 @@ angular.module('mentio', [])

// callable both with controller (menuItem) and without controller (local)
this.selectItem = $scope.selectItem = function (item) {
$scope.hideMenu();
$scope.parentMentio.replaceText($scope.triggerChar, item);
var text = $scope.select({
item: item
});
if (typeof text.then === 'function') {
/* text is a promise, at least our best guess */
text.then($scope.parentMentio.replaceText);
} else {
$scope.parentMentio.replaceText(text);
}
};

$scope.activateNextItem = function () {
Expand Down
2 changes: 1 addition & 1 deletion dist/mentio.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion ment.io/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ <h4 class="section-heading">Attributes</h4>
<strong>Optional.</strong>
Specifies a function call to invoke when the user has picked an item in the menu. The item in <code>mentio-items</code>
corresponding to the selected item in the menu is bound to the <code>item</code> parameter of the select function. The
select function must return a string to use to replace the current trigger char and mention text.
select function may return a string or a <code>Promise</code> that returns a string value to use to replace the
current trigger char and mention text.
<p></p>
If no select function is specified, the trigger char is prepended to the <code>label</code> property of the current item.

Expand Down
10 changes: 9 additions & 1 deletion ment.io/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ angular.module('mentio-demo', ['mentio', 'ngRoute'])
};

$scope.getProductTextRaw = function(item) {
return '#' + item.sku;
/* the select() function can also return a Promise which ment.io will handle
propertly during replacement */
return new Promise(function(fulfill) {
// simulated async promise
$timeout(function() {
fulfill('#' + item.sku);
}, 500);
}
);
};

$scope.getPeopleText = function(item) {
Expand Down
25 changes: 14 additions & 11 deletions src/mentio.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ angular.module('mentio', [])
}
};

$scope.replaceText = function (triggerChar, item) {
var remoteScope = $scope.triggerCharMap[triggerChar];
var text = remoteScope.select({
item: item
});
$scope.replaceText = function (text) {
$scope.hideAll();
mentioUtil.replaceTriggerText($scope.targetElement, $scope.targetElementPath,
$scope.targetElementSelectedOffset, $scope.triggerCharSet, text, $scope.requireLeadingSpace);
$scope.setTriggerText('');
Expand Down Expand Up @@ -190,7 +187,7 @@ angular.module('mentio', [])
'keydown keypress paste', function (event) {
var activeMenuScope = $scope.getActiveMenuScope();
if (activeMenuScope) {
if (event.which === 9) {
if (event.which === 9 || event.which === 13) {
event.preventDefault();
activeMenuScope.selectActive();
}
Expand All @@ -216,10 +213,9 @@ angular.module('mentio', [])
});
}

if (event.which === 13) {
if (event.which === 37 || event.which === 39) {
event.preventDefault();
activeMenuScope.selectActive();
}
}
}
}
);
Expand Down Expand Up @@ -363,8 +359,15 @@ angular.module('mentio', [])

// callable both with controller (menuItem) and without controller (local)
this.selectItem = $scope.selectItem = function (item) {
$scope.hideMenu();
$scope.parentMentio.replaceText($scope.triggerChar, item);
var text = $scope.select({
item: item
});
if (typeof text.then === 'function') {
/* text is a promise, at least our best guess */
text.then($scope.parentMentio.replaceText);
} else {
$scope.parentMentio.replaceText(text);
}
};

$scope.activateNextItem = function () {
Expand Down

0 comments on commit ae82d52

Please sign in to comment.