Code coverage report for src/sort/sort-component.js

Statements: 95.45% (42 / 44)      Branches: 89.29% (25 / 28)      Functions: 100% (8 / 8)      Lines: 95.45% (42 / 44)      Ignored: none     

All files » src/sort/ » sort-component.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981               40 40   40 40   9     40             40 40     40   198 52       1 92   92   92       92 92 39 39   92 39 39       92         1 6   6 2       1 4   4 2       1 198   198 10 8   2     188 174   14       198        
angular.module('patternfly.sort').component('pfSort', {
  bindings: {
    config: '='
  },
  templateUrl: 'sort/sort.html',
  controller: function () {
    'use strict';
 
    var ctrl = this;
    var prevConfig;
 
    ctrl.$onInit = function () {
      if (angular.isDefined(ctrl.config) && angular.isUndefined(ctrl.config.show)) {
        // default to true
        ctrl.config.show = true;
      }
 
      angular.extend(ctrl, {
        selectField: selectField,
        changeDirection: changeDirection,
        getSortIconClass: getSortIconClass
      });
    };
 
    ctrl.$onChanges = function () {
      setupConfig();
    };
 
    ctrl.$doCheck = function () {
      // do a deep compare on config
      if (!angular.equals(ctrl.config, prevConfig)) {
        setupConfig();
      }
    };
 
    function setupConfig () {
      var updated = false;
 
      prevConfig = angular.copy(ctrl.config);
 
      Iif (ctrl.config.fields === undefined) {
        ctrl.config.fields = [];
      }
 
      Eif (ctrl.config.fields.length > 0) {
        if (ctrl.config.currentField === undefined) {
          ctrl.config.currentField = ctrl.config.fields[0];
          updated = true;
        }
        if (ctrl.config.isAscending === undefined) {
          ctrl.config.isAscending = true;
          updated = true;
        }
      }
 
      Iif (updated === true && ctrl.config.onSortChange) {
        ctrl.config.onSortChange(ctrl.config.currentField, ctrl.config.isAscending);
      }
    }
 
    function selectField (field) {
      ctrl.config.currentField = field;
 
      if (ctrl.config.onSortChange) {
        ctrl.config.onSortChange(ctrl.config.currentField, ctrl.config.isAscending);
      }
    }
 
    function changeDirection () {
      ctrl.config.isAscending = !ctrl.config.isAscending;
 
      if (ctrl.config.onSortChange) {
        ctrl.config.onSortChange(ctrl.config.currentField, ctrl.config.isAscending);
      }
    }
 
    function getSortIconClass () {
      var iconClass;
 
      if (ctrl.config.currentField.sortType === 'numeric') {
        if (ctrl.config.isAscending) {
          iconClass = 'fa fa-sort-numeric-asc';
        } else {
          iconClass = 'fa fa-sort-numeric-desc';
        }
      } else {
        if (ctrl.config.isAscending) {
          iconClass = 'fa fa-sort-alpha-asc';
        } else {
          iconClass = 'fa fa-sort-alpha-desc';
        }
      }
 
      return iconClass;
    }
  }
});