bsTypeahead directive which adds entered value back
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 11 Jan 2013 16:42:49 +0000 (17:42 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 11 Jan 2013 16:42:49 +0000 (17:42 +0100)
app/drzb2013.html.ep
app/js/app.js
app/js/directives.js

index 53bb577..6951fa8 100644 (file)
@@ -66,7 +66,6 @@
   -->
   <script src="/lib/angular/angular.js"></script>
   <script src="/lib/angular/angular-resource.js"></script>
-  <script src="/lib/angular-strap/dist/angular-strap.min.js"></script>
   <script src="/js/app.js"></script>
   <script src="/js/services.js"></script>
   <script src="/js/controllers.js"></script>
index 03179c9..2533351 100644 (file)
@@ -2,7 +2,7 @@
 
 
 // Declare app level module which depends on filters, and services
-angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', '$strap.directives' ]).
+angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives' ]).
   config(['$routeProvider', function($routeProvider) {
     $routeProvider.when('/registration/:registrationId', {templateUrl: 'partials/registration.html', controller: RegistrationCtrl});
     $routeProvider.when('/confirmation/:registrationId', {templateUrl: 'partials/confirmation.html', controller: RegistrationCtrl});
index 2aa3a9c..cac0c3c 100644 (file)
@@ -18,10 +18,62 @@ angular.module('myApp.directives', []).
                        var my_type  = attrs['type']  || 'text';
                        var html = '<div class="'+my_class+'">'
                                +'<label for="'+attrs.ngModel+'">' + attrs.placeholder + '</label>'
-                               +'<input type="'+my_type+'" name="'+attrs.ngModel+'" ng-model="' + attrs.ngModel + '" ng-required="' + attrs.ngRequired + '" placeholder="' + attrs.placeholder + '" class="'+my_class+'" />'
-                               +'</div>'
+                               +'<input type="'+my_type+'" name="'+attrs.ngModel+'" ng-model="' + attrs.ngModel + '"'
+                               +' placeholder="'+attrs.placeholder+'" class="'+my_class+'"'
                        ;
+                       if ( attrs.ngRequired ) html += ' ng-required="' + attrs.ngRequired + '"';
+                       if ( attrs.bsTypeahead )  html += ' bs-typeahead="'+attrs.bsTypeahead+'"';
+                       if ( attrs.dataItems )  html += ' data-items="'+attrs.dataItems+'"'; // for typeahead
+                       html += '/></div>';
                        element.replaceWith(html);
                }
     };
-  });
+  })
+.directive('bsTypeahead', ['$parse', function($parse) {
+       'use strict';
+
+       return {
+               restrict: 'A',
+               require: '?ngModel',
+               link: function postLink(scope, element, attr, controller) {
+
+                       var getter = $parse(attr.bsTypeahead),
+                                       setter = getter.assign,
+                                       value = getter(scope);
+
+                       // Watch bsTypeahead for changes
+                       scope.$watch(attr.bsTypeahead, function(newValue, oldValue) {
+                               if(newValue !== oldValue) {
+                                       value = newValue;
+                               }
+                       });
+
+                       element.attr('data-provide', 'typeahead');
+                       element.typeahead({
+                               source: function(query) { return value; },
+                               items: attr.items,
+                               updater: function(value) {
+                                       // If we have a controller (i.e. ngModelController) then wire it up
+                                       if(controller) {
+                                               scope.$apply(function () {
+                                                       controller.$setViewValue(value);
+                                               });
+                                       }
+                                       return value;
+                               }
+                       });
+
+                       // add entered element into typeahead array for other fields
+                       element.bind('blur', function() {
+                               var new_value = element.val();
+                               if ( new_value.length > 1 && $.inArray( new_value, value ) === -1 ) { // IE doesn't have .indexOf
+                                       scope.$apply( function() {
+                                               value.unshift( element.val() );
+                                       });
+                               }
+                       });
+
+               }
+       };
+}])
+;