abstract <= 2000 chars
[angular-drzb] / app / js / directives.js
1 'use strict';
2
3 /* Directives */
4
5
6 angular.module('myApp.directives', []).
7   directive('appVersion', ['version', function(version) {
8     return function(scope, elm, attrs) {
9       elm.text(version);
10     };
11   }]).
12   directive('myInput', function() {
13     return {
14                 restrict: 'C',
15                 compile: function(element, attrs) {
16                         var my_class = attrs['class'] || '';
17                         my_class = my_class.replace(/ *my-input */,'');
18                         var my_type  = attrs['type']  || 'text';
19                         var html = '<div class="'+my_class+'">'
20                                 +'<label for="'+attrs.ngModel+'">' + attrs.placeholder + '</label>'
21                                 +'<input type="'+my_type+'" name="'+attrs.ngModel+'" ng-model="' + attrs.ngModel + '"'
22                                 +' placeholder="'+attrs.placeholder+'" class="'+my_class+'"'
23                         ;
24                         if ( attrs.ngRequired ) html += ' ng-required="' + attrs.ngRequired + '"';
25                         if ( attrs.bsTypeahead )  html += ' bs-typeahead="'+attrs.bsTypeahead+'"';
26                         if ( attrs.dataItems )  html += ' data-items="'+attrs.dataItems+'"'; // for typeahead
27                         html += '/></div>';
28                         element.replaceWith(html);
29                 }
30     };
31   })
32 .directive('bsTypeahead', ['$parse', function($parse) {
33         'use strict';
34
35         return {
36                 restrict: 'A',
37                 require: '?ngModel',
38                 link: function postLink(scope, element, attr, controller) {
39
40                         var getter = $parse(attr.bsTypeahead),
41                                         setter = getter.assign,
42                                         value = getter(scope);
43
44                         // Watch bsTypeahead for changes
45                         scope.$watch(attr.bsTypeahead, function(newValue, oldValue) {
46                                 if(newValue !== oldValue) {
47                                         value = newValue;
48                                 }
49                         });
50
51                         element.attr('data-provide', 'typeahead');
52                         element.typeahead({
53                                 source: function(query) { return value; },
54                                 items: attr.items,
55                                 updater: function(value) {
56                                         // If we have a controller (i.e. ngModelController) then wire it up
57                                         if(controller) {
58                                                 scope.$apply(function () {
59                                                         controller.$setViewValue(value);
60                                                 });
61                                         }
62                                         return value;
63                                 }
64                         });
65
66                         // add entered element into typeahead array for other fields
67                         element.bind('blur', function() {
68                                 var new_value = element.val();
69                                 if ( new_value.length > 1 && $.inArray( new_value, value ) === -1 ) { // IE doesn't have .indexOf
70                                         scope.$apply( function() {
71                                                 value.unshift( element.val() );
72                                         });
73                                 }
74                         });
75
76                 }
77         };
78 }])
79 ;