Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Move typeahead (and it's associated options) into a separate bs-tags-…
…typeahead directive.
  • Loading branch information
dwalters committed Sep 11, 2013
commit 32f71ebeae55c015896dad7b84a04a67d839aefb
6 changes: 2 additions & 4 deletions examples/assets/app_bs3.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ angular.module('AngularExample', ['bsTagsInput'])
.controller('Ctrl',
function Ctrl($scope) {
$scope.tags = ['Amsterdam', 'Washington'];
$scope.tagsOptions = {
typeahead: {
local: ['Sydney', 'Beijing', 'Cairo']
}
$scope.tagsTypeahead = {
local: ['Sydney', 'Beijing', 'Cairo']
};
}
);
62 changes: 33 additions & 29 deletions examples/assets/bsTagsInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ angular.module('bsTagsInput', [])
* @restrict A
*
* @description
* Sets up an input field for tag inputs, using the bootstrap-tagsinput jQuery plugin. Optionally
* uses typeahead.js for autocompletion.
* Sets up an input field for tag inputs, using the bootstrap-tagsinput jQuery plugin.
*
* @element INPUT or SELECT
* @param {Object} options passed to the bootstrap-tagsinput plugin at initialization.
* The typeahead option, if specified, will instead be passed to the typeahead.js plugin.
*
* @example
<doc:example>
<doc:source>
<script>
function Ctrl($scope) {
$scope.tags = ['Amsterdam', 'Washington'];
$scope.tagsOptions = {
typeahead: {
local: ['Sydney', 'Beijing', 'Cairo']
}
$scope.tagsTypeahead = {
local: ['Sydney', 'Beijing', 'Cairo']
}
}
</script>
<form ng-controller="Ctrl">
<input type="text" ng-model="tags" bs-tags-input="tagsOptions">
<input type="text" ng-model="tags" bs-tags-input bs-tags-typeahead="tagsTypeahead">
<pre>{{tags}}</pre>
</form>
</doc:source>
Expand All @@ -52,19 +48,11 @@ angular.module('bsTagsInput', [])

return {
require: 'ngModel',
controller: function() {
},
link: function(scope, element, attrs, ngModelCtrl) {
// parse options
var options = {};
if (attrs.bsTagsInput) {
options = scope.$eval(attrs.bsTagsInput);
}
var typeaheadOpts = options.typeahead;
delete options.typeahead;
if (jQuery.fn.typeahead === undefined) {
typeaheadOpts = undefined;
}

// initialize tagsinput
var options = scope.$eval(attrs.bsTagsInput) || {};
element.tagsinput(options);

// handle changes from the underlying model
Expand All @@ -89,16 +77,32 @@ angular.module('bsTagsInput', [])
scope.$on('$destroy', function() {
element.tagsinput('destroy');
});

// setup typeahead, if desired
if (typeaheadOpts !== undefined) {
element.tagsinput('input')
.typeahead(typeaheadOpts)
.bind('typeahead:selected', function(obj, datum) {
element.tagsinput('add', datum.value);
element.tagsinput('input').typeahead('setQuery', '');
});
}
}
};
})
/**
* @ngdoc directive
* @name bsTagsTypeahead
* @restrict A
*
* @description
* Using typeahead.js, adds autocompletion support to a bsTagsInput field.
*
* @element INPUT or SELECT
* @param {Object} options passed to the typeahead.js plugin.
*/
.directive('bsTagsTypeahead', function() {
return {
require: 'bsTagsInput',
link: function(scope, element, attrs, bsTagsInputCtrl) {
// setup typeahead on the 'input' element
var options = scope.$eval(attrs.bsTagsTypeahead) || {};
element.tagsinput('input')
.typeahead(options)
.bind('typeahead:selected', function(obj, datum) {
element.tagsinput('add', datum.value);
element.tagsinput('input').typeahead('setQuery', '');
});
}
}
});
12 changes: 6 additions & 6 deletions examples/bootstrap3/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ <h3>AngularJS support</h3>
<div class="bs-example">
<input type="text"
ng-model="tags"
bs-tags-input="tagsOptions">
bs-tags-input
bs-tags-typeahead="tagsTypeahead">
</div>
<div class="accordion">
<div class="accordion-group">
Expand All @@ -318,17 +319,16 @@ <h3>AngularJS support</h3>
<div class="accordion-inner">
<pre class="prettyprint linenums">&lt;input type="text"
ng-model="tags"
bs-tags-input="tagsOptions"&gt;
bs-tags-input
bs-tags-typeahead="tagsTypeahead"&gt;

&lt;script&gt;
angular.module('AngularExample', ['bsTagsInput'])
.controller('Ctrl',
function Ctrl($scope) {
$scope.tags = ['Amsterdam', 'Washington'];
$scope.tagsOptions = {
typeahead: {
local: ['Sydney', 'Beijing', 'Cairo']
}
$scope.tagsTypeahead = {
local: ['Sydney', 'Beijing', 'Cairo']
};
}
);
Expand Down
62 changes: 33 additions & 29 deletions src/bsTagsInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ angular.module('bsTagsInput', [])
* @restrict A
*
* @description
* Sets up an input field for tag inputs, using the bootstrap-tagsinput jQuery plugin. Optionally
* uses typeahead.js for autocompletion.
* Sets up an input field for tag inputs, using the bootstrap-tagsinput jQuery plugin.
*
* @element INPUT or SELECT
* @param {Object} options passed to the bootstrap-tagsinput plugin at initialization.
* The typeahead option, if specified, will instead be passed to the typeahead.js plugin.
*
* @example
<doc:example>
<doc:source>
<script>
function Ctrl($scope) {
$scope.tags = ['Amsterdam', 'Washington'];
$scope.tagsOptions = {
typeahead: {
local: ['Sydney', 'Beijing', 'Cairo']
}
$scope.tagsTypeahead = {
local: ['Sydney', 'Beijing', 'Cairo']
}
}
</script>
<form ng-controller="Ctrl">
<input type="text" ng-model="tags" bs-tags-input="tagsOptions">
<input type="text" ng-model="tags" bs-tags-input bs-tags-typeahead="tagsTypeahead">
<pre>{{tags}}</pre>
</form>
</doc:source>
Expand All @@ -52,19 +48,11 @@ angular.module('bsTagsInput', [])

return {
require: 'ngModel',
controller: function() {
},
link: function(scope, element, attrs, ngModelCtrl) {
// parse options
var options = {};
if (attrs.bsTagsInput) {
options = scope.$eval(attrs.bsTagsInput);
}
var typeaheadOpts = options.typeahead;
delete options.typeahead;
if (jQuery.fn.typeahead === undefined) {
typeaheadOpts = undefined;
}

// initialize tagsinput
var options = scope.$eval(attrs.bsTagsInput) || {};
element.tagsinput(options);

// handle changes from the underlying model
Expand All @@ -89,16 +77,32 @@ angular.module('bsTagsInput', [])
scope.$on('$destroy', function() {
element.tagsinput('destroy');
});

// setup typeahead, if desired
if (typeaheadOpts !== undefined) {
element.tagsinput('input')
.typeahead(typeaheadOpts)
.bind('typeahead:selected', function(obj, datum) {
element.tagsinput('add', datum.value);
element.tagsinput('input').typeahead('setQuery', '');
});
}
}
};
})
/**
* @ngdoc directive
* @name bsTagsTypeahead
* @restrict A
*
* @description
* Using typeahead.js, adds autocompletion support to a bsTagsInput field.
*
* @element INPUT or SELECT
* @param {Object} options passed to the typeahead.js plugin.
*/
.directive('bsTagsTypeahead', function() {
return {
require: 'bsTagsInput',
link: function(scope, element, attrs, bsTagsInputCtrl) {
// setup typeahead on the 'input' element
var options = scope.$eval(attrs.bsTagsTypeahead) || {};
element.tagsinput('input')
.typeahead(options)
.bind('typeahead:selected', function(obj, datum) {
element.tagsinput('add', datum.value);
element.tagsinput('input').typeahead('setQuery', '');
});
}
}
});