6 angular.module('myApp.directives', []).
7 directive('appVersion', ['version', function(version) {
8 return function(scope, elm, attrs) {
12 directive('myInput', function() {
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+'"'
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
28 element.replaceWith(html);
33 directive('myLabelInput', function() {
36 compile: function(element, attrs) {
37 var my_class = attrs['class'] || '';
38 my_class = my_class.replace(/ *my-input */,'');
39 var my_type = attrs['type'] || 'text';
40 var m = attrs.ngModel;
42 ' <div class="control-group">'+
43 ' <label class="control-label" for="'+m+'">'+attrs.label+'</label>'+
44 ' <div class="controls">'+
45 ' <input type="'+my_type+'" id="'+m+'" ng-model="'+m+'"';
47 if ( attrs.placeholder ) html += ' placeholder="' + attrs.placeholder + '"';
48 if ( attrs.ngRequired ) html += ' ng-required="' + attrs.ngRequired + '"';
53 element.replaceWith(html);
58 .directive('myLabelSelect', function() {
61 compile: function(element, attrs) {
62 var my_class = attrs['class'] || '';
63 my_class = my_class.replace(/ *my-input */,'');
64 var m = attrs.ngModel;
66 ' <div class="control-group">'+
67 ' <label class="control-label" for="'+m+'">'+attrs.label+'</label>'+
68 ' <div class="controls">'+
69 ' <select id="'+m+'" ng-model="'+m+'"';
71 if ( attrs.ngOptions ) html += ' ng-options="' + attrs.ngOptions + '"';
76 element.replaceWith(html);
81 .directive('bsTypeahead', ['$parse', function($parse) {
87 link: function postLink(scope, element, attr, controller) {
89 var getter = $parse(attr.bsTypeahead),
90 setter = getter.assign,
91 value = getter(scope);
93 // Watch bsTypeahead for changes
94 scope.$watch(attr.bsTypeahead, function(newValue, oldValue) {
95 if(newValue !== oldValue) {
100 element.attr('data-provide', 'typeahead');
102 source: function(query) { return value; },
104 updater: function(value) {
105 // If we have a controller (i.e. ngModelController) then wire it up
107 scope.$apply(function () {
108 controller.$setViewValue(value);
115 // add entered element into typeahead array for other fields
116 element.bind('blur', function() {
117 var new_value = element.val();
118 if ( new_value.length > 1 && $.inArray( new_value, value ) === -1 ) { // IE doesn't have .indexOf
119 scope.$apply( function() {
120 value.unshift( element.val() );