aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/components/search-auto-complete-list/search-auto-complete-list.js
blob: cfc706b0cea6199d6eb15f0713c92e3ccded44c2 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) and others /
//                                                                                      /
// All rights reserved. This program and the accompanying materials                     /
// are made available under the terms of the Apache License, Version 2.0                /
// which accompanies this distribution, and is available at                             /
// http://www.apache.org/licenses/LICENSE-2.0                                           /
/////////////////////////////////////////////////////////////////////////////////////////
/*
 * Template Component: SearchAutoCompleteList 
 */
    
//import { Meteor } from 'meteor/meteor'; 
import * as R from 'ramda';
import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { ReactiveVar } from 'meteor/reactive-var';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { EJSON } from 'meteor/ejson';
import { _idFieldDef } from '/imports/lib/simple-schema-utils';
        
//import { store } from '/imports/ui/store/store';

import '../auto-search-result-line/auto-search-result-line';

import './search-auto-complete-list.html';     
    
/*  
 * Lifecycles
 */   
  
Template.SearchAutoCompleteList.onCreated(function() {
  let instance = this;

  instance.state = new ReactiveDict();
  instance.state.setDefault({
    isOpen: false,
    envId: null,
    searchTerm: null,
    results: [],
  });

  instance.currentData = new ReactiveVar(null, EJSON.equals);
  instance.autorun((function(_this) {
    return function(_computation) {
      return _this.currentData.set(Template.currentData());
    };
  })(instance));

  instance.autorun(function () {
    let data = instance.currentData.get();

    new SimpleSchema({
      isOpen: { type: Boolean },
      envId: R.merge(_idFieldDef, { optional: true }), 
      searchTerm: { type: String, optional: true },
      onResultSelected: { type: Function },
      onCloseReq: { type: Function }, 
    }).validate(data);

    instance.state.set('isOpen', data.isOpen);
    instance.state.set('envId', data.envId);
    instance.state.set('searchTerm', data.searchTerm);
    
    instance.onCloseReq = R.defaultTo(() => console.log('close requested'), data.onCloseReq);
  });

  instance.opCounter = 0;

  instance.autorun(function () {
    let envId = instance.state.get('envId');
    let searchTerm = instance.state.get('searchTerm');
    performSearch(searchTerm, envId,
      function getLastOpCounter() {
        return instance.opCounter;
      },
      function setLastOpCounter(opCounter) {
        instance.opCounter = opCounter;
      }
    ).then(function (results) {
      instance.state.set('results', results);
    });
  });

});  

/*
Template.SearchAutoCompleteList.rendered = function() {
};  
*/

Template.SearchAutoCompleteList.onDestroyed(() => {
});

/*
 * Events
 */

Template.SearchAutoCompleteList.events({
  'click .sm-backdrop': function (event, instance) {
    instance.onCloseReq();
  }
}); // end - events
   
/*  
 * Helpers
 */

Template.SearchAutoCompleteList.helpers({    
  searchResults: function () {
    let instance = Template.instance();
    return instance.state.get('results');
  },

  createAutoSearchResultLineArgs: function (resultItem) {
    let instance = Template.instance();

    return {
      namePath: resultItem.name_path,
      objectName: resultItem.object_name,
      objectType: resultItem.type,
      environment: resultItem.environment,
      onClick() {
        instance.data.onResultSelected(resultItem); 
      }
    };
  },

}); // end - helpers

function performSearch(
  searchTerm, 
  envId, 
  getLastOpCounterFn, 
  setLastOpCounterFn
) {
  return new Promise((resolve, reject) => {
    let results = [];
    let opCounter = getLastOpCounterFn() + 1;
    setLastOpCounterFn(opCounter);

    Meteor.apply('inventorySearch', [ 
      searchTerm, envId, opCounter, 
    ], { 
      wait: false 
    }, function (err, res) {
      if (err) {
        console.error(R.toString(err));
        reject(err);
        return;
      }

      let currentOpCounter = getLastOpCounterFn();
      if (res.opCounter !== currentOpCounter) {
        reject('stale search result');
        return;
      }
       
      R.forEach((resultItem) => {
        results = R.append(resultItem, results);
      }, res.searchResults);

      resolve(results);
      return;
    });
  });
}