ManageIQ/manageiq-ui-classic

View on GitHub
app/javascript/oldjs/directives/scheduler/updateDropdownForTimer.js

Summary

Maintainability
A
1 hr
Test Coverage
ManageIQ.angular.app.directive('updateDropdownForTimer', ['$timeout', function($timeout) {
  return {
    require: 'ngModel',
    link: function(scope, elem, attr, ctrl) {
      scope['form_' + ctrl.$name] = elem[0];
      scope['form_' + ctrl.$name + '_timerHide'] = attr.timerHide;

      scope.$watch(attr.ngModel, function() {
        selectPickerShow(ctrl.$name);
      });

      scope.$watch('timer_items', function() {
        if (!scope[scope['form_' + ctrl.$name + '_timerHide']]) {
          $timeout(function() {
            selectPickerShow(ctrl.$name);
          }, 0);
        } else {
          hideTimerValue(scope, ctrl);
        }
      });

      var selectPickerShow = function(name) {
        if (scope.timer_items != undefined && scope.timer_items.length > 0) {
          $(scope['form_' + ctrl.$name]).selectpicker({
            dropupAuto: false,
            noneSelectedText: __('Nothing selected'),
          });
          $(scope['form_' + name]).selectpicker('show');
          $(scope['form_' + name]).selectpicker('refresh');
          $(scope['form_' + name]).addClass('span12').selectpicker('setStyle');
        }
      };

      var hideTimerValue = function(scope, ctrl) {
        if (scope.timer_items == undefined || scope.timer_items.length === 0) {
          $(scope['form_' + ctrl.$name]).selectpicker('hide');
        }
      };
    },
  };
}]);