2 * UI tools and directives.
4 angular.module('egUiMod', ['egCoreMod', 'ui.bootstrap'])
8 * <input focus-me="iAmOpen"/>
9 * $scope.iAmOpen = true;
13 function($timeout , $parse) {
15 link: function(scope, element, attrs) {
16 var model = $parse(attrs.focusMe);
17 scope.$watch(model, function(value) {
19 $timeout(function() {element[0].focus()});
21 element.bind('blur', function() {
22 scope.$apply(model.assign(scope, false));
29 * <input blur-me="pleaseBlurMe"/>
30 * $scope.pleaseBlurMe = true
31 * Useful for de-focusing when no other obvious focus target exists
35 function($timeout , $parse) {
37 link: function(scope, element, attrs) {
38 var model = $parse(attrs.blurMe);
39 scope.$watch(model, function(value) {
41 $timeout(function() {element[0].blur()});
43 element.bind('focus', function() {
44 scope.$apply(model.assign(scope, false));
51 // <input select-me="iWantToBeSelected"/>
52 // $scope.iWantToBeSelected = true;
53 .directive('selectMe',
55 function($timeout , $parse) {
57 link: function(scope, element, attrs) {
58 var model = $parse(attrs.selectMe);
59 scope.$watch(model, function(value) {
61 $timeout(function() {element[0].select()});
63 element.bind('blur', function() {
64 scope.$apply(model.assign(scope, false));
72 // <div ng-repeat="item in items | reverse">{{item.name}}</div>
73 // http://stackoverflow.com/questions/15266671/angular-ng-repeat-in-reverse
74 // TODO: perhaps this should live elsewhere
75 .filter('reverse', function() {
76 return function(items) {
77 return items.slice().reverse();
83 * egAlertDialog.open({message : 'hello {{name}}'}).result.then(
84 * function() { console.log('alert closed') });
86 .factory('egAlertDialog',
88 ['$modal','$interpolate',
89 function($modal , $interpolate) {
92 service.open = function(message, msg_scope) {
94 templateUrl: './share/t_alert_dialog',
95 controller: ['$scope', '$modalInstance',
96 function($scope, $modalInstance) {
97 $scope.message = $interpolate(message)(msg_scope);
98 $scope.ok = function() {
99 if (msg_scope && msg_scope.ok) msg_scope.ok();
100 $modalInstance.close()
111 * egConfirmDialog.open("some message goes {{here}}", {
112 * here : 'foo', ok : function() {}, cancel : function() {}});
114 .factory('egConfirmDialog',
116 ['$modal','$interpolate',
117 function($modal, $interpolate) {
120 service.open = function(title, message, msg_scope) {
122 templateUrl: './share/t_confirm_dialog',
123 controller: ['$scope', '$modalInstance',
124 function($scope, $modalInstance) {
125 $scope.title = $interpolate(title)(msg_scope);
126 $scope.message = $interpolate(message)(msg_scope);
127 $scope.ok = function() {
128 if (msg_scope.ok) msg_scope.ok();
129 $modalInstance.close()
131 $scope.cancel = function() {
132 if (msg_scope.cancel) msg_scope.cancel();
133 $modalInstance.dismiss();
144 * egPromptDialog.open(
145 * "prompt message goes {{here}}",
146 * promptValue, // optional
149 * ok : function(value) {console.log(value)},
150 * cancel : function() {console.log('prompt denied')}
154 .factory('egPromptDialog',
156 ['$modal','$interpolate',
157 function($modal, $interpolate) {
160 service.open = function(message, promptValue, msg_scope) {
162 templateUrl: './share/t_prompt_dialog',
163 controller: ['$scope', '$modalInstance',
164 function($scope, $modalInstance) {
165 $scope.message = $interpolate(message)(msg_scope);
166 $scope.args = {value : promptValue || ''};
168 $scope.ok = function() {
169 if (msg_scope.ok) msg_scope.ok($scope.args.value);
170 $modalInstance.close()
172 $scope.cancel = function() {
173 if (msg_scope.cancel) msg_scope.cancel();
174 $modalInstance.dismiss();
184 .directive('aDisabled', function() {
187 compile: function(tElement, tAttrs, transclude) {
189 tAttrs["ngClick"] = ("ng-click", "!("+tAttrs["aDisabled"]+") && ("+tAttrs["ngClick"]+")");
191 //Toggle "disabled" to class when aDisabled becomes true
192 return function (scope, iElement, iAttrs) {
193 scope.$watch(iAttrs["aDisabled"], function(newValue) {
194 if (newValue !== undefined) {
195 iElement.toggleClass("disabled", newValue);
199 //Disable href on click
200 iElement.on("click", function(e) {
201 if (scope.$eval(iAttrs["aDisabled"])) {
210 .directive('egBasicComboBox', function() {
215 list: "=", // list of strings
219 '<div class="input-group">'+
220 '<input type="text" class="form-control" ng-model="selected" focus-me="always" ng-change="makeOpen()">'+
221 '<div class="input-group-btn" dropdown ng-class="{open:isopen}">'+
222 '<button type="button" class="btn btn-default dropdown-toggle"><span class="caret"></span></button>'+
223 '<ul class="dropdown-menu dropdown-menu-right">'+
224 '<li ng-repeat="item in list|filter:selected"><a href ng-click="changeValue(item)">{{item}}</a></li>'+
228 controller: ['$scope','$filter',
229 function( $scope , $filter) {
231 $scope.always = true;
232 $scope.isopen = false;
234 $scope.makeOpen = function () {
235 return $scope.isopen = $filter('filter')(
238 ).length > 0 && $scope.selected.length > 0;
241 $scope.changeValue = function (newVal) {
242 $scope.selected = newVal;
251 * Nested org unit selector modeled as a Bootstrap dropdown button.
253 .directive('egOrgSelector', function() {
257 replace : true, // makes styling easier
259 selected : '=', // defaults to workstation or root org,
260 // unless the nodefault attibute exists
262 // Each org unit is passed into this function and, for
263 // any org units where the response value is true, the
264 // org unit will not be added to the selector.
267 // Each org unit is passed into this function and, for
268 // any org units where the response value is true, the
269 // org unit will not be available for selection.
272 // if set to true, disable the UI element altogether
275 // Caller can either $watch(selected, ..) or register an
279 // optional primary drop-down button label
283 // any reason to move this into a TT2 template?
285 '<div class="btn-group eg-org-selector" dropdown>'
286 + '<button type="button" class="btn btn-default dropdown-toggle" ng-disabled="disable_button">'
287 + '<span style="padding-right: 5px;">{{getSelectedName()}}</span>'
288 + '<span class="caret"></span>'
290 + '<ul class="dropdown-menu">'
291 + '<li ng-repeat="org in orgList" ng-hide="hiddenTest(org.id)">'
292 + '<a href ng-click="orgChanged(org)" a-disabled="disableTest(org.id)" '
293 + 'style="padding-left: {{org.depth * 10 + 5}}px">'
294 + '{{org.shortname}}'
300 controller : ['$scope','$timeout','egOrg','egAuth',
301 function($scope , $timeout , egOrg , egAuth) {
303 if ($scope.alldisabled) {
304 $scope.disable_button = $scope.alldisabled == 'true' ? true : false;
306 $scope.disable_button = false;
309 $scope.egOrg = egOrg; // for use in the link function
310 $scope.egAuth = egAuth; // for use in the link function
312 // avoid linking the full fleshed tree to the scope by
313 // tossing in a flattened list.
314 $scope.orgList = egOrg.list().map(function(org) {
317 shortname : org.shortname(),
318 depth : org.ou_type().depth()
322 $scope.getSelectedName = function() {
324 return $scope.selected.shortname();
328 $scope.orgChanged = function(org) {
329 $scope.selected = egOrg.get(org.id);
330 if ($scope.onchange) $scope.onchange($scope.selected);
334 link : function(scope, element, attrs, egGridCtrl) {
336 // boolean fields are presented as value-less attributes
340 if (angular.isDefined(attrs[field]))
343 scope[field] = false;
347 if (!scope.selected && !scope.nodefault)
348 scope.selected = scope.egOrg.get(scope.egAuth.user().ws_ou());
354 /* http://eric.sau.pe/angularjs-detect-enter-key-ngenter/ */
355 .directive('egEnter', function () {
356 return function (scope, element, attrs) {
357 element.bind("keydown keypress", function (event) {
358 if(event.which === 13) {
359 scope.$apply(function (){
360 scope.$eval(attrs.egEnter);
363 event.preventDefault();
370 http://stackoverflow.com/questions/18061757/angular-js-and-html5-date-input-value-how-to-get-firefox-to-show-a-readable-d
372 This directive allows us to use html5 input type="date" (for Chrome) and
373 gracefully fall back to a regular ISO text input for Firefox.
374 It also allows us to abstract away some browser finickiness.
378 function(dateFilter) {
381 template: '<input type="date"></input>',
383 link: function(scope, elm, attrs, ngModelCtrl) {
385 // since this is a date-only selector, set the time
386 // portion to 00:00:00, which should better match the
387 // user's expectations. Note this allows us to retain
389 function strip_time(date) {
390 if (!date) date = new Date();
394 date.setMilliseconds(0);
398 ngModelCtrl.$formatters.unshift(function (modelValue) {
399 // apply strip_time here in case the user never
400 // modifies the date value.
401 return dateFilter(strip_time(modelValue), 'yyyy-MM-dd');
404 ngModelCtrl.$parsers.unshift(function(viewValue) {
405 return strip_time(new Date(viewValue));