nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / angular-smart-table / src / stSearch.js
1 ng.module('smart-table')
2   .directive('stSearch', ['stConfig', '$timeout','$parse', function (stConfig, $timeout, $parse) {
3     return {
4       require: '^stTable',
5       link: function (scope, element, attr, ctrl) {
6         var tableCtrl = ctrl;
7         var promise = null;
8         var throttle = attr.stDelay || stConfig.search.delay;
9         var event = attr.stInputEvent || stConfig.search.inputEvent;
10
11         attr.$observe('stSearch', function (newValue, oldValue) {
12           var input = element[0].value;
13           if (newValue !== oldValue && input) {
14             ctrl.tableState().search = {};
15             tableCtrl.search(input, newValue);
16           }
17         });
18
19         //table state -> view
20         scope.$watch(function () {
21           return ctrl.tableState().search;
22         }, function (newValue, oldValue) {
23           var predicateExpression = attr.stSearch || '$';
24           if (newValue.predicateObject && $parse(predicateExpression)(newValue.predicateObject) !== element[0].value) {
25             element[0].value = $parse(predicateExpression)(newValue.predicateObject) || '';
26           }
27         }, true);
28
29         // view -> table state
30         element.bind(event, function (evt) {
31           evt = evt.originalEvent || evt;
32           if (promise !== null) {
33             $timeout.cancel(promise);
34           }
35
36           promise = $timeout(function () {
37             tableCtrl.search(evt.target.value, attr.stSearch || '');
38             promise = null;
39           }, throttle);
40         });
41       }
42     };
43   }]);