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 ['$uibModal','$interpolate',
89 function($uibModal , $interpolate) {
92 service.open = function(message, msg_scope) {
93 return $uibModal.open({
94 templateUrl: './share/t_alert_dialog',
95 controller: ['$scope', '$uibModalInstance',
96 function($scope, $uibModalInstance) {
97 $scope.message = $interpolate(message)(msg_scope);
98 $scope.ok = function() {
99 if (msg_scope && msg_scope.ok) msg_scope.ok();
100 $uibModalInstance.close()
111 * egConfirmDialog.open("some message goes {{here}}", {
112 * here : 'foo', ok : function() {}, cancel : function() {}},
115 .factory('egConfirmDialog',
117 ['$uibModal','$interpolate',
118 function($uibModal, $interpolate) {
121 service.open = function(title, message, msg_scope, ok_button_label, cancel_button_label) {
122 return $uibModal.open({
123 templateUrl: './share/t_confirm_dialog',
124 controller: ['$scope', '$uibModalInstance',
125 function($scope, $uibModalInstance) {
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 $uibModalInstance.close()
134 $scope.cancel = function() {
135 if (msg_scope.cancel) msg_scope.cancel();
136 $uibModalInstance.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 ['$uibModal','$interpolate',
160 function($uibModal, $interpolate) {
163 service.open = function(message, promptValue, msg_scope) {
164 return $uibModal.open({
165 templateUrl: './share/t_prompt_dialog',
166 controller: ['$scope', '$uibModalInstance',
167 function($scope, $uibModalInstance) {
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 $uibModalInstance.close()
175 $scope.cancel = function() {
176 if (msg_scope.cancel) msg_scope.cancel();
177 $uibModalInstance.dismiss();
188 * Warn on page unload and give the user a chance to avoid navigating
189 * away from the current page.
190 * Only one handler is supported per page.
191 * NOTE: we can't use an egUnloadDialog as the dialog builder, because
192 * it renders asynchronously, which allows the page to redirect before
193 * the dialog appears.
195 .factory('egUnloadPrompt', [
196 '$window','egStrings',
197 function($window , egStrings) {
198 var service = {attached : false};
200 // attach a page/scope unload prompt
201 service.attach = function($scope, msg) {
202 if (service.attached) return;
203 service.attached = true;
205 // handle page change
206 $($window).on('beforeunload', function() {
208 return msg || egStrings.EG_UNLOAD_PAGE_PROMPT_MSG;
213 // If a scope was provided, attach a scope-change handler,
214 // similar to the page-page prompt.
215 service.locChangeCancel =
216 $scope.$on('$locationChangeStart', function(evt, next, current) {
217 if (confirm(msg || egStrings.EG_UNLOAD_CTRL_PROMPT_MSG)) {
218 // user allowed the page to change.
219 // Clear the unload handler.
222 evt.preventDefault();
227 // remove the page unload prompt
228 service.clear = function() {
229 $($window).off('beforeunload');
230 if (service.locChangeCancel)
231 service.locChangeCancel();
232 service.attached = false;
238 .directive('aDisabled', function() {
241 compile: function(tElement, tAttrs, transclude) {
243 tAttrs["ngClick"] = ("ng-click", "!("+tAttrs["aDisabled"]+") && ("+tAttrs["ngClick"]+")");
245 //Toggle "disabled" to class when aDisabled becomes true
246 return function (scope, iElement, iAttrs) {
247 scope.$watch(iAttrs["aDisabled"], function(newValue) {
248 if (newValue !== undefined) {
249 iElement.toggleClass("disabled", newValue);
253 //Disable href on click
254 iElement.on("click", function(e) {
255 if (scope.$eval(iAttrs["aDisabled"])) {
264 .directive('egBasicComboBox', function() {
269 list: "=", // list of strings
274 '<div class="input-group">'+
275 '<input type="text" ng-disabled="egDisabled" class="form-control" ng-model="selected" ng-change="makeOpen()">'+
276 '<div class="input-group-btn" dropdown ng-class="{open:isopen}">'+
277 '<button type="button" ng-click="showAll()" class="btn btn-default dropdown-toggle"><span class="caret"></span></button>'+
278 '<ul class="dropdown-menu dropdown-menu-right">'+
279 '<li ng-repeat="item in list|filter:selected"><a href ng-click="changeValue(item)">{{item}}</a></li>'+
280 '<li ng-if="all" class="divider"><span></span></li>'+
281 '<li ng-if="all" ng-repeat="item in list"><a href ng-click="changeValue(item)">{{item}}</a></li>'+
285 controller: ['$scope','$filter',
286 function( $scope , $filter) {
289 $scope.isopen = false;
291 $scope.showAll = function () {
292 if ($scope.selected.length > 0)
296 $scope.makeOpen = function () {
297 $scope.isopen = $filter('filter')(
300 ).length > 0 && $scope.selected.length > 0;
304 $scope.changeValue = function (newVal) {
305 $scope.selected = newVal;
306 $scope.isopen = false;
315 * Nested org unit selector modeled as a Bootstrap dropdown button.
317 .directive('egOrgSelector', function() {
321 replace : true, // makes styling easier
323 selected : '=', // defaults to workstation or root org,
324 // unless the nodefault attibute exists
326 // Each org unit is passed into this function and, for
327 // any org units where the response value is true, the
328 // org unit will not be added to the selector.
331 // Each org unit is passed into this function and, for
332 // any org units where the response value is true, the
333 // org unit will not be available for selection.
336 // if set to true, disable the UI element altogether
339 // Caller can either $watch(selected, ..) or register an
343 // optional primary drop-down button label
346 // optional name of settings key for persisting
347 // the last selected org unit
351 // any reason to move this into a TT2 template?
353 '<div class="btn-group eg-org-selector" dropdown>'
354 + '<button type="button" class="btn btn-default dropdown-toggle" ng-disabled="disable_button">'
355 + '<span style="padding-right: 5px;">{{getSelectedName()}}</span>'
356 + '<span class="caret"></span>'
358 + '<ul class="dropdown-menu scrollable-menu">'
359 + '<li ng-repeat="org in orgList" ng-hide="hiddenTest(org.id)">'
360 + '<a href ng-click="orgChanged(org)" a-disabled="disableTest(org.id)" '
361 + 'style="padding-left: {{org.depth * 10 + 5}}px">'
362 + '{{org.shortname}}'
368 controller : ['$scope','$timeout','egOrg','egAuth','egCore','egStartup',
369 function($scope , $timeout , egOrg , egAuth , egCore , egStartup) {
371 if ($scope.alldisabled) {
372 $scope.disable_button = $scope.alldisabled == 'true' ? true : false;
374 $scope.disable_button = false;
377 $scope.egOrg = egOrg; // for use in the link function
378 $scope.egAuth = egAuth; // for use in the link function
379 $scope.hatch = egCore.hatch // for use in the link function
381 // avoid linking the full fleshed tree to the scope by
382 // tossing in a flattened list.
384 // Run-time code referencing post-start data should be run
385 // from within a startup block, otherwise accessing this
386 // module before startup completes will lead to failure.
387 egStartup.go().then(function() {
389 $scope.orgList = egOrg.list().map(function(org) {
392 shortname : org.shortname(),
393 depth : org.ou_type().depth()
397 if (!$scope.selected)
398 $scope.selected = egOrg.get(egAuth.user().ws_ou());
401 $scope.getSelectedName = function() {
402 if ($scope.selected && $scope.selected.shortname)
403 return $scope.selected.shortname();
407 $scope.orgChanged = function(org) {
408 $scope.selected = egOrg.get(org.id);
409 if ($scope.stickySetting) {
410 egCore.hatch.setLocalItem($scope.stickySetting, org.id);
412 if ($scope.onchange) $scope.onchange($scope.selected);
416 link : function(scope, element, attrs, egGridCtrl) {
418 // boolean fields are presented as value-less attributes
422 if (angular.isDefined(attrs[field]))
425 scope[field] = false;
429 if (scope.stickySetting) {
430 var orgId = scope.hatch.getLocalItem(scope.stickySetting);
432 scope.selected = scope.egOrg.get(orgId);
436 if (!scope.selected && !scope.nodefault)
437 scope.selected = scope.egOrg.get(scope.egAuth.user().ws_ou());
443 /* http://eric.sau.pe/angularjs-detect-enter-key-ngenter/ */
444 .directive('egEnter', function () {
445 return function (scope, element, attrs) {
446 element.bind("keydown keypress", function (event) {
447 if(event.which === 13) {
448 scope.$apply(function (){
449 scope.$eval(attrs.egEnter);
452 event.preventDefault();
459 * Handy wrapper directive for uib-datapicker-popup
462 'egDateInput', ['egStrings',
463 function(egStrings) {
475 templateUrl: './share/t_datetime',
477 link : function(scope, elm, attrs) {
478 if (!scope.closeText)
479 scope.closeText = egStrings.EG_DATE_INPUT_CLOSE_TEXT;
481 if ('showTimePicker' in attrs)
482 scope.showTimePicker = true;