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() {}},
115 .factory('egConfirmDialog',
117 ['$modal','$interpolate',
118 function($modal, $interpolate) {
121 service.open = function(title, message, msg_scope, ok_button_label, cancel_button_label) {
123 templateUrl: './share/t_confirm_dialog',
124 controller: ['$scope', '$modalInstance',
125 function($scope, $modalInstance) {
126 $scope.title = $interpolate(title)(msg_scope);
127 $scope.message = $interpolate(message)(msg_scope);
128 $scope.ok_button_label = $interpolate(ok_button_label || '')(msg_scope);
129 $scope.cancel_button_label = $interpolate(cancel_button_label || '')(msg_scope);
130 $scope.ok = function() {
131 if (msg_scope.ok) msg_scope.ok();
132 $modalInstance.close()
134 $scope.cancel = function() {
135 if (msg_scope.cancel) msg_scope.cancel();
136 $modalInstance.dismiss();
147 * egPromptDialog.open(
148 * "prompt message goes {{here}}",
149 * promptValue, // optional
152 * ok : function(value) {console.log(value)},
153 * cancel : function() {console.log('prompt denied')}
157 .factory('egPromptDialog',
159 ['$modal','$interpolate',
160 function($modal, $interpolate) {
163 service.open = function(message, promptValue, msg_scope) {
165 templateUrl: './share/t_prompt_dialog',
166 controller: ['$scope', '$modalInstance',
167 function($scope, $modalInstance) {
168 $scope.message = $interpolate(message)(msg_scope);
169 $scope.args = {value : promptValue || ''};
171 $scope.ok = function() {
172 if (msg_scope.ok) msg_scope.ok($scope.args.value);
173 $modalInstance.close()
175 $scope.cancel = function() {
176 if (msg_scope.cancel) msg_scope.cancel();
177 $modalInstance.dismiss();
187 .directive('aDisabled', function() {
190 compile: function(tElement, tAttrs, transclude) {
192 tAttrs["ngClick"] = ("ng-click", "!("+tAttrs["aDisabled"]+") && ("+tAttrs["ngClick"]+")");
194 //Toggle "disabled" to class when aDisabled becomes true
195 return function (scope, iElement, iAttrs) {
196 scope.$watch(iAttrs["aDisabled"], function(newValue) {
197 if (newValue !== undefined) {
198 iElement.toggleClass("disabled", newValue);
202 //Disable href on click
203 iElement.on("click", function(e) {
204 if (scope.$eval(iAttrs["aDisabled"])) {
213 .directive('egBasicComboBox', function() {
218 list: "=", // list of strings
223 '<div class="input-group">'+
224 '<input type="text" ng-disabled="egDisabled" class="form-control" ng-model="selected" ng-change="makeOpen()">'+
225 '<div class="input-group-btn" dropdown ng-class="{open:isopen}">'+
226 '<button type="button" ng-click="showAll()" class="btn btn-default dropdown-toggle"><span class="caret"></span></button>'+
227 '<ul class="dropdown-menu dropdown-menu-right">'+
228 '<li ng-repeat="item in list|filter:selected"><a href ng-click="changeValue(item)">{{item}}</a></li>'+
229 '<li ng-if="all" class="divider"><span></span></li>'+
230 '<li ng-if="all" ng-repeat="item in list"><a href ng-click="changeValue(item)">{{item}}</a></li>'+
234 controller: ['$scope','$filter',
235 function( $scope , $filter) {
238 $scope.isopen = false;
240 $scope.showAll = function () {
241 if ($scope.selected.length > 0)
245 $scope.makeOpen = function () {
246 $scope.isopen = $filter('filter')(
249 ).length > 0 && $scope.selected.length > 0;
253 $scope.changeValue = function (newVal) {
254 $scope.selected = newVal;
255 $scope.isopen = false;
264 * Nested org unit selector modeled as a Bootstrap dropdown button.
266 .directive('egOrgSelector', function() {
270 replace : true, // makes styling easier
272 selected : '=', // defaults to workstation or root org,
273 // unless the nodefault attibute exists
275 // Each org unit is passed into this function and, for
276 // any org units where the response value is true, the
277 // org unit will not be added to the selector.
280 // Each org unit is passed into this function and, for
281 // any org units where the response value is true, the
282 // org unit will not be available for selection.
285 // if set to true, disable the UI element altogether
288 // Caller can either $watch(selected, ..) or register an
292 // optional primary drop-down button label
296 // any reason to move this into a TT2 template?
298 '<div class="btn-group eg-org-selector" dropdown>'
299 + '<button type="button" class="btn btn-default dropdown-toggle" ng-disabled="disable_button">'
300 + '<span style="padding-right: 5px;">{{getSelectedName()}}</span>'
301 + '<span class="caret"></span>'
303 + '<ul class="dropdown-menu scrollable-menu">'
304 + '<li ng-repeat="org in orgList" ng-hide="hiddenTest(org.id)">'
305 + '<a href ng-click="orgChanged(org)" a-disabled="disableTest(org.id)" '
306 + 'style="padding-left: {{org.depth * 10 + 5}}px">'
307 + '{{org.shortname}}'
313 controller : ['$scope','$timeout','egOrg','egAuth',
314 function($scope , $timeout , egOrg , egAuth) {
316 if ($scope.alldisabled) {
317 $scope.disable_button = $scope.alldisabled == 'true' ? true : false;
319 $scope.disable_button = false;
322 $scope.egOrg = egOrg; // for use in the link function
323 $scope.egAuth = egAuth; // for use in the link function
325 // avoid linking the full fleshed tree to the scope by
326 // tossing in a flattened list.
327 $scope.orgList = egOrg.list().map(function(org) {
330 shortname : org.shortname(),
331 depth : org.ou_type().depth()
335 $scope.getSelectedName = function() {
337 return $scope.selected.shortname();
341 $scope.orgChanged = function(org) {
342 $scope.selected = egOrg.get(org.id);
343 if ($scope.onchange) $scope.onchange($scope.selected);
347 link : function(scope, element, attrs, egGridCtrl) {
349 // boolean fields are presented as value-less attributes
353 if (angular.isDefined(attrs[field]))
356 scope[field] = false;
360 if (!scope.selected && !scope.nodefault)
361 scope.selected = scope.egOrg.get(scope.egAuth.user().ws_ou());
367 /* http://eric.sau.pe/angularjs-detect-enter-key-ngenter/ */
368 .directive('egEnter', function () {
369 return function (scope, element, attrs) {
370 element.bind("keydown keypress", function (event) {
371 if(event.which === 13) {
372 scope.$apply(function (){
373 scope.$eval(attrs.egEnter);
376 event.preventDefault();
383 http://stackoverflow.com/questions/18061757/angular-js-and-html5-date-input-value-how-to-get-firefox-to-show-a-readable-d
385 This directive allows us to use html5 input type="date" (for Chrome) and
386 gracefully fall back to a regular ISO text input for Firefox.
387 It also allows us to abstract away some browser finickiness.
391 function(dateFilter) {
394 template: '<input type="date"></input>',
396 link: function(scope, elm, attrs, ngModelCtrl) {
398 // since this is a date-only selector, set the time
399 // portion to 00:00:00, which should better match the
400 // user's expectations. Note this allows us to retain
402 function strip_time(date) {
403 if (!date) date = new Date();
407 date.setMilliseconds(0);
411 ngModelCtrl.$formatters.unshift(function (modelValue) {
412 // apply strip_time here in case the user never
413 // modifies the date value.
414 return dateFilter(strip_time(modelValue), 'yyyy-MM-dd');
417 ngModelCtrl.$parsers.unshift(function(viewValue) {
418 return strip_time(new Date(viewValue));