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 + '"';
49 if ( attrs.ngChange ) html += ' ng-change="' + attrs.ngChange + '"';
54 element.replaceWith(html);
59 .directive('myLabelSelect', function() {
62 compile: function(element, attrs) {
63 var my_class = attrs['class'] || '';
64 my_class = my_class.replace(/ *my-input */,'');
65 var m = attrs.ngModel;
67 ' <div class="control-group">'+
68 ' <label class="control-label" for="'+m+'">'+attrs.label+'</label>'+
69 ' <div class="controls">'+
70 ' <select id="'+m+'" ng-model="'+m+'"';
72 if ( attrs.ngOptions ) html += ' ng-options="' + attrs.ngOptions + '"';
77 element.replaceWith(html);
82 .directive('bsTypeahead', ['$parse', function($parse) {
88 link: function postLink(scope, element, attr, controller) {
90 var getter = $parse(attr.bsTypeahead),
91 setter = getter.assign,
92 value = getter(scope);
94 // Watch bsTypeahead for changes
95 scope.$watch(attr.bsTypeahead, function(newValue, oldValue) {
96 if(newValue !== oldValue) {
101 element.attr('data-provide', 'typeahead');
103 source: function(query) { return value; },
105 updater: function(value) {
106 // If we have a controller (i.e. ngModelController) then wire it up
108 scope.$apply(function () {
109 controller.$setViewValue(value);
116 // add entered element into typeahead array for other fields
117 element.bind('blur', function() {
118 var new_value = element.val();
119 if ( new_value.length > 1 && $.inArray( new_value, value ) === -1 ) { // IE doesn't have .indexOf
120 scope.$apply( function() {
121 value.unshift( element.val() );