nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / angular-smart-table / src / stSort.js
1 ng.module('smart-table')
2   .directive('stSort', ['stConfig', '$parse', '$timeout', function (stConfig, $parse, $timeout) {
3     return {
4       restrict: 'A',
5       require: '^stTable',
6       link: function (scope, element, attr, ctrl) {
7
8         var predicate = attr.stSort;
9         var getter = $parse(predicate);
10         var index = 0;
11         var classAscent = attr.stClassAscent || stConfig.sort.ascentClass;
12         var classDescent = attr.stClassDescent || stConfig.sort.descentClass;
13         var stateClasses = [classAscent, classDescent];
14         var sortDefault;
15         var skipNatural = attr.stSkipNatural !== undefined ? attr.stSkipNatural : stConfig.sort.skipNatural;
16         var descendingFirst = attr.stDescendingFirst !== undefined ? attr.stDescendingFirst : stConfig.sort.descendingFirst;
17         var promise = null;
18         var throttle = attr.stDelay || stConfig.sort.delay;
19
20         if (attr.stSortDefault) {
21           sortDefault = scope.$eval(attr.stSortDefault) !== undefined ? scope.$eval(attr.stSortDefault) : attr.stSortDefault;
22         }
23
24         //view --> table state
25         function sort () {
26           if (descendingFirst) {
27             index = index === 0 ? 2 : index - 1;
28           } else {
29             index++;
30           }
31
32           var func;
33           predicate = ng.isFunction(getter(scope)) || ng.isArray(getter(scope)) ? getter(scope) : attr.stSort;
34           if (index % 3 === 0 && !!skipNatural !== true) {
35             //manual reset
36             index = 0;
37             ctrl.tableState().sort = {};
38             ctrl.tableState().pagination.start = 0;
39             func = ctrl.pipe.bind(ctrl);
40           } else {
41             func = ctrl.sortBy.bind(ctrl, predicate, index % 2 === 0);
42           }
43           if (promise !== null) {
44             $timeout.cancel(promise);
45           }
46           if (throttle < 0) {
47             func();
48           } else {
49             promise = $timeout(func, throttle);
50           }
51         }
52
53         element.bind('click', function sortClick () {
54           if (predicate) {
55             scope.$apply(sort);
56           }
57         });
58
59         if (sortDefault) {
60           index = sortDefault === 'reverse' ? 1 : 0;
61           sort();
62         }
63
64         //table state --> view
65         scope.$watch(function () {
66           return ctrl.tableState().sort;
67         }, function (newValue) {
68           if (newValue.predicate !== predicate) {
69             index = 0;
70             element
71               .removeClass(classAscent)
72               .removeClass(classDescent);
73           } else {
74             index = newValue.reverse === true ? 2 : 1;
75             element
76               .removeClass(stateClasses[index % 2])
77               .addClass(stateClasses[index - 1]);
78           }
79         }, true);
80       }
81     };
82   }]);